# GAMES101 Notes Ray Tracing - Acceleration (L14)

Posted by jonstu on Sat, 12 Feb 2022 04:29:16 +0100

# Ray Tracing - Acceleration

## 1.Uniform Spatial Partitions (Grids)

```Assumptions:
- It is time-consuming to judge whether the light intersects with the object
- Judge whether the light is consistent with bounding box Intersection is easy``` After the preprocessing, calculate each box through which the light passes, and judge whether there is an object in the box. If so, judge whether the light intersects with the object, so as to avoid calculating whether it intersects with all objects in space. Basic idea of accelerated structure: do more intersection between light and box, and avoid intersection between light and object.

Defect: you still need to calculate the grid through which all light passes.

## 2.Spatial Partitions

Basic idea: improvement (1.) In the grid division method, less grids are used in open places (i.e. larger grids) and more grids are used in dense places (i.e. smaller grids), which is conducive to dealing with the following classic case (there are a large number of open areas in the figure, so it will be slow to use a unified grid division method) San Miguel scene, 10.7M triangles

Examples: (key point: KD tree) The accelerated structure preprocessing of the scene should be completed before ray tracing calculation! ```Execution process:
The current node is divided in a certain way(x\y\z),Get two child nodes, and then continue to divide the child nodes.
Actual object\Triangles are stored only on leaf nodes```
```Question:
1.An object may appear in multiple leaf nodes
2.KD-Tree The intersection of triangle and box needs to be considered in the establishment of```

## 3.Bounding Volume Hierarchy (BVH)

It belongs to Object Partitions and is widely used! ### 3.1 basic process

BVH divides the objects into two piles each time, then calculates the bounding box respectively, continues the division, recalculates the bounding box again, and repeats the above process. The division method can be vertical \ horizontal without restrictions (such as selecting X-axis, Y-axis and Z-axis in turn), so as to reduce the number of triangles in each leaf node and divide the objects in the space as evenly as possible.

```Summary: Building BVHs
• Find bounding box
Find the bounding box of the current node object
• Recursively split set of objects in two subsets
Recursively divides the current object into two piles
• Recompute the bounding box of the subsets
Recalculate the bounding box of two piles of objects
• Stop when necessary
Stop when appropriate (the number of triangles in each leaf node is small, and the division of objects in space should be as uniform as possible)
• Store objects in each leaf node
Objects are stored in leaf nodes```

### 3.2 performance analysis

Advantages over KD tree: each object must strictly belong to a node. Therefore, the intersection of triangle and bounding box is omitted.

Existing problems: the space is not strictly "divided", that is, the bounding box es may intersect. Therefore, we pay more attention to "division".

```Heuristics of subdivision
Choose a dimension to split
• Heuristic #1: Always choose the longest axis in node
• Heuristic #2: Split node at location of median object
That is, the longest axis is selected for division, and the division boundary is the object in the middle after sorting.```

[supplement: fast selection algorithm]

Problem: for the sequence composed of n unordered numbers, find the i-largest number.
Time complexity: O(n)

This algorithm is used in BVH to find objects with middle size.

### 3.3 storage structure

```Data Structure for BVHs
[Internal nodes store]
• Bounding box
• Children: pointers to child nodes
[Leaf nodes store]
• Bounding box
• List of objects```

### 3.4 pseudo code

```Intersect(Ray ray, BVH node)
{
if (ray misses node.bbox) return; //If the light does not intersect the bounding box, it ends
//If it intersects, it is divided into whether it is a leaf node or not
if (node is a leaf node)
{
test intersection with all objs;
return closest intersection;
} //Leaf node, judge all objects inside the node and return to the nearest intersection
hit1 = Intersect(ray, node.child1);
hit2 = Intersect(ray, node.child2);
//If the node is not a leaf node, return the nearest value of the intersection of the two child nodes
return the closer of hit1, hit2;
}```

## 4. Comparison of acceleration structures: Spatial vs Object Partitions

### 4.1 Spatial partition (e.g.KD-tree) ```• Partition space into non-overlapping regions
Space division, and the divided nodes do not overlap
• An object can be contained in multiple regions
Disadvantages: an object may be divided into multiple areas```

### 4.2 Object partition (e.g. BVH) ```• Partition set of objects into disjoint subsets
Advantages: objects are divided into disjoint subsets, and the same object will not be divided into different nodes
• Bounding boxes for each set may overlap in space
Disadvantages: the space occupied by different nodes may overlap```

Topics: Programmer