Computer Graphics
Ray Tracing: Fundamentals
In 1980, Turner Whitted rendered a glass ball in 74 minutes. In 2023, an NVIDIA RTX 4090 renders the same scene in nanoseconds - an acceleration of 2.6 trillion times in 43 years. The idea is unchanged: ray from camera, intersection with geometry, recursive secondary rays. Understanding ray tracing fundamentals means understanding how Blender Cycles, Unreal Lumen, Disney's Hyperion renderer, and every RTX game work.
- **NVIDIA RTX (DXR/VK_RT):** hardware RT Cores implement BVH traversal and ray-triangle intersection. API: TraceRay() in HLSL, vkTraceRaysKHR in Vulkan
- **Blender Cycles / Disney Hyperion:** production ray/path tracers use BVH with SAH, Embree (Intel) for CPU tracing, OptiX for GPU
- **Game engines (Unreal Lumen, RTX Remix):** hybrid approach: Whitted-style reflection/shadow rays + screen space for diffuse effects
Ray-Geometry Intersection
In 1980, Turner Whitted rendered a single image in 74 minutes on a USD 50,000 computer. Today an NVIDIA RTX 4090 traces billions of rays per second. The principle is unchanged: a ray P(t) = O + t*D, where O is the origin and D is the direction. Ray-triangle intersection is the core operation: the Moller-Trumbore algorithm solves P = O + t*D = (1-u-v)*V0 + u*V1 + v*V2 via Cramer's rule in O(1). Validity checks: t > 0 (in front), u >= 0, v >= 0, u+v <= 1 (inside triangle).
Moller-Trumbore (1997): det = cross(D, e2) dot e1; if |det| < eps the ray is parallel. u = cross(D, e2) dot (O-V0) / det; v = cross(O-V0, e1) dot D / det; t = cross(O-V0, e1) dot e2 / det. Where e1 = V1-V0, e2 = V2-V0. SIMD vectorization: 8 rays in parallel at the cost of one.
The Moller-Trumbore algorithm checks |det| < eps at the start. What does this condition mean geometrically?
BVH: Bounding Volume Hierarchy
A scene with 1 million triangles using a naive approach: every ray tests 1,000,000 triangles. At 1920x1080 resolution with 100 rays per pixel that is 207 trillion tests per frame. BVH solves this: triangles are grouped into a hierarchy of axis-aligned bounding boxes (AABBs). During tracing a ray first tests the AABB node, and only on hit traverses child nodes. Complexity: O(log n) vs O(n). NVIDIA RTX implements BVH in hardware via dedicated RT cores.
BVH construction: split along the axis of maximum spread (Surface Area Heuristic - SAH minimizes expected query cost). AABB intersection via slab method: t_enter = max(t_x_min, t_y_min, t_z_min), t_exit = min(t_x_max, t_y_max, t_z_max), miss if t_enter > t_exit.
BVH reduces tracing time from O(n) to O(log n). Under what condition does BVH degrade to O(n)?
Recursive Tracing: Reflection and Refraction
Recursive ray tracing unfolds the optics: a ray hits a mirror surface and spawns a reflected ray; it hits glass and spawns a refracted ray. Reflection: R = D - 2*(D dot N)*N. Refraction (Snell's law): n1*sin(theta1) = n2*sin(theta2). Recursion is bounded by a maximum depth (typically 5-10) or ray weight (prune below threshold). Fresnel effect: at grazing angles even glass becomes nearly fully reflective - the Schlick approximation models this dependency.
Schlick approximation for reflectance: R(theta) = R0 + (1-R0)*(1-cos(theta))^5, where R0 = ((n1-n2)/(n1+n2))^2. At theta=0 (perpendicular) R=R0; at theta=90 (grazing) R=1 (total reflection). Used in PBR renderers universally.
Total internal reflection (TIR) occurs during refraction. Under what condition does the ray not refract but fully reflect?
The Whitted Model: Classic Ray Tracer Structure
Whitted (1980) described the first practical recursive ray tracer. The model: for each pixel a primary ray is fired from the camera. On hit: (1) direct illumination - shadow rays to lights, (2) mirror reflection - recursive reflected ray, (3) refraction - recursive refracted ray. Total color = diffuse*phong + reflection + refraction. Whitted-style ray tracing correctly renders mirrors, glass, sharp shadows, but does not account for diffuse inter-reflections - path tracing is needed for that.
Typical render structure: 1 primary ray -> up to 32 secondary rays per hit (shadow + reflection + refraction + recursion). At 4K resolution with 16 samples/pixel - 130 billion rays per frame. RTX 3080 handles ~22 billion rays/s - insufficient for real-time without denoising (DLSS / AI denoiser).
Ray tracing and path tracing are the same thing - path tracing just uses more rays
Whitted ray tracing traces deterministic secondary rays (reflection, refraction). Path tracing randomly samples the full hemisphere - fundamentally different algorithms with different guarantees
Whitted RT: O(depth) rays per pixel, deterministic, does not converge to a physically correct result. Path tracing: O(samples) rays, stochastic, converges to the rendering equation (Kajiya 1986) with sufficient samples.
Whitted ray tracing correctly renders mirrors and glass but not color bleeding - a red diffuse sphere does not tint neighboring surfaces. Why?
Key Ideas
- **Moller-Trumbore:** O(1) ray-triangle intersection via barycentric coordinates. The foundational operation of all ray tracing.
- **BVH:** AABB hierarchy reduces complexity from O(n) to O(log n). NVIDIA implements this in hardware RT Cores.
- **Recursive tracing:** reflection R = D - 2*(D*N)*N, refraction via Snell's law, TIR when n1 > n2. Schlick approximates Fresnel.
- **Whitted vs Path Tracing:** Whitted - deterministic, specular only. Path tracing - stochastic, converges to a physically correct result.
Related Topics
Ray tracing unifies models of illumination, shadows, and global illumination.
- Lighting: Phong, PBR — Direct illumination in Whitted RT uses the same lighting models
- Shadows and Global Illumination — Shadow rays are part of Whitted RT; global illumination requires path tracing
Вопросы для размышления
- BVH accelerates tracing from O(n) to O(log n) but requires O(n log n) to build. Under what conditions should the BVH be rebuilt every frame (dynamic scenes) versus once at load time?
- RTX cards have dedicated RT Cores for BVH traversal. Why is it advantageous to implement this in hardware rather than on shader cores? What fundamentally distinguishes BVH traversal from regular GPU computation?
- Whitted RT renders perfect mirrors, but there are no perfectly mirror surfaces in nature. How do PBR roughness-metallic materials change the reflection model? What does roughness > 0 mean physically?