Ray Tracing CPSC 453 – Fall 2018 Sonny Chan
Ray Tracing A method for synthesizing images of virtual 3D scenes.
Image Capture Devices Which one shall we use?
Goal: Simulate a Camera Obscura!
“Spheres & Checkerboard” Turner Whitted, 1979
Whitted Ray Tracing • “An improved illumination model for shaded display” - T. Whitted, SIGGRAPH 1979 • 512 × 512 image • Rendered on VAX 11/780 • 74 minutes compute time • How fast would your computer render this today? [image courtesy of P . Hanrahan, Stanford University]
The Ray Tracing Algorithm • For every pixel or position on your image, do 1. ray generation : where am I looking? 2. ray intersection : do I see something? 3. shading : what colour is it?
Ray Generation
virtual image plane
[from photojojo.com]
Let’s see how we might generate those rays
Ray Generation r ( t ) = e + t ( s − e )
Ray Intersection What do we see?
Ray Intersection • For every object we have in our scene, we need a way to determine whether or not we can see it • Usually amounts to solving an equation of one variable • We will examine three key intersection tests today: spheres - planes - triangles -
spheres
Ray-Sphere Intersection o d A ray: r ( t ) = o + t d A sphere: || p − c || = R R ( p − c ) · ( p − c ) − R 2 = 0 c ( r ( t ) − c ) · ( r ( t ) − c ) − R 2 = 0 Intersection: ( o + t d − c ) · ( o + t d − c ) − R 2 = 0 Solve for t…
planes
Ray-Plane Intersection o d A ray: r ( t ) = o + t d ˆ n A plane: ( p − q ) · ˆ n = 0 q Intersection: ( r ( t ) − q ) · ˆ n = 0 ( o + t d − q ) · ˆ n = 0 Solve for t…
triangles
How might we test intersection of a ray and a triangle?
Barycentric Coordinates p 2 v ( . 6 , . 4 , 0) p 1 ( . 3 , . 2 , . 5) w u p 0 f ( u, v ) = (1 − u − v ) p 0 + u p 1 + v p 2
p 2 Another Interpretation A 0 p 1 A 1 Ratio of (signed) areas of triangles: ( . 3 , . 2 , . 5) A = 1 2 ( p 1 − p 0 ) × ( p 2 − p 0 ) A 2 u = A 1 v = A 2 A A p 0 f ( u, v ) = (1 − u − v ) p 0 + u p 1 + v p 2
A Direct Approach for Intersection A ray: r ( t ) = o + t d A triangle: f ( u, v ) = (1 − u − v ) p 0 + u p 1 + v p 2 Ray-triangle intersect: o + t d = (1 − u − v ) p 0 + u p 1 + v p 2 t Rearrange terms: − d p 1 − p 0 p 2 − p 0 = o − p 0 u v Solve for t, u, and v…
Cramer’s Rule • Given a set of linear equations in matrix form x a b c = d y z • Write the determinant of the matrix � � a 1 b 1 c 1 � � � � det( a , b , c ) = a 2 b 2 c 2 � � � � a 3 b 3 c 3 � � • Then the solutions are x = det( d , b , c ) y = det( a , d , c ) z = det( a , b , d ) det( a , b , c ) det( a , b , c ) det( a , b , c )
A Direct Approach for Intersection t Our equation: − d p 1 − p 0 p 2 − p 0 = o − p 0 u v det( s , e 1 , e 2 ) t 1 Applying Cramer’s rule: = det( − d , s , e 2 ) u det( − d , e 1 , e 2 ) det( − d , e 1 , s ) v where e 1 = p 1 − p 0 , e 2 = p 2 − p 0 , s = o − p 0 Check that t > 0 and u, v, u+v are within [0,1] interval!
Ray-Object Intersection Tests • Now we know how to draw spheres, planes, and triangles in perspective with a ray tracer! • What else can we do?
[from photojojo.com]
I think we need some shading… What we’ve got now… and what we really want!
Shadows & More
What happens if we have more than one light?
How do we get these nice, soft shadows?
Depth of Field Can we ray trace it?
image object
The Ray Tracing Algorithm • For every pixel position on your image: 1. Generate a parametric viewing ray. 2. Test for intersection against each object in your scene. - What happens if it intersections more than one object? 3. Determine the colour to be carried back on the ray - Trace shadow ray(s) to obtain shadow effect
Things to Remember • Ray tracing is a relatively simple way for us to synthesize images of a 3D scene in perspective simulates the optics of a pinhole camera - • We need to devise a ray-object intersection for each kind of object geometry in the scene • Some effects like shadows are relatively easy to compute • Soft shadows and depth of field are possible if you’re willing to wait!
Recommend
More recommend