i
i
i
i
i
i
i
i
84 4. Ray Tracing
4.5.3 Ambient Shading
Surfaces that receive no illumination at all will be rendered as completely black,
which is often not desirable. A crude but useful heuristic to avoid black shadows
In the real world, surfaces
that are not illuminated by
light sources are illumi-
nated by indirect reflections
from other surfaces.
is to add a constant component to the shading model, one whose contribution
to the pixel color depends only on the object hit, with no dependence on the
surface geometry at all. This is known as ambient shading—it is as if surfaces
were illuminated by “ambient” light that comes equally from everywhere. For
convenience in tuning the parameters, ambient shading is usually expressed as
the product of a surface color with an ambient light color, so that ambient shading
can be tuned for surfaces individually or for all surfaces together. Together with
the rest of the Blinn-Phong model, ambient shading completes the full version of
a simple and useful shading model:
L = k
a
I
a
+ k
d
I max(0, n · l)+k
s
I max(0, n · h)
n
, (4.3)
where k
a
is the surface’s ambient coefcient, or “ambient color, and I
a
is theWhen in doubt set the am-
bient color to be the same
as the diffuse color.
ambient light intensity.
4.5.4 Multiple Point Lights
A very useful property of light is superposition—the effect caused by more than
one light source is simply the sum of the effects of the light sources individually.
For this reason, our simple shading model can easily be extended to handle N
light sources:
L = k
a
I
a
+
N
i=1
[k
d
I
i
max(0, n · l
i
)+k
s
I
i
max(0, n · h
i
)
p
] , (4.4)
where I
i
, l
i
,andh
i
are the intensity, direction, and half vector of the i
th
light
source.
4.6 A Ray-Tracing Program
We now know how to generate a viewing ray for a given pixel, how to nd the
closest intersection with an object, and how to shade the resulting intersection.
These are all the parts required for a program that produces shaded images with
hidden surfaces removed.
i
i
i
i
i
i
i
i
4.6. A Ray-Tracing Program 85
for each pixel do
compute viewing ray
if (ray hits an object with t [0, )) then
Compute n
Evaluate shading model and set pixel to that color
else
set pixel color to background color
Here the statement “if ray hits an object... can be implemented using the algo-
rithm of Section 4.4.4.
In an actual implementation, the surface intersection routine needs to some-
how return either a reference to the object that is hit, or at least its normal vec-
tor and shading-relevant material properties. This is often done by passing a
record/structure with such information. In an object-oriented implementation, it
is a good idea to have a class called something like surface with derived classes
triangle, sphere, group, etc. Anything that a ray can intersect would be under that
class. The ray-tracing program would then have one reference to a “surface” for
the whole model, and new types of objects and efciency structures can be added
transparently.
4.6.1 Object-Oriented Design for a Ray-Tracing Program
As mentioned earlier, the key class hierarchy in a ray tracer are the geometric
objects that make up the model. These should be subclasses of some geometric
object class, and they should support a hit function (Kirk & Arvo, 1988). To
avoid confusion from use of the word “object, surface is the class name often
used. With such a class, you can create a ray tracer that has a general interface
that assumes little about modeling primitives and debug it using only spheres. An
important point is that anything that can be “hit” by a ray should be part of this
class hierarchy, e.g., even a collection of surfaces should be considered a subclass
of the surface class. This includes efciency structures, such as bounding volume
hierarchies; they can be hit by a ray, so they are in the class.
For example, the “abstract” or “base” class would specify the hit function as
well as a bounding box function that will prove useful later:
class surface
virtual bool hit(ray e + td, real t
0
, real t
1
, hit-record rec)
virtual box bounding-box()
Here (t
0
,t
1
) is the interval on the ray where hits will be returned, and rec is a
record that is passed by reference; it contains data such as the t at the intersection
i
i
i
i
i
i
i
i
86 4. Ray Tracing
when hit returns true. The type box is a 3D “bounding box, that is two points that
dene an axis-aligned box that encloses the surface. For example, for a sphere,
the function would be implemented by
box sphere::bounding-box()
vector3 min = center - vector3(radius,radius,radius)
vector3 max = center + vector3(radius,radius,radius)
return box(min, max)
Another class that is useful is material. This allows you to abstract the material
behavior and later add materials transparently. A simple way to link objects and
materials is to add a pointer to a material in the surface class, although more
programmable behavior might be desirable. A big question is what to do with
textures; are they part of the material class or do they live outside of the material
class? This will be discussed more in Chapter 11.
4.7 Shadows
Once you have a basic ray tracing program, shadows can be added very easily.
Recall from Section 4.5 that light comes from some direction l. If we imagine
ourselves at a point p on a surface being shaded, the point is in shadow if we
“look” in direction l and see an object. If there are no objects, then the light is not
blocked.
This is shown in Figure 4.17, where the ray p + tl does not hit any objects
and is thus not in shadow. The point q is in shadow because the ray q + tl
does hit an object. The vector l is the same for both points because the light
is “far” away. This assumption will later be relaxed. The rays that determine
in or out of shadow are called shadow rays to distinguish them from viewing
rays.
Figure 4.17. The point p
is not in shadow while the
point q is in shadow.
To get the algorithm for shading, we add an if statement to determine whether
the point is in shadow. In a naive implementation, the shadow ray will check
for t [0, ), but because of numerical imprecision, this can result in an inter-
section with the surface on which p lies. Instead, the usual adjustment to avoid
that problem is to test for t [, ) where is some small positive constant
(Figure 4.18).
If we implement shadow rays for Phong lighting with Equation 4.3 then we
have the following:
i
i
i
i
i
i
i
i
4.8. Ideal Specular Reflection 87
Figure 4.18. By testing
in the interval starting at ,
we avoid numerical impre-
cision causing the ray to hit
the surface p is on.
function raycolor( ray e + td, real t
0
, real t
1
)
hit-record rec, srec
if (scenehit(e + td, t
0
, t
1
,rec)) then
p = e +(rec.t) d
color c = rec.k
a
I
a
if (not scenehit(p + sl, , ,srec)) then
vector3 h = normalized(normalized(l)+normalized(d))
c = c + rec.k
d
I max(0, rec.n ·l)+(rec.k
s
) I (rec.n · h)
rec.p
return c
else
return background-color
Note that the ambient color is added whether p is in shadow or not. If there are
multiple light sources, we can send a shadow ray before evaluating the shading
model for each light. The code above assumes that d and l are not necessarily unit
vectors. This is crucial for d, in particular, if we wish to cleanly add instancing
later (see Section 13.2).
4.8 Ideal Specular Reflection
Figure 4.19. When look-
ing into a perfect mirror, the
viewer looking in direction d
will see whatever the viewer
“below” the surface would
see in direction r.
It is straightforward to add ideal specular reection, or mirror reflection,toaray-
tracing program. The key observation is shown in Figure 4.19 where a viewer
looking from direction e sees what is in direction r as seen from the surface. The
vector r is found using a variant of the Phong lighting reection Equation (10.6).
There are sign changes because the vector d points toward the surface in this case,
so,
r = d 2(d · n)n, (4.5)
In the real world, some energy is lost when the light reects from the surface, and
this loss can be different for different colors. For example, gold reects yellow
more efciently than blue, so it shifts the colors of the objects it reects. This can
be implemented by adding a recursive call in raycolor:
Figure 4.20. Asimple
scene rendered with diffuse
and Blinn-Phong shading,
shadows from three light
sources, and specular re-
flection from the floor.
color c = c + k
m
raycolor(p + sr, , )
where k
m
(for “mirror reection”) is the specular RGB color. We need to make
sure we test for s [, ) for the same reason as we did with shadow rays; we
don’t want the reection ray to hit the object that generates it.
The problem with the recursive call above is that it may never terminate. For
example, if a ray starts inside a room, it will bounce forever. This can be xed by
i
i
i
i
i
i
i
i
88 4. Ray Tracing
adding a maximum recursion depth. The code will be more efcient if a reection
ray is generated only if k
m
is not zero (black).
4.9 Historical Notes
Ray tracing was developed early in the history of computer graphics (Appel,
1968) but was not used much until a while later when sufcient compute power
was available (Kay & Greenberg, 1979; Whitted, 1980).
Ray tracing has a lower asymptotic time complexity than basic object-order
rendering (Snyder & Barr, 1987; Muuss, 1995; Parker, Martin, et al., 1999; Wald
et al., 2001). Although it was traditionally thought of as an ofine method, real-
time ray tracing implementations are becoming more and more common.
Frequently Asked Questions
Why is there no perspective matrix in ray tracing?
The perspective matrix in a z-buffer exists so that we can turn the perspective pro-
jection into a parallel projection. This is not needed in ray tracing, because it is
easy to do the perspective projection implicitly by fanning the rays out from the
eye.
Can ray tracing be made interactive?
For sufciently small models and images, any modern PC is sufciently pow-
erful for ray tracing to be interactive. In practice, multiple CPUs with a shared
frame buffer are required for a full-screen implementation. Computer power is in-
creasing much faster than screen resolution, and it is just a matter of time before
conventional PCs can ray trace complex scenes at screen resolution.
Is ray tracing useful in a hardware graphics program?
Ray tracing is frequently used for picking. When the user clicks the mouse on a
pixel in a 3D graphics program, the program needs to determine which object is
visible within that pixel. Ray tracing is an ideal way to determine that.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset