Anda di halaman 1dari 40

1

Ray Tracing
Mani Thomas
CISC 440/640
Computer Graphics
2
Photo-Realism
Created by David Derman CISC 440
3
Created by Jan Oberlaender CISC 640
4
Created by Jan Oberlaender CISC 640
5
Created by Donald Hyatt
http://www.tjhsst.edu/~dhyatt/superap/povray.html
6
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
7
Introduction
What is Ray Tracing?
Ray Tracing is a global illumination based rendering
method for generating realistic images on the
computer
In ray tracing, 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
8
Overview
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
Courtesy: Angel
9
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
Courtesy: Angel
10
Algorithm Ray casting
Courtesy F.S. Hill, Computer Graphics using OpenGL
11
Ray Tracing
Sometimes a ray misses all objects
created by Michael Sweeny, et al for ACM SIGGRAPH Education slide set 1991
12
Ray Tracing (contd.)
Sometimes a ray hits an object
created by Michael Sweeny, et al for ACM SIGGRAPH Education slide set 1991
13
Ray Tracing (contd.)
Is the intersected point in shadow?
Shadow Rays to light source
created by Michael Sweeny, et al for ACM SIGGRAPH Education slide set 1991
14
Ray Tracing (contd.)
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
15
Ray Tracing (contd.)
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
16
Ray Tracing (contd.)
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
17
Ray Tracing (contd.)
Local illumination model applied at the
point of intersection

created by Michael Sweeny, et al for ACM SIGGRAPH Education slide set 1991
18
Ray Tracing (contd.)
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
19
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
20
Ray object intersection
Equation of a ray
S is the starting point and c is the direction of the
ray
Given a surface in implicit form F(x,y,z)
plane:
sphere:
cylinder:
All points on the surface satisfy F(x,y,z)=0
Thus for ray r(t) to intersect the surface
The hit time can be got by solving
( ) t t r c S+ =
( ) 1 , ,
2 2 2
+ + = z y x z y x F
( ) 1 0 1 , ,
2 2
< < + = z y x z y x F
( ) 0 = +
hit
t F c S
( ) ( ) 0 = t r F
( ) d d cz by ax z y x F + = + + + = x n , ,
21
Ray object intersection
Ray polygon intersection
Plug the ray equation into the implicit
representation of the surface
Solve for t
Substitute for t to find point of intersection
Check if the point of intersection falls within
the polygon
22
Ray object intersection
Ray sphere intersection
Implicit form of sphere given center (a,b,c) and radius
r
Intersection with r(t) gives
By the identity
the intersection equation is a quadratic in t

