Ray Tracing
Mani Thomas
CISC 440/640
Computer Graphics
1
Photo-Realism
Created by David Derman – CISC 440
2
Created by Jan Oberlaender – CISC 640
3
Created by Jan Oberlaender – CISC 640
4
Created by Donald Hyatt
5
http://www.tjhsst.edu/~dhyatt/superap/povray.html
Bunny – A computer animated
short film
Blue Sky Studios Inc
A film by Chris Wedge with music by Tom Waits and
produced by Nina Rappaport
6
Introduction - Light
Three Ideas about light
Light rays travel in straight lines
Light rays do not interfere with each other if
they cross
Light rays travel from light source to the eye,
but the physics is invariant under path
reversal (Helmholtz reciprocity)
Sen, et al., “Dual Photography”, SIGGRAPH
P.
2005
Novel photographic technique to interchange the lights
and cameras in a scene
7
Introduction - Ray tracing
What is Ray Tracing?
Ray Tracing is a global
illumination based
rendering method for
generating realistic
images on the computer
Originators
Appel 1968
Goldstein and Nagel
Whited 1979
Courtesy of Pat Hanrahan, Computer Graphics: Image Synthesis Techniques 8
Introduction – Ray tracing
Appel
Ray Casting
Goldstein and Nagel
Scene Illumination
Whited
Recursive ray tracing (reflection and refraction)
Forward and Backward Ray tracing
Courtesy of Pat Hanrahan, Computer Graphics: Image Synthesis Techniques 9
Introduction – Ray tracing
Forward Ray tracing
Rays from light source
bounce of objects
before reaching the
camera
Computational
wastage
Backward Ray tracing
Track only those rays
that finally made it to
the camera
10
Courtesy: Angel
Ray Casting
Ray Casting
visible surfaces of
objects are found by
throwing (or casting)
rays of light from the
viewer into the scene
Ray Tracing
Extension to Ray
casting
Recursively cast rays
from the points of
intersection
11
Courtesy: Angel
Ray casting
In ray casting, a ray of light is traced in a
backwards direction.
We start from the eye or camera and trace the
ray through a pixel in the image plane into the
scene and determine what it intersects
The pixel is then set to the color values
returned by the ray.
If the ray misses all objects, then that pixel is
shaded the background color
12
Algorithm – Ray casting
Courtesy F.S. Hill, “Computer Graphics using OpenGL”
13
Ray tracing - overview
Ray casting finds the visible surfaces of objects
Ray tracing determines what each visible
surface looks like
This extra curiosity is quite heavy on your processor
But it allows you to create effects that are very
difficult or even impossible to do using other
methods.
Reflection
Transparency
Shadows
Courtesy: http://www.geocities.com/jamisbuck/raytracing.html
14
Ray tracing - overview
Ray tracing algorithm is a “finitely” recursive image
rendering
First stage: Like ray casting
Shoot a ray from the eye (Primary ray)
Determine all the objects that intersect the ray
Find the nearest of intersections
Second stage:
Recurses by shooting more rays from the point of intersection
(Secondary rays)
Find the objects that are reflected at that point
Find other objects that may be seen through the object at that point
Find out light sources that are directly visible from that point
Repeat the second stage until required
15
Ray Tracing – example
Sometimes a ray misses all objects
created by Michael Sweeny, et al for ACM SIGGRAPH Education slide set 1991
16
Ray Tracing – example
Sometimes a ray hits an object
created by Michael Sweeny, et al for ACM SIGGRAPH Education slide set 1991
17
Ray Tracing – example
Is the intersected point in shadow?
“Shadow Rays” to light source
created by Michael Sweeny, et al for ACM SIGGRAPH Education slide set 1991
18
Ray Tracing – example
Shadow rays intersect another object
First intersection point in shadow of the
second object
created by Michael Sweeny, et al for ACM SIGGRAPH Education slide set 1991
19
Ray Tracing – example
Shadow rays intersect another object
First intersection point in shadow of the
second object
created by Michael Sweeny, et al for ACM SIGGRAPH Education slide set 1991
20
Ray Tracing - example
Reflected ray generated at point of
intersection
Tested with all the objects in the scene
created by Michael Sweeny, et al for ACM SIGGRAPH Education slide set 1991
21
Ray Tracing - example
Local illumination model applied at the
point of intersection
created by Michael Sweeny, et al for ACM SIGGRAPH Education slide set 1991
22
Ray Tracing – example
Transparent object
Spawn a transmitted ray and test against all
objects in the scene
created by Michael Sweeny, et al for ACM SIGGRAPH Education slide set 1991
23
Requirements for Ray tracing
Compute 3D ray into the scene for each 2D image pixel
Compute 3D intersection point of ray with nearest object
in scene
Test each primitive in the scene for intersection
Find nearest intersection
Recursively spawn rays from the point of intersection
Shadow Rays
Reflected rays
Transmitted rays
Accumulate the color from each of the spawned rays at
the point of intersection
24
Ray object intersection
Equation of a ray r t S ct
“S” is the starting point and “c” is the direction of the
ray
Given a surface in implicit form F(x,y,z)
plane: F x, y, z ax by cz d n x d
sphere: F x, y , z x 2 y 2 z 2 1
cylinder: F x, y , z x 2 y 2 1 0 z 1
All points on the surface satisfy F(x,y,z)=0
Thus for ray r(t) to intersect the surface F r t 0
The hit time can be got by solving F S cthit 0
25
Ray object intersection
26
3D Object Intersection: http://www.realtimerendering.com/int/
Ray plane intersection
Equation of a ray r t S ct
Equation of a plane F x, y, z ax by cz d n x d
n is the normal to the plane and d is the
distance of the plane from the origin
Substituting and solving for t
F x, y , z r t n x d r t 0
n S ct d 0
n S d
n S n c t d 0 t
n c
27
Ray triangle intersection
Tomas Möller and Ben Trumbore, “Fast, minimum
storage ray-triangle intersection”, Journal of graphics
tools, 2(1):21-28, 1997
Barycentric coordinates
p 1 u v P0 uP1 vP2 u, v 0, u v 1
p is a point in a triangle with vertices P0, P1, P2
If r(t) belongs to both the line and triangle
r t S ct 1 u v P0 uP1 vP2
Solve for (t, u, v)
If (u, v) complies with the restriction, then ray r(t) intersects the
triangle
Using t the intersection point can be easily computed
Courtesy of http://www.lighthouse3d.com/opengl/maths/index.php?raytriint 28
Ray triangle intersection
• R. J. Segura, F. R.
Feito,”Algorithms to
test Ray-triangle
Intersection
Comparative Study”,
WSCG 2001
29
Courtesy of http://www.lighthouse3d.com/opengl/maths/index.php?raytriint
Ray Sphere intersection
Implicit form of sphere given center (a,b,c) and
radius r p p r p x, y, z , p (a, b, c)
c
2 2
c
Intersection with r(t) gives S ct p r c
2 2
By the identity a b a b 2a b
2 2 2
the intersection equation is a quadratic in “t”
2
S ct pc r 2 t 2 c 2tc S pc S pc r 2
2 2
Solving for “t” t c S p c c S p c 2 c 2 S p c 2 r 2
Real solutions, indicate one or two intersections
Negative solutions are behind the eye
If discriminant is negative, the ray missed the sphere
30
Adding shadows
P is not in shadow with respect to
L1
P is in the shadow of the cube
with respect to L2
“Self-shadowing” of P with
respect to L3
“Shadow Feelers”
Spawn a ray from P to the light
sources
If there is an intersection of the
shadow ray with any object then
P is in shadow
NOTE: Intersection should be
between the point and light
source and not behind light Courtesy F.S. Hill, “Computer Graphics using OpenGL”
source
31
Adding shadows
“Self-Shadowing”
Always an intersection of
shadow feeler with
object itself
Move start point of the
shadow ray towards the
eye by a small amount
No intersection with
itself
Courtesy F.S. Hill, “Computer Graphics using OpenGL”
32
Reflection
Given surface normal “n” and incident ray “a”
find the reflected ray “r”
n n a n
m a
n n
n
2
n
r e m a m m a 2m
a n n
ˆ ˆ n 1 a 2a n n
ˆ ˆ
a n cos180 1 n a n n
ˆ ˆ ˆ ˆ
180 1
adapted from F.S. Hill, “Computer Graphics using OpenGL” 33
Reflection
34
Created by David Derman – CISC 440
Reflection
35
Created by David Derman – CISC 440
Reflection
36
Created by David Derman – CISC 440
Reflection
37
Created by David Derman – CISC 440
Refraction
Bending of light rays as it crosses
interface between media having different
refractive indices
Snell’s Law
c1 sin 1 c2 sin 2
c1,c2 – Refractive
Courtesy F.S. Hill, “Computer Graphics using OpenGL”
index
38
Refraction
Estimation the refracted
ray T, given u and N (unit
vectors)
If we assume that ni and
nr are the refractive
indices of the medium
having the incoming ray
and refracted ray
respectively
Let and be the
i r
corresponding angle of
incidence and refraction
Adapted from Hearn and Baker, “Computer Graphics with openGL”
Let k be a unit vector
perpendicular to N
By Snell’s Law we have ni sin i nr sin r
39
Refraction
Decomposing the
incident ray (u)
u u n n u k k
u k k u n n
sin i k cos i n
Decomposing the
refracted ray (T)
T T n n T k k
T k k T n n
sin r k cos r n
Adapted from Hearn and Baker, “Computer Graphics with openGL”
Solving for k from u
k
1
u cos in
sin i
40
Refraction
Substituting in T
sin r
T cos r n u cos i n
sin i
From Snell’s Law
sin r ni
sin i nr
Solving for T
T cos r n u cos i n
ni
nr
n
u i cos i n cos r n
ni
n
nr r Adapted from Hearn and Baker, “Computer Graphics with openGL”
ni n
u cos r i cos i n
nr nr
41
Tree of Light
Contributions of light grows at each contact point
I is the sum of reflected component R1, transmitted
component T1 and the local component L1
Local component is the ambient, diffuse and specular reflections
at Ph
R1 is the sum of R3, T3 and local L3 and so on ad
infinitum
42
Ray tracing flow
Local Phong
illumination
Figure out
reflected/
refracted ray
direction
and recurse
43
Adapted from F.S. Hill and CISC 640/440, Fall 2005
References
Textbooks
F. S. Hill, “Computer Graphics Using OpenGL”
Commonly used ray tracing program (completely
free and available for most platforms)
http://www.povray.org/
Interesting Links
Interactive Ray Tracer – Alyosha Efros
Ray Tracing explained
http://www.geocities.com/jamisbuck/raytracing.html
http://www.siggraph.org/education/materials/HyperGr
aph/raytrace/rtrace0.htm
44