OpenGL地球(自转+贴图)

#include 
#include
#include 

GLuint texture_id1;  //纹理id
int angle = 0;
GLUquadricObj* q = gluNewQuadric();

int load_texture(char* file_name, int width, int height, int depth, GLenum colour_type, GLenum filter_type)
{
    GLubyte* raw_bitmap;
    FILE* file;
    int errnum;
    if ((errnum = fopen_s(&file,file_name, "rb")) != 0)
    {
        return 1;
    }

    raw_bitmap = (GLubyte*)malloc(width * height * depth * (sizeof(GLubyte)));
    if (raw_bitmap == NULL)
    {
        fclose(file);
        return 2;
    }

    fread(raw_bitmap, width * height * depth, 1, file);
    fclose(file);

    //  设置过滤类型
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter_type);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter_type);

    //  设置纹理环境
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, colour_type,
        GL_UNSIGNED_BYTE, raw_bitmap);
    free(raw_bitmap);
    return 0;
}

void LoadTextures(GLuint texture_id, int MaxNrOfTextures)
{
    char name[] = "ear.bmp";
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glGenTextures(MaxNrOfTextures, &texture_id);
    glBindTexture(GL_TEXTURE_2D, 1);
    if (load_texture(name, 512, 257, 3, GL_BGR_EXT, GL_NEAREST))
    {
        exit(1);
    }
}

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
    LoadTextures(texture_id1, 1);
    glEnable(GL_TEXTURE_2D);
    glClearDepth(1.0f);    // 设置深度缓存
    glDepthFunc(GL_LEQUAL);  // 选择深度测试方式
    glEnable(GL_DEPTH_TEST);  // 开启深度测试
    glShadeModel(GL_SMOOTH);  // 阴暗处理采用平滑方式
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // 最精细的透视计算
    glBindTexture(GL_TEXTURE_2D, 1);
    gluQuadricDrawStyle(q, GLU_FILL);
    gluQuadricNormals(q, GLU_SMOOTH);
    gluQuadricTexture(q, GL_TRUE);
    return;
}
void display(void)
{
    glPushMatrix();
    angle = (angle + 1)%360;
    // 清除颜色和深度缓存
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glRotatef(angle, 0.0, 0.0, 1.0);
    gluSphere(q, 1.0, 50, 40);
    glPopMatrix();
    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 0.1, 20.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0);

}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(1000, 600);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

你可能感兴趣的:(OpenGL,C++,c++,opengl,算法)