Open GL Super Bible

Previous Table of Contents Next


Part II
Using OpenGL

It seems that every programming language class in college started with that same goofy “How many miles per gallon did you get on the way to New York” example program. First you needed to learn to use the terminal, then the editor, compiler, and linker, how the programs were structured, and finally some language syntax. Unfortunately, we must all learn to crawl before we can walk, and learning OpenGL is no exception.

Part I of this book introduced OpenGL, the hows and whys of 3D, and the format of OpenGL functions. Then we started gluing this to the Windows API, building Windows-based programs that used OpenGL to paint in the client area. We learned how to look for errors, how to interpret them, and how to make sure we don’t take advantage of features that don’t exist!

Now it’s time to graduate from our baby walkers and start stumbling across the room. First, in Chapter 6, we’ll cover all the OpenGL drawing primitives. You’ll use these building blocks to make larger and more complex objects. Next you’ll find out about all the things you can do in 3D space with your newfound object-building tools: translation, rotation, and other coordinate transformation goodies. Walking with more confidence, you’ll be ready for Chapters 8 and 9, which give you color, shading, and lighting for photo-realistic effects. The remaining chapters offer advanced object-manipulation tools, techniques for juggling images and texture maps with ease, and some more specialized 3D object primitives.

When you’re done with Part II, you’ll be ready for your first 100-yard dash! By the end of the book, the Olympics!

Be sure and follow along with the tank/robot simulation development that starts in this section of the book. This special sample program won’t be discussed in the chapters ahead, and can only be found on the CD, where the simulation will be enhanced with that chapter’s techniques and functions. The readme.txt file for each step discusses the enhancements along the way.

Anybody else tired of bouncing squares? Read on! Now we’re into the good stuff!

Chapter 6
Drawing in 3D: Lines, Points, and Polygons

What you’ll learn in this chapter:

How To… Functions You’ll Use

Draw points, lines, and shapes glBegin/glEnd/glVertex
Set shape outlines to wireframe or solid objects glPolygonMode
Set point sizes for drawing glPointSize
Set line drawing width glLineWidth
Perform hidden surface removal glCullFace
Set patterns for broken lines glLineStipple
Set polygon fill patterns glPolygonStipple

If you’ve ever had a chemistry class (and probably even if you haven’t), you know that all matter is made up of atoms, and that all atoms consist of only three things: protons, neutrons, and electrons. All the materials and substances you have ever come into contact with—from the petals of a rose to the sand on the beach—are just different arrangements of these three fundamental building blocks. Although this is a little oversimplified for most anyone beyond the third or fourth grade, it demonstrates a powerful principle: With just a few simple building blocks, you can create highly complex and beautiful structures.

The connection is fairly obvious. Objects and scenes that you create with OpenGL are also made up of smaller, simpler shapes, arranged and combined in various and unique ways. In this chapter we will explore these building blocks of 3D objects, called primitives. All primitives in OpenGL are one- or two-dimensional objects, ranging from single points to lines and complex polygons. In this chapter you will learn everything you need to know in order to draw objects in three dimensions from these simpler shapes.

Drawing Points in 3D

When you first learned to draw any kind of graphics on any computer system, you usually started with pixels. A pixel is the smallest element on your computer monitor, and on color systems that pixel can be any one of many available colors. This is computer graphics at its simplest: Draw a point somewhere on the screen, and make it a specific color. Then build on this simple concept, using your favorite computer language to produce lines, polygons, circles, and other shapes and graphics. Perhaps even a GUI…

With OpenGL, however, drawing on the computer screen is fundamentally different. You’re not concerned with physical screen coordinates and pixels, but rather positional coordinates in your viewing volume. You let OpenGL worry about how to get your points, lines, and everything else translated from your established 3D space to the 2D image made by your computer screen.

This chapter and the next cover the most fundamental concepts of OpenGL or any 3D graphics toolkit. In the upcoming chapter, we’ll go into substantial detail about how this transformation from 3D space to the 2D landscape of your computer monitor takes place, as well as how to manipulate (rotate, translate, and scale) your objects. For now, we shall take this ability for granted in order to focus on plotting and drawing in a 3D coordinate system. This may seem backwards, but if you first know how to draw something, and then worry about all the ways to manipulate your drawings, the material coming up in Chapter 7 will be more interesting and easier to learn. Once you have a solid understanding of graphics primitives and coordinate transformations, you will be able to quickly master any 3D graphics language or API.

Setting Up a 3D Canvas

Figure 6-1 shows a simple viewing volume that we will use for the examples in this chapter. The area enclosed by this volume is a Cartesian coordinate space that ranges from –100 to +100 on all three axes, x, y, and z. (For a review of Cartesian coordinates, see Chapter 2.) Think of this viewing volume as your three-dimensional canvas on which you will be drawing with OpenGL commands and functions.


Figure 6-1  Cartesian viewing volume measuring 100 x 100 x 100


Previous Table of Contents Next