Ray-tracing Acceleration Acceleration Data Structures for Ray Tracing Thanks to Fredo Durand and Barb Cutler Soft shadows Antialiasing (getting rid of jaggies) Glossy reflection Motion blur Depth of field (focus) Shadows Shadows & Light Sources one shadow ray per intersection per point light source no shadow rays http://www.davidfay.com/index.php one shadow ray http://3media.initialized.org/photos/2000-10-18/index_gall.htm clear bulb frosted bulb http://www.pa.uky.edu/~sciworks/light/preview/bulb2.htm Soft Shadows Antialiasing Supersampling Multiple shadow rays to sample area light source Distribute rays over an interval light source one shadow ray Multiple rays per pixel Distribute rays over an interval multiple rays per pixel point light jaggies w/ antialiasing lots of shadow rays area light 1
Reflection one reflection ray per intersection Glossy Reflection multiple reflection rays Distribute rays over an interval multiple reflection rays Justin Legakis θ θ perfect mirror θ θ polished surface Motion Blur Sample objects temporally Distribute rays over an interval - time Depth of Field Distribute rays over an interval - lens area Rob Cook film focal length Justin Legakis Ray Tracing Algorithm Analysis Ray-Tracing Taxonomy Ray casting Lots of primitives Recursive Distributed Ray Tracing Effects Soft shadows Anti-aliasing Glossy reflection Motion blur Depth of field cost height * width * num primitives * intersection cost * size of recursive ray tree * num shadow rays * num supersamples * num glossy rays * num temporal samples * num focal samples *... can we reduce this? by James Arvo and David Kirk 2
Ray-tracing Acceleration of each primitive of groups of ed primitives Acceleration of Ray Casting Goal: Reduce the number of ray/primitive intersections Conservative Bounding Region Conservative Bounding Regions First check for an intersection with a conservative bounding region Early reject tight avoid false positives fast to intersect non-aligned bounding box bounding sphere axis-aligned bounding box arbitrary convex region (bounding half-spaces) Intersection with Axis-Aligned Box Bounding Box of a Triangle t 1y t near t far t 2y t 1x x=x1 t 2x x=x2 y=y2 y=y1 For all 3 axes, calculate the intersection distances t 1 and t 2 t near = max (t 1x, t 1y, t 1z ) t far = min (t 2x, t 2y, t 2z ) If t near > t far, box is missed If t far < t min, box is behind If box survived tests, report intersection at t near = (min(x 0,x 1,x 2 ), min(y 0,y 1,y 2 ), min(z 0,z 1,z 2 )) (x 1, y 1, z 1 ) (x 0, y 0, z 0 ) (x 2, y 2, z 2 ) = (max(x 0,x 1,x 2 ), max(y 0,y 1,y 2 ), max(z 0,z 1,z 2 )) 3
Bounding Box of a Sphere Bounding Box of a Plane = (x+r, y+r, z+r) = (+, +, + )* (x, y, z) r ax + by + cz = d n = (a, b, c) = (x-r, y-r, z-r) = (-, -, - )* * unless n is exactly perpendicular to an axis Bounding Box of a Group Bounding Box of a Transform (x max_a, y max_a, z max_a ) (x max_b, y max_b, z max_b ) = (max(x max_a,x max_b ), max(y max_a,y max_b ), max(z max_a,z max_b )) M (x 3,y 3,z 3 ) = M (x max,y max,z min ) (x' max, y' max, z' max ) = (max(x 0,x 1,x 2,x 3,x 4,x 5,x 6,x 7 ), max(y 0,y 1,y 2,y 3,y 4,x 5,x 6,x 7 ), max(z 0,z 1,z 2,z 3,z 4,x 5,x 6,x 7 )) (x min_b, y min_b, z min_b ) (x min_a, y min_a, z min_a ) (x 2,y 2,z 2 ) = M (x min,y max,z min ) (x 1,y 1,z 1 ) = M (x max,y min,z min ) = (min(x min_a,x min_b ), min(y min_a,y min_b ), min(z min_a,z min_b )) (x' min, y' min, z' min ) = (min(x 0,x 1,x 2,x 3,x 4,x 5,x 6,x 7 ), min(y 0,y 1,y 2,y 3,y 4,x 5,x 6,x 7 ), min(z 0,z 1,z 2,z 3,z 4,x 5,x 6,x 7 )) (x 0,y 0,z 0 ) = M (x min,y min,z min ) Special Case: Transformed Triangle Special Case: Transformed Triangle Can we do better? M (x 0, y 0, z 0 ) M (x' 0,y' 0,z' 0 ) = M (x 0,y 0,z 0 ) (x' 1,y' 1,z' 1 ) = M (x 1,y 1,z 1 ) (x' 2,y' 2,z' 2 ) = M (x 2,y 2,z 2 ) = (max(x' 0,x' 1,x' 2 ), max(y' 0,y' 1,y' 2 ), max(z' 0,z' 1,z' 2 )) (x 1, y 1, z 1 ) (x 2, y 2, z 2 ) = (min(x' 0,x' 1,x' 2 ), min(y' 0,y' 1,y' 2 ), min(z' 0,z' 1,z' 2 )) 4
Ray-tracing Acceleration Regular Grid Regular Grid Adaptive Grids Hierarchical Bounding Volumes Create Grid Find bounding box of scene Choose grid resolution (n x, n y, n z ) grid x need not = grid y Cell (i, j) Insert Primitives into Grid Primitives that overlap multiple cells? Insert into multiple cells (use pointers) grid y grid x For Each Cell Along a Ray Does the cell contain an intersection? Yes: return closest intersection No: continue Preventing Repeated Computation Perform the computation once, "mark" the object Don't re-intersect marked objects 5
Don't Return Distant Intersections If intersection t is not within the cell range, continue (there may be something closer) Which Cells Should We Examine? Should we intersect the ray with each voxel? No! we can do better! Where Do We Start? Is there a Pattern to Cell Crossings? Intersect ray with scene bounding box Ray origin may be inside the scene bounding box Cell (i, j) t next_y Yes, the horizontal and vertical crossings have regular spacing dt x = grid x / dir x t next_x t min t min t next_x t next_y dir x dir y dt y = grid y / dir y grid y grid x What's the Next Cell? What's the Next Cell? if ( t next_x < t next_y ) i += sign x t min = t next_x Cell (i, j) Cell (i+1, j) 3DDDA Three Dimensional Digital Difference Analyzer t next_x += dt x else j += sign y t min = t next_y t min t next_x t next_y Similar to Bresenham s Line Rasterization! t next_y += dt y (dir x, dir y ) dt y dt x if (dir x > 0) sign x = 1 else sign x = -1 if (dir y > 0) sign y = 1 else sign y = -1 6
Pseudo-Code create grid insert primitives into grid for each ray r find initial cell c(i,j), t min, t next_x & t next_y compute dt x, dt y, sign x and sign y while c!= NULL for each primitive p in c intersect r with p if intersection in range found return c = find next cell Regular Grid Discussion Advantages? easy to construct easy to traverse Disadvantages? may be only sparsely filled geometry may still be clumped Ray-tracing Acceleration Regular Grid Adaptive Grids Hierarchical Bounding Volumes Adaptive Grids Subdivide until each cell contains no more than n elements, or maximum depth d is reached Nested Grids Octree/(Quadtree) Primitives in an Adaptive Grid Can live at intermediate levels, or be pushed to lowest level of grid Adaptive Grid Discussion Advantages? grid complexity matches geometric density Disadvantages? more expensive to traverse (especially octree) Octree/(Quadtree) 7
Ray-tracing Acceleration Regular Grid Adaptive Grids Hierarchical Bounding Volumes Bounding Volume Hierarchy Find bounding box of objects Split objects into two groups Recurse Bounding Volume Hierarchy Find bounding box of objects Split objects into two groups Recurse Bounding Volume Hierarchy Find bounding box of objects Split objects into two groups Recurse Bounding Volume Hierarchy Find bounding box of objects Split objects into two groups Recurse Bounding Volume Hierarchy Find bounding box of objects Split objects into two groups Recurse 8
Where to split objects? At midpoint OR Sort, and put half of the objects on each side OR Use modeling hierarchy Intersection with BVH Check sub-volume with closer intersection first Intersection with BVH Don't return intersection immediately if the other subvolume may have a closer intersection Bounding Volume Hierarchy Discussion Advantages easy to construct easy to traverse binary Disadvantages may be difficult to choose a good split for a node poor split may result in minimal spatial pruning Ray-tracing Acceleration Transformation Hierarchy A B group C group D E Flatten Group & Transformation hierarchy may not be a good spatial hierarchy A B group C D C E 9
Ray Marching Visualization Next time: ray-tracing at Pixar sphere voxelization cells traversed primitive density entered faces 10