opengl:关于glutMainLoop()的问题

有人可以解释一下glutMainLoop如何工作的吗? 第二个问题,为什么glClearColor(0.0f, 0.0f, 1.0f, 1.0f);glutDisplayFunc(RenderScene);之后定义glutDisplayFunc(RenderScene); 首先我们调用glClear(GL_COLOR_BUFFER_BIT); 然后才定义glClearColor(0.0f, 0.0f, 1.0f, 1.0f);

 int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(800, 00); glutInitWindowPosition(300,50); glutCreateWindow("GLRect"); glutDisplayFunc(RenderScene); glutReshapeFunc(ChangeSize); glClearColor(0.0f, 0.0f, 1.0f, 1.0f); <-- glutMainLoop(); return 0; } void RenderScene(void) { // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT); // Set current drawing color to red // RGB glColor3f(1.0f, 0.0f, 1.0f); // Draw a filled rectangle with current color glRectf(0.0f, 0.0f, 50.0f, -50.0f); // Flush drawing commands glFlush(); } 

glutMainLoop()只运行一个特定于平台的事件循环,并根据需要调用任何已注册的glut*Func()回调函数。

在调用glutMainLoop()之前,GLUT不会调用RenderScene() glutMainLoop() 。 所以实际上glClearColor()调用glClear() ,而不是glClear()

 glutDisplayFunc(RenderScene); 

这只设置了回调函数,它实际上没有调用它,直到它在对glutMainLoop的调用中进入主应用程序循环。 所以glClearColor出现在glClear之前。