我目前正在创建NTSC信号解码器/模拟器,基本上,我希望能够在渲染之前准备好帧,例如读取数组,处理数据,相应地绘制一些像素,然后渲染框架。我尝试摆脱glutMainLoop();
的问题,而只是使用手工循环:
for(;;) {
glClearColor(0.0f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Whatever I might have to do goes somewhere in here
glFlush();
}
但是,这不起作用,glClearColor
和glClear
,也可能glFlush
被执行了,但是只有一次之后,程序挂起了,我该怎么办?避免这种情况?
答案 0 :(得分:1)
我尝试摆脱
glutMainLoop();
的问题,而只是使用手工制作的循环
...之后,程序就挂了...
这是一个坏主意,因为通常使用GLUT时获得事件处理的唯一方法是glutMainLoop()
。
请参见glutMainLoop
:
glutMainLoop
进入GLUT事件处理循环。该例程在GLUT程序中最多应调用一次。一旦调用,该例程将永远不会返回。它将在必要时调用已注册的所有回调。
请注意,glutMainLoop()
不仅会调用glutDisplayFunc
设置的回调函数,还会接收并处理IO事件,例如鼠标和键盘事件。如果不使用glutMainLoop()
,则没有事件处理,当然也没有IO事件处理。这导致程序似乎挂起,并且对任何输入均不响应。
您必须使用glutMainLoop()
,或者必须切换其他窗口API,例如GLFW,您可以在其中通过glfwPollEvents()
较新的GLUT实现(例如freeglut)提供了一些附加功能。 glutMainLoopEvent()
与glutMainLoop()
的功能相同,但仅执行一次。它只对事件循环进行一次迭代,并立即将控制权交还给用户。因此,您可以实现自己的循环处理您的应用程序。
例如
void display( void )
{
glClearColor(0.0f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Whatever I might have to do goes somewhere in here
glFlush();
}
int main()
{
.....
glutDisplayFunc(display);
.....
for(;;)
{
glutMainLoopEvent(); // handle the main loop once
glutPostRedisplay(); // cause `display` to be called in `glutMainLoopEvent`
}
.....
}
甚至可以设置一个 dummy 显示功能,该功能什么也不做,并在循环中绘制:
例如
void dummyDisplay( void )
{
// does nothing
}
int main()
{
.....
glutDisplayFunc(dummyDisplay);
.....
for(;;)
{
glClearColor(0.0f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Whatever I might have to do goes somewhere in here
glFlush();
glutMainLoopEvent(); // does the event handling once
}
.....
}