CSc 433/533: OpenGL and GLUT Tutorial

  1. Double Buffering in GLUT
  2. Double buffering provides two complete color buffers for use in drawing. One buffer is displayed while the other buffer is being drawn into. When the drawing is complete, the two buffers are swapped so that the one that was being viewed is now used for drawing. The swap is almost instantaneous, and thus provides a means of performing animation, like the way a sequence of still photographs, rapidly displayed, appear to depict a moving object.

    GLUT provides commands for setting up and using double buffers on a per window basis. The initial display mode for the window must be set using GLUT_DOUBLE mode (glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE)). The function glutSwapBuffers(), which is typically the last function called in the windows display callback, performs the actual buffer swap.

    Since GLUT is event-driven, you have to do a little more if you want animation without user input. One way to arrange for displaying the next frame of an animation is to arrange for a function to be called whenever GLUT is idle (i.e. when there are no events). This is simply another callback, the idle callback, and it is set up by calling glutIdleFunc(). Each time the idle callback is invoked, you can determine if it's time to draw a new frame.

    Another way to obtain control from GLUT is to set up a timer callback using glutTimerFunc(). This function takes as input a delay (in milliseconds) and a callback function, and will call the callback after the given delay. If the callback registers itself as a new timer function, then it will continue to be called, at the specified interval, forever.