3D OpenGL example using the "glut" library

Written by Paul Bourke
May 1996


OpenGL is currently the dominate and most powerful 3D graphics API available for hardware accelerated interactive graphics.

This document is designed to give people wishing to learn OpenGL a headstart. The examples here use the OpenGL Utility Toolbox, otherwise known as the GLUT library. GLUT is a programming interface with C and FORTRAN bindings for writing window system independent OpenGL programs. While there are a lot of tricky things that can be done using OpenGL this example concentrates on what the author thinks is the most commonly required functionality in an OpenGL based application. If compile the code provided at the bottom of this page, linking it with the appropriate OpenGL and X libraries, it should give you a window not unlike the following. (Sorry the geomerty is so ugly but then it is designed to illustrate specific features not to be attractive).

In fact the above view of the window doesn't tell the whole story, the ball is bouncing and the camera is slowly rotating about the center of the scene. The demo will respond to keyboard events including arrow keys.

>mydemo -h
Usage:    mydemo [-h] [-q n]
          -h   this text
          -q n quality, 0,1,2
Key Strokes and Menus:
           q - quit
           s - toggle camera spin
           b - toggle ball bounce
  left arrow - change rotation direction
 right arrow - change rotation direction
  down arrow - rotate camera down
    up arrow - rotate camera up
The demo includes a couple of menus, see the image below.

The demo also illustrates how picking object in the scene with the mouse is handled, each object in the scene is given a unique ID and this is returned on a mouse click. So for the mouse position below the demo returns "Picked 3 objects: 4 3 2" where object 2 is the ball, object 3 is the box and object 4 is the ground plane.

The demo can be run in one of 3 modes, draft-medium-best quality. The best quality mostly is just for testing antialiasing, the medium mode is what has been used in the above screen shots, the draft mode draws the geometry as wireframes.

An alphabetical list of the OpenGL routines used in the demo is

glBegin
glClear
glClearColor
glColor3f
glColorMaterial
glDisable
glEnable
glEnd
glFlush
glFrontFace
glGetIntegerv
glInitNames
glLightModelfv
glLightfv
glLineWidth
glLoadIdentity
glLoadName
glMaterialfv
glMatrixMode
glNormal3f
glPointSize
glPolygonMode
glPopMatrix
glPushMatrix
glPushName
glRenderMode
glScalef
glSelectBuffer
glShadeModel
glTranslatef
glTranslatef
glTranslatef
glVertex3f
gluLookAt
gluPerspective
gluPickMatrix
glutAddMenuEntry
glutAddSubMenu
glutAttachMenu
glutCreateMenu
glutCreateWindow
glutDisplayFunc
glutIdleFunc
glutInit
glutInitDisplayMode
glutKeyboardFunc
glutMainLoop
glutMouseFunc
glutPostRedisplay
glutSolidCube
glutSolidSphere
glutSpecialFunc
glutStrokeCharacter
glutSwapBuffers
glutVisibilityFunc
glutWireCube
glutWireSphere

Source code

Here is the C source code the generated the example shown above. It has been written in a straightforward style in an attempt to introduce a wide range of glut routines while not getting bogged down in the more "tricky" aspects of OpenGL programming. For more information consult one of the many books or online tutorials.
While the compiler/linker flags will vary between machines, on my SGI they are

cc -o mydemo mydemo.c /usr/lib/libglut.a -lGLU -lGL -lXmu -lXi -lX11 -lm

References

OpenGL Programming Guide
The official Guide to Learning OpenGL
OpenGL Architecture Review Board, J.Neider, T.Davis, M.Woo.

The OpenGL Utility Toolkit (GLUT) Programming Interface
API Version 2, May 1995
Mark J. Killgard

The OpenGL Graphics System, A Specification.
Version 1.0, Silicon Graphics, June 1992
Mark Segal, Kurt Akeley