tags:

views:

365

answers:

2

hey guys, i just started off using opengl and it seems it is not easy to understand the working of the glutmainloop() what really happens there? does it stay there doing nothing till any of the function calls responds?

+2  A: 

It calls your display callback over and over, calling idle between so that it can maintain a specific framerate if possible, and others if necessary (such as if you resize the window or trigger an input event).

Essentially, within this function is the main program loop, where GLUT does most of the work for you and allows you to simply set up the specific program logic in these callbacks. It's been a while since I've worked with GLUT, and it is certainly confusing at first.

In your display callback should obviously be your main logic to draw whatever it is that should be going on. In the idle callback should be some very lightweight operations to determine what the change in state should be from the last time display was called to the next time. For example, if you're animating something, this would be where you change its position or orientation.

StrixVaria
Glut has scarred me for life
Joe Philllips
GLUT is painful and it's not pretty...it turned me off from graphics programming, actually.
StrixVaria
Rotation was crazy for me, like 'wtf, there is a z-coordinate?' Once I took physics and learned some rules, everything calmed down
Anthony Forloney
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.
Mr. Berna
+1  A: 

It is exactly as StrixVaria has stated.

glutMainLoop enters the GLUT event processing loop. This routine should be called at most once in a GLUT program. Once called, this routine will never return. It will call as necessary any callbacks that have been registered.

Taken from here

Anthony Forloney