OpenGL Asteroids

by Apostolos Lerios

OpenGL Asteroids is an X Windows/OpenGL version of the popular asteroids game. It was written as a teaching aid for Stanford's 1996 introductory course in computer graphics (CS 248).

The provided execution environment comprises the executable game as well as parameter files, sample textures and sample sounds. game executes only on Silicon Graphics workstations equipped with OpenGL. For those interested in porting OpenGL Asteroids to other platforms, the source code is also available in tar archive format; use

gunzip -c source.tar.gz | tar xf -
to unpack the archive. This source code was developed on a Silicon Graphics workstation, equipped with the IRIS Digital Media library. It incorporates code specific to OpenGL Asteroids, as well as a general purpose library for graphics applications, adapted from the auxiliary library of The OpenGL Programming Guide.

We provide no support for OpenGL Asteroids.


Copy the provided execution environment, available in tar archive format, to your local disk. Then use
gunzip -c asteroids.tar.gz | tar xf -
to unpack the archive.

Executing game

Executing game as
game -help
will show a short help screen and terminate OpenGL Asteroids.

To play the game,

  1. Create a parameter file. This is a text file containing the desired setup of OpenGL Asteroids, including your preferred sounds and textures. The parameters subdirectory of the execution environment contains several sample parameter files, for platforms with different capabilities:
  2. Disable automatic keystroke repetition, using
    xset r off
    To re-enable automatic keystroke repetition when the game is over, use
    xset r on
  3. Make sure that your current directory is the one containing game.
  4. Execute game as
    game parameters/<parameter file>
    where <parameter file> is the name of a parameter file.

Playing the game

OpenGL Asteroids is a simple game where you navigate your ship in the treacherous asteroid belt, and try to destroy the asteroids before they destroy you. Your weapons comprise a shield of limited energy and a cannon. You control your ship using the following keys and mouse buttons:
Left mouse button
Allows you to position your ship on the game board, when the computer asks you to do so.
Left arrow key
Continuously rotates your ship counter-clockwise.
Right arrow key
Continuously rotates your ship clockwise.
Up arrow key
Continuously accelerates your ship in the direction it is facing.
Space bar
Fires the cannon.
Left shift key
Activates the shield, provided you still have some shield energy left.
The j key
Makes your ship jump through hyperspace. That is, your ship dematerializes, and instantaneously rematerializes at a random position of the game board, possibly in the interior of a fatal asteroid.
The p key
Pauses an on-going game, or resumes a paused game.
The Q key
Destroys your ship.
The escape key
Terminates OpenGL Asteroids.

Technical features

OpenGL Asteroids is a teaching aid. While it may (or may not) be a fun game to play, its main intent is to illustrate several features of OpenGL, use of audio for Silicon Graphics machines, and several other programming techniques.

Collision detection and signal handling

Each asteroid is drawn at a specific depth. Also, all asteroids are drawn closer to the front than the player's ship and the bullets. Collisions are detected after a game board position is fully drawn, in four steps:
  1. The depth buffer values under the bullets are retrieved.
  2. If any of them is equal to a depth value characteristic of an asteroid, or the depth value of the player's ship, the corresponding object is destroyed.
  3. The depth buffer values under the ship are retrieved.
  4. If any of them is equal to a depth value characteristic of an asteroid, the asteroid and the player's ship are both destroyed.
This collision detection scheme has as its primary advantages accuracy to the level of individual pixels and simplicity. It also illustrates a creative use of the depth buffer, as a repository of object IDs. However, this scheme has several drawbacks, too:

Texture mapping, alpha test, and display lists

When texturing is enabled, asteroids are drawn as texture-mapped squares. In particular, at initialization time, a picture file in raw PPM format is read from disk. This picture is loaded onto texture memory, with its black pixels having their alpha channel set to zero. Then, the picture is texture-mapped onto a square as a decal. Finally, the asteroid is rendered with the alpha test enabled and configured to discard fragments with an alpha value of zero. The end result is that the asteroid Moreover, in order to battle the cost of texture mapping, display lists are used. The effect of display lists on performance is substantial, as a texture in a display list resides permanently in the texture memory of the graphics system; using texture mapping without display lists requires reloading the texture memory every time a texture-mapped object is rendered, a very slow operation.

