0% found this document useful (0 votes)
28 views72 pages

06 - Raytracing

Uploaded by

Benoni Tang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views72 pages

06 - Raytracing

Uploaded by

Benoni Tang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

Ray Tracing

Christoph Garth
Scientific Visualization Lab
Motivation
So far, we have worked on establishing the rendering equation as the basis for
physically-based rendering:
Z
Lo (x, ωr ) = Le (x, ωr ) + f (x, ωr , ωi ) Li (x, ωi ) cos θi dωi

outgoing emitted BRDF incident incident


radiance radiance radiance direction

unknown known known unknown known

The central quantity radiance L describes the propagation of light in the scene.

We have also already discussed the vacuum assumption: radiance is only modified on
surfaces. Hence, Li (x, ωi ) = Lo (x0 , ωi ) if the two points x and x0 can “see” each other, i.e.
can be connected by a ray.

Ultimately, Li (x, ω) must be determined at sensor points. Our goal today is a simple
algorithm for image synthesis that is based on this principle.

Computer Graphics – Ray Tracing – Motivation 6–1


High-Level Overview:

Most image synthesis algorithms based on the rendering equation construct light
paths that connect light sources to sensors and represent transport of light.

• A light paths begins at a light source, and ends at the


sensor.
• Each vertex of the path (“bounce”) represents
interaction of transported light with an object surface,
and is described by the BRDF.
• Path segments do not intersect any other objects in
the scene.

Which paths should be considered?

Computer Graphics – Ray Tracing – Motivation 6–2


At first glance, it appears “natural” to follow paths from the light to the sensor, in
the same way that photons travel.

At each surface interaction, the continuation of a path is determined by the


surface’s BRDF (or local lighting model); hence we cannot ensure that the path
will eventually reach the sensor.

(Consider the 1045 /s photons emitted by the sun – only 1017 /cm2 /s reach the Earth surface.)

To generate an image, we interpret it as an array of sensors (one per pixel), and


we need values for every sensor. How can we construct corresponding light paths?

Let’s consider a specific setup in more detail.

Computer Graphics – Ray Tracing – Motivation 6–3


Camera Model
Typical cameras (and also the eye) use complex systems of lenses.

Due to the apertue (iris), light arrives at each sensor / image location only from a
small set of directions.

Computer Graphics typically does not simulate complicated optics. Instead, a


much simpler model is used.
Computer Graphics – Ray Tracing – Camera Model 6–4
Camera Model

In the real world, cameras (and also the eye) involve complex systems of lenses. We will
consider a much simplified setup.

camera ray
camera optical axis

view volume

dimage

image
plane

Camera rays (also primary rays) originate at the camera location and go through the
center of pixels in the image. Light is measured only if it arrives along such rays.

Computer Graphics – Ray Tracing – Camera Model 6–5


Assume the image plane is normalized to [−1, 1]2 .

The field-of-view angle α is given by dimage , and vice versa.

The set of rays (“camera”) can be


specified completely by giving
dimage
• image resolution rx × ry ,
• eye position e, e vlook
α
• vectors vlook , vright , and vup (3D), vright

• either α or dimage .

The view volume is the volume of the scene that can be seen by the camera; in this case
a so-called frustum.

Computer Graphics – Ray Tracing – Camera Model 6–6


The ray through the center of pixel (i, j) is then given by
 
r(t) = e + t · d = e + t · dimage · vlook + (i + 0.5)/rx · vright + (j + 0.5)/ry · vup .

Computer Graphics – Ray Tracing – Camera Model 6–7


Whitted Algorithm
It appears difficult to follow (“trace”) photons through the scene and have them
hit the sensor in exactly the right direction.

Better idea: reverse the direction of light paths (Helmholtz reciprocity!) and trace
photons backwards from the image plane.

Ray tracing (fundamental idea): construct light paths,

• segment by segment, i.e .from one surface interaction to the next,


• starting at the image plane and ending at a light source.

The throughput T of the path denotes the fraction of light intensity emitted by
the light source that arrives at the sensor.

Computer Graphics – Ray Tracing – Whitted Algorithm 6–8


T. Whitted described an algorithm in 1980 to construct light paths in a recursive
manner: for every pixels, a light path is constructed recursively, path segment by
path segment, by shooting rays in the scene.

He made two further key assumptions:

