Thursday, February 28, 2013

[opengl] glutMainLoop(), glutDisplayFunc(), glutPostRedisplay()



http://stackoverflow.com/a/1862505

glutMainLoop only calls the display callback when a glut event triggers the display callback, such as resizing the window, uncovering the window, or calling glutPostRedisplay. You have to put code in your program to trigger glut into calling your display callback at a proper frame rate to produce animation in your program. For example, you can put a call to glutPostResdisplay into your idle callback function, so when GLUT is idle your display callback will get called.



http://www.gamedev.net/topic/379278-what-exactly-does-glutmainloop-do/

glutMainLoop() is a function that loops within itself, processing events and triggering your callback functions when necessary (glutDisplayFunc, etc). After a call to glutMainLoop(), you have no control over the execution of your program besides the callback functions you initialized before the call.

And another thing, the glutDisplayFunc() callback only gets triggered when your app's window receives a refresh event, unless you explicity post a redisplay. If you want your display function to be called repeatedly (i.e. for a game), you need to set your glutIdleFunc to post a redisplay.

Something like this:

int main()
{
    /* ... */
    glutIdleFunc(updateGame);
    /* ... */
   
    glutMainLoop();
}

void updateGame()
{
    glutPostRedisplay();
}


http://www.lighthouse3d.com/tutorials/glut-tutorial/glutpostredisplay-vs-idle-func/

No comments:

Post a Comment