Square Fountain
Chris Mueller
Fall, 2004
The Square Fountain is a first attempt to create an OpenGL animation.
The fountain starts by placing several hundred squares, layered on top of each other, in the middle of the screen. Each square is given a random velocity and color. In each frame of the animation, the squares move outward from the center according to their velocities. When a square crosses the edge of the screen, it is redrawn in the middle with a new random color.
The Square Fountain can also be run so that the squares bounce against the edges of the screen instead of teleporting back to the center.
Mouse clicks on the drawing window will change the color palette of the squares.
This program uses OpenGL drawing and animation routines. GLUT (the OpenGL Utility Toolkit) provides a simple interface for interacting with windows—including window resizing and mouse and keyboard events. Specifically, the Square Fountain uses glRectf to draw squares, callback functions with glutDisplayFunc and glutIdleFunc to manage the animation, and a callback function with glutMouseFunc to handle mouse events.
What is a callback function? Callback functions are powerful mechanisms used by OpenGL to allow programmers to specify what they wish to have happen when certain events happen within OpenGL. For example, the programmer can write a function doKeyboard(...) that he wishes to have called whenever a button on the keyboard is pressed. This function could check to see whether the ‘R' key was pressed, and if so it could restart the animation. But how will the programmer know when a button is pressed? OpenGL takes care of all this internally. All the programmer needs to do is pass his doKeyboard(…) function as the parameter to glutKeyboardFunc. Then, whenever GLUT receives a keyboard event, it makes a callback to doKeyboard, and the code inside doKeyboard is executed.
| CG Index | Square Fountain | Van Gogh | Raytracer | Computer Vision |