09 Raytracing Basics
09 Raytracing Basics
Reading
Required:
• Foley et al., 16.12
• Important: study the textbook before attempting project 3 if
you want to save hours of debugging!
Optional (recommended):
• Hearn & Baker, 14.6
• Glassner, chapter 1
Geometric optics
• Modern theories of light treat it as both a wave and a particle.
• We will take a combined and somewhat simpler view of light -
the view of geometric optics.
• Here are the rules of geometric optics:
– Light is a flow of photons with wavelengths. We’ll call these
flows “light rays.”
– Light rays travel in straight lines in free space.
– Light rays do not interfere with each other as they cross.
– Light rays obey the laws of reflection and refraction.
– Light rays travel from the light sources to the eye, but the
physics is invariant under path reversal (reciprocity)
Ray Tracing
• A term from optics
• A “physical” simulation of the particle theory of light
• In the 1960s, ray tracing seemed like a great idea, but nobody
could do it well enough to beat cheaper image synthesis
methods.
• These days, we can follow the simulation deeply enough to get
great results!
• But there are some visual phenomena that ray tracing cannot
do.
Why Ray Tracing?
• So far, we can do ray casting:
– for each pixel in the projection plane, find the object visible
at that pixel and
– color that pixel according to the object color
• What does this model miss? What if the object is reflective?
Light Source
Object
Eye Ray
Object
Light Source
Forward Ray Tracing
• Rays emanate from light sources and bounce around in the scene.
• Rays that pass through the projection plane and enter the eye contribute
to the final image.
Appel (1968)
• Cast one eye ray + one ray to light
Whitted ray-tracing algorithm
• In 1980, Turner Whitted introduced ray tracing to the graphics
community.
– Combines backward ray tracing + rays to light
– Recursively traces rays
Algorithm:
1. For each pixel, trace a eye (primary) ray in direction V to the first
visible surface (or ray casting).
2. For each intersection, trace secondary rays.
• Shadow rays in directions Li to light sources.
• Reflected ray in direction R.
• Refracted ray or transmitted ray in direction T.
Kinds of (Backward) Rays
• Three kinds of rays
– Shadow rays
– Reflection rays Light Source
– Transparency rays
Shadow Ray
Object
Eye Ray
Shadow Ray
Object
Light Source
Kinds of (Backward) Rays
• A ray that leaves the eye and travels out to the scene is called a primary ray.
• When a ray hits an object, we spawn three new (backward) rays to collect light that must
contribute to the incoming primary ray:
– Shadow rays to light sources, used to attenuate incoming light when applying the shading
model
– Reflection rays, which model light bouncing off of other surfaces before hitting this surface
– Transparency rays, which model light refracting through the surface before leaving along
the primary ray
Light Source
Shadow Ray
Object
Eye Ray
• Shadow rays stop at light sources, but reflection and transparency rays behave just like primary
rays! Shadow Ray
Object
Light Source
Whitted algorithm
Example of Ray Tracing
Ray Tree
• A primary ray hits a surface and spawns reflection and
transparency rays. Those rays may hit surfaces and spawn their
own rays, etc.
• We can represent this process schematically using a ray tree:
Controlling Tree Depth
• Ideally, we’d spawn child rays at every object intersection
forever, getting a “perfect” color for the primary ray.
• In practice, we need heuristics for bounding the depth of the
tree (i.e., recursion depth)
Shading
• A ray is defined by an origin p and a
unit direction d and is parameterized
by t:
p+td
where
– Idirect is computed from the Phong model
– Ireflected = krI(Q,R)
– Itransmitted = ktI(Q,T)
rt
Өt
• Law of reflection: Өi = Өr
double t = 0.0;
// for ray
vec3 r = P + d*t;
// if you have done some double precision computation on t
// no shadow attenuation
if (ZERO(t))
… ..
light
L
light
L
light
L
Intersecting with xformed geometry