State Management and Drawing Geometric Objects
After reading this chapter, you'll be able to do the following:
- Clear the window to an arbitrary color
- Force any pending drawing to complete
- Draw with any geometric primitive - points, lines, and
polygons - in two or three dimensions
- Turn states on and off and query state variables
- Control the display of those primitives - for example,
draw dashed lines or outlined polygons
- Specify normal vectors at appropriate points on the surface
of solid objects
- Use vertex arrays to store and access a lot of
geometric data with only a few function calls
- Save and restore several state variables
Although you can draw complex and interesting pictures using
OpenGL, they're all constructed from a small number of primitive graphical items.
This shouldn't be too surprising - look at what Leonardo da Vinci accomplished
with just pencils and paintbrushes.
At the highest level of abstraction, there are three basic
drawing operations: clearing the window, drawing a geometric object, and drawing
a raster object. Raster objects, which include such things as two-dimensional
images, bitmaps, and character fonts, are covered in Chapter 8. In this chapter, you learn how to clear the screen and to draw geometric
objects, including points, straight lines, and flat polygons.
You might think to yourself, "Wait a minute. I've seen
lots of computer graphics in movies and on television, and there are plenty
of beautifully shaded curved lines and surfaces. How are those drawn, if all
OpenGL can draw are straight lines and flat polygons?" Even the image on
the cover of this book includes a round table and objects on the table that
have curved surfaces. It turns out that all the curved lines and surfaces you've
seen are approximated by large numbers of little flat polygons or straight lines,
in much the same way that the globe on the cover is constructed from a large
set of rectangular blocks. The globe doesn't appear to have a smooth surface
because the blocks are relatively large compared to the globe. Later in this
chapter, we show you how to construct curved lines and surfaces from lots of
small geometric primitives.
This chapter has the following major sections:
- "A Drawing Survival Kit"
explains how to clear the window and force drawing to be completed. It also
gives you basic information about controlling the color of geometric objects
and describing a coordinate system.
- "Describing Points, Lines, and Polygons"
shows you what the set of primitive geometric objects is and how to draw them.
- "Basic State Management"
describes how to turn on and off some states (modes) and query state variables.
- "Displaying Points, Lines, and Polygons"
explains what control you have over the details of how primitives are drawn
- for example, what diameter points have, whether lines are solid or dashed,
and whether polygons are outlined or filled.
- "Normal Vectors"
discusses how to specify normal vectors for geometric objects and (briefly)
what these vectors are for.
- "Vertex Arrays"
shows you how to put lots of geometric data into just a few arrays and how,
with only a few function calls, to render the geometry it describes. Reducing
function calls may increase the efficiency and performance of rendering.
- "Attribute Groups"
reveals how to query the current value of state variables and how to save
and restore several related state values all at once.
- "Some Hints for Building Polygonal Models
of Surfaces" explores the issues and techniques involved in constructing polygonal
approximations to surfaces.
One thing to keep in mind as you read the rest of this chapter
is that with OpenGL, unless you specify otherwise, every time you issue a drawing
command, the specified object is drawn. This might seem obvious, but in some
systems, you first make a list of things to draw. When your list is complete,
you tell the graphics hardware to draw the items in the list. The first style
is called immediate-mode graphics and is the default OpenGL style. In
addition to using immediate mode, you can choose to save some commands in a
list (called a display list) for later drawing. Immediate-mode graphics
are typically easier to program, but display lists are often more efficient.
Chapter 7 tells you how to use display lists and why you might want to use them.