Developers' note: the source code module image.* contains a host of utilities for reading and writing PPM files, reading from and writing images to the framebuffer, and loading images onto texture memory.

Transformation matrices

As stated earlier, asteroids are rendered using a display list. The glVertex*() commands in this display list specify the asteroid's shape relative to a local coordinate system. In order to render an asteroid, a transformation is pushed onto the MODELVIEW stack prior to calling the display list. Thus, each asteroid is translated, rotated, and scaled as appropriate for its particular configuration.

This technique should be used with care, as matrix concatenation is an expensive operation, justified only when many vertices will be subsequently transformed by the compound matrix - or when a display list is used for rendering. Hence, in OpenGL Asteroids, all game board objects other than asteroids are rendered by direct calculation of their transformed coordinates.

Pixel-accurate wrap-around

A mere six lines of code can be used to achieve pixel-accurate wrap-around: that is, making objects which disappear gradually from one side of the screen simultaneously reappear smoothly on the other side. All it takes is rendering the scene nine times, each time shifting the location of all objects prior to rendering, by pushing a matrix onto the MODELVIEW stack. The shift amounts are such that the objects are offset by zero or one screenful to the right, left, top, and bottom - in all meaningful permutations. This is not a a very expensive technique, since clipping quickly discards objects which do not appear on the window.

Audio for Silicon Graphics machines

OpenGL Asteroids makes use of sound to enhance the game-playing experience. It uses the IRIS Digital Media library, which is the standard interface to the audio hardware of Silicon Graphics workstations. Unlike many personal computers, Silicon Graphics workstations support real-time playback of sampled audio signals; but they do not support simple commands generating single tones. Hence, playing any kind of sound is not easy from a programmer's standpoint.

OpenGL Asteroids implements a parallel, real-time, sound player. The main process spawns off child processes, each of which is in charge of playing an audio clip, either until its completion, or indefinitely by repeated looping. This division of labour between the main process and its children is necessary in order to guarantee that sound and graphics both proceed in real-time, without one's performance impinging on the other's. Child processes communicate with the main process using signals, thus allowing the main process to interrupt a child when OpenGL Asteroids terminates, for example.

Developers' note: the source code module audio.* contains a host of utilities for reading and playing AIFF/AIFC files using child processes.

Acknowledgments: the sounds distributed with OpenGL Asteroids were copied from the sound archives of the TV series Star Trek, Ren & Stimpy, and Danger Mouse.

Event-driven programming paradigm

OpenGL Asteroids, combined with our slightly modified version of the auxiliary library of The OpenGL Programming Guide, illustrate event-driven programming. This is a hard programming technique, as it enforces a non-linear control flow within an application: control flows into the application from the auxiliary library, and back out, and back in, and so forth. Here are some examples of event-driven programming using the auxiliary library:

Text file parsing

Parameter files are fairly structured text files: fields are separated by whitespace, and comment lines, starting with the # character, are supported. OpenGL Asteroids reads and parses parameter files, performing full error checking as it does so. Helpful error messages notify the user of missing fields, illegal parameter values, etc.

Other programming techniques

OpenGL Asteroids illustrates several other programming techniques:

User interface issues

OpenGL Asteroids is somewhat user friendly, including user interface features such as the following:

Distribution notice

Copyright © 1996 The Board of Trustees of The Leland Stanford Junior University. All rights reserved.

Permission to use, copy, modify and distribute this software for any purpose is hereby granted without fee, provided that the above copyright notice and this permission notice appear in all copies of this software and that you do not sell the software. Commercial licensing is available by contacting the author.

This software is provided "as is" and without warranty of any kind, express, implied or otherwise, including without limitation, any warranty of merchantability or fitness for a particular purpose.

Author: Apostolos Lerios.

© 2003 Apostolos Lerios