Open GL Super Bible

Previous Table of Contents Next

Chapter 13
Quadrics: Spheres, Cylinders, and Disks

What you’ll learn in this chapter:

How to… Functions You’ll Use

Create quadrics to draw simple geometric shapes gluNewQuadric
Control the quality of drawn shapes
Draw the shapes using different OpenGL primitives gluQuadricDrawStyle
Use lighting and texturing with quadrics gluQuadricNormals/gluQuadricTexture

We can hear you asking: “What the heck are quadrics?” Well, quadrics are a part of the OpenGL Utility Library (GLU32.LIB) that supports the drawing of simple three-dimensional geometric shapes. In particular, functions are provided to draw cones, cylinders, disks, and spheres. In this chapter we’ll explore the practical uses of these quadric functions in your programs.

Creating a Quadric

Every quadric you draw on the screen has a state (or collection of settings) associated with it. The gluNewQuadric function creates an opaque state variable that describes the current drawing style, orientation, lighting mode, texturing mode, and callback functions, as follows:

GLUquadricObj *obj;

obj = gluNewQuadric();

Note that a quadric state does not include the geometric shape to be drawn. Instead, it describes how to draw geometric shapes. This allows you to reuse quadrics for many different kinds of shapes.

Changing the Way Quadrics Are Drawn

Once you have created a quadric, you can customize the drawing of shapes by changing the quadric state. The GLU functions for this are gluQuadricDrawStyle, gluQuadricNormals, gluQuadricOrientation, and gluQuadricTexture.

void gluQuadricDrawStyle(GLUquadricObj *obj, GLenum drawStyle)
void gluQuadricNormals(GLUquadricObj *obj, GLenum normals)
void gluQuadricOrientation(GLUquadricObj *obj, GLenum orientation)
void gluQuadricTexture(GLUquadricObj *obj, GLboolean textureCoords)

The gluQuadricDrawStyle function selects the type of OpenGL drawing primitives that are used to draw the shape. The default style is to fill shapes using polygon and strip primitives (GLU_FILL). Table 13-1 shows the possible styles.

Table 13-1 Quadric Drawing Styles

Style Description

GLU_FILL Quadrics are drawn filled in, using polygon and strip primitives.
GLU_LINE Quadrics are drawn “wireframe,” using line primitives.
GLU_SILHOUETTE Quadrics are drawn using line primitives; only the outside edges are drawn.
GLU_POINT Quadrics are drawn using point primitives.

Lighting normals are usually generated automatically for quadrics. The gluQuadricNormals function controls calculation of normals. Table 13-2 lists the possible lighting calculations.

Table 13-2 Quadric Lighting Normal Modes

Normal Mode Description

GLU_NONE No lighting normals are generated.
GLU_FLAT Lighting normals are generated for each polygon to create a faceted appearance.
GLU_SMOOTH Lighting normals are generated for each vertex to create a smooth appearance.

To control the direction of lighting normals, the gluQuadricOrientation function is provided to make normals point outwards (GLU_OUTSIDE) or inwards (GLU_INSIDE). This has particular application with spheres (if you are inside or outside the sphere).

Finally, texture coordinates can be generated automatically for your quadrics. The gluQuadricTexture function enables (GL_TRUE) or disables (GL_FALSE) texture coordinate generation. We’ll cover exactly how texture coordinates are chosen as we start drawing quadrics on the screen.

As you may remember, texture coordinates are used for texture mapping images onto polygons (see Chapter 12).

Drawing Cylinders

Cylinders are drawn using gluCylinder. A cylinder drawn with this function is essentially a tube that runs along the z-axis (see Figure 13-1). The ends of the cylinder are never filled in!

Figure 13-1  Quadric cylinders

void gluCylinder(GLUquadricObj *obj,
                 GLdouble baseRadius,
                 GLdouble topRadius,
                 GLdouble height,
                 GLint slices,
                 GLint stacks)

The baseRadius and topRadius arguments specifiy the radius of the cylinder at the bottom and top of the cylinder. The height argument specifies the actual height (or length) of the tube.

The slices and stacks arguments control how many subdivisions (sides) are generated around and along the cylinder. Generally, you will make slices a number around 20 to give the cylinder a smooth appearance. Values below this will yield a faceted appearance; values greater than 20 may cause display jitter. When you utilize spotlighting or a lot of specular highlights, you will also want the stacks argument set high, usually the same as the height argument. Otherwise, set stacks to 2 to cover the top and bottom of the cylinder.

Cylinders can also be employed for the generation of faceted surfaces, such as a pencil or a tool socket.

Drawing Cones

While the OpenGL Utility Library does not include a special cone-drawing function, the gluCylinder function can be used to make cones simply by specifying a topRadius or bottomRadius of 0.0.

Texturing and Cylinders

When texturing a gluCylinder shape, textures are wrapped from the forward edge (0,radius,0) of the cylinder. This means your texture images should be upside-down to display properly on the cylinder. We’ll use textures with cylinders in the pencil project in this chapter.

Previous Table of Contents Next