• The scene only contains point lights. This means that local lighting models
that we discussed in the previous chapter (e.g. Phong’s model) can be used
to determine surface reflection.
• Surface reflection can be classified into either purely (perfectly) specular
(reflection and refraction), or diffuse, which describes all other forms of
reflection (incl. local lighting models).

In this manner, he was able to eschew the full complexity of BRDFs, which could
not realistically be simulated at the time.
Computer Graphics – Ray Tracing – Whitted Algorithm 6–9
Whitted’s Algorithm

Step 1: construct camera ray r for current pixel and set T = 1.0

diffuse
mirror

diffuse

Computer Graphics – Ray Tracing – Whitted Algorithm 6–10


Step 2: determine nearest intersection (“hit”) of r with scene objects

diffuse
mirror

diffuse

Computer Graphics – Ray Tracing – Whitted Algorithm 6–11


Step 3: specular case
• Set r0 to the reflected / refracted ray.
• Multiply the throughput T by the surface reflectance / transmittance.
• Continue (recursively) at step 2 with r = r0 .

diffuse
mirror
r0

diffuse

Computer Graphics – Ray Tracing – Whitted Algorithm 6–12


Step 3: diffuse case (ray 1)
• Test whether hit point is in shadow by casting shadow ray rs to light.
• If not in shadow: apply local lighting model to obtain intensity I reflected at hit
point. Terminate path and set pixel color to T · I.

rs
r

diffuse
mirror

diffuse

Computer Graphics – Ray Tracing – Whitted Algorithm 6–13


Step 3: diffuse case (ray 2)
• Test whether hit point is in shadow by casting shadow ray rs to light.
• If not in shadow: apply local lighting model to obtain intensity I reflected at hit
point. Terminate path and set pixel color to T · I.

rs rs
r

diffuse
mirror

diffuse

Computer Graphics – Ray Tracing – Whitted Algorithm 6–14


Step 3: diffuse case (ray 3)
• Test whether hit point is in shadow by casting shadow ray rs to light.
• If in shadow: Set I to ambient intensity. Terminate path and set pixel color to T · I.

rs rs
r
rs

diffuse
mirror

diffuse

Computer Graphics – Ray Tracing – Whitted Algorithm 6–15


With this algorithm, Whitted was able to produce images such as this:

from Whitted’s original work

Computer Graphics – Ray Tracing – Whitted Algorithm 6–16


Practical details:

Finding hit points:


“Black box function”: given ray and scene, returns closest hit thit such that the hit point is

phit = o + thit d,

and corresponding object.

No hit:
If no object is hit, background intensity is returned.

Avoiding self-intersections:
Offset next ray (reflect / refracted / shadow ray) origin by εn, where n is the surface
normal at the hit point, and ε > 0 is very small.

Path Throughput:
Typically not a single number but an RGB triple.
Computer Graphics – Ray Tracing – Whitted Algorithm 6–17
Practical details (cont’d):

Multiple light sources:


Cast shadow ray per light source, add contributions.

Mixed surfaces:
Whitted’s algorithm is easily extended to surfaces that mix diffuse and (perfect) specular
characteristics. At each hit point, shadows rays as well as specular rays are followed.

Computer Graphics – Ray Tracing – Whitted Algorithm 6–18


Illustration and homework assignment:

only diffuse surfaces – no recursion

Computer Graphics – Ray Tracing – Whitted Algorithm 6–19


Illustration and homework assignment:

diffuse and specular surfaces

Computer Graphics – Ray Tracing – Whitted Algorithm 6–20


Illustration and homework assignment:

surfaces with mixed characteristics

Computer Graphics – Ray Tracing – Whitted Algorithm 6–21


Whitted’s (enhanced) algorithm performs a tree-like computation: at each surface hit

I = kd Idiffuse + ks Ispecular + kr Ireflected + kt Irefracted

direct illumination by point lights perfect specular reflection perfect specular transmission
shadow rays reflected ray refracted ray

Directly encodes the recursive nature of the rendering


equation; integral reduced to sum of four terms.
Deeper recursion levels create more work, but
contribute less to the final image because of absortion
(throughput shrinks quickly).

Computer Graphics – Ray Tracing – Whitted Algorithm 6–22


Illustration: Effect of Recursion Depth (Reflection – Depth 1)

Computer Graphics – Ray Tracing – Whitted Algorithm 6–23


Illustration: Effect of Recursion Depth (Reflection – Depth 2)

Computer Graphics – Ray Tracing – Whitted Algorithm 6–24


Illustration: Effect of Recursion Depth (Reflection - Depth 3)

