To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
(Publisher: The Coriolis Group)
Author(s): Stan Trujillo
ISBN: 1576100502
Publication Date: 11/01/96
Chapter 6 Light Sources And Shadows
- Ambient Lights
- Point Lights
- Directional Lights
- Parallel Lights
- Spotlights
- Multiple Light Sources
- Shadows
You learned a little about light sources in Chapters 4 and 5. In this chapter, well engage in a more detailed discussion about each type of light source that Direct3D supports. Well also talk about shadows.
For most of the topics, well be using demos from the CD-ROM. The demos well study in this chapter are:
- Firefly
- SpaceStation
- SpaceDonut
- Spotlight
- Fork
Rendering Methods And Color Models
Before we talk about specific light source types, we should review the distinction between rendering methods and color models, and talk about how they affect light sources.
A rendering method (wireframe, flat, Gouraud, etc.) is the technique that is used to produce a programs final output. Rendering methods are sometimes called lighting or shading methods.
Color models (RGB versus Ramp), are independent from the rendering method. The RGB color model supports colored lighting, and the Ramp model does not. The Ramp model typically outperforms the RGB model.
Rendering methods and color models are independent; any rendering method can be used with any color model. However, both rendering methods and color models affect the way light sources are interpreted. The differences are as follows:
- The Ramp color model does not support colored lighting. This doesnt mean that colored lights are ignored; it means that a lights color settings are converted to grayscale in the Ramp color model.
- Light sources have no effect with wireframe and unlit rendering methods. Both of these methods use only the color of the face and the color of any texture that is applied to the face. Light sources are ignored.
- The flat and Gouraud rendering methods both take light sources into consideration, but each does so differently. The flat rendering method uses the light sources in the scene to determine a single color for each face. Gouraud rendering uses light sources to determine colors for each vertex. The vertex colors are then used to calculate the colors that will be used to represent each face.
Many of the demos on the CD-ROM provide menu options that allow the rendering method to be changed at runtime, so the difference between rendering methods is easy to evaluate.
Most of the demos on the CD-ROM use the Ramp color model for the performance advantage. We will, however, use the RGB color model in this chapter to demonstrate colored lighting.
Ambient Lights
In the real world, ambient light is indirect light. Ambient light is light that has been reflected and diffused. In the evening, for example, ambient light allows us to see even though the sun has gone down. The suns rays are scattered by the atmosphere and provide an even, low-intensity light that has no single direction and no single origin.
Light that truly has no direction and no origin doesnt exist in the real world. The suns light being diffused and scattered by the atmosphere is an approximation of ambient lightthe suns light still travels in specific directions and has an indirect origin (the sun). In Direct3D, ambient light has no direction and no origin.
Technically, an ambient light looks like any other light source. It is represented by the Direct3DRMLight interface and must be attached to a frame to be visible. An ambient light, however, ignores the location and orientation of the frame to which it is attached.
Demonstrating Ambient Light
Frankly, ambient light is pretty boring. It is useful, especially when combined with other light types, but by itself, ambient lighting is not very impressive. After several unsatisfactory attempts at a demo that used only ambient light, I gave up and decided that ambient light didnt deserve its own demo. Instead, well use the Direct3D AppWizard to create an application that uses ambient lighting to illuminate an object of your choice.
From the Visual C++ Developer Studio, select the File|New menu option. The New window will appear. Select Project Workspace. The New Project Workspace window will appear. Select the Direct3D AppWizard in the Type listbox, and enter a name for the project, such as AmbientLight, then press the Create button.
The introductory Direct3D AppWizard dialog box will appear. Press the Next button. The dialog box that appears next allows you to choose the object that will be displayed by the new application. By default, the Swirl object is used. Select the Let me choose an object radio button, and enter the name of an object in the Object edit box, or use the Browse button to invoke a file selection dialog. The Object Selection dialog box appears in Figure 6.1 with the DirectX SDK sphere1.x file selected.
Figure 6.1 The Direct3D AppWizard object selection dialog box.
TIP: X file contents Remember that you must select an X file that contains a single mesh. X files containing animations or frame hierarchies will not display correctly. The majority of the X files on the CD-ROM contain single meshes.
Once you have selected the mesh that is to be displayed, press the Next button.
The dialog that appears allows you to specify the types of light sources that will be in the project. The light source selection dialog box appears in Figure 6.2.
Figure 6.2 The Direct3D AppWizard light source selection dialog box.
A directional light source is used by default. Deselect the Directional check-box, and select the Ambient checkbox. Also, change the Color Model from Ramp to RGB. This will allow us to use colored lighting. Figure 6.2 shows the Light Selection dialog as it appears with the new settings.
|