### 引言

GAMES101现代图形学入门是由闫令琪老师教授。今天我们来讲Whitted Style的光线追踪和其实现的若干技术细节。

### Why Ray Tracing?

Rasterization couldn’t handle global effects well

• And especially when the light bounces more than once

Rasterization is fast, but quality is relatively low

Ray tracing is accurate, but is very slow

• Rasterization: real-time, ray tracing: offline
• ~10K CPU core hours to render one frame in production

### Basic Ray-Tracing Algorithm

#### Light Rays

1. Light travels in straight lines (though this is wrong)
2. Light rays do not “collide” with each other if they cross (though this is still wrong)
3. Light rays travel from the light sources to the eye (but the physics is invariant under path reversal - reciprocity).

“And if you gaze long into an abyss, the abyss also gazes into you.” — Friedrich Wilhelm Nietzsche (translated)

1. 光线沿着直线传播而不考虑其波动性
2. 光线和光线不会发生碰撞
3. 光线一定是从光源发射最终进入人的眼睛

#### Ray Casting

Appel 1968 - Ray casting

1. Generate an image by casting one ray per pixel
2. Check for shadows by sending a ray to the light

### Ray-Surface Intersection

#### Möller Trumbore Algorithm

A faster approach, giving barycentric coordinate directly

Derivation in the discussion section!

### Accelerating Ray-Surface Intersection

#### Ray Tracing – Performance Challenges

Simple ray-scene intersection

• Exhaustively test ray-intersection with every triangle
• Find the closest hit (i.e. minimum t)

Problem:

• Naive algorithm = #pixels ⨉ # traingles (⨉ #bounces)
• Very slow!

For generality, we use the term objects instead of triangles later (but doesn’t necessarily mean entire objects)

### 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

Understanding: box is the intersection of 3 pairs of slabs

#### Ray Intersection with Axis-Aligned Box

Recall: a box (3D) = three pairs of infinitely large slabs

Key ideas

• 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 $t_{min}$ and $t_{max}$ (negative is fine)

For the 3D box, $t_{enter} = max{t_{min}}$ ,$t_{exit} = min{t_{max}}$

If $t_{enter} < t_{exit}$, we know the ray stays a while in the box (so they must intersect!) (not done yet, see the next slide)

However, ray is not a line

• Should check whether t is negative for physical correctness!

What if $t_{exit} < 0$?

• The box is “behind” the ray — no intersection!

What if $t_{exit} >= 0 and t_{enter} < 0$?

• The ray’s origin is inside the box — have intersection!

In summary, ray and AABB intersect iff

• $t_{enter} < t_{exit} \space && \space t_{exit} >= 0$