To find the amount of specular color to attribute to a given
vector with a given light, you use the adjacent equations
(taken from the Microsoft DirectX 10.0 SDK documentation):
The meanings of the variables are given in Table 4.2.
Ta b l e 4 . 2 : Meanings of the specular reflection variables
p
c
Location of the camera.
p
v
Location of the surface.
l
d
Direction of the light.
h The “halfway” vector. Think of this as the vector bisecting the angle made by the light
direction and the viewer direction. The closer this is to the normal, the brighter the
surface should be. The normal-halfway angle relation is handled by the dot product.
n The normal of the surface.
R
s
Specular reflectance. This is, in essence, the intensity of the specular reflection. When
the point you’re computing lies directly on a highlight, it will be 1.0; when it isn’t in a
highlight at all, it’ll be 0.
p The “power” of the surface. The higher this number, the sharper the specular
highlight. A value of 1 doesn’t look much different from diffuse lighting, but using a
value of 15 or 20 gives a nice sharp highlight.
S
s
The color being computed (this is what you want).
C
s
Specular color of the surface. That is, if white specular light were hitting the surface,
this is the specular color you would see.
A Attenuation of the light (how much of the total energy leaving the light actually hits the
surface).
L
s
Specular color of the light.
Note that this only solves for one light; you need to solve the same equa
-
tion for each light, summing up the results as you go.
Light Types
Now that you have a way to find the light hitting a surface, you’re going to
need some lights! There are typically three types of lights I am going to
discuss, although we’ll see more advanced lighting models when we look
at HLSL shaders.
Parallel Lights (or Directional Lights)
Parallel lights cheat a little bit. They represent light that comes from an
infinitely far away light source. Because of this, all of the light rays that
reach the object are parallel (hence the name). The standard use of a par
-
allel light is to simulate the sun. While it’s not infinitely far away, 93
million miles is good enough!
Chapter 4: 3D Math Foundations n 183
()
vp p
hv1
nh
=-
=-
=
cv
d
s
p
ssss
R
SCARL
The great thing about parallel lights is that a lot of the ugly math goes
away. The attenuation factor is always 1 (for point/spotlights, it generally
involves divisions if not square roots). The incoming light vector for calcu-
lation of the diffuse reflection factor is the same for all considered points,
whereas point lights and spotlights involve vector subtractions and a nor-
malization per vertex.
Typically, lighting is the kind of effect that is sacrificed for processing
speed. Parallel light sources are the easiest and therefore fastest to process.
If you can’t afford to do the nicer point lights or spotlights, falling back to
parallel lights can keep your frame rates at reasonable levels.
Point Lights
One step better than directional lights are point lights. They represent
infinitesimally small points that emit light. Light scatters out equally in all
directions. Depending on how much effort you’re willing to expend on the
light, you can have the intensity falloff based on the inverse squared dis
-
tance from the light, which is how real lights work.
The light direction is different for each surface location (otherwise the
point light would look just like a directional light). The equation for it is:
184 n Chapter 4: 3D Math Foundations
Figure 4.26: Parallel light sources
2
tionlight_locacationsurface_lo
n_factorattenuatio
k
=
tionlight_locacationsurface_lo
tionlight_locacationsurface_lo
ctionlight_dire =
Spotlights
Spotlights are the most expensive type of light. They model a spotlight not
unlike the type you would see in a theatrical production. They are point
lights, but light only leaves the point in a particular direction, spreading
out based on the aperture of the light.
Spotlights have two angles associated with them. One is the internal
cone whose angle is generally referred to as theta (q). Points within the
internal cone receive all of the light of the spotlight; the attenuation is the
same as it would be if point lights were used. There is also an angle that
definestheoutercone;theangleisreferredtoasphi(f). Points outside
the outer cone receive no light. Points outside the inner cone but inside the
outer cone receive light, usually a linear falloff based on how close the
point is to the inner cone.
Chapter 4: 3D Math Foundations n 185
Figure 4.27:
Point light source
Figure 4.28: Spotlight source

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.