OpenGL Examples
Here are some OpenGL example programs, presented in an
order in which new topics are added in each successive example.
Triangle
- Introductory program; just a static picture of a colored triangle.
- Shows how to use GLUT.
- Has minimal structure: only main() and a display callback
- Uses only the default viewing parameters (in fact, it never
mentions viewing at all). This is an orthographic view volume
with bounds of -1..1 in all three dimensions.
- Draws only using glColor and glVertex within
glBegin and glEnd in the display
callback.
- Uses only the GL_POLYGON drawing mode.
- Illustrates glClear and glFlush.
Tetrahedron
- Displays a static picture of a tetrahedron sitting on a grid "floor".
- Sets up a viewing volume at the beginning of the program. Uses
glFrustum in order to make a perspective projection.
- Shows that you have to define the viewing volume when the matrix
mode is GL_PROJECTION.
- Draws the object in the display callback using GL_LINES
and GL_TRIANGLE_STRIP modes.
- Defines the object at the origin then moves it INTO the view volume.
- Shows that the rotations and translations that move the object
into the view volume must be performed when the matrix mode
is GL_MODELVIEW.
- Shows that if you are not doing animation it is best to define
the transformations that position and orient the object should
be done only once, and not inside the display callback.
- Shows that transformations are cumulative.
- Shows that before setting up view volumes or object transformations,
you usually want to do a glLoadIdentity or else
transforamtions from previous manipulations will accumulate.
- Does backface culling as a very cheap way to do hidden surface
removal. This does not work in all cases.
Torus
- Display a static picture of a torus and the coordinate system axes.
- Instead of using a primitive GL function (such as glOrtho
or glFrustum) to set the viewing volume, it instead uses
gluPerspective. gluPerspective must be done
when the matrix mode is GL_PROJECTION.
- Instead of using translates and rotates to position the objects
into the view volume, it instead uses gluLookAt
to position and orient the camera so that it is looking at the
objects. gluLookAt must be done when the matrix
mode is GL_MODELVIEW.
- Shows that the combination of gluPerspective and
gluLookAt is considerably more intuitive than the
more primitive mechanism of glFrustum and plain
object transformations.
- Draws the torus using the fancy glutWireTorus function
(which internally calls glVertex a zillion times).
Sierpinski2D
- Draws 20000 or so points of a Sierpinski Gasket.
- Draws with GL_POINTS.
- Illustrates very poor programming practice: the display callback
contains long-running code. The program will appear to
hang if you wanted to draw, say, twenty million points.
Sierpinski3D
- Draws points in the iteration of a 3-D Sierpinski Gasket for
as long as the program runs.
- Illustrates the right way to perfom long-running drawing: in bursts
during the idle time callback. The display callback doesn't do
any drawing, other than clearing the windows (er, color buffer, I mean).
- Uses depth buffering so that points that would appear behind points
already drawn will not themselves be drawn.
- Shows that depth buffering must be done by specifying the GL_DEPTH
flag in glutInitDisplayMode and enable GL_DEPTH_TEST.
- Illustrates the reshape callback, which is absolutely essential
for preserving the aspect ratio of the drawing.
- Shows that in order to preserve the aspect ration of the drawing,
you set up the camera (in this case using gluPerspective)
every time the window is reshaped. Note that the
reshape callback is automatically called by GLUT shortly after
the windowing system creates your window, so you don't need to force
a call to a camera setup at the beginning of your program.
- Sets colors based on depth to make the picture look pretty cool.
Spinning Square
- Animates a green spinning square.
- Illustrates animation with double buffering. This requires passing
GLUT_DOUBLE to glutInitDisplayMode, generating the
parameters for the next animation frame in the idle callback
which posts a redisplay request, and finishing off the display
callback with a call to glutSwapBuffers.
- Shows that it is better to accumulate transformation parameters,
rather than the transformations themselves, when doing cyclical
animation.
- Illustrates the mouse callback.
- Shows that callbacks can be removed. In this case, the idle
callback gets removed.
- Shows that maintaining the aspect ratio of a drawing when an
orthographic projection is used is a bigger pain than doing so with
a perspective projection using gluPerspective.
Interactive Robot Arm
- Lets you move a robot arm, with a shoulder and an elbow, with the arrow
keys.
- Is a very simple program that manages to illustrate the very important
concept of hierarchical modeling.
- Shows that to draw an object in the context of a "parent" object
you arrange drawing and matrix manipulation code in a certain order.
- Illustrates glPushMatrix and glPopMatrix
(note how this makes myWireBox "reusable" in any context).
- Shows that the keyboard callback is called the "special" callback
in GLUT.
Color Cube Flyby
- Is our first example of a "flyby" program.
- Performs animation by moving the camera on a closed orbit around
a stationary object, which just happens to be the standard
RGB color cube.
- Shows that on flybys, you set the camera shape (gluPerspective
in GL_PROJECTION mode) in the reshape callback, but set
the camera position and orientation (gluLookAt in
GL_MODELVIEW mode) for every frame you draw.
- Draws with GL_QUADS and the moderately ugly
glVertex3iv function.
Comet Ride
- Animates the sun and earth from the point of view of a comet.
- Is another flyby with a predefined flight path.
- Flies around a scene with multiple objects which themselves are moving.
Lit Solids
- Displays a static picture of three cyan solids lit by a single
yellow light source.
- Shows that lighting, like depth buffering, is something that
must be enabled.
- Uses ambient, diffuse and specular parameters for lighting.
- Uses a directional light source, as opposed to a point light source.
- Illustrates three of the GLUT easy shape functions.
- Each object is independently moved into the viewing volume,
showing the importance of glPushMatrix and
glPopMatrix.
Phases of the Moon
- Simulates the phases of the moon with a simple animation.
- Draws with display lists!
- Shows that disply lists are defined once, but called many times,
and hence are virtually necessary in all nontrivial animations
that require high performance.
- Puts the lighting definitions in the display lists for cool
effects.
Bouncing Balls
- Shows balls bouncing on a checkerboard.
- Features a point light source.
- Moves the camera in response to key presses.
Flight Simulator
- Is a flyby that is controlled by user interaction (the previous
flybys moved the camera along a predermined orbit).
- Makes use of several supporting modules:
- geometry.h: a
module defining basic classes for points, lines, planes and rays.
- ship.h: a
class representing the control of an interactive spaceship:
you can set its position, speed, orientation, and direction
in different ways. Uses real mathematics!
- landscape.h
and landscape.cpp:
a class for fractal landscapes.
- cockpit.h: a
class with a draw method which renders a ship's
cockpit, sort of.
- Illustrates rendering a scene in which different parts of the
scene use different projections and views.
Fish Bitmaps
- A first program illustrating bitmaps, drawing twenty instances
of a fish bitmap at random positions in a window.
- Uses only the default settings for all pixel store
settings, so the program is not very fancy.
Checkered
Triangles
- A first program illustration of textures.
- Illustrates how to construt textures "by hand"
- Illustrates some pixel store and texture settings.
Spinners (Win32 Only)
- Is an example from the Win32 documentation which I cleaned up.
- Illustrates how to use "raw" OpenGL from Win32, without the use
of a windowing toolkit like GLUT. Shows that this is a lot
more involved than using GLUT; in Win32 you need to deal with
redering contexts, device contexts and pixel formats.