gpt4 book ai didi

c++ - 按需渲染帧

转载 作者:行者123 更新时间:2023-11-30 04:55:34 34 4
gpt4 key购买 nike

我目前正在创建一个 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();
}

然而,这不起作用,glClearColorglClear,可能还有 glFlush 被执行,但仅执行一次,之后,程序挂起,我该怎么做才能避免这种情况?

最佳答案

I've tried getting rid of the glutMainLoop(); thing, and just using a handmade loop
... after that, the program just hangs ...

这是个坏主意,因为通常只有在使用 GLUT 时才能获得事件处理是 glutMainLoop()

参见 glutMainLoop :

glutMainLoop enters the GLUT event processing loop. This routine should be called at most once in a GLUT program. Once called, this routine will never return. It will call as necessary any callbacks that have been registered.

注意,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
}

.....
}

关于c++ - 按需渲染帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52960066/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com