RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Drawing with Direct3D, Part 2: Lighting and Textures

Learn how to use Direct3D's lighting model and textures to create more realistic three-dimensional scenes.

n Part 1 of this article series you saw how to get started with Direct3D, Microsoft's high-performance three-dimensional graphics library. That article explained how to initialize Direct3D, build simple scenes, and display them. It also explained how you can use transformations to modify the data or change the viewing position over time.

The techniques you've seen so far let you specify colors for the objects in a scene—but those colors remain static. The system displays exactly the colors you specify; they aren't produced by any sort of lighting model. In complicated scenes, displaying only specified colors can produce a poor result, because adjacent shapes that have the same colors blend together, making it difficult for viewers to discern what the scene represents. Imagine a cube where sides all have the same color displayed on a two-dimensional surface. The sides would blend so that you couldn't tell where one side ended and the next began.

To help solve such problems, you can improve the realism of Direct3D scenes by using a lighting model and materials, letting Direct3D change the colors of objects depending on their relative orientation to light sources and the viewing position. Lighting models and materials prevent adjacent surfaces from blending together and make objects such as a cube appear more realistic.

You'll also explore using textures to give objects more interesting appearances than those provided by simple colors. Using these techniques, you can not only make a cube with sides that look different depending on the light source, but you can also make the cube appear to be made out of brick, wood, or other materials.

Understanding Light
Before you can add lighting effects to your three-dimensional scenes, you need to understand a little about the physics of light. Direct3D treats light as if it is broken into three distinct components: ambient, diffuse, and specular.

Ambient light represents the overall brightness of the scene—those areas where light does not fall directly. For example, ambient light lets you see under your desk even though there's no direct light source. Direct3D treats ambient light as a background level of illumination that applies equally to everything in the scene, no matter where it is, or how it is oriented. This is a bit of a simplification, but it works quite well in practice and lets you see parts of the scene that are not lit directly (see the sidebar "Real World Lighting" for more information).

When light from a light source shines on an object, much of that light is scattered in other directions. This scattered or diffuse light usually provides the greatest effect on an object's perceived color.

Diffuse light is scattered more or less uniformly in all directions. The amount of light that the object scatters depends on how much light energy hits the object—and that depends on the angle at which the light hits the object's surface. For example, imagine a white sheet of paper. If a light shines directly on the paper at a 90-degree angle, the paper appears white. If the light hits the paper at a 45-degree angle, the paper appears less white or possibly light gray. If the light barely hits the paper at a slim 10-degree angle, the paper may appear dark gray. You might want to take a few moments to look at some paper in your office and notice how its color seems to change when you tilt it at different angles to the light.

Figure 1. Mirror Math: The mirror angle is the same size as the incident angle but on the other side of the surface's normal.
Specular light is the final component of the Direct3D lighting model. To understand specular light, you need to know a few simple terms. Figure 1 shows the terms graphically and makes them a lot easier to understand.

A surface's normal is a vector that points directly away from the surface at a particular point (such as the blue arrow in Figure 1). The normal for a floor typically points directly upward, the normal for a ceiling points downward, and the normal for a wall points horizontally away from the wall. More complicated surfaces such as spheres have different normals at every point on their surfaces, all pointing away from the center of the sphere.

When light strikes a surface, the angle that the light makes with the surface's normal is called the incident angle.

A mirror angle is the angle at which a perfectly mirrored surface would reflect light that hits it at a particular point. The mirror angle is the same as the incident angle but reflected onto the other side of the normal.

At one extreme, a perfect mirror reflects all the light that hits it along its mirror angle. At the other extreme, a perfectly non-shiny matte surface reflects no extra light along its mirror angle; it reflects only diffuse light.

Between these two extremes are objects that are somewhat but not perfectly shiny. Surfaces such as polished billiard balls, plastic toys, and shiny apples reflect some extra light more or less along the mirror angle. But because these surfaces are not perfect reflectors, they do not reflect this light exactly along the mirror angle. Most of the reflected light lies along this angle and then drops off quickly at angles farther away from the mirror angle. If you look along the mirror angle, you'll see a bright spot. If you move slightly to the side, you'll see less light and if you move farther from the mirror angle you'll see no reflected component. This extra reflected light is the specular component.

Rounded surfaces such as billiard balls and apples have locations that are positioned so your eye lies close to the mirror angle. Those spots produce shiny highlights when you look at the object. To see the effect yourself, look at a shiny apple in a room without any bright lights, so you're seeing it primarily from ambient light. If you then hold the apple under a very bright light, you should see the highlights (and if you pull out your protractor, you may be able to measure the incident and mirror angles).

These three components make up the bulk of the lighting model used by Direct3D. Ambient light represents the scene's general level of illumination, and is the same everywhere. Diffuse light represents light striking an object, and depends on the light's incident angle. Specular light represents partial reflection along the mirror angle and depends on both the angle of incidence and the angle from the point to the viewer's eye.

However, there are two more pieces to the complete model: determining how light gets into the scene and how materials react to the light. Direct3D handles these issues with lights and materials.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date