Code samples for the OpenGL v1.1 Programming Guide (Redbook)

Code Samples released by SGI with the OpenGL 1.1 distribution in 1997. These are very useful for beginning OpenGL coding and learning OpenGL program structure. Advanced rendering and later extensions are not covered in these examples.

Download All Sample Code as a single Zip file

This program shows how to draw anti-aliased lines in color index mode. It draws two diagonal lines to form an X; when 'r' is typed in the window, the lines are rotated in opposite directions.

Source code: aaindex.c.


This program shows how to draw anti-aliased lines. It draws two diagonal lines to form an X; when 'r' is typed in the window, the lines are rotated in opposite directions.

Source code: aargb.c.


A simple example of using accumulation buffer to anti-alias.

Source code: accanti.c.


Use the accumulation buffer to do full-scene antialiasing on a scene with perspective projection, using the special routines accFrustum() and accPerspective().

Source code: accpersp.c.


This program draws several overlapping filled polygons to demonstrate the effect order has on alpha blending results. Use the 't' key to toggle the order of drawing polygons.

Source code: alpha.c.


This program demonstrates how to intermix opaque and alpha blended polygons in the same scene, by using glDepthMask. Press the 'a' key to animate moving the transparent object through the opaque object. Press the 'r' key to reset the scene.

Source code: alpha3D.c.


This program uses evaluators to draw a Bezier curve.

Source code: bezcurve.c.


This program renders a lighted, filled Bezier surface, using two-dimensional evaluators.

Source code: bezmesh.c.


This program renders a wireframe Bezier surface, using two-dimensional evaluators.

Source code: bezsurf.c.


This program texture maps a checkerboard image onto two rectangles. This program clamps the texture, if the texture coordinates fall outside 0.0 and 1.0.

Source code: checker.c.


This program demonstrates arbitrary clipping planes.

Source code: clip.c.


After initialization, the program will be in ColorMaterial mode. Interaction: pressing the mouse buttons will change the diffuse reflection values.

Source code: colormat.c.


This program demonstrates a single modeling transformation, glScalef() and a single viewing transformation, gluLookAt(). A wireframe box is rendered.

Source code: cube.c.


This program demonstrates use of the accumulation buffer to create an out-of-focus depth-of-field effect. The teapots are drawn several times into the accumulation buffer. The viewing volume is jittered, except at the focal point, where the viewing volume is at the same position, each time. In this case, the gold teapot remains in focus.

Source code: dof.c.


This is a simple double buffered program. Pressing the left mouse button rotates the rectangle. Pressing the middle mouse button stops the rotation.

Source code: double.c.


Draws the bitmapped letter F on the screen (several times). This demonstrates use of the glBitmap() call.

Source code: drawf.c.


This program demonstrates use of OpenGL feedback. First, a lighting environment is set up and a few lines are drawn. Then feedback mode is entered, and the same lines are drawn. The results in the feedback buffer are printed.

Source code: feedback.c.


This program draws 5 red teapots, each at a different z distance from the eye, in different types of fog. Pressing the left mouse button chooses between 3 types of fog: exponential, exponential squared, and linear. In this program, there is a fixed density value, as well as fixed start and end values for the linear fog.

Source code: fog.c.


This program demonstrates fog in color index mode. Three cones are drawn at different z values in a linear fog. 32 contiguous colors (from 16 to 47) are loaded with a color ramp.

Source code: fogindex.c.


This is a simple, introductory OpenGL program.

Source code: hello.c.


This program demonstrates drawing pixels and shows the effect of glDrawPixels(), glCopyPixels(), and glPixelZoom(). Interaction: moving the mouse while pressing the mouse button will copy the image in the lower-left corner of the window to the mouse position, using the current pixel zoom factors. There is no attempt to prevent you from drawing over the original image. If you press the 'r' key, the original image and zoom factors are reset. If you press the 'z' or 'Z' keys, you change the zoom factors.

Source code: image.c.


This program demonstrates the use of the OpenGL lighting model. A sphere is drawn using a grey material characteristic. A single light source illuminates the object.

Source code: light.c.


This program demonstrates geometric primitives and their attributes.

Source code: lines.c.


This program demonstrates how to make and execute a display list. Note that attributes, such as current color and matrix, are changed.

Source code: list.c.


This program demonstrates the use of the GL lighting model. Several objects are drawn using different material characteristics. A single light source illuminates the objects.

Source code: material.c.


This program demonstrates using mipmaps for texture maps. To overtly show the effect of mipmaps, each mipmap reduction level has a solidly colored, contrasting texture image. Thus, the quadrilateral which is drawn is drawn with several different colors.

Source code: mipmap.c.


This program demonstrates using mipmaps for texture maps. To overtly show the effect of mipmaps, each mipmap reduction level has a solidly colored, contrasting texture image. Thus, the quadrilateral which is drawn is drawn with several different colors.

Source code: model.c.


This program demonstrates when to issue lighting and transformation commands to render a model with a light which is moved by a modeling transformation (rotate or translate). The light position is reset after the modeling transformation is called. The eye position does not change. A sphere is drawn using a grey material characteristic. A single light source illuminates the object. Interaction: pressing the left mouse button alters the modeling transformation (x rotation) by 30 degrees. The scene is then redrawn with the light in a new position.

