If you think all of this sounds mathematically expensive, you’re right. Spot
lights can slow down your application a great deal. Then again, they do
provide an incredible amount of atmosphere when used correctly, so you
will have to figure out a line between performance and aesthetics.
Shading Models
Once you’ve determined and set up the lighting
information, you need to know how to draw
the triangles with the supplied information.
With DirectX 10’s new HLSL (high level shader
language) you have unlimited ways to render
lighting; however, we’ll discuss three common
methods: Lambert, Gouraud, and Phong. Fig
ure 4.29 shows a polygon mesh of a sphere,
which I’ll use to explain the shading models.
Triangles that use Lambertian shading are
painted with one solid color instead of gradi-
ents. Typically, each triangle is lit using that
triangle’s normal. The resulting object looks
very angular and sharp. Lambertian shading
was used mostly back when computers weren’t
fast enough to do modern shading in real time.
To light a triangle, you compute the lighting
equations using the triangle’s normal and any
of the three vertices of the triangle.
Gouraud (pronounced garrow)shadingusedto
be the de facto shading standard in accelerated 3D hardware, although
more advanced models are now available. Instead of specifying one color
to use for the entire triangle, each vertex has its own separate color. The
color values are linearly interpolated across the
triangle, creating a smooth transition between
the vertex color values. To calculate the light
ing for a vertex, you use the position of the
vertex and a vertex normal.
Of course, it’s a little hard to correctly
define a normal for a vertex. What people do
instead is average the normals of all the poly
gons that share a certain vertex, using that as
the vertex normal. When the object is drawn,
the lighting color is found for each vertex
(rather than each polygon), and then the colors
186 n Chapter 4: 3D Math Foundations
Figure 4.29: Wireframe mesh
Figure 4.30: Lambert shaded
Figure 4.31: Gouraud shaded

Get Advanced 3D Game Programming with DirectX 10.0 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.