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
© Copyright 2026 Paperzz