Ray Tracing 2 Trace Harder COMP 575/770 Spring 2013
Ray-Triangle Intersection Core operation while ray tracing triangle meshes Given: Ray with origin 𝑜 and direction 𝑑 Triangle with vertices 𝑣0 , 𝑣1 , and 𝑣2
Figure out the intersection point
Ray-Triangle Intersection Any point on the ray is given by: 𝑝 = 𝑜 + 𝑡𝑑 Any point on the triangle is given by: 𝑝 = 𝑣0 + 𝛽 𝑣1 − 𝑣0 + 𝛾 𝑣2 − 𝑣0 Barycentric coordinates!
Ray-Triangle Intersection One equation for each coordinate: 𝑥𝑜 + 𝑡𝑥𝑑 = 𝑥𝑎 + 𝛽 𝑥𝑏 − 𝑥𝑎 + 𝛾 𝑥𝑐 − 𝑥𝑎 𝑦𝑜 + 𝑡𝑦𝑑 = 𝑦𝑎 + 𝛽 𝑦𝑏 − 𝑦𝑎 + 𝛾 𝑦𝑐 − 𝑦𝑎 𝑧𝑜 + 𝑡𝑧𝑑 = 𝑧𝑎 + 𝛽 𝑧𝑏 − 𝑧𝑎 + 𝛾(𝑧𝑐 − 𝑧𝑎 )
In matrix form: 𝑥𝑎 − 𝑥𝑏 𝑦𝑎 − 𝑦𝑏 𝑧𝑎 − 𝑧𝑏
𝑥𝑎 − 𝑥𝑐 𝑦𝑎 − 𝑦𝑐 𝑧𝑎 − 𝑧𝑐
𝑥𝑑 𝑦𝑑 𝑧𝑑
Solve using Cramer’s rule
𝑥𝑎 − 𝑥𝑒 𝛽 𝛾 = 𝑦𝑎 − 𝑦𝑒 𝑧𝑎 − 𝑧𝑒 𝑡
Ray-Triangle Intersection A faster approach: Moller-Trumbore intersection algorithm
http://www.cs.virginia.edu/~gfx/Courses/2003/ImageSynthesis/papers/Acc eleration/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf
Ray-Triangle Intersection In general, two parts to any intersection test: Determining 𝑡 (ray-plane test) Determining 𝛽, 𝛾 (barycentric test)
Ray-plane test finds a potential hit point Barycentric test checks that a hit point exists Does the ordering matter?
Ray-Triangle Intersection Eye Rays Need to find the closest hit If 𝑡 is greater than the current closest hit, bail Ray-plane test before barycentric test
Shadow Rays Need to find any hit in an interval If the ray can’t possible intersect, bail Barycentric test before ray-plane test
Triangle Meshes Test each ray with each triangle? Too slow! 1M pixels (rays), 1000 triangles = 1 billion tests!
Rasterization is much better at this Why?
Triangle Meshes Ray Tracing: for each pixel for each triangle test (ray, triangle) Rasterization: for each triangle for each pixel test (pixel, triangle)
Triangle Meshes Ray Tracing: for each pixel for each triangle test (ray, triangle) Rasterization: for each triangle for each pixel potentially covered test (pixel, triangle)
Triangle Meshes Ray Tracing: for each pixel for each triangle potentially intersecting test (ray, triangle) Rasterization: for each triangle for each pixel potentially covered test (pixel, triangle)
Acceleration Structures A special data structure to accelerate ray tracing Used to quickly determine which triangles might intersect a ray
Usually a hierarchical tree structure Object hierarchies Spatial hierarchies
Outline Ray-Triangle Intersection Bounding Volume Hierarchy Ray-Box Intersection
kD Trees Efficient Ray Tracing
Bounding Volume Hierarchies Hierarchical clustering of objects (triangles) Each cluster represented as a bounding volume
Bounding Volume Hierarchies
Bounding Volume Hierarchies Typically binary trees Each internal node splits a set of triangles into two
Each node stores its bounding volume
Each leaf typically contains one triangle Variations are possible
Bounding Volumes A BVH can be built using any kind of bounding volume Typical choices:
Spheres Axis-Aligned Bounding Boxes (AABBs) Oriented Bounding Boxes (OBBs) Discrete Oriented Polytopes (DOPs)
Need to test whether a ray passes through a bounding volume
Ray-AABB Intersection An AABB is a region bounded by 3 slabs 2D example shown here for convenience
Ray-AABB Intersection AABB intersection expressed as 3 slab intersection tests 2 tests in 2D
Need to find interval of 𝑡 in which ray is in AABB
Ray-AABB Intersection Consider a box with min. coordinates (𝑥0 , 𝑦0 , 𝑧0 ) and max. coordinates 𝑥1 , 𝑦1 , 𝑧1 The 𝑥-slab goes from 𝑥 = 𝑥0 to 𝑥 = 𝑥1
Which will the ray encounter first? Depends on which way the ray is traveling
If 𝑑𝑥 > 0, 𝑥𝑚𝑖𝑛 = 𝑥0 , 𝑥𝑚𝑎𝑥 = 𝑥1 , otherwise the other way around
Ray-AABB Intersection Ray intersection with 𝑥 = 𝑥𝑚𝑖𝑛 : 𝑜𝑥 + 𝑡𝑑𝑥 = 𝑥𝑚𝑖𝑛 Similarly for intersection with 𝑥 = 𝑥𝑚𝑎𝑥 Ray passes through the 𝑥-slab in the interval [𝑡𝑥𝑚𝑖𝑛 , 𝑡𝑥𝑚𝑎𝑥 ] 𝑥𝑚𝑖𝑛 − 𝑜𝑥 𝑑𝑥 𝑥𝑚𝑎𝑥 − 𝑜𝑥 = 𝑑𝑥
𝑡𝑥𝑚𝑖𝑛 = 𝑡𝑥𝑚𝑎𝑥
Ray-AABB Intersection Repeat process for 𝑦-slab (and 𝑧-slab) Ray is in AABB when it’s in both (all three) slabs Need to find the intersection of intervals
Ray-AABB Intersection Ray intersects AABB if 𝑡𝑚𝑖𝑛 ≤ 𝑡𝑚𝑎𝑥
BVH Traversal Recursive algorithm: at current node: if ray doesn’t intersect current node’s bounding volume, bail if current node is leaf: check for intersection with stored triangle else (if current node is internal node): recurse on children
Start with root node
Still have to test against multiple triangles Hopefully, bounding volume tests eliminate most triangles
Outline Ray-Triangle Intersection Bounding Volume Hierarchy Ray-Box Intersection
kD Trees Efficient Ray Tracing
kD Trees Or, binary spatial partitioning (BSP) trees Instead of a hierarchy of objects, a hierarchy of spatial regions
Allows front-to-back spatial sorting Stop as soon as a ray hits something
kD Trees Binary tree of spatial regions At each node, the spatial region is split using an axis-aligned plane x y
y
kD Trees
kD Trees
kD Trees
kD Trees
kD Tree Traversal Recursive algorithm: at current node: if current node is leaf: intersect with all stored triangles if ray hits a stored triangle, stop else (if current node is internal node): find intersection of ray with splitting plane if ray intersects both children: recurse on near side, then far side otherwise: recurse on side it intersects
kD Tree Construction Given: An AABB describing a region of space (“cell”) List of triangles in the cell
Core operation: Pick an axis-aligned plane to split the cell Assign triangles to each sub-cell Some triangles may be in both
Recurse Subject to some termination criteria
kD Tree Construction What axis to split along?
Where to place splitting plane?
When to terminate recursion?
kD Tree Construction What axis to split along? Round-robin? Largest extent?
Where to place splitting plane? Middle of extent? Median of geometry?
When to terminate recursion? 1 triangle left in cell? Max. tree depth?
Outline Ray-Triangle Intersection Bounding Volume Hierarchy Ray-Box Intersection
kD Trees Efficient Ray Tracing
Efficient Ray Tracing Many techniques developed to speed up ray tracing
Sophisticated tree construction algorithms Compact, cache-friendly memory layout Parallel programming on multi-core CPUs SIMD instructions for exploiting coherence Replace recursion with iteration Optimize inner loops …
BVH vs kD Tree BVH: Pros: Can be updated for dynamic geometry Cons: Slower traversal, takes more memory
kD Tree: Pros: Faster traversal, takes less memory Cons: Static scenes only