Open GL Super Bible

Previous Table of Contents Next


Chapter 8
Color and Shading

What you’ll learn in this chapter:

How to… Functions You’ll Use

Specify a color in terms of RGB components glColor
Set the shading model glShadeModel
Create a 3-3-2 palette CreatePalette
Make use of a palette RealizePalette, SelectPalette, UpdateColors

At last we are going to talk about color! This is perhaps the single most important aspect of any graphics library—even above animation support. You must remember one thing as you develop graphics applications: In this case, the old adage isn’t true; looks ARE everything! Don’t let anyone tell you otherwise. Yes, it’s true that features, performance, price, and reliability are important factors when you’re selecting and working with a graphics application, but let’s face it—on the scales of product evaluation, looks have the largest impact most of the time.

If you want to make a living in this field, you cannot develop just for the intellectual few who may think as you do. Go for the masses! Consider this: Black-and-white TVs were cheaper to make than color sets. Black-and-white video cameras, too, were cheaper and more efficient to make and use—and for a long time they were more reliable. But look around at our society today and draw your own conclusions. Of course, black-and-white has its place, but color is now paramount. (Then again, we wish they hadn’t colorized all those Shirley Temple movies…)

What Is a Color?

First let’s talk a little bit about color itself. How is a color made in nature, and how do we see colors? Understanding color theory and how the human eye sees a color scene will lend some insight into how you create a color programmatically. (If color theory is old hat to you, you can probably skip this section.)

Light as a Wave

Color is simply a wavelength of light that is visible to the human eye. If you had any physics classes in school, you may remember something about light being both a wave and a particle. It is modeled as a wave that travels through space much as a ripple through a pond; and it is modeled as a particle, such as a raindrop falling to the ground. If this seems confusing, you know why most people don’t study quantum mechanics!

The light you see from nearly any given source is actually a mixture of many different kinds of light. These kinds of light are identified by their wavelengths. The wavelength of light is measured as the distance between the peaks of the light wave, as illustrated in Figure 8-1.


Figure 8-1  How a wavelength of light is measured

Wavelengths of visible light range from 390 nanometers (one billionth of a meter) for violet light, to 720 nanometers for red light; this range is commonly called the spectrum. You’ve undoubtedly heard the terms ultraviolet and infrared; these represent light not visible to the naked eye, lying beyond the ends of the spectrum You will recognize the spectrum as containing all the colors of the rainbow. See Figure 8-2.


Figure 8-2  The spectrum of visible light

Light as a Particle

“OK, Mr. Smart Brain,” you may ask, “If color is a wavelength of light and the only visible light is in this 'rainbow’ thing, where is the brown for my Fig Newtons or the black for my coffee, or even the white of this page?” We’ll begin answering that question by telling you that black is not a color; nor is white. Actually, black is the absence of color, and white is an even combination of all the colors at once. That is, a white object reflects all wavelengths of colors evenly, and a black object absorbs all wavelengths evenly.

As for the brown of those fig bars and the many other colors that you see, they are indeed colors. Actually, at the physical level they are composite colors. They are made of varying amounts of the “pure” colors found in the spectrum. To understand how this works, think of light as a particle. Any given object when illuminated by a light source is struck by “billions and billions” (my apologies to Carl Sagan) of photons, or tiny light particles. Remembering our physics mumbo jumbo, each of these photons is also a wave, which has a wavelength, and thus a specific color in the spectrum.

All physical objects are made up of atoms. The reflection of photons from an object depends on the kinds of atoms, the amount of each kind, and the arrangement of atoms in the object. Some photons will be reflected and some will be absorbed (the absorbed photons are usually converted to heat), and any given material or mixture of materials (such as your fig bar) will reflect more of some wavelengths than others. Figure 8-3 illustrates this principle.


Figure 8-3  An object reflects some photons and absorbs others


Previous Table of Contents Next