[ Previous | Next | Contents | Glossary | Home | Search ]
GL3.2 Version 4.1 for AIX: Programming Concepts

Animation Example Program

The previous program demonstrated some basic concepts of GL: how to open a window, how to set attributes, and how to draw. The following example program demonstrates how to create an animated scene. When this example program executes, the Hello, World! text moves around in a circle. This is done by clearing and redrawing the text again and again, each time at a new location.

This action is complicated by image flicker, which occurs because the system draws each image quickly, but perceptibly; that is, you do not see each individual character being drawn, only an irregular flashing and flickering. The flashing can be mild to severe, depending on what else is happening in the system or on the screen at that time.

To avoid this flashing, double-buffering is used. The frame buffer is partitioned into two pieces, front and back. The front buffer contains data for the pixels that are visible. The back buffer, which also contains pixel data, is invisible, but identical to the front buffer in other respects. To get smooth animation, never draw to the front buffer; instead, limit all drawing to the back buffer. When drawing is complete, the front and back buffers are swapped, and what was previously hidden is now visible. The result is smooth, flicker-free animation. The actual, step-by-step drawing process is not visible, only the final result. The following example shows how to create an animated scene by using double-buffering:

#include <math.h>

#include <gl/gl.h>
main ()
{
   int i, ix, iy;
   prefsize (200, 100);
   winopen ("HI THERE");
   doublebuffer ();
   gconfig ();
  for (i=1; i<1800; i++) {
     color (BLACK);
     clear ();
     color (GREEN);
     ix = (int) 40.0 * cos (((double) i) / 20.0);
     iy = (int) 40.0 * sin (((double) i) / 20.0);      cmov2 (50+ix, 50+iy);
     charstr ("Hello, World!");
     swapbuffers ();
   }
}

To compile this program, enter the following at the command line prompt:

cc hello2.c -o hello2 -lgl -lm

The -lm flag option on the cc command tells the linker to link to the math library, where the sin and cos functions are located.

The operation of this program is as follows: First, a window is created exactly as before. Next, the system is told to convert this window into a double-buffered window. The program does this in two steps:

  1. Alerts the system with the doublebuffer subroutine.
  2. Sets double-buffering into operation with the gconfig subroutine.

The process requires two steps because there are, in fact, a number of configurations into which a window can be placed. To learn more about configuring the frame buffer, an advanced topic, see "Understanding the Frame Buffer" .

Next, the program goes into a loop that is repeated 1800 times. Inside this loop, we clear the screen and draw the text as before. The sin and cos subroutines are AIX system calls that return the sine and cosine of an angle. They are useful for drawing circular primitives. The program uses the loop counter as an angle, and moves the current character position accordingly.

Finally, when drawing is complete, the swapbuffers subroutine exchanges the front and the back buffers. After looping 1800 times, the program exits and the window disappears.

To learn more about creating animated scenes, see "Creating Animated Scenes".

Note: Not all adapters support double buffering. You must have one of the following:


[ Previous | Next | Contents | Glossary | Home | Search ]