Solving for t
Real solutions, indicate one or two intersections
Negative solutions are behind the eye
If discriminant is negative, the ray missed the sphere
( ) ) , , ( , , ,
2
2
c b a z y x r
c c
= = = p p p p
2
2
r t
c
= + p c S
( ) b a b a b a + + = + 2
2 2 2
( ) ( )
2
2 2
2 2
2
2 r t c t r t
c c c
+ + = + p S p S c p c S
( ) ( ) ( ) ( )
2
2 2 2
r c t
c c c
= p S p S c p S c
23
Adding shadows
P is not in shadow with respect to
L
1
P is in the shadow of the cube
with respect to L
2
Self-shadowing of P with
respect to L
3
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
source
Courtesy F.S. Hill, Computer Graphics using OpenGL
24
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
25
Reflection
Given surface normal n and incident ray a
find the reflected ray r

adapted from F.S. Hill, Computer Graphics using OpenGL
( )
( )
( ) ( ) ( )n n a n n a
n n n a
n
n
n a
n
n
n
n
a m
180 cos
1
1
2
= =
= =

|
|
.
|

\
|
=
u
( ) ( ) ( )
( )n n a a
m a m m a m e r

2
2
=
= + = + =
1
180 u
26
Reflection
Created by David Derman CISC 440
27
Reflection
Created by David Derman CISC 440
28
Reflection
Created by David Derman CISC 440
29
Reflection
Created by David Derman CISC 440
30
Refraction
Bending of light rays as it crosses
interface between media having different
refractive indices
Snells Law


c
1
,c
2
Refractive
index
2 2 1 1
sin sin u u c c =
Courtesy F.S. Hill, Computer Graphics using OpenGL
31
Refraction
Estimation the refracted
ray T, given u and N (unit
vectors)
If we assume that n
i
and
n
r
are the refractive
indices of the medium
having the incoming ray
and refracted ray
respectively
Let and be the
corresponding angle of
incidence and refraction
Let k be a unit vector
perpendicular to N
By Snells Law we have

r r i i
n n u u sin sin =
i
u
r
u
Adapted from Hearn and Baker, Computer Graphics with openGL
32
Refraction
Decomposing the
incident ray (u)


Decomposing the
refracted ray (T)


Solving for k from u
Adapted from Hearn and Baker, Computer Graphics with openGL
( )( ) ( )( )
( ) ( )
( ) ( )n k
n n u k k u
k k u n n u u
i i
u u cos sin =
=
+ =
( )( ) ( )( )
( ) ( )
( ) ( )n k
n n T k k T
k k T n n T T
r r
u u cos sin =
=
+ =
( ) n u k
i
i
u
u
cos
sin
1
+ =
33
Refraction
Substituting in T

From Snells Law

Solving for T
Adapted from Hearn and Baker, Computer Graphics with openGL
( ) ( ) ( ) n u n T
i
i
r
r
u
u
u
u cos
sin
sin
cos + + =
r
i
i
r
n
n
=
u
u
sin
sin
( ) ( ) ( )
( ) ( )
n u
n n u
n u n T
|
|
.
|

\
|
=
|
|
.
|

\
|
+ =
+ + =
i
r
i
r
r
i
r i
r
i
r
i
i
r
i
r
n
n
n
n
n
n
n
n
n
n
u u
u u
u u
cos cos
cos cos
cos cos
34
Tree of Light
Contributions of light grows at each contact point
I is the sum of reflected component R
1
, transmitted
component T
1
and the local component L
1
Local component is the ambient, diffuse and specular reflections
at P
h
R
1
is the sum of R
3
, T
3
and local L
3
and so on ad
infinitum
35
Ray tracing flow
Figure out
reflected/
refracted ray
direction
and recurse
Local Phong
illumination
Adapted from F.S. Hill and CISC 640/440, Fall 2005
36
Super-sampling
Ray tracing is a point-sampling process
Take discrete looks at the scene along individual rays
passing through each pixel
Reduce aliasing due to this discrete signal
sampling
Courtesy F.S. Hill, Computer Graphics using OpenGL
37
Super-sampling
Instead of shooting one ray per pixel, shoot four rays through the
corners of a pixel
Color at the pixel is the average of the colors at each corners
Adaptive super-sampling (Whitteds approach)
Compute the intensity variation between the four corners with the
average
Shoot more rays through corners with higher intensity variation
Compute final color as a weighted average rather than the regular
average
Courtesy F.S. Hill, Computer Graphics using OpenGL
38
Using Extents
Ray tracing is slow, performing the same
functions.
Most of the time is spent in computing
intersections
Each ray should be intersected with every
object in the scene
Each ray, spawns out reflected/transmitted
rays which have to be interested with the
objects in the scene
39
Using Extents
Extent of an object is a shape that encloses the object
Compute complicated intersections if and only if the ray
hits the extent
Two shapes most commonly used as extents
Sphere specified by a center and radius (C , r)
Box specified by sides aligned to the coordinate axis
40
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
http://www.cs.berkeley.edu/~efros/java/tracer/tracer.html
Ray Tracing explained
http://www.geocities.com/jamisbuck/raytracing.html
http://www.siggraph.org/education/materials/HyperGr
aph/raytrace/rtrace0.htm

Anda mungkin juga menyukai