Computer Science › Section K · Computer graphics › Topic K.3
Rendering
Rendering turns a finished scene graph into an image by working out what the camera sees and how light makes it look. The method used by games and everyday graphics hardware is a fixed sequence of steps called the rendering pipeline.
From scene graph to pixels
Rendering decides how each object in the scene graph appears on the image window. We see objects only by the light they send us, so rendering simulates light. This topic follows the approach used by most games and everyday graphics hardware, the rendering pipeline: discard what the camera cannot see, clip, convert patches into pixels, remove hidden surfaces and shade. Throughout, the objects are assumed opaque. The topic "Global lighting: ray tracing and radiosity" covers two alternatives.
How light meets a surface
Light striking a surface may be absorbed, reflected or, in transparent material, bent as it passes through, which is refraction (see the Physics topics "Rays and the law of reflection" and "Refraction and total internal reflection"). A smooth surface reflects parallel rays in one direction, with the angle of reflection equal to the angle of incidence, both measured from the normal. This specular light makes highlights in the colour of the light source. Rough surfaces scatter diffuse light in all directions, coloured by the material. Ambient light, stray light from no particular direction, keeps shaded sides from turning black. Anisotropic surfaces such as satin look shinier or duller as they turn.
A local lighting model
The classic Phong reflection model adds the three kinds of light. With unit vectors \(\mathbf{N}\) (the normal), \(\mathbf{L}\) (towards the light), \(\mathbf{V}\) (towards the viewer) and \(\mathbf{R}\) (the mirror direction of \(\mathbf{L}\)):
\(I_a\) and \(I_L\) are the ambient and light intensities; the \(k\) values are the surface's ambient, diffuse and specular fractions. The diffuse term is Lambert's cosine law: for unit vectors \(\mathbf{N}\cdot\mathbf{L} = \cos\theta\), so light arriving 60° from the normal gives half the brightness. The dot product was defined in "Modeling objects". The mirror direction is \(\mathbf{R} = 2(\mathbf{N}\cdot\mathbf{L})\mathbf{N} - \mathbf{L}\), and a larger \(n\) gives a smaller, sharper highlight.
Worked example: lighting one point
Take \(\mathbf{N} = \mathbf{V} = (0, 0, 1)\) and \(\mathbf{L} = (0, 0.6, 0.8)\). The surface is red, with \(k_a = k_d = (0.9, 0.2, 0.2)\) for red, green, blue, \(k_s = 0.5\) and \(n = 10\), under white light with \(I_L = 1\) and \(I_a = 0.1\). 1. \(\mathbf{N}\cdot\mathbf{L} = 0.8\), the cosine of about 36.9°. 2. Diffuse: 0.8 × (0.9, 0.2, 0.2) = (0.72, 0.16, 0.16). 3. Ambient: 0.1 × (0.9, 0.2, 0.2) = (0.09, 0.02, 0.02). 4. \(\mathbf{R} = 1.6(0, 0, 1) - \mathbf{L} = (0, -0.6, 0.8)\), so \(\mathbf{R}\cdot\mathbf{V} = 0.8\) and the specular term is \(0.5 \times 0.8^{10} \approx 0.054\) in every channel. 5. Total: about (0.864, 0.234, 0.234), lit red with a faint white sheen.
View volume and clipping
The camera sees only a pyramid of space, the view volume, whose apex is the viewpoint and whose four edges pass through the image window's corners; near and far limits usually cut it off front and back. Objects wholly outside are discarded first, and a scene graph that groups objects by region lets whole branches be skipped. Patches crossing the boundary are clipped: the outside part is cut away. Discarding work early is the pipeline's guiding idea, because later steps cost effort per pixel.
Scan conversion and aliasing
Next each patch is matched with the pixels it covers: scan conversion, or rasterisation, since a raster is a grid of pixels filled along rows called scan lines. In effect, a projector runs from the viewpoint through each pixel's centre, and where it meets a patch is what that pixel shows. Detail between pixel centres is lost, and sloping edges become jagged steps. A fine pattern that beats against the pixel spacing, like a striped shirt on video, can flicker or show false patterns: aliasing. Antialiasing averages several samples per pixel, so an edge pixel with 2 of 4 samples on a black shape over white shows 50% grey.
Hidden-surface removal
Where patches overlap on screen, each pixel must show the nearest one. Back-face elimination drops the patches of a closed object that face away from the camera: with \(E\) the camera and \(P\) a point on the patch, the patch faces away when \(\mathbf{N}\cdot(E - P) \le 0\). That is not enough: a car can hide part of a building. The painter's algorithm draws whole objects from farthest to nearest, so near ones paint over far ones, but it fails when objects interlock, like a vine woven in front of and behind a fence. The z-buffer, or depth buffer, solves the problem pixel by pixel: it stores, for each pixel, the depth of the nearest point drawn so far.
Worked example: the z-buffer
Every entry Z[P] starts at the maximum depth, and the frame buffer F holds colours:
Trace one pixel, maximum depth 100, as a wall (depth 40), a ball (12) and a leaf (25) arrive in that order. 1. Wall: 40 < 100, so the pixel turns wall-grey and Z = 40. 2. Ball: 12 < 40, so it turns ball-red and Z = 12. 3. Leaf: 25 is not below 12, so nothing changes. 4. The nearest object wins in any order. 5. At 4 bytes per depth, a 1920 × 1080 z-buffer takes 8,294,400 bytes.
Shading: flat, Gouraud and Phong
Shading finds the colour at each pixel's point. Flat shading uses the patch's own normal everywhere on it, so a sphere looks faceted: you see the mesh, not the object. Smooth shading starts from a normal at each vertex, stored or averaged from neighbouring patches. Gouraud shading lights the vertices and interpolates the colours: with vertex brightnesses 0.2 and 0.8, a point a quarter of the way along gets 0.2 + 0.25 × 0.6 = 0.35. Phong shading interpolates the normals and lights each pixel with its own, which costs more but catches a highlight in the middle of a patch. Bump mapping alters the normals using a stored pattern, so a smooth mesh looks rough.
Graphics hardware and its limits
These steps are so standard that GPUs perform them in hardware, today with programmable stages run by small programs called shaders. Software hands the GPU the scene data and gets finished frames back, and standard interfaces such as OpenGL, Direct3D, Vulkan and Metal hide the differences between chips. GPUs also run other number-heavy work, such as training neural networks. The pipeline's main limit is its local lighting model: each object is lit as if it were alone, so shadows and reflections of other objects do not appear by themselves. Tricks fill the gap: a drop shadow is a flattened dark copy of an object laid on the ground.
Common mistakes to avoid
First, forgetting to use unit vectors: \(\mathbf{N}\cdot\mathbf{L}\) equals \(\cos\theta\) only when both have length 1. Second, letting a surface facing away from the light get negative brightness; the max with 0 prevents it. Third, mixing up the shading methods: Gouraud interpolates colours, Phong interpolates normals. Fourth, thinking back-face elimination alone solves hidden surfaces, or that the painter's algorithm always works. Fifth, confusing the z-buffer, which stores depths, with the frame buffer, which stores colours.
Key takeaways
Rendering simulates light. Specular light makes highlights, diffuse light shows the material's colour and ambient light fills shadows; the Phong model adds them, with Lambert's law max(0, N·L) for the diffuse part. The pipeline discards and clips to the view volume, rasterises patches into pixels, removes hidden surfaces with back-face tests and a z-buffer, then shades by flat, Gouraud or Phong methods. GPUs run the pipeline, but its local lighting model misses shadows and reflections.
Flashcards
Flashcard
Discard and clip, rasterise, remove hidden surfaces, shade
Test your understanding
Question 1 of 5
A surface with diffuse fraction 0.8 is lit by a light of intensity 1 whose direction makes 60° with the surface normal. Ignoring ambient and specular light, what is its brightness?
Question 2 of 5
A pixel's z-buffer entry starts at 100. Patches arrive in this order: blue at depth 30, green at 55, yellow at 18, white at 22. What colour does the pixel end up?
Question 3 of 5
A large triangle should show a bright highlight near its centre, far from all three vertices. Which method is most likely to show it?
Question 4 of 5
The camera is at E(0, 0, 10). Each patch is given by a point P on it and its outward normal N. Which patch does back-face elimination remove?
Question 5 of 5
Why does a basic rendering pipeline not draw the shadow a ball casts on the floor by itself?
