What is Ray Tracing?
Ray tracing is a rendering algorithm that simulates the physical behaviour of light by following rays from the camera through each pixel and computing how they interact with scene geometry. Unlike rasterisation — which projects triangles onto a screen — ray tracing produces global illumination effects such as reflections, refractions, shadows and colour bleeding naturally from the underlying physics.
A path tracer extends this further: instead of evaluating all possible light paths, it importance-samples random paths through the scene using Monte Carlo integration. The rendering equation
is the Kajiya rendering equation (1986). The left side is outgoing radiance; the integral sums all incoming light weighted by the BRDF fr and cosine foreshortening. Path tracing estimates this integral stochastically — each ray bounce samples one random incoming direction.
Ray–Sphere Intersection
A ray is defined as P(t) = O + tD. Substituting into the sphere equation |P - C|² = r² gives a quadratic in t:
t² (D·D) + 2t (D·(O-C)) + (O-C)·(O-C) - r² = 0
discriminant ? = b² - c ? t = -b ± v?
If ? < 0 the ray misses the sphere. The smallest positive root gives the closest hit.
Reflection
The reflected direction is simply the incident direction with the normal component negated. Mirror surfaces bounce the ray without any colour or angle randomness.
Refraction (Snell's Law)
Glass objects use Snell's law for refraction and the Schlick approximation for Fresnel blending between reflection and transmission:
At grazing incidence (? ? 90°) almost all light reflects; at normal incidence (? = 0°) most transmits.
Monte Carlo Path Tracing
The rendering equation cannot be solved analytically for complex scenes. Monte Carlo integration approximates it by drawing N random samples from the hemisphere above each hit point:
where p(?i) is the sampling PDF. Using cosine-weighted hemisphere sampling (p = n^·? / p) cancels the cosine factor, leaving:
Error decreases as 1/vN — doubling quality requires four times the samples. Progressive refinement accumulates samples over multiple frames, so the image automatically improves while you watch.
Russian Roulette
Recursion is terminated stochastically: at each bounce a ray is killed with probability (1 - albedo) and surviving rays are scaled up by 1/albedo to keep the estimator unbiased. This prevents infinite loops while correctly handling diffuse global illumination.
Tone Mapping
Real-world luminance ranges far exceed a monitor's [0, 1] range. Reinhard tone mapping compresses HDR radiance values:
Gamma correction converts linear light values to the perceptual sRGB colour space of your display.
Scene Presets — Phenomena Demonstrated
| Preset | Key Phenomena | Materials | Educational Highlight |
|---|---|---|---|
| Cornell Box | Colour bleeding, area shadows, global illumination | Diffuse, mirror, glass | Classic GI benchmark scene (Cornell 1984) |
| Mirrors | Infinite reflections, light tunnelling | Mirror walls, diffuse spheres | Eigenvalue recursion depth limit |
| Glass Spheres | Fresnel reflection/refraction, caustics | Glass (IOR 1.33–1.6), diffuse floor | Index of refraction table: water/glass/diamond |
| Soft Shadows | Penumbra, umbra, two-light interference | Area lights, diffuse/mirror spheres | Shadow hardness vs light source size |
| Night City | Neon colour bleeding, glossy reflections | Emissive neon, mirror, glass, diffuse | Multiple light sources, additive colour |
| Colourful | Interreflections between coloured spheres | Diffuse, mirror, glass mix | Colour bleeding — light carries surface colour |
Material Models
| Material | BRDF Type | Parameters | Real-World Examples |
|---|---|---|---|
| Diffuse (Lambertian) | fr = ?/p (constant) | Albedo colour | Chalk, plaster, matte paint |
| Mirror (Specular) | Delta BRDF (Dirac peak) | Reflectance tint | Polished silver, perfect metal |
| Glass (Dielectric) | Fresnel blend — reflect + transmit | IOR n (1.33–1.9), tint | Water (1.33), glass (1.5), diamond (2.42) |
| Emissive (Area Light) | Le = constant emission | Emission colour + intensity | Light panels, neon tubes, LEDs |
Curriculum Connections
| Topic | Qualification | Concepts Covered |
|---|---|---|
| Optics — reflection & refraction | GCSE / A-Level Physics | Snell's law, total internal reflection, critical angle |
| Wave & ray optics | A-Level Physics / IB | Huygen's principle, Fermat's principle of least time |
| Computer graphics pipeline | A-Level / BTEC CS | Rasterisation vs ray tracing, z-buffer, shading models |
| Numerical methods | A-Level Maths / Further Maths | Monte Carlo integration, random sampling, convergence 1/vN |
| Linear algebra | Further Maths / University | Vector dot/cross products, ONB construction, matrix transforms |
| Probability & statistics | A-Level Maths | Variance, standard error, importance sampling, PDF/CDF |