Computer Science › Section K · Computer graphics › Topic K.4

Global lighting: ray tracing and radiosity

Shadows, mirrors, glass and the soft glow that one wall throws onto another all come from light passing between objects. Ray tracing and radiosity capture those interactions, at a far higher computing cost than the rendering pipeline.

Local versus global lighting

The rendering pipeline lights each object as if it were alone: a local lighting model. Real light passes between objects. A ball casts a shadow, a mirror shows the room, water bends what lies behind it, and a red wall tints a white floor pink. A global lighting model captures these interactions. Two classic methods provide one: ray tracing follows individual rays, and radiosity tracks light energy exchanged between whole patches. Both cost far more than the pipeline.

Tracing light backwards

A lamp sends light in every direction, and almost none of it enters the camera, so following rays forward from the lights would waste nearly all the work. Ray tracing runs backwards. For each pixel it follows the ray from the centre of projection through that pixel to the first object it meets. If that is a light, the pixel shows the light. Otherwise the surface tells which incoming rays could have sent light along this ray, and those are followed backwards in turn, so every traced ray matters to some pixel. A 1920 × 1080 image starts with 2,073,600 primary rays, one per pixel.

Where a ray hits a sphere

A ray from point \(E\) in unit direction \(\mathbf{D}\) reaches \(P(t) = E + t\mathbf{D}\) after distance \(t\). It meets a sphere of centre \(C\) and radius \(r\) where \(|P(t) - C| = r\). Squaring gives a quadratic in \(t\):

\begin{aligned} t^2 &+ 2t\,\mathbf{D}\cdot(E - C) \\ &+ |E - C|^2 - r^2 = 0 \end{aligned}

No real root means a miss; otherwise the smaller positive root is the visible hit, with unit normal \((P - C)/r\). Keeping the nearest hit over all objects removes hidden surfaces automatically. For shadows, a shadow ray goes from the hit point towards each light; if an object blocks it, that light does not reach the point.

Worked example: a ray meets a sphere

The eye is at the origin, the ray direction is \(\mathbf{D} = (0, 0, 1)\), and a sphere has centre (1, 0, 10) and radius 2. 1. \(E - C = (-1, 0, -10)\), so \(\mathbf{D}\cdot(E - C) = -10\) and \(|E - C|^2 = 101\). 2. The quadratic is \(t^2 - 20t + 97 = 0\). 3. \(t = 10 \pm \sqrt{100 - 97} = 10 \pm \sqrt{3}\), about 8.268 and 11.732: the ray enters and leaves. 4. The nearer root gives the visible point \(P = (0, 0, 8.268)\). 5. Normal: \((P - C)/2 \approx (-0.5, 0, -0.866)\), facing back towards the eye as a visible surface should. Check: \(|P - C|^2 = 1 + 3 = 4 = r^2\).

Recursion: mirrors and glass

A ray arriving with direction \(\mathbf{D}\) at a shiny surface reflects to \(\mathbf{R} = \mathbf{D} - 2(\mathbf{D}\cdot\mathbf{N})\mathbf{N}\), the same law as in "Rendering" with \(\mathbf{D}\) pointing into the surface. Ray tracing is naturally recursive, with k the surface's reflectivity:

\begin{array}{l} \texttt{def Trace(Ray, Depth):} \\ \quad \texttt{if (Misses(Ray)):} \\ \qquad \texttt{return Background} \\ \quad \texttt{H = NearestHit(Ray)} \\ \quad \texttt{C = DirectLight(H)} \\ \quad \texttt{if (Shiny(H)} \\ \quad\qquad \texttt{and Depth < Max):} \\ \qquad \texttt{R = Reflect(Ray, H)} \\ \qquad \texttt{C = C + k * Trace(R,} \\ \qquad\qquad \texttt{Depth + 1)} \\ \quad \texttt{return C} \end{array}

At glass the ray splits into a reflected ray and a refracted ray, both traced. Recursion stops at a light, on leaving the scene, at a dark matte surface or at the depth limit.

Worked example: a bounce and its cost

A ray with \(\mathbf{D} = (0.6, -0.8, 0)\) hits a mirror floor with \(\mathbf{N} = (0, 1, 0)\). 1. \(\mathbf{D}\cdot\mathbf{N} = -0.8\). 2. \(\mathbf{R} = (0.6, -0.8, 0) + 1.6(0, 1, 0) = (0.6, 0.8, 0)\). 3. Check: both rays make an angle with cosine 0.8, about 36.9°, with the normal, and \(\mathbf{R}\) has length 1. 4. The cost: if every hit among mirrors and glass spawns a reflected and a refracted ray, a depth limit of 5 allows 1 + 2 + 4 + 8 + 16 + 32 = 63 rays per pixel. 5. Over 2,073,600 pixels that is up to 130,636,800 rays per frame, before shadow rays.

