Graphics Reference
In-Depth Information
Listing 31.2: Recursive ray tracing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
foreach pixel (x, y):
R = ray from eyepoint E through pixel
image[x, y] = raytrace( R )
raytrace( R ):
P = raycast( R ) // first scene intersection
return lightFrom( P , R ) // light leaving P in direction opposite R
lightFrom( P , R ):
color = emitted light from P in direction opposite R .
foreach light source S :
if S is visible from P :
contribution = light from S scattered at
P in direction opposite R
color += contribution
if scattering at P is specular:
R new = reflected or refracted ray at P
color += raytrace( R new )
return color
the image we're computing, and we often use multiple samples per pixel, with
some kind of averaging. But the broad idea remains: Cast rays from the eye; com-
pute direct illumination; add in recursive rays. Ray tracing computes the contribu-
tions of paths of the form LD ? S E .
The essential features of a ray tracer (and all the subsequent algorithms) are
a ray-casting function (something that lets us shoot a ray into a scene and find
the first surface it encounters), and a BRDF or bidirectional scattering distribution
function (BSDF), which takes a surface point P and two rays
v i and
v o and returns
f s ( P ,
v o ) . (In the pseudocode, that BRDF is hidden in the “light from S scat-
tered at P in direction opposite R ,” which has to be computed by multiplying the
radiance from the light by a cosine and the BRDF to get the scattered radiance.) A
slightly more sophisticated ray tracer removes the “if scattering is specular” con-
dition, and if the scattering is non specular, the scattered radiance is estimated by
casting multiple recursive rays in many directions, weighting the returned results
by the BSDF according to the reflectance equation. It's this form of ray tracing
that we'll refer to henceforth.
Later, in addition to the BRDF or BSDF, we'll also want a function that takes a
surface point P and a ray
v i ,
Figure 31.18: Each blue vertical
line represents all of
v o with probability density
approximately proportional to the BRDF or BSDF, or cosine-weighted versions
of these.
v i and returns a random ray
,theset
of points of the scene; a green
path from the eye (at left) meets
M at some point, and then recur-
sively, multiple rays are traced;
these meet the second copy of M ,
etc. Each branching at such an
intersection is a scattering event.
The red rays from the upper
right to the ray-surface inter-
sections represent light arriv-
ing from light sources, which in
this schematic representation are
placed infinitely far away so that
all direct illuminations can be
drawn parallel.
M
Following Kajiya, we can draw a highly schematic representation of ray trac-
ing (see Figure 31.18). Rays are traced from the eye to the scene; at an intersec-
tion, we compute direct lighting and accumulate it as part of the radiance from
the light to the eye along the leftmost segment of the path. We also cast recursive
rays. In basic ray tracing, we only do so in the specular-bounce direction (if the
surface is partly specular); in more sophisticated approaches, we may do so in
many directions. When a recursive ray meets a different surface point, the direct
lighting there is propagated back to the first intersection, and thence to the eye.
This propagation involves two scattering operations. Further depths involve fur-
ther scattering. In the end, we have a branching tree representing the gathering of
light into a single pixel of the final image. The branching factor of the tree depends
on the number of recursively cast rays at each scattering event.
 
Search WWH ::




Custom Search