WrapTorus is a C program illustrating the use of OpenGL.   It is intended to accompany the book 3D Computer Graphics: A mathematical approach with OpenGL, by S. Buss, Cambridge University Press, 2003.   However, it can be used independently as well.

WrapTorus draws a torus in a non-standard way, as a ribbon of quad strips that wrap around the torus in one long strip.  The C code consists of two source files, WrapTorus.c and WrapTorus.h.   There are several options available for downloading this program:

Things to try out:

  1. Compile and run the program.  Test out the keyboard controls. 
    1. Press arrow keys (left, right, up and down) to make the torus rotate.
    2. Press "s" to single step the animation,
    3. Press "a" to start the animation running again without stopping.  (Undoes the "s" effect.)
    4. Press "r" to reset the torus back to its original orientation with no velocity.
    5. Press "0" (zero) to zero the velocity of the torus, but leave its position fixed.
    6. Press "W" to increase the number of wraps,  "w" to decrease the number of wraps.
    7. Press "N" to increase the number of segments per wrap.  "n" to decrease the number of segments per wrap.
    8. Press "q" to toggle between drawing quadrangles and triangles.
    9. Press "p" to toggle between rendering in filled polygon mode, and in wireframe mode.
  2. Observe how bad the torus looks in "filled polygon" mode. (Keyboard control: "p".)  This will be improved by the LightTorus program with the addition of material properties and Phong lighting. 
  3. Examine the source code to see how the torus is generated.  In particular, note that integer variables are used to control the loops that draw the vertices on the torus, and we are careful when revisiting a vertex to calculate the x,y,z coordinates of the vertex by exactly the same floating point calculation as the first time.  This avoids having small, pixel-size holes in the torus, as can occur due to floating point roundoff (see the last section of Chapter 2 of the above book).