
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