Lecture 12 & 13: Intro to Ray-Tracing & Ray-Surface Acceleration Computer Graphics and Imaging UC Berkeley
Course Roadmap Rasterization Pipeline Core Concepts Sampling Antialiasing Transforms Geometric Modeling Core Concepts Splines, Bezier Curves Topological Mesh Representations Subdivision, Geometry Processing Rasterization Transforms & Projection Texture Mapping Visibility, Shading, Overall Pipeline Intro to Geometry Curves and Surfaces Geometry Processing Today: Ray-Tracing & Acceleration Lighting & Materials Cameras & Imaging
Basic Ray-Tracing Algorithm
Light Rays Three ideas about light rays 1. Light travels in straight lines (mostly) 2. Light rays do not interfere with each other if they cross (photons do not have interactions; ignoring wave effects like interference) 3. Light rays travel from the light sources to the eye (but the physics is invariant under path reversal - reciprocity).
Ray Casting Appel 1968 - Ray casting 1. Generate an image by casting one ray per pixel 2. Check for shadows by sending a ray to the light
Ray Casting - Generating Eye Rays Pinhole Camera Model eye ray (starts at eye and goes through pixel) closest scene intersection point note: more intersection points eye point image plane light source
Ray Casting - Shading Pixels (Local Only) Pinhole Camera Model eye ray (starts at eye and goes through pixel) eye point image plane perform shading calculation here to compute color of pixel (e.g. Blinn Phong model) light source
Discussion: Ray Casting vs Rasterization Remember: Rasterization: 4x4 perspective matrix, project triangles to 2D screen, rasterize pixels Ray Casting: shoot rays through pixels into scene, intersect with triangles Discussion topics: Will they make the same picture? Which algorithm is faster, when? Other pros/cons? When would you use each algorithm, and why?
Discussion: Ray Casting vs Rasterization Will they make the same picture? Which algorithm is faster, when? Other pros/cons? When would you use each algorithm, and why? Ray approach better shaded than the rasterization? Shadows, sense of depth? Ray casting would be inefficient with respect to occlusion What happens when light is at the eye? Probably the same image? Because the same shading models? Transparency might change equivalence between models Ray casting, for every pixel must intersect against all triangles, very slow! Might need O(log n) algorithm, n number of triangles.
Recursive Ray Tracing An improved Illumination model for shaded display T. Whitted, CACM 1980 Time: VAX 11/780 (1979) 74m PC (2006) 6s GPU (2012) 1/30s Spheres and Checkerboard, T. Whitted, 1979
Recursive Ray Tracing eye point image plane light source
Recursive Ray Tracing Mirror ray (specular reflection) eye point image plane light source
Recursive Ray Tracing Refractive rays (specular transmission) eye point image plane light source
Recursive Ray Tracing eye point image plane light source Shadow rays
Recursive Ray Tracing secondary rays primary ray eye point image plane light source shadow rays Trace secondary rays recursively until hit a non-specular surface (or max desired levels of recursion) At each hit point, trace shadow rays to test light visibility (no contribution if blocked) Final pixel color is weighted sum of contributions along rays, as shown Gives more sophisticated effects (e.g. specular reflection, refraction, shadows), but we will go much further to derive a physically-based illumination model
Recursive Ray Tracing
Ray-Surface Intersection
Ray Intersection With Triangle Mesh Why? Rendering: visibility, shadows, lighting Geometry: inside/outside test How to compute? Let s break this down: Simple idea: just intersect ray with each triangle Simple, but slow (accelerate next time) Note: can have 0, 1 or multiple intersections
Ray Equation Ray is defined by its origin and a direction vector Example: Ray equation: 0 apple t<1 point along ray time origin unit direction
Plane Equation Plane is defined by normal vector and a point on plane Example: p 0 ) Plane Equation: 0 p :(p p 0 ) N =0 ax + by + cz + d =0 all points on plane any point normal vector
Ray Intersection With Plane Ray equation: r(t) =o + t d, 0 apple t<1 0 Plane equation: p :(p p 0 ) N =0 p 0 ) Solve for intersection Set p = r(t) and solve for t (p p 0 ) N =(o + t d p 0 ) N =0 t = (p0 o) N Check: 0 apple t<1 d N
Ray Intersection With Triangle Triangle is in a plane Ray-plane intersection Test if hit point is inside triangle (Assignment 1!) Many ways to optimize
Möller Trumbore Algorithm Why care about performance?
Ray Intersection With Sphere Ray: r(t) =o + t d, 0 apple t<1 0 Sphere: p :(p c) 2 R 2 =0 (p R 2 c) Solve for intersection: (o + t d c) 2 R 2 =0 at 2 + bt+ c =0, where t = b ± p b 2 4ac 2a a = d d b = 2(o c) d c =(o c) (o c) R 2
Ray Intersection With Implicit Surface Ray: 0 r(t) =o + t d, 0 apple t<1 General implicit surface: p : f(p) =0 Substitute ray equation: f(o + t d) =0 Solve for real, positive roots
Accelerating Ray-Surface Intersection
Ray Tracing Performance Challenges Simple ray-scene intersection Exhaustively test ray-intersection with every object Problem: Exhaustive algorithm = #pixels #objects Very slow!
Ray Tracing Performance Challenges Jun Yan, Tracy Renderer San Miguel Scene, 10.7M triangles
Ray Tracing Performance Challenges Deussen et al; Pharr & Humphreys, PBRT Plant Ecosystem, 20M triangles
Discussion: Accelerating Ray-Scene Intersection Deussen et al; Pharr & Humphreys, PBRT Next time: Brainstorm 3-4 accelerations, small or big ideas
To Be Continued
Acknowledgments Thanks to Pat Hanrahan, Kayvon Fatahalian, Mark Pauly and Steve Marschner for lecture resources.