Open GL Super Bible

Previous Table of Contents Next


Drawing Disks

Disks are round, flat shapes that may contain holes. Examples of disks include coins and washers.

void gluDisk(GLUquadricObj *obj,
             GLdouble innerRadius,
             GLdouble outerRadius,
             GLint slices,
             GLint loops)

The innerRadius and outerRadius arguments control the size of the hole and disk, respectively. If the innerRadius argument is 0.0, the disk is drawn as a solid circle (see Figure 13-2).


Figure 13-2  Quadric disks

The slices argument sets the number of sides the disk has and generally should be a number around 20 to make the disk look round. The loops argument controls the number of concentric rings that are drawn for the disk (between the inner and outer radii); this usually should be set to 1 for circles and 2 for washers. As is true for cylinders, using larger values for loops will improve specular lighting and spotlight effects.

Disks and Textures

Texture images for disks are mapped so that the texture image just touches the cylinder at the edges. The top of the texture image is mapped to the top of the disk, the left side to the left side of the disk, and so forth.

Drawing Partial Disks

The OpenGL Utility Library also provides a function to display partial disks. When drawing a partial disk, you specify a start angle and sweep angle for the disk. The startAngle argument specifies a clockwise angle in degrees from the top of the disk. The sweepAngle argument specifies the number of degrees of arc to draw. For instance, 90? would be a quarter disk, and so forth.

void gluPartialDisk(GLUquadricObj *obj,
             GLdouble innerRadius,
             GLdouble outerRadius,
             GLint slices,
             GLint loops,
	        GLdouble startAngle,
             GLdouble sweepAngle)

Drawing Spheres

Spheres are hollow balls or globes. When you draw a sphere, you specify the radius of the sphere.

void gluSphere(GLUquadricObj *obj,
               GLdouble radius,
               GLint slices,
               GLint stacks)

If you think of the sphere as a globe, the slices argument represents the number of lines of longitude, and the stacks argument represents the number of lines of latitude (see Figure 13-3).


Figure 13-3  A quadric sphere

Spheres and Textures

Texture images are mapped to spheres using longitude and latitude coordinates. A world map image would wrap perfectly around the sphere.

Drawing a Pencil

To close this chapter, we’ll write a little program that rotates an image of a pencil (see Figure 13-4). The pencil consists of three cylinders and two texture images. The first texture image has the typical symbol for a #2 pencil, and the words “OpenGL Country Club” wrapped around the pencil. For the end and the sharpened point of the pencil, we’ll use a second image of wood with exposed lead (well, carbon).


Figure 13-4  Quadric pencil window

The point of the pencil, obviously, is a cone. The end of the pencil isn’t quite as obvious. Since it’s flat, you might expect to use a disk for the end. Unfortunately, the result of the way texture images are applied to disks doesn’t look right with our texture image (see Figure 13-5). So instead, the end is made using a cylinder with a height and topRadius of 0.0.


Figure 13-5  Pencil and lead texture images

Since quadrics are drawn from (0, 0, 0), you have to translate the coordinates of the pieces prior to drawing them. For example, to draw the body of the pencil you would do this:

glPushMatrix();
  glTranslatef(0.0, 0.0, -20.0);
  gluCylinder(PencilObj, 5.0, 5.0, 40.0, 6, 2);
glPopMatrix();

In the pencil drawing program, Listing 13-1, the RepaintWindow function handles drawing everything. The first thing we display is the body of the pencil, which is a six-sided cylinder.

gluQuadricNormals(PencilObj, GLU_FLAT);
glCallList(PencilTexture);

glPushMatrix();
  glTranslatef(0.0, 0.0, -20.0);

  gluCylinder(PencilObj, 5.0, 5.0, 40.0, 6, 2);
glPopMatrix();

Next, we display the point and end of the pencil using the “lead” texture image. Again, we’ll use six-sided cylinders to do the work we need.

gluQuadricNormals(PencilObj, GLU_SMOOTH);
glCallList(LeadTexture);

glPushMatrix();
  glTranslatef(0.0, 0.0, 20.0);

  gluCylinder(PencilObj, 5.0, 0.0, 7.5, 6, 2);
glPopMatrix();

glPushMatrix();
  glTranslatef(0.0, 0.0, -20.0);

  gluCylinder(PencilObj, 5.0, 0.0, 0.0, 6, 2);
glPopMatrix();


Previous Table of Contents Next