
Lecture on Animation
1 Animation Basics
Animation of simple motion is fairly straightforward. Instead of producing one frame, we produceseveral. The way
this is usually done is via an idle callback. This is similar to other callbacks: we register it and the window system
calls it. This callback, however, is called when the graphics system has nothing better to do — it’s idle. Essentially,
this means that when it’s done rendering your scene, it calls this callback.
If the idle callback does the following:
adjusts some global variables, and
glutPostRedisplay()
The system will continually adjust the variables and redisplay your scene.
What this means is that you need to code your display function so that the computation depends on some global
variables that you will adjust in youridle callback.
Examples:
˜cs307/public_html/demos/animation/MovingMobile.cc This program has an array of part
angles and an array of speeds that is used to increment the part angles. The display function uses the array
of part angles when drawing the mobile.
spinning around an axis. This is what TW programs do when you hit “x” or “y” or “z”: spin the entire scene
around the given axis. We’ll look at the TW code to see how this is done. This is in ˜cs307/public_html/
tw/tw-camera.cc Look at the functions twSpin,startSpinning, and twSpinCommand.
But you didn’t think it was that easy, did you? It’s not. The hard part is always in modeling the physics of the
situation. We’ll look at some hard cases later.
2 Double Buffering
To animate smoothly, we have to use double-buffering. All of our example programs have used double-buffering, but
now we’ll learn about why they do, and the effects of not using double-buffering.
To understand the concept of double-buffering, let’s first look at the problem it solves.
Demo: ˜cs307/public_html/demos/animation/SingleDouble.cc
This program opens two windows, bothof which have an idle callback that rotates a square. One looks like a fine
animation, the other has a terrible flicker. What causes the flickering?
The graphics system is constantly erasing and redrawing the scene. The monitor is constantly refreshing the screen.
(Most modern monitors refresh between 50–100 times per second, so every 10 to 20 milliseconds.) If the screen is
refreshed when the new image is only partly drawn (this includes filling areas in the framebuffer), you’ll see, briefly,
that partial image. That’s what causes the flicker.
The solution is to somehow “synchronize” the two so that the monitor never draws an incomplete image. The way
this is done is:
The monitor reads out from the “front” buffer, while
the graphics system draws into the “back” buffer, and when it’s done,
they swap
The names “front” and “back” buffer are conventional: the front buffer is the one that is “on stage” and the back buffer
is the one that is being prepared forthe next scene.
All throughout this course, our programs have done:
use glInit(GL_DOUBLE...)
1