Simple animation is surprisingly easy. OpenGL draws a succession of frames, starting the next one the moment the previous one is finished. Technically, it invokes the idle callback function whenever it is idle, so by putting code in that callback that
A moderately complex demo is
tw/demos/animation/MovingMobile.ccUse the "2" keyboard callback to start/stop spinning. Use the "3" keyboard callback to go one step at a time. Look at the code to see how these work.
Here's the code to start/stop spinning. To register a function as the idle callback, use the first of the following, and the second to turn it off:
if(Spinning)
glutIdleFunc(spin);
else
glutIdleFunc(NULL);
break;
The hard part of animation can be:
tw/demos/animation/Laser.cc
Use the '+' key to start the animation, and 'p' to pause it.
An important issue comes up that is demonstrated by the following demo:
tw/demos/animation/SingleDouble.cc
Essentially, you want to synchronize with the display updating process, which is reading out from the frame buffer. You never want it to be refreshing the display from a partially completed picture.
This demo doesn't work on the Macs, because Macs automatically double-buffer for us. However, it does work on the Linux machines, so try it!
I've written this up in the reading. Remember on the Linux machines to do your work (save all your frames, that is, your PPM files) in /tmp, so that you don't run into any quota issues.
If there's time, let's spend a bit more time on the tw/demos/animation/MassSpring.cc animation. Here is a good web sites about Mass Spring systems; there are many others.
"Tweening" is when software computes the intermediate frames between two positions. Maya will do this. It's essentially a linear mapping (though it could, in principle, be quadratic or cubic, if we gave more key frames). We've done many linear mappings this semester.
How would you design software to let you do tweening easily? For example, a robotic arm? What are some difficulties?
Make an animation of the barn growing:
Here's a result, not necessarily ideal, but good enough for a short exercise. We'll discuss deficiencies.
tw/demos/animation/animationLab.cc
Let's discuss how to create interesting objects:
So far, our animations run as fast as the machine is capable of: as soon as it's done rendering a scene, it becomes idle, the idle callback is executed, and the machine starts rendering the next scene. There are two drawbacks this:
To have the state variables updated when a certain amount of time has passed, rather than as soon as possible (that is, when the machine is idle), we specify a timer callback, using:
void glutTimerFunc(unsigned int msecs, void (*func)(int value), value);
Arguments:
Let's talk about how to read that information from the man page.
The tw/demos/animation/Timer.cc demo shows this in action.
Written by Scott D. Anderson
scott.anderson@acm.org

This work is licensed under a Creative Commons
License.