Source code: movelight.c.


Picking is demonstrated in this program. In rendering mode, three overlapping rectangles are drawn. When the left mouse button is pressed, selection mode is entered with the picking matrix. Rectangles which are drawn under the cursor position are "picked." Pay special attention to the depth value range, which is returned.

Source code: pickdepth.c.


Use of multiple names and picking are demonstrated. A 3x3 grid of squares is drawn. When the left mouse button is pressed, all squares under the cursor position have their color changed.

Source code: picksquare.c.


This program shows how to composite modeling transformations to draw translated and rotated models. Interaction: pressing the d and y keys (day and year) alters the rotation of the planet around the sun.

Source code: planet.c.


This program demonstrates polygon offset to draw a shaded polygon and its wireframe counterpart without ugly visual artifacts ("stitching").

Source code: polyoff.c.


This program demonstrates polygon stippling.

Source code: polys.c.


This program demonstrates the use of some of the gluQuadric* routines. Quadric objects are created with some quadric properties and the callback routine to handle errors. Note that the cylinder has no top or bottom and the circle has a hole in it.

Source code: quadric.c.


This program shows how to composite modeling transformations to draw translated and rotated hierarchical models. Interaction: pressing the s and e keys (shoulder and elbow) alters the rotation of the robot arm.

Source code: robot.c.


This program demonstrates the use of the GL lighting model. Objects are drawn using a grey material characteristic. A single light source illuminates the objects.

Source code: scene.c.


This is an illustration of the selection mode and name stack, which detects whether objects which collide with a viewing volume. First, four triangles and a rectangular box representing a viewing volume are drawn (drawScene routine). The green triangle and yellow triangles appear to lie within the viewing volume, but the red triangle appears to lie outside it. Then the selection mode is entered (selectObjects routine). Drawing to the screen ceases. To see if any collisions occur, the four triangles are called. In this example, the green triangle causes one hit with the name 1, and the yellow triangles cause one hit with the name 3.

Source code: select.c.


This program demonstrates smooth shading. A smooth shaded polygon is drawn in a 2-D projection.

Source code: smooth.c.


This program draws two rotated tori in a window. A diamond in the center of the window masks out part of the scene. Within this mask, a different model (a sphere) is drawn in a different color.

Source code: stencil.c.


This program demonstrates some characters of a stroke (vector) font. The characters are represented by display lists, which are given numbers which correspond to the ASCII values of the characters. Use of glCallLists() is demonstrated.

Source code: stroke.c.


This program draws a NURBS surface in the shape of a symmetrical hill.

Source code: surface.c.


This program demonstrates lots of material properties. A single light source illuminates the objects.

Source code: teapots.c.


This program demonstrates polygon tessellation. Two tesselated objects are drawn. The first is a rectangle with a triangular hole. The second is a smooth shaded, self-intersecting star. Note the exterior rectangle is drawn with its vertices in counter-clockwise order, but its interior clockwise. Note the combineCallback is needed for the self-intersecting star. Also note that removing the TessProperty for the star will make the interior unshaded (WINDING_ODD).

Source code: tess.c.


This program demonstrates the winding rule polygon tessellation property. Four tessellated objects are drawn, each with very different contours. When the w key is pressed, the objects are drawn with a different winding rule.

Source code: tesswind.c.


This program demonstrates using glBindTexture() by creating and managing two textures.

Source code: texbind.c.


This program draws a texture mapped teapot with automatically generated texture coordinates. The texture is rendered as stripes on the teapot. Initially, the object is drawn with texture coordinates based upon the object coordinates of the vertex and distance from the plane x = 0. Pressing the 'e' key changes the coordinate generation to eye coordinates of the vertex. Pressing the 'o' key switches it back to the object coordinates. Pressing the 's' key changes the plane to a slanted one (x + y + z = 0). Pressing the 'x' key switches it back to x = 0.

Source code: texgen.c.


This program texture maps a checkerboard image onto two rectangles. This program clamps the texture, if the texture coordinates fall outside 0.0 and 1.0. If the s key is pressed, a texture subimage is used to alter the original texture. If the r key is pressed, the original texture is restored.

Source code: texsub.c.


This program uses evaluators to generate a curved surface and automatically generated texture coordinates.

Source code: texturesurf.c.


This program demonstrates the creation of a display list.

Source code: torus.c.


This program draws a NURBS surface in the shape of a symmetrical hill, using both a NURBS curve and pwl (piecewise linear) curve to trim part of the surface.

Source code: trim.c.


This program demonstrates vertex arrays.

Source code: varray.c.


This program texture maps a checkerboard image onto two rectangles. This program demonstrates the wrapping modes, if the texture coordinates fall outside 0.0 and 1.0. Interaction: Pressing the 's' and 'S' keys switch the wrapping between clamping and repeating for the s parameter. The 't' and 'T' keys control the wrapping for the t parameter. If running this program on OpenGL 1.0, texture objects are not used.

Source code: wrap.c.