Course Roadmap
Rasterization Pipeline Rasterization Core Concepts Transforms & Projection • Sampling Texture Mapping • Antialiasing Visibility, Shading, Overall Pipeline • Transforms Geometric Modeling Intro to Geometry Curves and Surfaces Core Concepts Meshes and Geometry Processing • Splines, Bezier Curves • Topological Mesh Representations Today: Ray-Tracing & Acceleration • Subdivision, Geometry Processing Lighting & Materials
Cameras & Imaging
CS184/284A Ren Ng Lecture 10: Intro to Ray-Tracing & Accelerating Ray-Scene Intersection
Computer Graphics and Imaging UC Berkeley CS184/284A Basic Ray-Tracing Algorithm 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
CS184/284A Ren Ng Ray Casting - Generating Eye Rays
Pinhole Camera Model
eye ray closest scene (starts at eye and intersection point goes through pixel)
note: more intersection points eye point
image plane
light source
CS184/284A Ren Ng 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 light source (e.g. Blinn Phong model)
CS184/284A Ren Ng 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?
CS184/284A Ren Ng 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
CS184/284A Ren Ng Recursive Ray Tracing
eye point
image plane light source
CS184/284A Ren Ng Recursive Ray Tracing
Mirror ray (specular reflection)
eye point
image plane light source
CS184/284A Ren Ng Recursive Ray Tracing
Refractive rays (specular transmission)
eye point
image plane light source
CS184/284A Ren Ng Recursive Ray Tracing
eye point
image plane Shadow rays light source
CS184/284A Ren Ng Recursive Ray Tracing secondary rays
primary ray
eye point
image plane shadow rays light source
• 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
CS184/284A Ren Ng Recursive Ray Tracing
CS184/284A Ren Ng 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
CS184/284A Ren Ng Ray Equation
Ray is defined by its origin and a direction vector
Example:
Ray equation: 0 t< 1
point along ray “time” origin unit direction
CS184/284A Ren Ng Plane Equation
Plane is defined by normal vector and a point on plane
Example: r(t)=o + t d, 0 t< 1 p :(p p0) N =0 r(t)=o + t d, 0 t< · 1 Plane Equation:ax +r(tby)=+o +czt d,+0 d pt<=0:(p p0) N =0 1 · p :(p p0) N =0 ax + by + cz + d =0 · Set p = r(t) and solve for t ax + by + cz + d =0 Set p = r(t) and solve for t all points on plane any pointSet(normalpp= vectorr(tp)0 and) N solve=( for(pot +p0)t dN =(po +0)t dNp=00) N =0 · · · · CS184/284A (p p0) N =(o + t d p0) N =0 Ren Ngp0 o) N · · p0 o)t =N · p0 o) N d N t = · t = · · d N d N · · Ray Intersection With Plane
Ray equation: r(t)=o + t d, 0 t< 1 p :(p p0) N =0 Plane· equation:r(t)=o + t d, 0 t< r(t)=o + t d, 0 t< ax + by + cz + d =0 1 r(t)=p :(op+ t dp,)0 N t<=0 1 0 1 p :(p p0) N =0 p :(p p0) NSet=0p = r(t) and· solve for t · ax + by + cz + d =0 · ax + by +(pczSolve+p0d) =0 Nfor=( intersectiono + t d p0) N =0 · Set p = r(t·) and solveax for t+ by + cz + d =0 Set p =pr0 (t)o and) N solve for t t(p= p 0) N· =(o + t d p0) N =0 Set p = r(t) and solve for t (p p 0) Nd ·=(N o + t d p 0) N·=0 · p0 o) N · t = · · (p0 o) N d N (p p0) N =(o + t d p0) N =0 t = · r(t)=oCheck:+ t d, 0 t< d N · 1 · · p :(p· p0) N =0 p0 o) N CS184/284A · Ren Ng t = · ax + by + cz + d =0 d N Set p = r(t) and solve for t ·
(p p0) N =(o + t d p0) N =0 · · p0 o) N t = · 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…
CS184/284A Ren Ng Can Optimize: e.g. Möller Trumbore Algorithm
CS184/284A Ren Ng p :(p c)2 R2 =0
(o + t d c)2 R2 =0 Ray Intersection With Sphere
2 Ray: r(t)=o + t d, 0 t< at + bt+ c =0, where 1 2 2 pp:(:(pp cc))2 RR2 =0=0 p :(p p0) N =0 2 2 a = d d Sphere: p :(·p pc:()2p Rc2)=0R =0 p :(· p c )2 R2 =0 ax + by +pcz:(+p d =0c)2 R2 =0 b = 2(o c) d · 2 2 Set p = r(t) and solve for t ((oo++ttdd cc))2 RR2 =0=02 Solve for intersection:2 2 c =(o c ) (o c) R (o + t d(o +c)t2d Rc2)=0R =0 (o + t d· c ) 2 R 2 =0 (p (po0)+ tNd=( co)2+ t Rd2 =0p 0) N =0 · · 2 at2 + bt+ c =0, where p0 o) N at +bbt+pbc2=04,acwhere t = 2 · 2 at2 + btat+ +c =0dbt+N, wherec =0, where t a==at d ±+dbt+ c =0, where at2 + bt+ c =0· , where a = d ·d 2a a = d ad= d d b =a 2(=·od dc) d a = d ·d · b = 2(o ·c) ·d b = 2(·ob =c 2() od c) d b = 2( o ·c) d 2 · cc=(=(oo cc)) ((oo cc)) RR2 b = 2(o c) ·d 2 · · c =(o c =(c) o·(o c)c)(o R2c) R c =( o ·c) (o c) R2 c =(o c) ·(o c)· R 2 · 2 · b pb2 4ac CS184/284A t = b ±pb 4Renac Ng 2b pb2 4ac t = ± 2 b pb 4ac b 22aapb 4ac t = b t±=pb 2 ±4ac t = ± t = ± 2a 2a 2a 2a Ray Intersection With Implicit Surface
Ray: r(t)=o + t d, 0 t< 1 p :(p p0) N =0 p : f(p)=0 · p : f(p)=0 ax + byGeneral+ cz + d implicit=0 surface: Set p = r(t) and solve forf(ot+ t d)=0 Substitute ray equation: f(o + t d)=0 (p p0) N =(o + t d p0) N =0 Solve ·for real, positive roots· p0 o) N t = · d N ·
CS184/284A Ren Ng 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!
CS184/284A Ren Ng 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 Pre-Class Discussion: Accelerating Ray-Scene Intersection ~1 million pixels, ~20 million triangles
Deussen et al; Pharr & Humphreys, PBRT In pairs, brainstorm accelerations, small or big ideas. Write down 3-4 ideas. Pre-Class Discussion: Accelerating Ray-Scene Intersection
Brainstorm 3 or 4 accelerations, small or big ideas.
• Subdivide the world • If recursive, truncate the perhaps using an octtree number of recursions, and estimate the tail of • Raycasting for a group computation of pixels at a time • Parallelize raycasting • Something like • Do every second ray, and mipmapping, LOD -- interpolate further away, simpler • Sort triangles from front to geometry back • Bounding box • Ray-marching until in proximity
CS184/284A Ren Ng Bounding Volumes Bounding Volumes
Quick way to avoid intersections: bound complex object with a simple volume • Object is fully contained in the volume • If it doesn’t hit the volume, it doesn’t hit the object • So test bvol first, then test object if it hits
CS184/284A Ren Ng Ray-Intersection With Box
Could intersect with 6 faces individually Better way: box is the intersection of 3 slabs
CS184/284A Ren Ng Ray Intersection with Axis-Aligned Box
2D example; 3D is the same! Compute intersections with slabs and take intersection of tmin/tmax intervals
tmax x0 x1 y0 y1 x0 x1 y0 y1 x x y y tmax0 1 0 1 tmax
tmin tmin
o, d o, d o, d o, d o, d o, d
tmin x0 x1 y0 y1 x x y y x x y y 0 1 0 1 0 1 0 1
Note: tmin < 0 x0 x1 x0y0 x1y1 y0 y1 x0 x1 x0y0 x1y1 y0 y1 x0 x1 x y0 x y1 y y 0 1 0 1 Intersections with x planes Intersections with y planes Final intersection result How do we know when the ray misses the box? r(t)=o + t d, 0 t< 1 p :(p p0) N =0 Optimize Ray-Plane Intersection For· Axis-Aligned Planes? ax + by + cz + d =0 Set p = r(t) and solve for t
(p p0) N =(o + t d p0) N =0 · · r(t)=o + t d,(p00 t