Computer Graphics – Ray Tracing – Whitted Algorithm 6–25


Illustration: Effect of Recursion Depth (Reflection - Depth 5)

Computer Graphics – Ray Tracing – Whitted Algorithm 6–26


Illustration: Effect of Recursion Depth (Reflection - Depth 6)

Computer Graphics – Ray Tracing – Whitted Algorithm 6–27


Illustration: Effect of Recursion Depth (Refraction - Depth 1)

Computer Graphics – Ray Tracing – Whitted Algorithm 6–28


Illustration: Effect of Recursion Depth (Refraction - Depth 2)

Computer Graphics – Ray Tracing – Whitted Algorithm 6–29


Illustration: Effect of Recursion Depth (Refraction - Depth 3)

Computer Graphics – Ray Tracing – Whitted Algorithm 6–30


Illustration: Effect of Recursion Depth (Refraction - Depth 6)

Computer Graphics – Ray Tracing – Whitted Algorithm 6–31


Ray-Object Intersection
How to determine if a ray hits an object?
d
Ray equation:
r(t) = o + t · d hit t = 16

with ray origin o and (often normalized) direction d.


hit t = 10
Procedure:
hit t=7
• Intersect each object and ray, typically by solving a
system of equations, giving one or more solutions t
hitclosest hit t=2
per object.
o
• An intersection (“hit”) is found if at least one solution t = −1

with t > 0 exists (solutions with t ≤ 0 are discarded).


• Determine, over all objects, the intersection with the t = −7
smallest positive t – the closest hit.

Computer Graphics – Ray Tracing – Ray-Object Intersection 6–32


Ray-Sphere Intesection: consider a sphere with center c and radius R.

The sphere is implicitly given as kx − ck2 = R2 .


0
With q = o − c, intersection candidates are solutions t of D
<
0
=
D 0
>
R2 = ko + td − ck2 = hq + td, q + tdi D

which expands to the quadratic equation

t2 hd, di + 2thd, qi + hq, qi = R2

If d is normalized and D := hd, qi2 − hq, qi + r2 the solutions are



t1,2 = −hd, qi ± D

There can be no (D < 0) solution, one solution (D = 0) or two solutions (D > 0).

Computer Graphics – Ray Tracing – Ray-Object Intersection 6–33


Ray-Plane Intersection: consider a plane with normal n and support vector p.

The plane is implicitly given as hn, x − pi = D with D = hn, pi.

Set q = o − p then intersection candidates are solutions t of

D = hn, o + td − pi = hn, q + tdi = hn, qi + thn, di

Again, d is normalized hence the solution is

D − hn, qi
t=
hn, di

There is one solution unless hn, di = 0; in this case the ray is parallel to the plane.

Problem?
Intersection tests are arithmetically inexpensive, but must be done for many primitives
Computer Graphics – Ray Tracing – Ray-Object Intersection 6–34
Slightly more formal:

The complexity of the ray tracing algorithm is O(mp), where m the number of
objects and p is the number of pixels.

• Each ray must be tested against every object for intersection.


• Recursion does not affect this;
for recusion depth r each object is checked at most 2r times.

Typical contemporary production scenes can contain 108 primitives; so this naïve
is prohibitive for realistic scenes with 108 triangles.

Computer Graphics – Ray Tracing – Ray-Object Intersection 6–35


Illustration: Disney Moana Island Scene, 95 million primitives

Computer Graphics – Ray Tracing – Ray-Object Intersection 6–36


Illustration: Disney Moana Island Scene, 95 million primitives

Computer Graphics – Ray Tracing – Ray-Object Intersection 6–37


Opportunities for acceleration:

• Efficient intersection tests (→ Chapter 8)

• Fewer rays
• adaptive sampling (do not shoot every ray)
• stochastic sampling (→ Chapter 7)

• Fewer intersection tests


• early exclusion of non-relevant objects
• bounding volumes (boxes, spheres)
• spatial subdivision

Computer Graphics – Ray Tracing – Ray-Object Intersection 6–38


Reduction of intersection tests with bounding volumes

• A bounding volume is a simple-to-describe


volume that contains a more complex object.

Usually: spheres or cuboids (“bounding box”).


• A ray is tested for bounding volume intersection
first. If it does not intersect the bounding volume,
the contained object can not be intersected either.
• Seems like overhead (intersect both volume and
object), but drastically decreases effort if the ray
does not hit the bounding volume.
• Allows bounding of ray interval: if another object
is closer, can skip full intersection test.

