To keep things simple, we shall concentrate on a single convex
object with a diffusely reflective surface, illuminated by a single
light source of monochrome light at infinity. The corresponding
setup is shown in Figure . We take the viewpoint to
be
(with
). The object is shown on the side
of the viewplane with
, but it could actually be
anywhere with
. The light source is at infinity with
direction given by the unit vector
s
.
Physically, light travels from the light source to the object and then to the viewpoint (the eye). This will not work computationally, however, since we would have to trace a great number of rays, only a few of which would reach the eye. A much easier procedure is to trace the rays in the opposite direction, one for each pixel on the screen. The lines involved are the same either way!
1.1 Method: For each pixel position
in the viewplane (screen):
Step 1. Find the equation of the line (ray) from the viewpoint
to
. This will be
x
, which simplifies to
x
.
Step 2. Find the point where this line first intersects
the object, if it does at all. (If it does not intersect the
object, then the brightness of the pixel is 0; stop here.)
Step 3. Find the outward unit normal
n at .
Step 4. Use ``Phong shading'': The brightness of the pixel at
is
n
s, representing a fraction of the
maximum possible brightness, provided
n
s
; otherwise the brightness is 0. This formula does not
involve the direction of the ray itself, because diffuse
reflection scatters light equally in all directions.
Suppose the viewpoint is , the lighting direction is
s
(straight up, which is the positive
direction), the object is the sphere of radius 3 centered at
, and we are interested in the pixel at
, so
. The equation of the sphere is then
, or
.
In Step 1, the line is
x.
In Step 2, we simply substitute ,
,
into the
equation for the sphere. We get
.
Expanding and collecting terms we get
,
or after division by 2 to simplify,
.
By the quadratic formula, the roots are
, so
or
.
Because the ray starts at the viewpoint with , the first
time it reaches the sphere is at
, which
corresponds to the point
.
In Step 3, notice that the equation for the sphere has the form
, a ``level surface'' of
. One normal is
therefore the gradient,
, which at
is
. Because
for the sphere grows from
the center out, the gradient gives an outward normal.
Now we need to normalize the gradient to get the unit outward
normal vector
n. It is harmless to scale first by a
positive scalar, so let's leave off the factor of and
use the outward normal
. The length of this vector
is
, so the unit outward normal is
n
.
In Step 4,
n s
. Again,
this is the fraction of the maximum possible brightness.