OpenGL功能显示房子不工作

时间:2015-11-08 06:27:28

标签: c++ opengl

此程序使用opengl显示房屋 输出只显示一个空白屏幕,没有别的,我没有做任何改变窗口位置和大小的工作 我不知道什么是错的

#include<Gl/glut.h>
GLfloat square[][2]={{100,200},{100,100},{200,100},{200,200}};//coordinates of the vertices of the square part of the house
GLfloat door[][2]={{125,150},{125,100},{175,100},{175,150}};// coordinates of the door
GLfloat roof[][2]={{50,200},{250,200},{150,300}};// coordinates of the roof
void Myinit()
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,500.0,0.0,500.0);
glMatrixMode(GL_MODELVIEW);
}

此功能用于显示房屋:

void drawHouse()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f(0,1,1);
glBegin(GL_POLYGON);  display the square part of the house
glVertex2fv(square[0]);
glVertex2fv(square[1]);
glVertex2fv(square[2]);
glVertex2fv(square[3]);
glEnd();
glColor3f(1,0,1);
glBegin(GL_POLYGON);  display the roof
glVertex2fv(door[0]);
glVertex2fv(door[1]);
glVertex2fv(door[2]);
glVertex2fv(door[3]);
glEnd();
glColor3f(1,0,0);
glBegin(GL_TRIANGLES);  display the triangle
glVertex2fv(roof[0]);
glVertex2fv(roof[1]);
glVertex2fv(roof[2]);
glVertex2fv(roof[3]);
glEnd();
glFlush();
}

int main(int argv, char** argc)
   {
     glutInit(&argv,argc);
     glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
     glutCreateWindow("House Drawing which rotates\n");
     glutInitWindowPosition(100,100);
     glutInitWindowSize(1000,1000);
     Myinit();
     glutDisplayFunc(drawHouse);
     glutMainLoop();
}

1 个答案:

答案 0 :(得分:2)

由于您使用的是双缓冲窗口,因此显示功能必须在最后调用glutSwapBuffers()。有关双缓冲的更多详细信息,请参阅this question