Section 4.1
Vision and Color
Most people are familiar with some basic facts about the human visual system. They know that the full spectrum of colors can be made from varying amounts red, blue, and green. And they might know that this is because the human eye has three different kinds of "cone cells" for color vision, one kind that sees red light, one that sees green, and one that sees blue. These facts are mostly wrong. Or, at least, they are only approximations.
The detailed physics of light is not part of our story here, but for our purposes, light is electromagnetic radiation, a kind of wave that propagates thorough space. The physical property of light that corresponds to the perception of color is wavelength. Electromagnetic radiation that can be detected by the human visual system has a wavelength between about 400 and 700 nanometers. All the colors of the rainbow, from red to violet, are found in this range. This is a very narrow band of wavelengths; outside this band are other kinds of electromagnetic radiation including ultraviolet and infrared, just next to the visible band, as well as X-rays, gamma rays, microwave, and radio waves.
To fully describe a light source, we have to say how much of each visible wavelength it contains. Light from the sun is a combination of all of the visible wavelengths of light (as well as wavelengths outside the visible range), with a characteristic amount of each wavelength. Light from other sources will have a different distribution of wavelengths. When light from some source strikes a surface, some of it is absorbed and some is reflected (and some might pass into and through the object). The appearance of an object depends on the nature of the light that illuminates it and on how the surface of the object interacts with different wavelengths of light. An object that reflects most of the red light that hits it and absorbs most of the other colors will appear red -- if the light that illuminates it includes some red for it to reflect.
The visual system in most people does indeed have three kinds of cone cells that are used for color vision (as well as "rod cells" that do not see in color). However, the three types of cone cell do not detect exclusively red, green, and blue light. Each type of cell responds to a range of wavelengths, and the wavelength bands for the three types of cell have a large overlap. It is true that red, green, and blue light can be combined to duplicate many of the colors that can be seen. That is, to the extent that our experience of a color boils down to a certain level of excitation of each of the three types of cone cells, it is possible in many cases to find a combination of red, green, and blue that will produce the same levels of excitation.
Many but not all. "Red", "green", and "blue" here are inexact terms, but they can be taken to mean three wavelengths or combination of wavelengths that are used as primary colors. But no matter what three primary colors are used, it is impossible to duplicate all visible colors with combinations of the three primary colors. Display devices such as computer screens and projectors do, in fact, produce color by combining three primary colors. The range of colors that can be shown by a given device is called the color gamut of that device. No device has a color gamut that includes all possible colors.
We have been discussing an RGB (red/green/blue) color system, which is appropriate for devices that make color by combining light of different wavelengths. Not all devices do this. A printer, for example, combines differently colored dyes or inks, and the light that will produce the perceived color has to come from an outside source. In this case, the color depends on which colors are absorbed by the ink and which are reflected by the inks. For example, a yellow ink absorbs blue light and reflects red and green. A magenta ink absorbs green while reflecting red and blue. A combination of yellow ink and magenta ink will absorb both blue and green to some extent, leaving mostly red light to be reflected. So in this system, red is a combination of yellow and magenta. By adding cyan ink to the yellow and magenta, a wide range of colors can be produced. Cyan, magenta, and yellow make up the CMY color system. This is a "subtractive" color system, since each color of ink subtracts some wavelengths from the light that strikes the ink. In practice, black ink is often added to the mix, giving the CMYK color system, where the "K" stands for black. The color gamut of a CMYK printer is different from and smaller than the color gamut of a computer screen. The color gamut of a printer can be improved by using additional inks, but it is difficult to match the colors from a printer to the colors on the screen (let alone to those in real life).
RGB and CMYK are only two of many color systems that are used to describe colors. Users of Java are probably familiar with the HSB (hue/saturation/brightness) color system. OpenGL uses RGB colors, so I will not discuss the other possibilities further here.
A substantial number of people, by the way, are color blind to some extent because they lack one or more of the three types of cone cells. For people with the most common type of color blindness, red and green look the same. Programmers should avoid coding essential information only in the colors used, especially when the essential distinction is between red and green.
In the rest of this chapter, we'll look at the OpenGL approach to light and material. There are a few general ideas about the interaction of light and materials that you need to understand before we begin. When light strikes a surface, some of it will be reflected. Exactly how it reflects depends in a complicated way on the nature of the surface, what I am calling the material properties of the surface. In OpenGL (and in many other computer graphics systems), the complexity is approximated by two general types of reflection, specular reflection and diffuse reflection.
In perfect specular ("mirror-like") reflection, an incoming ray of light is reflected from the surface intact. The reflected ray makes the same angle with the surface as the incoming ray. A viewer can see the reflected ray only if the viewer is in the right position, somewhere along the path of the reflected ray. Even if the entire surface is illuminated by the light source, the viewer will only see the reflection of the light source at those points on the surface where the geometry is right. Such reflections are referred to as specular highlights. In practice, we think of a ray of light as being reflected not as a single perfect ray, but as a cone of light, which can be more or less narrow. Specular reflection from a very shiny surface produces very narrow cones of reflected light; specular highlights on such a material are small and sharp. A duller surface will produce wider reflected light cones and bigger, fuzzier specular highlights. In OpenGL, the material property that determines the size and sharpness of specular highlights is called shininess.
In pure diffuse reflection, an incoming ray of light is scattered in all directions equally. A viewer would see see reflected light from all points on the surface, and the surface would appear to be evenly illuminated.
When a light strikes a surface, some wavelengths of light can be absorbed, some can be reflected diffusely, and some can be reflected specularly. The degree to which a material reflects light of different wavelengths is what constitutes the color of the material. We now see that a material can have two different colors -- a diffuse color that tells how the material reflects light diffusely and a specular color that tells how it reflects light specularly. The diffuse color is the basic color of the object. The specular color determines the color of specular highlights.
In fact, OpenGL goes even further. There are in fact four colors associated with a material. The third color is the ambient color of the material. Ambient light refers to a general level of illumination that does not come directly from a light source. It consists of light that has been reflected and re-reflected so many times that it is no longer coming from any particular source. Ambient light is why shadows are not absolutely black. In fact, ambient light is only a crude approximation for the reality of multiply reflected light, but it is better than ignoring multiple reflections entirely. The ambient color of a material determines how it will reflect various wavelengths of ambient light. Ambient color is generally set to be the same as the diffuse color.
The fourth color associated with a material is an emission color. The emission color is color that does not come from any external source, and therefore seems to be emitted by the material itself. This does not mean that the object is giving off light that will illuminate other objects, but it does mean that the object can be seen even if there is no source of light (not even ambient light). In the presence of light, the object will be brighter than can be accounted for by the light that illuminates it, and in that sense it might appear to glow. The emission color is used only rarely.
In the next section, we will look at how to use materials in OpenGL. The section after that will cover working with light sources, which have their own set of properties.