Ray Tracing

1
RAY TRACING
Afshin Ameri, Thomas Larsson
Mälardalen University
WHAT IS RAY TRACING?
Artwork: Gilles Tran, Source:
Wikipedia
WHAT IS RAY TRACING?
•
Highly Realistic
•
Soft Shadows
•
Reflections, Inter-reflections
•
Refractions
•
Depth of Field
•
Aperture Shape
•
…
•
Expensive Calculations
•
Used in many movies, animations
Artwork: Gilles Tran, Source:
Wikipedia
MORE SAMPLES…
Which one is rendered by Ray Tracing methods? How do you know?
Source: deviantArt
5
IMAGE RESOLUTION
• How many pixels do we want?
• The image resolution is important for
• Image quality
• Rendering speed
• Example resolutions
• Common 4:3 resolutions
• 640 x 480, 800 x 600, 1024 x 768
• Wide-screen 16:9
• 1280 x 720 (HD)
• 1920 x 1080 (Full HD)
• 7680 x 4320 (Ultra High Definition
Video)
• Other
• 720 x 576 (DVD-Pal, 25 fps)
6
THE BASICS
RAY TRACING – THE IDEA
How do we “see” things?
• Light from different light sources travels in different directions
• It interacts with different objects and surfaces
• The interaction includes, absorption, reflection and refraction
• A tiny part of this light reaches our eyes and we “see” things
• The final color of what we see depends on light’s color, the surfaces involved and
how they interacted with the light
Let’s do the calculations for that and get a nice realistic picture 
•
Most of the light does not reach our eyes
•
Very expensive
RAY TRACING – THE IDEA
Solution?
• Trace the rays backwards
• We will only simulate the rays that actually reach the eye
Light
Image
Plane
Eye
Object
RAY TRACING – THE IDEA
10
BASIC STEPS
• Generate primary rays
• Send rays from viewpoint directed through the pixel
locations in the image plane into the 3D scene
• Ray tracing & traversal
• For each ray, find closest surface hit point in scene
• Shading
• Calculate the amount of direct light for each hit
point
• Includes checking if point is in shadow or not
• Gather secondary light contributions by shooting
recursive rays from the hit points
11
GENERATION OF RAYS
for all pixels i, j in image
ray.setStart(0, 0, 0);
ray.setDir(i, j);
ray.normalize();
image[i][j] =
rayTrace(ray);
return image;
12
DEFINITION OF A RAY
• A ray is defined by a function of a parameter t:
r=o+td
• The direction d of the ray is normalized.
t = 2.5
d = (xd, yd, zd)
t = 2.0
t = 1.0
o = (xo, yo, zo)
t = 0.0
13
INTERSECTION TESTS
• For each geometric object in a scene, we
must be able to compute if and where it is
hit by a ray
• Intersection tests
•
•
•
•
•
ray - triangle
ray - polygon
ray - sphere
ray - torus
ray - Bézier patch
• More general tests
• ray - implicit surface
• ray - NURBS
14
RAY/TRIANGLE TEST
• Calculate the point p where the ray hit the triangle
• May also need to calculate other type of data at the
hitpoint
• normal n, texturencoords, etc.
p
R=o+td
tp = ?
p=?
n=?
15
RAY/OBJECT
INTERSECTIONS
• Parametric ray: r(t) = o + t d
• t0
• Since ||d|| = 1, t is distance along ray
• Implicit object: f(x) = 0
• Intersection occurs when f(r(t)) = 0
• Real function of one real variable
• Intersection  root finding
• Simple for planes and quadrics
16
• Easy to compute ray/surface
intersection points
• At most two ray/surface
intersection points
• Types
•
•
•
•
•
Sphere: x2 + y2 + z2 – r2
Cylinder: x2 + y2 – r2
Cone: x2 + y2 – z2
Paraboloid: x2 + y2 – z
Hyperboloid: x2 + y2 – z2  r2
• Use transformation trick for
variations
QUADRICS
RAY TRACING – THE ALGORITHM
Ray/Sphere Intersection
• In vector notation a sphere is noted by ||x-c||2 = r2
• c is the center of the circle
• r is the radius
• So we have f(x)=(x - c).(x - c) - r2
• We will have:
• f(r(t)) = (o + t d - c).(o + t d - c) – r2
= d.d t2 + 2 (o-c).d t + (o-c).(o-c) - r2
• Let
A = d.d (= 1 since d has unit length)
B = 2 (o-c).d
C = (o-c).(o-c) - r2
• Then
c
r
D>0
D=0
D<0
Hit position p = o + t d
Normal n = (x – c)/||x – c||
18
RAY/ELLIPSOID
INTERSECTION
• Two possible solutions:
• Solve f(r(t)) = 0
• Transform problem to a corresponding Ray/Sphere
intersection test
• Finding the normal of an ellipsoid
19
FINDING THE CLOSEST HIT
• Brute force method:
• Check all objects one by one
• Keep the closest hit found so far
hit(objects, ray) {
for each object in scene
does ray intersect object?
if(intersected and was closer)
save that intersection
if(intersected)
return intersection point and normal
}
20
A LOCAL ILLUMINATION
MODEL
• Still, a Phong-like model is useful to lit a point locally
Light
N
Eye
I
for each light
In = IambientKambient +
IdiffuseKdiffuse (L.N) +
IspecularKspecular (R.V)n
R
V
Surface
RAY TRACING
Shadows
• Shadows are easy in Ray Tracing 
• We check to see if there is any object between the hit point and the light source
• Fire a “shadow ray”
• If there is any hit, then the point is in shadow
1
2
RAY TRACING
Reflections
• If the material at the point to be lit is
reflective, fire a reflection ray recursively to
gather light contributions from the
surroundings
• Note, the reflection direction is computed
also in the Phong Lighting model for
computation of specular highlights
The red objects are reflective.
RAY TRACING
Refraction
• Rays should be bent when passing through a
boundary between two media with different
refractive indices
• When a ray hits a transparent surface, send
a refraction ray into the object
• Note, if the ray passes through the other side
of the object, it bends again.
The red objects are reflective.
The hollow object is
transparent.
24
CALCULATING
REFRACTION RAYS
• Refraction
• Means the bending of light due to its
different velocities through different
materials
• Rays bend toward the normal when going
from sparser to denser materials (e.g. air
to water), and away from normal in the
opposite case
• Refractive index
• Light travels at speed c/n in a material of
refractive index n
• c is the speed of light in a vacuum
• c varies with wavelength, hence rainbows
and prisms
• Use Snell’s law n1 sin 1 = n2 sin 2 to derive
refracted ray direction
n
n1
n2
2
1
25
A “WHOLE” RAY TRACER
main() {
scene = readObjects();
image = renderImage();
showImage(image);
}
renderImage(){
for all pixels i, j in image
ray.setStart(0, 0, 0);
ray.setDir(i, j);
ray.normalize();
image[i][j] =
rayTrace(ray);
return image;
}
rayTrace(ray) {
hit(ray, p, n, object);
if(any hit)
return(lighting(p, n, color));
else
return backgrund color;
}
lighting(point, norm, color) {
color = ambient color;
for each light l
if(not hit(shadow ray))
color += Phong(l, point, norm);
color += rayTrace(reflection) *
pow(dot(reflection, ray), shininess);
return(color);
}
26
SOFT SHADOWS
• Point light sources produce sharp
shadow edges
• the point is either shadowed or not
• only one shadow ray per hit point is
required
• Area light sources cast soft shadows
• Only part of the light from the
sources reaches the point
• Penumbra
• Umbra
• Fire a whole bunch of shadow rays
towards different positions on the
light source
• Count how many rays that reach to
the light
27
ANTI-ALIASING
• Ray tracing computes a color for
every point in the image
• However, a square pixel contains
an infinite number of points
• These points may not all have
the same color
• Simplest sampling: choose the
color of one point (center of
pixel)
• This leads to aliasing
• jaggies
• moire patterns
• Solutions
• Supersampling
• Adaptive supersampling
• Stochastic sampling
Images by Don Mitchell
28
• Temporal aliasing
MOTION BLUR
• Aliasing happens in time as well as space
• the sampling rate is the frame rate, 30Hz for NTSC video, 24Hz for
film
• fast moving objects move large distances between frames
• if we point-sample time, objects have a jerky, strobed look
• To avoid temporal aliasing we need to filter in time too
• so compute frames at 120Hz and average them together (with
appropriate weights)
• fast-moving objects become blurred streaks
• Real media (film and video) automatically do temporal anti-aliasing
• photographic film integrates over the exposure time
• video cameras have persistence (memory)
• this shows up as motion blur in the photographs
29
CLASSIC MOTION BLUR EXAMPLE
• From Foley et. al. Plate III.16
• Rendered using distribution
ray tracing at 4096x3550
pixels, 16 samples per pixel.
• Note motion-blurred
reflections and shadows
with penumbrae cast by
extended light sources.
30
DISADVANTAGES OF RAY TRACING
• Computationally expensive
• Doing real-time ray tracing on current hardware is
challenging
• Overwhelming complexity for realistic scenes
• Not physically accurate
• E.g. no diffuse inter-reflection between surfaces
• Other techniques exist to handle these
shortcomings
• E.g. Radiosity
• Even more computationally expensive
31
RAY TRACING TEST
QUESTION
• Which types of inter-reflections are well captured by
classical ray tracing?
•
•
•
•
Specular/Specular
Diffuse/Diffuse
Specular/Diffuse
Diffuse/Specular
32
MOTIVATION FOR
ACCELERATION
• Ray tracing is slow!
• Hundreds of billions intersections tests may be
required to get a single image
• Brute force example
• Resolution 1000x1000 and 10 rays/pixel
• 1000 geometric primitives (e.g. triangles)
• 10 billion intersection tests
• 10 million tests/second
• Rendering time: 1000 seconds
33
SPEED-UP TECHNIQUES
• Brute force method checks each ray against every object.
• Whitted suggested usage of bounding volumes
• Faster intersection algorithms
• Acceleration data structures
•
•
•
•
Gives less intersection computations
Space and/or object partitioning
5D partitioning (space and direction)
Hierarchcial searching
• Parallelism
• Vectorized instruction sets (SIMD SSE)
• Multiple cores
• GPU-based ray tracing
• Exploit coherence
• Image coherence
• Spatial coherence
• Temporal coherence
• Adaptive Depth Control for recursive ray
34
SHADOW CACHING
• Save hit object when a shadow ray is cast
• Start shadow ray intersection search with objects
intersected by neighboring shadow queries
• Consider
• Neighboring eye rays
• Sampling of area lights
• Use a shadow cache
to keep track of the most
interesting shadow ray hits
35
DATA STRUCTURES
• Uniform Grid
• Hierarchical Grid
• Octree
• BSP, k-d-tree
• Bounding Volume Hierarchy (BVH)
• Constrained Tetrahedralizations
36
BOUNDING VOLUME
TREES
37
UNIFORM GRIDS
38
RAY GROUPING
• Coherent ray tracing
• Sending ray packets
• Leads to fewer traversals
• Exploit vectorized instructions
• In general, the degree of coherence depends on the
ray type
• eye, shadow
• reflection, refraction
39
PARALLELLISM
• Vectorized instruction sets
• Multiple core CPUs
• PC clusters (~50 PCs)
• Supercomputers with ~50 CPUs
• Many-core GPUs
40
FRAMELESS RENDERING
“Traditional” frameless rendering
Adaptive Frameless Rendering
[Dayal et al., EGSR05]
RAY TRACING IN THE
MOVIE “CARS”
41
• Previous Pixar movies were rendered with rasterization
• Why ray tracing
• Cars are very shiny and reflective!
• Detailed shadows,
• Ambient occlusion
• Goal: Extend RenderMan to use the
best of both worlds
• Typical scene at Pixar
• 100s of lights
• 1,000s of textures
• 10,000s of objects
• 100,000,000s of polygons
• Shaders with 10,000s lines of code
• For more information, see presentation
(Image source: Per Christensen’s presentation)
• Ray tracing for the movie ‘Cars’
by Per Christensen,
Pixar Animation Studios
42
EXAMPLE: SELF-REFLECTIONS
Environment map
Ray tracing
(Image source: Per Christensen’s presentation)
43
GLOBAL ILLUMINATION
• Physical correct model
• Photons are emitted from light sources.
• Photons hitting surfaces are scattered or absorbed.
• What we see is actually photons hitting the eye!
• Far to expensive to simulate
• A light source may emit e.g. 1020 photons/second
• A tiny fraction of these emitted photons hit the eye
• Need to solve simultaneously for incoming and outgoing light at
all surface points
• This is a massive integral equation
• Radiosity deals with the “easy” case of purely diffuse scenes
• Alternatively, try to sample a huge number of complete paths
from light source to camera.
RAY TRACING
Radiosity
• Only purely diffuse scenes.
• Viewport independent. We can calculate once and use it again, if the light sources
do not change.
RAY TRACING
Radiosity
Source: Waylon Brinck, http://www.waylonart.com
46
DISTRIBUTED RAY
TRACING
• Suggeste by Cook et al., 1984.
• Distribute rays throughout a pixel to get spatial antialiasing
• Distribute rays in time to get temporal antialiasing (Motion blur)
• Distribute rays in reflected ray direction to simulate glossy reflections
• Distribute rays across area light source to simulate soft shadows
• Distribute rays throughout lens area to simulate depth of field
• Distribute rays across hemisphere to simulate diffuse inter-reflection
(Radiosity)
47
PHOTON MAPPING
• Introduced by Henrik Wann Jensen
• Simulates transport of individual
photons
•
•
•
•
Photons emitted from source
Photons deposited on surfaces
Photons reflected between surfaces
Photons collected by rendering
• Two pass algorithm
• Pass one: photon tracing
• Build the photon map data structure
• Pass two: rendering
• Similar to ray tracing: Query both object and
nearby photons to determine color
CAUSTICS
(Image by Henrik Wann Jensen)
48
49
RAY-TRACED GAMES
• When will ray tracing replace rasterization in game
development?
• Lots of people, students, game developers, researchers and others, find this
question very interesting
• Problems
The competition is really hard
Full HD (1920x1080) and 60 fps
Complex dynamic scenes and special effects
Rasterization is super-fast
Current hardware have been optimized for parallel processing of triangles
and pixel for several decades
• Programmable shader stages provide many opportunities
•
•
•
•
•
• Spectacular progress have already been accomplished to
realize interactive ray tracing
• Many efforts have been discussed in the literature
• Hybrid algorithms already exist.
• However, real-time ray tracing continues to be a main challenge
50
RAY TRACING VS.
RASTERIZATION
Ray tracing
• Pros
• Embarrassingly parallel
• Conceptually simple
• Intersectable shapes are
renderable shapes.
• Natural support for realistic
shadows, reflections, and
refractions.
• Advanced effects can be
added by shooting even more
rays.
• Expected O(log n) performance
• Cons
• Computationally very expensive
Rasterization
• Pros
• Embarrassingly parallel
• Blazingly fast hardware after
decades of refinements and
optimizations
• Established APIs
• Support for programmable
shader stages
• Lots of special effects possible
(advanced lighting effects)
• Cons
• Rendering artefacts
• Parameter tuning
51
RAY TRACING LIBRARIES
• Major hardware companies show a lot of interest
in real-time ray tracing
• Embree by Intel
• OptiX by Nvidia
• FireRays by AMD
• This gives developers access to highly optimized
ray tracing kernels
• Performance still not enough for real-time ray
tracing to replace rasterization in games
52
CHALLENGES AHEAD
Old joke: ”Ray tracing is the
technology of the future, and
it always will be!”
• Performance requirement, example
• 100 rays/pixel, 1920x1080, 60 fps
• 12.5 billion rays/second
• New hardware designs
• Moore’s law is breaking down
• Heterogeneous architectures
• Dedicated ray tracing chips
• New and faster parallel algorithms
• Improved acceleration data structures
• Support for complex dynamic scenes
• The BVH might not be the final answer!
• Interesting things are definitely happening in these areas
• The quest for real-time ray tracing continues …
53
• There is a nice book
on ray tracing
• Ray tracing from the
ground up, Kevin Suffern,
2007
http://www.raytracegroundup.com/
THE END
“D’oh!”
Homer Simpson