Computer Graphics – Ray Tracing – Ray-Object Intersection 6–39


Hierarchical Bounding Volumes (BVH):

• If only a few bounding volumes are used,


the effective effort stays the same.

• Better: Arrange bounding volumes in a


nesting (tree) structure
• bigger bounding volumes
enclose smaller ones
• complexity can often
be reduced to O(log(p))

Computer Graphics – Ray Tracing – Ray-Object Intersection 6–40


Hierarchical bounding volumes (BVH, cont’d):

• Intersection algorithm traverses the tree recursively, starting at the root


• if the ray misses the bounding volume, no interesection test
• if the ray hits, recursive continuation in the child-volumes
• closer child-volumes can be testet earlier than farther ones
• Effectivness typically very good, but strongly depends on scene and hierarchy.

Computer Graphics – Ray Tracing – Ray-Object Intersection 6–41


Bounding volumes: fit (empty space) vs. complexity

fit

bounding sphere bounding box bounding box


axis-aligned oriented

complexity

In practice, bounding spheres and bounding boxes are typically used.


Computer Graphics – Ray Tracing – Ray-Object Intersection 6–42
Example: Bounding Spheres

• Construction:
• determine maximal distance to all object points from object centering
• heuristic: compute bounding box, set bounding sphere with same center point

• mostly bad fit (lots of empty space around object)


• analytical description faster, but conservative intersection test
• construction of hierarchy through either
• partitioning into increasingly smaller bounding volumes (top-down)
• summary of increasinly bigger bounding volumes (bottom-up)

Computer Graphics – Ray Tracing – Ray-Object Intersection 6–43


Illustration: bounding sphere hierarchy, top-down

Computer Graphics – Ray Tracing – Ray-Object Intersection 6–44


Example: Axis-aligned bounding boxes (AABBs)

• construction through min/max computation of object points


• hierarchy through successive partitioning along x/y/z-axes
• possible criteria for partitioning axis:
• longest dimension
• number of objects in child-volumes
• volume of child-volumes

Computer Graphics – Ray Tracing – Ray-Object Intersection 6–45


Example: Oriented bounding boxes (OBBs)

• very complex construction for slightly tigher fit

Computer Graphics – Ray Tracing – Ray-Object Intersection 6–46


Space partitioning: subdivision of space into disjoint subsets (partitions)

• Each region saves a list of contained objects


• At runtime only the regions are testet that are hit by the ray

General problem of spatial subdivision:


Objects are intersected by region boundaries

Two possibilities:

• objects saved in every touched region (increases number of object


references)
• objects are cut (increases number of objects)

Computer Graphics – Ray Tracing – Ray-Object Intersection 6–47


Example: Spatial Subdivision

• Five references must to be saved instead of two


• C1 always needs to be tested

Computer Graphics – Ray Tracing – Ray-Object Intersection 6–48


Example: Spatial subdivision

• Three lines need to be saved instead of one

Computer Graphics – Ray Tracing – Ray-Object Intersection 6–49


Spatial subdivision via Grid

• regular subdivision, 3D array of


cells
• rays march from cell to cell and
test with contained objects
(Bresenham!)
• very fast traversal, but
ineffective if objects
concentrate in small regions
(many empty cells)
• grid resolution tricky to choose

BVH typically preferred unless scene type well-suited for grids.


Computer Graphics – Ray Tracing – Ray-Object Intersection 6–50
Properties of grid subdivisions:

• grid traversal from cell to cell is faster


• ineffective subdivision if many objects concetrate in small region (overhead
through empty many cells)
• grid resolution
• too low: too many objects / primitives per cell
• too high: too many empty cells to save and traverse

Non-uniform subdivision allows for more flexibility and adjusts better to scene
geometry.

Computer Graphics – Ray Tracing – Ray-Object Intersection 6–51


Ray Marching
We have so far not considered actual geometric models, i.e. how to describe
objects. This will follow in Chapter 8.

However, here, I would like to briefly discuss a technique that works without
explicit geometrical description, but still allows to model reasonably complex
scenes.

This technique will also be used in some of the homeworks. The core idea is to
represent the entire scene through distance functions.

Computer Graphics – Ray Tracing – Ray Marching 6–52


Objects can be explicitly described as geometrical primitives (triangles, . . . ), but also
implicitly through distance functions.

