i
i
i
i
i
i
i
i
82 4. Ray Tracing
part of the light to the camera. Simple shading models are defined in terms of
illumination from a point light source. The important variables in light reflection
are the light direction l, which is a unit vector pointing towards the light source;
the view direction v, which is a unit vector pointing toward the eye or camera; the
surface normal n, which is a unit vector perpendicular to the surface at the point
where reflection is taking place; and the characteristics of the surface—color,
shininess, or other properties depending on the particular model.
4.5.1 Lambertian Shading
Illumination from real point
sources falls off as distance
squared, but that is often
more trouble than it’s worth
in a simple renderer.
The simplest shading model is based on an observation made by Lambert in the
18th century: the amount of energy from a light source that falls on an area of
surface depends on the angle of the surface to the light. A surface facing directly
towards the light receives maximum illumination; a surface tangent to the light
direction (or facing away from the light) receives no illumination; and in between
the illumination is proportional to the cosine of the angle θ between the surface
normal and the light source (Figure 4.12). This leads to the Lambertian shading
model:
L = k
d
I max(0, n · l)
where L is the pixel color; k
d
is the diffuse coefficient, or the surface color; and
I is the intensity of the light source. Because n and l are unit vectors, we can
l
n
v
Figure 4.12. Geometry for
Lambertian shading.
use n · l as a convenient shorthand (both on paper and in code) for cos θ.This
equation (as with the other shading equations in this section) applies separately to
the three color channels, so the red component of the pixel value is the product of
the red diffuse component, the red light source intensity, and the dot product; the
same holds for green and blue.
When in doubt, make light
sources neutral in color,
with equal red, green, and
blue intensities.
The vector l is computed by subtracting the intersection point of the ray and
surface from the light source position. Don’t forget that v, l,andn all must be
unit vectors; failing to normalize these vectors is a very common error in shading
computations.
4.5.2 Blinn-Phong Shading
Lambertian shading is view independent: the color of a surface does not depend
on the direction from which you look. Many real surfaces show some degree
of shininess, producing highlights, or specular reflections, that appear to move
around as the viewpoint changes. Lambertian shading doesn’t produce any high-
lights and leads to a very matte, chalky appearance, and many shading models