About the OpenGL/GLUT Display Callback:

So some of you may be confused about what goes into the display callback, and how it works. The idea of this page is to clarify what I think should go in there. Obviously, this is not set in stone -- this is just my suggestion. You may approach it another way.

What does the display callback do? I think of the display callback as the "draw the frame" function. If you think of it that way, you need the following things:

  1. Clear various buffers (e.g., the color buffer, the depth buffer, etc.)
  2. Set up your viewpoint.
  3. If using lighting (and not simple RGB colors), set up your lights.
  4. Enable effects (such as culling, depth testing, shade mode, etc.) to be applied to all objects in the scene.

  5. Perhaps there are transformations to be applied to all objects (but not lights). Put them here.

  6. Draw objects. This involves a number of substeps:
    1. I suggest you call glPushMatrix() so that you do not mistakenly apply transformations to the wrong objects. This certainly isn't always necessary, especially if you really understand how OpenGL matrices work.
    2. Enable effects for this single object (things like the object texture, special shaders for this object, etc)
    3. Apply per-object transformations.
      • Since each call to glMultMatrixf(), glTranslatef(), glScalef(), et cetera multiplies the current matrix on the right (i.e., C = C * M_new), they are applied in the reverse of the order you specify them.
    4. Start drawing ( with glBegin(...) ). For each vertex, call the appropriate function to set up vertex attributes:
      • Vertex color ( use glColor*() )
      • Vertex normal ( use glNormal*() )
      • Vertex texture coordinate ( use glTexCoord*() )
      • Vertex position ( use glVertex*(), note all other vertex attributes should come before glVertex() )
    5. Finish drawing ( with glEnd() ).
    6. Disable effects applied to only this object.
    7. Call glPopMatrix() if you called glPushMatrix() in Step 6-1.

  7. Perform cleanup.
  8. Flush the GL buffer so all your geometry is drawn (optional, but it does not hurt, and can avoid some problems).

  9. If you're using a doubled-buffered window make sure to call glutSwapBuffers() so what you just drew appears on screen.


Last Modified: Thursday, September 23, 2004

Chris Wyman (cwyman@cs.uiowa.edu)