Example: Sphere 0 = dsphere (x) := kx − ck − r

float sdSphere( vec3 x, float r ) {


return length(p)-r;
}

Object points satistfy d(x) = 0, or approximately d(x) <  for a small  > 0

While this works for arbitrary distance functions, signed distance functions (SDFs) also give d(x) < 0 if x is
inside the object.

Computer Graphics – Ray Tracing – Ray Marching 6–53


Objects can be explicitly described as geometrical primitives (triangles, . . . ), but also
implicitly through distance functions.

Example 2: Box with center p and widths b (formula from here)

float sdBox( vec3 p, vec3 b ) {


vec3 q = abs(p) - b;
return length(max(q,0.0)) +
min(max(q.x,max(q.y,q.z)),0.0);
}

Object points satistfy d(x) = 0, or approximately d(x) <  for a small  > 0.

While this works for arbitrary distance functions, signed distance functions (SDFs) also give d(x) < 0 if x is
inside the object.

Computer Graphics – Ray Tracing – Ray Marching 6–54


Explicit intersections can be computed for a few specific distance functions, but not for
general or arbitrary distance functions.

Ray Marching:

• A ray is traversed starting at origin point.


• The distance function indicates how far
the current point is away from an object.
Skip ahead by this distance along the ray.
• If the distance falls below ε, an
intersection / hit is found.

This also works if the distance function underestimates the actual distance.

Multiple objects can considered by taking the minimum of all their distances.

Computer Graphics – Ray Tracing – Ray Marching 6–55


Distance functions can be created for many simple objects, see e.g. here for an extensive
overview.

Multiple objects can be combined by taking the minimum over the individual distance
functions. Effects such as repetition, deformation, etc. can be achived with surprising
ease (→ homework).

from iquilezles.org

Computer Graphics – Ray Tracing – Ray Marching 6–56


Example: Implicit model of a complex scene

from iquiezles.org

Computer Graphics – Ray Tracing – Ray Marching 6–57


Example: Implicit representation of a fractal (Mandelbulb + Mandelbox)

Computer Graphics – Ray Tracing – Ray Marching 6–58


Path Notation
Ray Tracing is a global illumination (GI) algorithm. GI algorithms can be classified by the
types of paths and the interactions the calculate.

Notation / symbols for light path vertices:

• E: eye • S: specular interaction (perfect)


• D: diffuse interaction • L: light source

Paths can be expressed as strings over this alphabet (Heckbert, 1990), and regular
expressions can be used to describe families of paths.

“Fully” GI algorithms need to simulate all combinations of interactions, i.e. all paths that
satisfy the regular expression:

L(D|S)*E

Computer Graphics – Ray Tracing – Path Notation for GI Algorithms 6–59


Illustration: Light Paths

Computer Graphics – Ray Tracing – Path Notation for GI Algorithms 6–60


Path Types in Ray Tracing L L

Ray tracing terminates all light paths


by shadow rays at a diffuse hit (or as S S D S

no hit).

All paths are of the form A: specular to specular B: diffuse to specular


NOT possible in ray tracing possible in ray tracing
LDS∗ E
L L

Some characteristics of GI are not


reproduced by ray tracing, e.g.: S D D D

• caustics (C)
• diffuse interreflection (indirect
C: specular to diffuse D: diffuse to diffuse
illumination / color bleeding, D) NOT possible in ray tracing NOT possible in ray tracing

Computer Graphics – Ray Tracing – Path Notation for GI Algorithms 6–61


Recap & Outlook
In this chapter, we considered

• a straightforward camera model to describe a sensor set up suitable for


computer graphics,
• Whitted’s classical ray tracing algorithm as a first photorealistic / GI
rendering algorithm,
• the path notation to obtain an understanding of which light paths a GI
algorithm simulates,
• techniques for ray-object intersection and their acceleration (BVH)
• implicit modeling of scenes through distance functions and ray marching

Computer Graphics – Ray Tracing – Recap & Outlook 6–62


We also saw that Whitted’s algorithm only
treats a subset of all light paths.
Next chapter: Full simulation of the rendering
equation, including all GI phenomena
(caustics, diffuse interreflection) through
approximation techniques.
• Monte-Carlo integration
• Radiosity

This will lead us to path tracing, a standard


algorithm for physically-based rendering with
full global illumination.

Computer Graphics – Ray Tracing – Recap & Outlook 6–63


Computer Graphics – Ray Tracing – Recap & Outlook

You might also like