Distributed ray tracing

Basic ray tracing follows only exact mirror directions, so scenes look too shiny and sharp. Distributed ray tracing sends several rays from each point, spread around the ideal direction, and averages them. Spread reflections look glossy; shadow rays aimed across a large lamp give soft shadow edges; samples spread in time or across a lens give motion blur and depth of field. The price: 16 rays per pixel means 33,177,600 primary rays per 1920 × 1080 frame. Related random-sampling methods are now widely used in film production, and some recent consumer GPUs have hardware that speeds up ray tests, so some games mix limited ray tracing into the pipeline.

Radiosity: energy between patches

Radiosity divides every surface into patches and finds each patch's radiosity \(B\), the diffuse light power it gives off per unit area: the light it emits, \(E\), plus a fraction \(\rho\), its reflectance, of the light arriving from all other patches:

B_i = E_i + \rho_i \sum_j F_{ij} B_j

The form factor \(F_{ij}\), between 0 and 1, is the share of light leaving patch \(i\) that reaches patch \(j\). It depends on their distance, their angles to each other and whether anything blocks the view. One equation per patch gives a large system of linear equations, usually solved by repeated passes.

Worked example: two patches

A glowing panel, patch 1, has \(E_1 = 10\) and \(\rho_1 = 0.5\); a wall, patch 2, emits nothing and has \(\rho_2 = 0.8\). Take \(F_{12} = 0.2\) and \(F_{21} = 0.25\). 1. The equations are \(B_1 = 10 + 0.1B_2\) and \(B_2 = 0.2B_1\). 2. Start from \(B = E\): (10, 0). 3. Pass 1: \(B_1 = 10\), then \(B_2 = 0.2 \times 10 = 2\). 4. Pass 2: \(B_1 = 10 + 0.1 \times 2 = 10.2\), then \(B_2 = 2.04\). Pass 3 changes them only to 10.204 and 2.0408. 5. Check exactly: \(B_1 = 10 + 0.02B_1\), so \(B_1 = 10/0.98 \approx 10.20\) and \(B_2 \approx 2.04\). The wall glows with reflected light; a red wall would tint its neighbours: colour bleeding.

Comparing the two methods

Radiosity does not depend on the camera: once solved, the scene can be viewed from any position quickly, and soft diffuse effects such as colour bleeding come out naturally. But it handles only diffuse light over whole patches, so it misses highlights and mirrors and everything looks matte, and \(n\) patches need about \(n^2\) form factors, 100 million for 10,000 patches. It suits architectural walkthroughs, where light is mostly diffuse and views change often. Ray tracing is the opposite: camera-dependent, excellent for mirrors, glass and sharp shadows, and reliant on distributed sampling for soft light. Some renderers combine both methods.

Common mistakes to avoid

First, thinking ray tracing follows light forwards from the lamps; it starts at the eye. Second, taking the larger root of the ray-sphere quadratic, which is the far side of the sphere. Third, forgetting the factor 2 in \(\mathbf{R} = \mathbf{D} - 2(\mathbf{D}\cdot\mathbf{N})\mathbf{N}\). Fourth, recursing without a depth limit. Fifth, expecting radiosity to show mirror reflections or highlights. Sixth, forgetting that radiosity's solution serves every camera position, while ray tracing must redo each view.

Key takeaways

A global lighting model captures light passing between objects. Ray tracing follows rays backwards from the eye through each pixel, finds the nearest hit, sends shadow rays to the lights and recurses for reflection and refraction until a stopping condition; distributed ray tracing averages many rays for glossy and soft effects. Radiosity solves B = E + ρ Σ F B for diffuse energy between patches, giving camera-independent soft lighting and colour bleeding but no highlights. Both are costly.

Flashcards

Flashcard

Local lighting model

Each object lit as if alone in the scene

The rendering pipeline's model.

Test your understanding

Question 1 of 5

Why does ray tracing follow rays backwards from the eye rather than forwards from the light sources?

Question 2 of 5

A ray starts at the origin with direction (0, 0, 1). A sphere has centre (0, 0, 20) and radius 5. At what distance t does the ray first hit the sphere?

Question 3 of 5

A ray with direction (0.8, -0.6, 0) hits a mirror whose normal is (0, 1, 0). What is the direction of the reflected ray?

Question 4 of 5

A patch emits no light and has reflectance 0.5. Its only light comes from a lamp patch of radiosity 40, with form factor 0.3. What is the patch's radiosity?

Question 5 of 5

An architect wants to show a client many views of a building lit mostly by soft daylight. Why is radiosity a good fit?

← Previous topicNext topic →