Light Rays:
- Light travels in straight lines.
- Light rays do not "collide" with each other if they cross.
- Light rays travel from the light sources to the eye (but the physics is invariant under path reversal - reciprocity).
Ray Casting:
Generate an image by casting one ray per pixel
Check for shadows by sending a ray to the light
Pinhole Camera Model:
Recursive (Whitted-Style) Ray Tracing
Ray-Surface Intersection
Ray Equation: Ray is defined by its origin and a direction vector. Ray equation is
: point along ray; t: time; : origin; : (normalized) direction
Ray Intersection With Sphere:
Ray:
Sphere:
: Solve for intersection:
Expand into a quadratic polynomial:
, where
Ray Intersection With Implicit Surface:
- Ray:
- General implicit surface:
: - Substitute ray equation:
- Solve for real, positive roots
Plane Equation: Plane is defined by normal vector and a point on plane. Plane Equation (if
: points on plane; : one point on plane; : normal vector
Triangle is in a plane:
- Ray-plane intersection
- Test if hit point is inside triangle
Ray Intersection With Triangle Mesh:
Ray equation:
Plane equation:
: Solve for intersection: Set
and solve for t Check:
Moller Trumbore Algorithm: A faster approach, giving barycentric coordinate directly.
Accelerating Ray-Surface Intersection
Bounding Volumes: Quick way to avoid intersections: bound complex object with a simple volume
- Object is fully contained in the volume
- If it doesn't hit the volume, it doesn't hit the object
- So test BVol first, then test object if it hits
Ray Intersection With Box: box is the intersection of 3 pairs of slabs. Specifically, we often use an Axis-Aligned Bounding Box (AABB, 轴对齐包围盒).
Ray Intersection With Axis-Aligned Box: Compute intersections with slabs and take intersection of
2D example; 3D is the same!
The ray enters the box only when it enters all pairs of slabs
The ray exits the box as long as it exits any pair of slabs
For each pair, calculate the
and (negative is fine) For the 3D box,
, If
, we know the ray stays a while in the box (so they must intersect) Ray is not a line. Should check whether t is negative for physical correctness.
- If
: The box is "behind" the ray —— no intersection - If
and : The ray's origin is inside the box —— have intersection
In summary, ray and AABB intersect if and only if:
- If
Uniform Spatial Partitions (Grids)
Grids
Preprocess —— Build Acceleration Gird:
Find bounding box
Create grid
Store each object in overlapping cells
Step through grid in ray traversal order
For each grid cell, test intersection with all objects stored at that cell
Grid Resolution:
- One cell: No speedup
- Too many cells: Inefficiency due to extraneous grid traversal
- Heuristic: #cells = C * #objs, C
27 in 3D
When They Work Well: Grids work well on large collections of objects that are distributed evenly in size and space
When They Fail: "Teapot in a stadium" problem
Spatial Partitions
KD-Tree Pre-Processing
Data Structure for KD-Trees:
Internal nodes store
- split axis: x-, y-, or z-axis
- split position: coordinate of split plane along axis
- children: pointers to child nodes
- No objects are stored in internal nodes
Leaf nodes store
- list of objects
Traversing a KD-Tree:
- split A:
- assume 1 is leaf node:
- split B and assume 2 is leaf node:
- split C and assume 3 is leaf node:
- Intersection found:
KD-Tree's problem:
- Objects may exist in multiple boxes
- The establishment of KD-Tree is not simple, need to consider the intersection of triangles and boxes
Object Partitions & Bounding Volume Hierarchy (BVH)
Bounding Volume Hierarchy (BVH)
- Continue to divide:
Building BVHs:
- Find bounding box
- Recursively split set of objects in two subsets
- Recompute the bounding box of the subsets
- Stop when necessary
- Store objects in each leaf node
How to subdivide a node:
- Choose a dimension to split
- Heuristic #1: Always choose the longest axis in node
- Heuristic #2: Split node at location of median object
Termination criteria: stop when node contains few elements (e.g. 5)
Data Structure for BVHs:
Internal nodes store:
- Bounding box
- Children: pointers to child nodes
Leaf nodes store
- Bounding box
- List of objects
Nodes represent subset of primitives in scene: All objects in subtree
BVH Traversal:
Intersect(Ray ray, BVH node){
if (ray misses node.bbox) return;
if (node is a leaf node){
test intersection with all objs;
return closest intersection;
}
hit1 = Intersect(ray, node.child1);
hit2 = Intersect(ray, node.child2);
return the closer of hit1, hit2;
}
Spatial vs Object Partitions
Spatial partition (e.g. KD-tree)
- Partition space into non-overlapping regions
- An object can be contained in multiple regions
Object partition (e.g. BVH)
- Partition set of objects into disjoint subsets
- Bounding boxes for each set may overlap in space