Rasterization asks, for each triangle, "which pixels do I cover?" Ray tracing turns the question around: for each pixel it asks "what do I see in this direction?" This change of perspective makes natural effects that rasterization can only approximate, such as precise shadows, reflections of objects outside the frame, refraction through glass and light bouncing between surfaces. The price is a much higher computational cost.
The basic idea
In reality light leaves the sources, bounces off objects and a tiny fraction reaches our eyes. Simulating it that way would be hugely wasteful: almost no rays would ever reach the camera. Ray tracing therefore follows the path backwards, starting from the eye.
For each pixel:
- a primary ray is cast from the camera through the pixel;
- the first surface the ray intersects is found;
- from that point, the amount of light travelling towards the camera is calculated.
Computing the intersection between a ray and a triangle, or a sphere, is a well-known and relatively simple geometric problem. The hard part is doing it for millions of rays against millions of triangles.
Classical ray tracing
In the model described by Turner Whitted in 1980, secondary rays are cast from the hit point:
- shadow rays towards each light source: if they meet an obstacle, the point is in shadow with respect to that light;
- reflected rays in the mirror direction, for glossy surfaces;
- refracted rays through transparent materials, bent according to the refractive index.
The procedure is recursive: a reflected ray can hit another reflective surface, and so on, up to a depth limit. The result is sharp shadows and perfect reflections, but diffuse light bounced from one wall to another is still missing.
The rendering equation and path tracing
In 1986 James Kajiya formulated the rendering equation, which describes in general terms the light leaving a point as the sum of the light emitted and all the light arriving from every direction, weighted by the material's properties. Solving it exactly is impossible for real scenes: the integral over all directions contains, recursively, further integrals.
Path tracing solves it by statistical approximation with the Monte Carlo method:
- many paths are cast for each pixel;
- at each bounce a single random direction is chosen, according to the material's properties;
- the path continues until it meets a light, leaves the scene or is terminated;
- the pixel's color is the average of all the paths.
With enough samples, the result converges to the correct solution and naturally includes global illumination, soft shadows, caustics, depth of field and motion blur.
Noise and its price
With few samples per pixel, the image is full of noise: light and dark specks caused by randomness. Noise decreases slowly: halving it takes about four times the samples, because the error falls with the square root of the number of samples. Scenes where light enters through small openings, such as an interior lit by a window, are the hardest.
Techniques for reducing noise without multiplying samples are essential:
- importance sampling: casting more rays in the directions that contribute most, such as towards the sources or in the reflection direction;
- direct light sampling: at each bounce a source is queried explicitly, rather than hoping to hit it by chance;
- Russian roulette: insignificant paths are terminated at random, compensating the weight of those that continue;
- denoising: filters, often based on neural networks, that reconstruct a clean image from few samples using auxiliary information such as normals and base color.
Speeding up intersections
Testing every ray against every triangle would be prohibitive. Acceleration structures are used, the most widespread being the BVH (bounding volume hierarchy): objects are enclosed in boxes, the boxes in larger boxes, forming a tree. A ray that does not pass through a box rules out everything inside it in one go. This way the number of tests per ray grows logarithmically with the number of triangles, not linearly.
Recent graphics cards include dedicated hardware units for BVH traversal and ray-triangle intersection, which have made real-time ray tracing possible in games, usually in hybrid form: the scene is rasterized and ray tracing is used for reflections, shadows or global illumination, with few samples and an aggressive denoiser.
Rasterization or ray tracing?
| Aspect | Rasterization | Path tracing |
|---|---|---|
| Speed | very high | low, improved by dedicated hardware |
| Shadows | approximate techniques (shadow maps) | natural and precise |
| Reflections | approximate, limited to what is visible | correct, even outside the frame |
| Bounced light | precomputed or approximated | naturally included |
| Typical artifact | transparency errors, jagged shadows | noise |
For an animation or a product image where fidelity matters, path tracing is today the standard; for interactivity, rasterization remains the foundation, increasingly accompanied by ray tracing.
In short
- Ray tracing starts from the camera and asks, for each pixel, what can be seen in that direction.
- The classical model uses shadow, reflected and refracted rays, but not diffuse bounced light.
- Path tracing solves the rendering equation with random samples and includes global illumination.
- Noise falls slowly: importance sampling and denoising are indispensable.
- BVHs and dedicated hardware make intersections fast enough for hybrid real-time rendering.