关于用glRect*()画矩形的问题

在使用glRect*()函数进行平面矩形的绘制的时候,如果不进行窗口大小改变所要做的处理(即代码中reshape函数),会绘制不出来所要绘制的矩形。例,如下代码是错误的。

#include #include #include void display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0,1.0); glRectf(25.0,25.0,125.0,125.0); glFlush(); } void init() { glClearColor(0.0,0.0,0.0,0.0); glShadeModel(GL_FLAT); } /* void reshape(int w,int h) { glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h); } */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(350,350); glutInitWindowPosition(100,100); glutCreateWindow("hellw"); init(); glutDisplayFunc(display); //glutReshapeFunc(reshape); glutMainLoop(); return 0; } 

只有加上对reshape进行处理的函数才可以绘制出矩形,一时半会儿还想不出为神马。

如下代码才是正常的可以画出矩形的代码。

#include #include #include void display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0,1.0); glRectf(25.0,25.0,125.0,125.0); glFlush(); } void init() { glClearColor(0.0,0.0,0.0,0.0); glShadeModel(GL_FLAT); } void reshape(int w,int h) { glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h); } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(350,350); glutInitWindowPosition(100,100); glutCreateWindow("hellw"); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; } 

你可能感兴趣的:(OpenGL)