Freeglut error: ERROR: No display callback registered for window 1 when destroyed a window and created a new window

Freeglut error: ERROR: No display callback registered for window 1 when destroyed a window and created a new window

我想使用 freeglut 创建 opengGL 上下文。我将首先通过使用 glew 和一些其他参数检查支持版本来决定使用哪个上下文。我知道要使 glew 工作,它需要一个 opengl 上下文。所以我首先使用 glutCreateWindow 创建一个上下文,然后检查支持的版本,然后使用 glutInitContextVersion() 设置所需的版本并使用 glutDestroyWindow 销毁之前的 window 并使用 glutCreateWindow 重新创建新的 window。我收到此错误 Freeglut 错误:错误:没有为 window 1 注册显示回调(我检查过 1 是我之前销毁的 window 的 ID)。以下是我的代码

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(900, 600);

int winID = glutCreateWindow("Rotating Cube"); //winID is 1 here

glewExperimental = GL_TRUE;
glewInit();

//I decide the context on some other parameters also except the supported version reported by glew. 
//I have tested this with opengl 3.2 core profile as well. 
//This is not working even if I forcefully set the opengl version to 2.0
if (glewIsSupported("GL_VERSION_3_1"))
{
    glutInitContextVersion (3, 1);
    glutInitContextFlags (GLUT_FORWARD_COMPATIBLE);

    //glutInitContextVersion (3, 2);             
    //glutInitContextFlags (GLUT_CORE_PROFILE);
}
else if (glewIsSupported("GL_VERSION_2_0"))
{
    glutInitContextVersion (2, 0);
}

glutDestroyWindow(winID);
winID = glutCreateWindow("Rotating Cube"); //winID is 2 here

glutSetWindow(winID);

glutDisplayFunc(RenderScene);
glutIdleFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutKeyboardFunc(ProcessNormalKeys);
glutSpecialFunc(ProcessSpecialKeys);

glutMainLoop();

我认为我需要这样做,因为 glew 始终需要 openGL 上下文才能工作。我也已经尝试在第一个 window 上设置显示功能(尽管我知道我要销毁它),但这也没有用。我将当前的 window 设置为新的 window,然后调用 glutMainLoop。所以我认为这应该有效

根据rhashimoto的回答,我尝试把destroy命令放在不同的位置

if (g_winID>=0)
{
    glutDestroyWindow(g_winID);
    g_winID = -1;
}

我将 destroy 命令放在 reshape 回调的开始处

ERROR:  No display callback registered for window 1

我把销毁命令放在显示函数的开头

ERROR:  Function <glutSwapBuffers> called with no current window defined.

如果我把它放在显示回调的末尾,它不会报错,但显示的场景不正确。现场少了点东西

那么我需要一些特定的回调函数来放置这个显示命令吗?我不认为我可以设置任何销毁回调。是的,有 glutCloseFunc 但我认为这是在 window 被销毁时调用的,也就是在 window

上调用 glutDestroyWindow

我认为您可以争辩说这是一个 FreeGLUT 错误,无论是在实现中还是在文档中。看起来 glutDestroyWindow() 需要从 GLUT 回调中调用才能正常工作。

glutDestroyWindow() 主要是 puts the window on a list to be destroyed, as well as clearing all callbacks(销毁回调除外)。这可能就是设置显示功能对您不起作用的原因 - 它在您调用 glutDestroyWindow().

时被删除

Windows 实际上在 end of each main loop. So on the first time through the loop, your window still exists. The fact that it has no display callback makes GLUT unhappy.

被摧毁

最好的解决方法可能是安排仅通过 GLUT 回调之一调用 glutDestroyWindow()。我不知道这是否会使 window 在屏幕上短暂闪烁。我猜不会,但这可能取决于平台。