opengl – Freeglut错误:错误:在销毁窗口并创建新窗口时,没有为窗口1注册显示回调

我想使用freeglut创建opengGL上下文.我将首先通过使用glew和其他一些参数检查支持版本来确定哪个上下文.我知道glew工作,它需要一个opengl上下文.所以我首先使用glutCreateWindow创建一个上下文,然后检查支持的版本,然后使用glutInitContextVersion()设置所需的版本,并使用glutDestroyWindow销毁前一个窗口,并使用glutCreateWindow重新创建新窗口.我收到此错误Freeglut错误:错误:没有为窗口1注册显示回调(我检查1是我之前窗口的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上下文.我已经尝试在第一个窗口设置显示功能(虽然我知道我要破坏它)但是这也没有用.我将当前窗口设置为新窗口,然后调用glutMainLoop.所以我认为这应该有效

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

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

我把destroy命令放在reshape回调的开头

ERROR:  No display callback registered for window 1

我把destroy命令放在显示功能的开头

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

如果我把它放在显示回调的末尾,它不会给出错误,但显示的场景不正确.场景中缺少某些东西

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

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

glutDestroyWindow()主要是puts the window on a list to be destroyed,以及clearing all callbacks(除了一个destroy回调).这可能是为什么设置显示功能对你不起作用的原因 – 当你调用glutDestroyWindow()时它被删除了.

Windows实际上是在end of each main loop销毁的.所以在第一次循环时,你的窗口仍然存在.它有no display callback makes GLUT unhappy的事实.

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

点赞