我想使用freeglut创建opengGL上下文。我将首先通过使用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();

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

根据rhashimoto的回答,我试图将销毁命令放在不同的位置
if (g_winID>=0)
{
    glutDestroyWindow(g_winID);
    g_winID = -1;
}

我在重塑回调的开始处使用了destroy命令
ERROR:  No display callback registered for window 1

我把destroy命令放在显示功能的开头
ERROR:  Function <glutSwapBuffers> called with no current window defined.

如果我将其放在Display回调的末尾,它不会给出错误,但显示的场景不正确。场景中缺少一些东西

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

最佳答案

我认为您可以认为这是FreeGLUT错误,无论是在实现中还是在文档中。看起来需要从GLUT回调中调用glutDestroyWindow()才能正常工作。
glutDestroyWindow()主要是puts the window on a list to be destroyedclearing all callbacks(destroy回调除外)。这可能就是为什么设置显示功能对您不起作用的原因-当您调用glutDestroyWindow()时,该功能已被删除。

Windows实际上是在end of each main loop销毁的。因此,在第一次执行循环时,您的窗口仍然存在。它具有no display callback makes GLUT unhappy的事实。

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

10-06 11:12