The typical structure of the main() function of a GLUT program is:
glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(512,512); |
Set initial window options
|
glutCreateWindow(argv[0]); |
Open window
|
glutDisplayFunc(drawEverything); glutKeyboardFunc(key); glutSpecialFunc(specialkey); glutIdleFunc(idle); |
Define callback functions
|
myGLinit(); |
Do some OpenGL stuff
|
glutMainLoop(); |
Run GLUT's event loop
|
The GLUT main loop detects and responds to events. "Events" include
such things as keys being pressed, the mouse moving, or the window being resized.
glutPostRedisplay() generates a special event indicating that the drawing
function should be called again.
The main loop has the following rough structure:
There are generally two approaches to how a program redraws:
Which approach is used defines where glutPostRedisplay() is called - in the event-handling functions, or in the idle function.
Programs that animate their scenes in real-time, not just in immediate response to user inputs, will want to redraw continually, and so call glutPostRedisplay() in their idle function.