Ray Tracing III Wen-Chieh (Steve) Lin National Chiao-Tung University Shirley, Fundamentals of Computer Graphics, Chap 10 Doug James CG slides, I-Chen Lin s CG slides
Ray-tracing Review For each pixel, trace a primary ray (eye ray) to the first visible surface. For each intersection trace secondary rays: Shadow rays: in directions L to light sources Reflected ray: in direction R Refracted ray (transmitted ray): in direction T L L R R V V DCP4516 Introduction to Computer Graphics 2 T
Ray-tracing Review (cont.) Every surface intersection spawns 1 reflected ray 1 transmitted ray L R 1 shadow ray per light Shaded color of Vi = Valid(Li) x PhongModel + ReflectedRay + TransmittedRay L R T V V Valid(Li) = 1, if visible to the light source 0, otherwise DCP4516 Introduction to Computer Graphics 3
Ray Tracing Preliminary Assessment Global illumination method Image-based Pros: Relatively accurate shadows, reflections, refractions Cons: Slow Aliasing Inter-object diffuse reflections DCP4516 Introduction to Computer Graphics 4
Ray Tracing Acceleration Faster intersections Faster ray-object intersections Object bounding volume Efficient intersectors Fewer ray-object intersections Hierarchical bounding volumes (boxes, spheres) Spatial data structures Fewer rays Stochastic sampling DCP4516 Introduction to Computer Graphics 5
Spatial Data Structures Data structures to store geometric information Sample applications Collision detection / Location queries Chemical simulations Rendering Spatial data structures for ray tracing Object-centric data structures (bounding volumes) Space subdivision (grids, octrees, BSP trees) Speed-up of 10x, 100x, or more DCP4516 Introduction to Computer Graphics 7
Bounding Volumes Wrap complex objects in simple ones Does ray intersect bounding box? No: does not intersect enclosed objects Yes: calculate intersection with enclosed objects Common types Boxes, axis-aligned Boxes, oriented Spheres DCP4516 Introduction to Computer Graphics 8
Axis-aligned Bounding Box The ray is only tested for intersection with the surfaces if it hits the bounding box Box orientation aligned with axes DCP4516 Introduction to Computer Graphics 9
Intersection Condition Ray intersects box if the intervals [t xmin, t xmax ] and [t ymin, t ymax ] overlap t ymax t xmax x e tx d x min t xmin y max t x min x min x x d e t ymin x min x max y min t=0 t= t ymin t xmin t ymax t xmax DCP4516 Introduction to Computer Graphics 10
Intersection Condition (cont.) Ray intersects box if the intervals [t xmin, t xmax ] and [t ymin, t ymax ] overlap Note that ray direction affects the order of t xmin, t xmax, t ymin, t ymax along t-axis t ymin t xmin t ymax t xmax y max y min x min x max t= t=0 t ymin t xmin t ymax t xmax DCP4516 Introduction to Computer Graphics 11
Intersection Condition (cont.) Think about how to deal with vertical and horizontal rays? y max t xmin t xmax y min x min x max DCP4516 Introduction to Computer Graphics 12
Selection of Bounding Volumes Effectiveness depends on: Probability that ray hits bounding volume, but not enclosed objects (tight fit is better) Expense to calculate intersections with bounding volume and enclosed objects Reduce calculation of bounding volumes Use heuristics good bad DCP4516 Introduction to Computer Graphics 13
Bounding Volume Hierarchy (BVH) With simple bounding volumes, ray casting still has requires O(n) intersection tests Idea: use tree data structure Larger bounding volumes contain smaller ones etc. Sometimes naturally available (e.g. human figure) Sometimes difficult to compute Often reduces complexity to O(log(n)) Sub-linear ray-object intersection DCP4516 Introduction to Computer Graphics 14
Hierarchical Bounding Boxes Hierarchy made by partitioning the set of objects in a box and placing a box around each partition Convenient to use a binary tree that are roughly balanced and less overlap between siblings DCP4516 Introduction to Computer Graphics 15
Ray Intersection Algorithm using BVH Recursively descend tree If ray misses bounding volume, no intersection If ray intersects bounding volume, recurse with enclosed volumes and objects Overall effectiveness depends on model and constructed hierarchy DCP4516 Introduction to Computer Graphics 16
Spatial Subdivision Bounding volumes enclose objects, recursively Alternatively, divide space For each segment of space keep list of intersecting surfaces or objects Basic techniques Regular grids (uniform partition) Octrees (axis-aligned, non-uniform partition) BSP trees (recursive Binary Space Partition, planes) DCP4516 Introduction to Computer Graphics 17
Uniform Spatial Subdivision 3D array of cells (voxels) that tile space Each cell points to all intersecting surfaces Intersection algorithm steps from cell to cell DCP4516 Introduction to Computer Graphics 18
Caching Intersection points Objects can span multiple cells Multiple objects can be in one cell For A need to test intersection only once For B need to cache intersection and check next cell for closer one If not, C could be missed (white ray) A B DCP4516 Introduction to Computer Graphics 19 C
Assessment of Regular Grids Poor choice when world is non-homogeneous Cells at empty space are wasted Size of grid Too large: too many surfaces per cell Too small: too many empty cells to traverse Need an appropriate traversal order Can use alg. like Bresenham s for efficient traversal Non-uniform spatial subdivision more flexible Can adjust to objects that are present DCP4516 Introduction to Computer Graphics 20
BSP Trees Split space with any line (2D) or plane (3D) Applications Painters algorithm for hidden surface removal Ray casting Inherent spatial ordering given viewpoint Left subtree: in front, right subtree: behind Problem: finding good space partitions Proper ordering for any viewpoint Balance tree DCP4516 Introduction to Computer Graphics 21
Building a BSP Tree Use hidden surface removal as intuition Using line 1 or line 2 as root is easy Line 1 3 2 1 1 A D C Line 3 Line 2 B A C D a BSP tree using 2 as root 3 B the subdivision of space it implies 2 Viewpoint DCP4516 Introduction to Computer Graphics 22
Splitting of surfaces Using line 3 as root requires splitting Line 1 3 Line 2a 2b 2a Line 3 Line 2b 1 Viewpoint DCP4516 Introduction to Computer Graphics 23
Painter s Algorithm with BSP Trees Building the tree May need to split some polygons Slow, but done only once Traverse back-to-front or front-to-back Order is viewer-direction dependent What is front and what is back of each line changes Determine order on the fly DCP4516 Introduction to Computer Graphics 25
BSP for Ray-Tracing Similar to the BSP used in painter s algorithm, but uses axis-aligned cutting planes for easier ray intersection DCP4516 Introduction to Computer Graphics 27
Quadtrees Generalization of binary trees in 2D Node (cell) is a square Recursively split into 4 equal sub-squares Stop subdivision based on number of objects Ray intersection has to traverse quadtree More difficult to step to next cell DCP4516 Introduction to Computer Graphics 29
Octrees Generalization of quadtree in 3D Each cell may be split into 8 equal sub-cells Internal nodes store pointers to children Leaf nodes store list of surfaces Adapts well to inhomogeneous scenes DCP4516 Introduction to Computer Graphics 30
Comparison of Spatial Data Structure on Ray-Tracing Hierarchical bounding volumes Natural for hierarchical objects Better for dynamic scenes Regular grids Easy to implement Require a lot of memory Poor results for inhomogeneous scenes Octrees Better on most scenes (more adaptive) DCP4516 Introduction to Computer Graphics 31
Other Spatial Subdivision Techniques Relax rules for quadtrees and octrees k-dimensional tree (k-d tree) Split at arbitrary interior point Split one dimension at a time Binary space partitioning tree (BSP tree) In 2 dimensions, split with any line In k dims. split with k-1 dimensional hyperplane Useful for painter s algorithm and ray tracing DCP4516 Introduction to Computer Graphics 32
Demos BSP Tree construction http://symbolcraft.com/graphics/bsp/index.html Interactive ray tracing via space subdivision http://www.cs.utah.edu/~reinhard/egwr/ Interactive ray tracing with good hardware http://www.cs.utah.edu/vissim/projects/raytracing/ DCP4516 Introduction to Computer Graphics 33
Constructive Solid Geometry (CSG) Generate complex shapes with simple building blocks (boxes, spheres, cylinders, cones,...) Particularly applicable for machined objects Efficient with ray tracing Subtract From To get DCP4516 Introduction to Computer Graphics 34
CSG Trees Set operations yield tree-based representation Use these trees for ray/objects intersections Think about how! DCP4516 Introduction to Computer Graphics 35
Implicit Functions for Booleans Solid as implicit function, F(x,y,z) F(x, y, z) < 0 interior F(x, y, z) = 0 surface F(x, y, z) > 0 exterior For CSG, use F(x, y, z) 2 {-1, 0, 1} F A and B (p) = max (F A (p), F B (p)) F A union B (p) = min (F A (p), F B (p)) F A B (p) = max (F A (p), - F B (p)) DCP4516 Introduction to Computer Graphics 36
Distributed Ray Tracing Used for Anti-aliasing Generates more realistic effects Soft shadows Fuzzy reflections Soft focus / Depth of field DCP4516 Introduction to Computer Graphics 37
Antialiasing by Supersampling We can approximate the average color of a pixel s area by firing multiple rays and averaging the result DCP4516 Introduction to Computer Graphics 38
Sampling Strategies Regular sampling Random sampling Jittering or stratified sampling (randomly perturbs a regular grid) DCP4516 Introduction to Computer Graphics 39
Soft Shadows Treat the light as an area rather than a point Approximate area light source with a distributed set of N point lights r = c + ξ 1 a + ξ 2 b Uniform random numbers in [0,1) a c b DCP4516 Introduction to Computer Graphics 40
Depth of Field Real camera collects light at a non-zero size lens rather than a point Simulate soft focus effects by randomly placing the eye point in a square region DCP4516 Introduction to Computer Graphics 41
Glossy Reflection Randomly sample on a square perpendicular to r r' r u u v v a v ' a 2 a u a 2 0, ' 1 a a v u DCP4516 Introduction to Computer Graphics 42
Motion Blur Looks like squash and stretch!! T. Porter, Pixar, 1984 16 samples/pixel Cook, Stochastic Sampling in Computer Graphics, TOG, 1986 DCP4516 Introduction to Computer Graphics 43
Motion Blur In real camera, aperture is open for some time interval during objects move Simulated by firing rays randomly within a time interval [T 0, T 1 ] T T ( T T 0) 0 1 DCP4516 Introduction to Computer Graphics 44
Raytracing Example I www.povray.org DCP4516 Introduction to Computer Graphics 45
Raytracing Example II www.povray.org DCP4516 Introduction to Computer Graphics 46
Raytracing Example II Saito, Saturn Ring DCP4516 Introduction to Computer Graphics 47
Raytracing Example IV www.povray.org DCP4516 Introduction to Computer Graphics 48