Ray Tracing 2 Trace Harder

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