The Ray Tracing Algorithm - University of Utah
Transcript of The Ray Tracing Algorithm - University of Utah
![Page 1: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/1.jpg)
CS6620
The Ray Tracing Algorithm
January 25, 2010
1
![Page 2: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/2.jpg)
CS6620
Logistics
OptiX updateSchedule updateWeb site updateOutstanding procedural questions?
![Page 3: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/3.jpg)
CS6620
Rays
Line segment: two pointsRay: a point and a vectorLine segment: a ray and a distanceLine segment: a ray and two distancesBounded ray: two ordered pointsBounded ray: a ray and two distancesDirected line segment: bounded rayLine: any of the above
![Page 4: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/4.jpg)
CS6620
Rays
Usyally ray consists of a point and a vector:Class Ray { Point origin; Vector direction; …}; Origin
Direction
![Page 5: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/5.jpg)
CS6620
Parametric Rays
We usually parameterize rays:
Where O is the origin,V is direction,and t is the “ray parameter”
t=0
t=1.0
t=2.0
P= O+ tV
![Page 6: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/6.jpg)
CS6620
Bounded Rays
The interval [t1,t2] says which part of the ray is “live”
t1=1.0
t2=2.0
P= O+ tV
![Page 7: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/7.jpg)
CS6620
A implicit equation for a plane can be defined with a Vector (the normal to the plane) and a point on the plane:
A parametric ray is :
A ray intersection is an combination of those equations:
Ray-Planes
7
(P − P0)• N = 0P(t) = O + tV
P(t) = O + tV
P(t) = O + tV
(O + tV − P0)• N = 0
![Page 8: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/8.jpg)
CS6620
Ray plane 2
Equation from last slide:
Rearrange:
Solve for t:
8
t(V • N ) + (O − P0)• N = 0
(O + tV − P0)• N = 0
t =(P0 −O)• N
V • N
![Page 9: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/9.jpg)
CS6620
Colors
For the purpose of this class, Color is Red, Green, BlueRange is 0-1 for LDR and positive (usually) for HDROther color models will be discussed briefly in a few weeksColors should be represented using the “float” datatype - others just don’t make senseDefine operators that make sense
![Page 10: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/10.jpg)
CS6620
Image gotchas
Be careful - image coordinate system is “upside down”
y=0
y=0
Real worldOur ray tracerOpenGLTaught since 2nd grade
TelevisionsRaster ImagesOther 1950’s technology
![Page 11: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/11.jpg)
CS6620
Geometric Queries
Back to the original question:What queries can we perform on our virtual geometry?
Ray tracing: determine if (and where) rays hit an object
Where?
O + t
V
![Page 12: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/12.jpg)
CS6620
Ray plane again
We have:
What does it mean when the denominator is small?
What does it mean when t is negative?
12
t =(P0 −O)• N
V • N
![Page 13: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/13.jpg)
CS6620
Ray-sphere intersection
Points on a sphere are equidistant from the center of the sphere
Our measure of distance: dot product
Equation for sphere:
P −C( )i P −
C( ) − r2 = 0
t 2V iV + 2t
O −C( )i V +
O −C( )i O −
C( ) − r2 = 0
![Page 14: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/14.jpg)
CS6620
Solve for the roots the using quadratic equation.Note that because b has a “2” in it we can
dervive some efficiencies.
Ray-sphere intersection, improved
t 2 V iV + 2t
O −C( )i V +
O −C( )i O −
C( ) − r2 = 0
Vector ′O =
O −C
a =V iV
b = 2 ′O iV
c = ′O i ′O − r2
![Page 15: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/15.jpg)
CS6620
Ray tracing architecture
The major components in a ray tracer are:Camera (Pixels to Rays)Objects (Rays to intersection info)Materials (Intersection info and light to color)LightsBackground (Rays to Color)
All together: a Scene
![Page 16: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/16.jpg)
CS6620
Ray tracing algorithmCreate scene (objects, materials, lights, camera,
background)Preprocess sceneforeach frame foreach pixel foreach sample generate ray intersect ray with objects find normal of closest object shade intersection point
Mutually recursive
![Page 17: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/17.jpg)
CS6620
Details Scene
LightsCameraLights
Lights
TopGroup
Bunny
TeapotTable
Glass Plastic Metal
Background
![Page 18: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/18.jpg)
CS6620
Details
Create scenePreprocess scene
Scene
LightsCameraLights
Lights
TopGroup
Bunny
TeapotTable
Glass Plastic Metal
Background
![Page 19: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/19.jpg)
CS6620
Details
Create scenePreprocess sceneforeach pixel
Row-major order
Framelessrendering
Tiled ParallelProgressive
![Page 20: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/20.jpg)
CS6620
Details
Create scenePreprocess sceneforeach pixel foreach sample
![Page 21: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/21.jpg)
CS6620
DetailsCreate scenePreprocess sceneforeach pixel foreach sample generate ray
![Page 22: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/22.jpg)
CS6620
Details
Create scenePreprocess sceneforeach pixel foreach sample generate ray intersect ray with objects
tnear
![Page 23: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/23.jpg)
CS6620
Details
Create scenePreprocess sceneforeach pixel foreach sample generate ray intersect ray with objects find normal of closest object
N
![Page 24: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/24.jpg)
CS6620
Details
Create scenePreprocess sceneforeach pixel foreach sample generate ray intersect ray with objects find normal of closest object shade intersection point
![Page 25: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/25.jpg)
CS6620
Ray tracing algorithmCreate scene (objects, materials, lights, camera,
background)Preprocess sceneforeach frame foreach pixel foreach sample generate ray intersect ray with objects find normal of closest object shade intersection point
Mutually recursive
![Page 26: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/26.jpg)
CS6620
Ray tracing architecture
The major components in a ray tracer are:Camera (Pixels to Rays)Objects (Rays to intersection info)Materials (Intersection info and light to color)LightsBackground (Rays to Color)
All together: a Scene
![Page 27: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/27.jpg)
CS6620
Ray tracing algorithmCreate scene (objects, materials, lights, camera,
background)Preprocess sceneforeach frame foreach pixel foreach sample generate ray intersect ray with objects find normal of closest object shade intersection point
Mutually recursive
![Page 28: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/28.jpg)
CS6620
Camera models
The camera maps pixels to raysWhat kind of camera models might we want?
![Page 29: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/29.jpg)
CS6620
Camera models
Typical:OrthographicPinhole (perspective)
Advanced:Depth of field (thin lens approximation)Sophisticated lenses (“A realistic camera model for computer graphics,” Kolh, Mitchell, Hanrahan)Fish-eye lensArbitrary distortions
![Page 30: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/30.jpg)
CS6620
Camera models
Map pixel coordinates -1 to 1Pay careful attention to pixel centers
Non-square imagesLongest dimension is -1 to 1, shorter is smaller (still centered at 0)Or camera knows about aspect ratio
1
-1
-1 1
(-.75, -.75)
(-.25, .25)
![Page 31: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/31.jpg)
CS6620
Orthographic projection
“Film” is just a rectangle in spaceRays are parallel (same direction)
![Page 32: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/32.jpg)
CS6620
Orthographic projectionSpecify with center (P) and two vectors (u, v)
O= P+ xu+ yv
V= u× v
u
, v
: image size
u
v = aspect ratio
square image: u⋅ v= 0
v
u
P
V
O
![Page 33: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/33.jpg)
CS6620
Pinhole camera
Most common model for ray tracingImage is projected upside down onto image plane
![Page 34: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/34.jpg)
CS6620
Pinhole camera
Easier to think about rightside upFocal point is also called the eye point
![Page 35: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/35.jpg)
CS6620
Pinhole cameraParameters:
E
: Eye point (focal point)C
: Lookat pointUp
: Up vectorθ: Field of view
E
C
Up
θ
![Page 36: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/36.jpg)
CS6620
Pinhole camera
Construction:
E
C
Up
θ
u
v
θ2
L= C− E
(look or gaze direction)
Ln
=L
L
utmp
= Ln
×Up
vtmp
= utmp
× Ln
Top View
L
![Page 37: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/37.jpg)
CS6620
Pinhole cameraHow do we get the lengths of u/v?
E
C
Up
θ
u
v
θ2
tanθ2=
u
Ln
u
= tanθ2
u=utmp
utmp tanθ
2
Top View
Ln
![Page 38: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/38.jpg)
CS6620
Pinhole cameraWhat about v?
E
C
Up
θ
u
v
θ2
aspect ratio = a = u
v
u
= tanθ2
v=
tanθ2
a
v=vtmp
vtmp
tanθ2
a
Top View
Ln
![Page 39: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/39.jpg)
CS6620
Pinhole camera
Finally
E
u
v
O= E
V= Ln
+ xu+ yv
Top View
Ln
V
![Page 40: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/40.jpg)
CS6620
Intersection
Use the sphere algorithm from earlierLoop over spheres to find minimum t value
![Page 41: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/41.jpg)
CS6620
Shading
The shading step is the key aspect of ray tracing
![Page 42: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/42.jpg)
CS6620
ShadingPath tracing: consider light from all directions
Whitted Ray tracing: consider the dominant directions:direct (unobstructed from light source)reflectionrefraction Light source
![Page 43: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/43.jpg)
CS6620
Shadow raysShadows are computed by tracing rays from (to) the light sourceIntersection point: Origin:Direction: max t: 1.0
P= O+ tV
L− P
P
L
} max t
![Page 44: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/44.jpg)
CS6620
Shadow ray bugsTwo bugs might show up when you do this:
False shadows (considering rays <0 or > 1)Freckles (considering rays == 0)
L− P
P
L
} max t
![Page 45: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/45.jpg)
CS6620
Numerical precisionZoomed in: ideal
![Page 46: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/46.jpg)
CS6620
Numerical precision
Zoomed in (numerical roundoff)
![Page 47: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/47.jpg)
CS6620
Numerical precision
Zoomed in (numerical roundoff)
![Page 48: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/48.jpg)
CS6620
Solutions:
Only consider intersections where t>small_numOffset ray in normal direction: P+=N*small_numOffset ray in light source direction: P+=(L-P)*small_num
small_num = 1.e-6
![Page 49: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/49.jpg)
CS6620
Reflection
θi
V
N
R
P
θI=θr
S= V+ Ncosθi
R= Ncosθi + S
R= Ncosθi +V
+ Ncosθi
R= 2Ncosθi +V
R= 2N
−N⋅V( ) +V
R= V− 2 N⋅V( )N
S
S
N cos θi
θr
![Page 50: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/50.jpg)
CS6620
Review
The guts of a ray tracer have ray generation, ray intersection, and shadingMost ray tracers use RGB colorNow that ray tracers are interactive, the order in which you generate the rays may matter
50
![Page 51: The Ray Tracing Algorithm - University of Utah](https://reader031.fdocuments.in/reader031/viewer/2022020705/61fb70972e268c58cd5e3038/html5/thumbnails/51.jpg)
CS6620
Questions?
51