Chapter 8: Lighting and Shading
Total Page:16
File Type:pdf, Size:1020Kb
Chapter 8: Lighting and Shading This chapter introduces the next level beyond color for creating improved computer graphics images. It focuses on two related areas that give improved representations of how images look: lighting that is based on a simple model of the interaction of lights and surfaces, and shading that is based on a simple model of how color changes across a graphics surface primitive. The lighting model is based on three simple components of light that model indirect, direct, and reflective lighting, and introduces both the idea of a light and of the light properties of an object’s materials. The shading model is based on the idea of averaging the color of the vertices of a polygon to get the color of the interior of the polygon. Between them, you will be able to make images that look much more realistic than the images you could make with simple color. This chapter needs you to have an understanding of color at the level of the earlier chapter on color, an observation of the way lights work in the world around you, and an understanding of the concept of polygons and of simple averaging across a polygon. Lighting There are two ways to think of how we see things. The first is that things have an intrinsic color and we simply see the color that they are. The color is set by defining a color in RGB space and simply instructing the graphics system to draw everything with that color until a new color is chosen. This approach is synthetic and somewhat simplistic, but it’s very easy to create images this way because we must simply set a color when we define and draw our geometry. In fact, when objects don’t have an intrinsic color but we use their color to represent something else, as we did sometimes in the science examples in an earlier chapter, then this is an appropriate approach to color for images. However, it’s clear that things in the real world don’t simply have a color that you see; the color you see is strongly influenced by the lighting conditions in which you see them, so it’s inaccurate not to take the light into account in presenting a scene. So the second way to think of how we see things is to realize that we see things because of light that reaches us after a physical interaction between light in the world and the objects we see. Light sources emit energy that reaches the objects and the energy is then re-sent to us by reflections that involve both the color of the light and the physical properties of the objects. In computer graphics, this approach to lighting that considers light that comes only from lights in the scene is called the local illumination model. It is handled by having the graphics programmer define the lights in the scene. In this chapter we will discuss how this approach to light allows us to model how we see the world, and how computer graphics distills that into fairly simple definitions and relatively straightforward computations to produce images that seem to have a relationship to the lights and materials present in the scene. A first step in developing graphics with the local illumination model depends on modeling the nature of light. In the simplified but workable model of light that we will use, there are three fundamental components of light: the ambient, diffuse, and specular components. We can think of each of these as follows: ambient : light that is present in the scene because of the overall illumination in the space. This can include light that has bounced off objects in the space and that is thus independent of any particular light. diffuse : light that comes directly from a particular light source to an object, where it is then sent directly to the viewer. Normal diffuse light comes from an object that reflects a subset of the wavelengths it receives and that depends on the material that makes up an object, with the effect of creating the color of the object. specular : light that comes directly from a particular light source to an object, where it is then reflected directly to the viewer because the object reflects the light without interacting with it and giving the light the color of the object. This light is generally the color of the light source, not the object that reflects the light. These three components are computed based on properties of the lights, properties of the materials that are in your scene, and the geometric relationship between the lights and the objects. All three of these components contribute to the overall light as defined by the RGB light components, and the graphics system models this by applying them to the RGB components separately. The sum of these three light components is the light that is actually seen from an object. Just as the light is modeled to create an image, the materials of the objects that make up the scene are modeled in terms of how they contribute to the light. Each object will have a set of properties that defines how it responds to each of the three components of light. The ambient property will define the behavior (essentially, the color) of the object in ambient light, the diffuse property in diffuse light, and the specular property in specular light. As we noted above, our simple models of realistic lighting tend to assume that objects behave the same in ambient and diffuse light, and that objects simply take on the light color in specular light, but because of the kind of calculations that are done to display a lighted image, it is as easy to treat each of the light and material properties separately. So in order to use lights in a scene, you must define your lights in terms of the three kinds of color they provide, and you must define objects not in simple terms of their color, but in terms of their material properties. This will be different from the process we saw in the earlier module on color and will require more thought as you model the objects in your scenes, but the changes will be something you can handle without difficulty. Graphics APIs have their own ways to specify the three components of light and the three components of materials, and we will also discuss this below when we talk about implementing lighting for your work. Definitions Ambient, diffuse, and specular light Ambient light is light that comes from no apparent source but is simply present in a scene. This is the light you would find in portions of a scene that are not in direct light from any of the lights in the scene, such as the light on the underside of an object, for example. Ambient light can come from each individual light source, as well as from an overall ambient light value, and you should plan for each individual light to contribute to the overall brightness of a scene by contributing something to the ambient portion of the light. The amount of diffuse light reflected by an object from a single light is given simply by A = LA *CA for a constant CA that depends on the material of the object and the ambient light LA present in the scene, where the light LA and constant CA are to be thought of as RGB triples, not simple constants, and the calculation is to yield another RGB value. If you have multiple lights, the total diffuse light is A = å LA * CA , where the sum is over all the lights. If you want to emphasize the effect of the lights, you will probably want to have your ambient light at a fairly low level (so you get the effect of lights at night or in a dim room) or you can use a fairly high ambient level if you want to see everything in the scene with a reasonably uniform light. If you want to emphasize shapes, use a fairly low ambient light so the shading can bring out the variations in the surface. Diffuse light comes from specific light sources and is reflected by the surface of the object at a particular wavelength depending on properties of the object’s material. The general model for diffuse light, used by OpenGL and other APIs, is based on the idea of brightness, or light energy per unit area. A light emits a certain amount of energy per unit area in the direction it is shining, and when this falls on a surface, the intensity of light seen on the surface is proportional to the surface area illuminated by the unit area of the light. As the diagram in Figure 8.1 shows, one unit of area as seen by the light, or one unit of area perpendicular to the light direction, illuminates an 2/13/03 Page 8.2 area of 1/cos(Q) in the surface. So if we have LD light energy per unit of area in the light direction, we have LD cos(Q) units of light energy per unit of area on the surface if the cosine is positive. As the angle of incidence of the light on the surface decreases, the amount of light at the surface becomes reduced, going to zero when the light is parallel to the surface. Because it is impossible to talk about “negative light,” we replace any negative value of the cosine with zero, which eliminates diffuse light on surfaces facing away from the light.