在C中显示OpenGL地形网格的问题



我在窗口中显示openGL地形时遇到了问题。我只有一个透明的窗户,我不知道我错过了什么。

下面是我的代码:
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>

#define MAP_SCALE   20.0f
#define MAP_X   5
#define MAP_Z   5   
int height[5][5] ={
{ 0, 0, 1, 1, 2 },
{ 0, 1, 2, 2, 3 },
{ 0, 1, 3, 2, 3 },
{ 0, 1, 2, 1, 2 },
{ 0, 0, 1, 1, 1 } };
float terrain[5][5][3];
int x, z;
float aspect = 1.0f;
void initializeTerrain()
{
    for (z = 0; z < MAP_Z; z++)
    {
        for (x = 0; x < MAP_X; x++)
        {
            terrain[x][z][0] = (float)(x);
            terrain[x][z][1] = (float)height[x][z];
            terrain[x][z][2] = -(float)(z);
        }
    }
}
void display(void){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     
    glLoadIdentity();

    glColor3f(1.0, 0.0, 0.0);
    for (z = 0; z < MAP_Z-1; z++)
    {
        glBegin(GL_TRIANGLE_STRIP);
        for (x = 0; x < MAP_X-1; x++)
        {
            glVertex3f(terrain[x][z][0], terrain[x][z][1], terrain[x][z][2]);
            glVertex3f(terrain[x+1][z][0], terrain[x+1][z][1], terrain[x+1][z][2]);
            glVertex3f(terrain[x][z+1][0], terrain[x][z+1][1], terrain[x][z+1][2]);
            glVertex3f(terrain[x+1][z+1][0], terrain[x+1][z+1][1], terrain[x+1][z+1][2]);
        }
        glEnd();
    }
    glFlush();
}
void myInit(){
    initializeTerrain();
    glMatrixMode(GL_PROJECTION);
    glViewport(0, 0, 500, 500); 
    gluLookAt(1,1,0, 0,0,0, 0.0, 1.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);                                                 
    glClearColor(0.0, 0.0, 1.0, 1.0);

}
void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w <= h)
        glOrtho(-10.0, 10.0, -5.0 * (GLfloat) h / (GLfloat) w,
            15.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
    else
        glOrtho(-10.0 * (GLfloat) w / (GLfloat) h,
            10.0 * (GLfloat) w / (GLfloat) h, -5.0, 15.0, -10.0, 10.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
void main(int argc, char** argv){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutCreateWindow("A3");
    myInit();   
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutMainLoop();
}

当我调整大小时,它填充了白色,但我没有得到网格。有人能帮忙吗?

谢谢!

你请求一个双缓冲窗口,所以你必须做一个双缓冲交换当你完成绘图。将显示函数中的glFinish()替换为glutSwapBuffers()

最新更新