我有一个使用XCB和OpenGL的应用程序。在开始的时候,我选择一个帧缓存的配置具有以下属性:GLX动画慢于预期
const int attributes[] = {GLX_BUFFER_SIZE, 32, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, True, GLX_RENDER_TYPE, GLX_RGBA_BIT, None};
fb_configs = glXChooseFBConfig(display, screen_index, attributes, &fb_configs_count);
我运行一个简单的动画,这是应该持续一个固定的时间(1秒),但显示在屏幕上它更需要较长的时间(约5秒)。在添加日志以显示进度值后,我发现实际循环只持续1秒。
struct timeval start; // start time of the animation
gettimeofday(&start, 0);
while (1)
{
double progress = timer_progress(&start);
if (progress > 1.0)
break; // end the animation
draw(progress);
glXSwapBuffers(display, drawable);
xcb_generic_event_t *event = xcb_poll_for_event(connection);
if (!event)
{
usleep(1000);
continue;
}
switch (event->response_type & ~0x80)
{
case XCB_EXPOSE:
default:
free(event);
continue;
}
}
我不确定到底发生了什么。我想在每次迭代中,glXSwapBuffers()
都会将opengl命令排入画图,并且当循环结束时,大多数命令仍然需要执行。
调整usleep()
的参数除了使动画更不流畅或使动画变得更慢之外没有其他效果。当我切换到单缓冲时,问题消失(但我遇到与单缓冲相关的问题)。
看来我没有做正确的事,但我不知道是什么。
我只使用延迟来限制绘图速度。 'timer_progress()'返回'start'和'draw()'使用该值后的时间。 – martinkunev