Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to create the opengGL context using freeglut. I will first decide the which context to by checking the supporting version using glew and some other parameters. I know for glew to work, it needs a opengl context. So I first create a context using glutCreateWindow, then check the supported version and then set the version required using the glutInitContextVersion() and destroy the previous window using glutDestroyWindow and recreate the new window by using glutCreateWindow. I get this error Freeglut error: ERROR: No display callback registered for window 1 (I checked 1 is ID for my previous window which I destroyed) . Following is my code

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();

I think I need to do this as a openGL context is always required for glew to work. I already tried setting the display function on first window as well (though I know I am going to destroy it) but that also didn't work. I am setting the current window to new window and then calling the glutMainLoop. So I think this should work

According to answer by rhashimoto, I tried to put the destroy command at different positions

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

I put the destroy command at begining of reshape callback

ERROR:  No display callback registered for window 1

The I put the destroy command at beginning of display function

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

If I put this at end of Display callback, it does not give error but the displayed scene is not correct. Somethings are missing from the scene

So is there some specific callback function I need to put this display command? I dont think there is any destroy callback I can set. yes there is glutCloseFunc but I think that is meant to be called when window is being destroyed that is when glutDestroyWindow has been called on window

like image 722
Pankaj Bansal Avatar asked Oct 31 '22 01:10

Pankaj Bansal


1 Answers

I think you can argue that this is a FreeGLUT bug, either in the implementation or the documentation. It looks like glutDestroyWindow() needs to be called from a GLUT callback to work properly.

glutDestroyWindow() mainly puts the window on a list to be destroyed, as well as clearing all callbacks (except a destroy callback). This is probably why setting the display function didn't work for you - it was removed when you called glutDestroyWindow().

Windows are actually destroyed at the 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.

The best workaround is probably to arrange to call glutDestroyWindow() only via one of the GLUT callbacks. I don't know if this will make the window briefly flash on the screen. My guess is it won't, but it might depend on the platform.

like image 111
rhashimoto Avatar answered Nov 08 '22 05:11

rhashimoto