Sheet 12: Depth of Field

Exercise: Mipmap-Based Depth of Field

Start from the completed deferred renderer of sheet 10. The provided starter already contains that solution: the same city, camera, 400 street lamps, surface materials, and deferred illumination. Unlike sheet 10's starter, this program already stores real surface attributes and computes lighting in the fullscreen pass.

Add depth of field by selecting mip levels of the completed lighting image according to distance from a focus plane. Surfaces on that plane remain sharp; nearer and farther surfaces use increasingly filtered colors. The G-buffer remains sharp: filtering positions or normals before illumination would create artificial surfaces and distorted lighting rather than a blurred image.

The four RGBA16F textures retain the layout from sheet 10: diffuseBuffer stores diffuse RGB and coverage in alpha; specularBuffer stores specular RGB and shininess divided by 128 in alpha; normalBuffer stores world-space normals encoded into [0, 1]; positionBuffer stores world positions and coverage in alpha. The additional sceneBuffer is an RGBA16F color texture containing the fully lit city in linear HDR values. The 400-light illumination calculation is unchanged; tone mapping and gamma encoding have moved to the final display pass, after filtering.

Camera controls, effect parameters, uniforms and the call between geometry and post-processing are supplied. Complete the three marked functions in DepthOfField.h. Initially the scene is sharp even when the effect toggle is enabled.

Task 1: Build the Lighting Image Mipmaps

Implement generateSceneMipmaps() in DepthOfField.h. After deferred illumination has written sceneBuffer and its framebuffer has been unbound, regenerate the color texture's mip chain. The scene changes with the camera, so this must happen every frame, not just during initialization.

Use linear magnification and trilinear minification (GL_LINEAR_MIPMAP_LINEAR) so fractional mip levels interpolate smoothly. GLTexture2D already provides setFilter() and generateMipmap(). The supplied textures use GL_CLAMP_TO_EDGE to avoid wrapping across image borders. The four G-buffer textures and the lamp data texture are not part of this pyramid and must remain nearest-filtered.

Task 2: Choose a Depth-Dependent Mip Level

Implement the GLSL function depthOfFieldLod() in DepthOfField.h. Read the sharp world position from mip level zero. Compute positive camera-axis depth z by projecting position - cameraPosition onto cameraForward. The focus plane is perpendicular to that direction at focusDistance; Euclidean distance would instead define a focus sphere.

For this exercise, use the following simplified blur-radius model, with distances in world units and radius in pixels:

z = max(dot(position - cameraPosition, cameraForward), 0.5)
radius = clamp(apertureRadius * abs(z - focusDistance) / z,
               0, apertureRadius)
lod = clamp(log2(1 + 2 * radius), 0, maxMipLevel)

Return zero when enableDoF is zero or the aperture radius is zero. The radius vanishes on the focus plane. One mip texel covers approximately 2^lod base-level pixels, motivating the logarithm of the filter diameter. Keep the level fractional and clamp it to the available mip chain.

Task 3: Sample the Filtered Lighting Image

Implement depthOfFieldColor() in DepthOfField.h. Read the sharp positionBuffer to obtain world position and select a level with depthOfFieldLod(). For background pixels with position alpha zero, use cameraPosition + cameraForward * farPlane as a proxy position.

Use textureLod() to sample sceneBuffer at the chosen level and return its RGB value. Do not filter material, normal or position data, and do not recompute illumination. The supplied final shader applies tone mapping and gamma encoding to the filtered linear color.

Keep the diagnostic G-buffer view sharp, independent of the effect toggle. With depth of field disabled, the final image should agree with the completed renderer from sheet 10.

Approximation and Limitations

This is a deliberately simple image-space approximation, not a physically correct thin-lens renderer. A mip footprint averages already illuminated colors, so it does not invent geometry or change how the lamps illuminate surfaces. However, it can mix colors across depth discontinuities, producing halos. Hidden surfaces are unavailable, and square mip footprints do not produce aperture-shaped bokeh. Separate foreground and background filtering would be a possible extension.

Compare near-focus and far-focus settings, inspect building edges, and explain where this approximation breaks down. No performance improvement is expected: the extra color buffer, mip chain and display pass add memory and per-frame work.

Reference Solution

Loading

F: toggle depth of field. Left/right arrows: focus nearer/farther. Up/down arrows: increase/decrease aperture radius. G: show the sharp G-buffer. R: reset. W/S: forward/backward; A/D: strafe; left mouse drag: look around; mouse wheel in the native app: move forward/backward.

Text description and demo controls

The colored city from sheet 10 is lit by 400 street lamps. Surfaces close to a movable focus plane remain sharp, while nearer and farther surfaces use mip-filtered colors from the completed lighting image. Positions, normals and materials remain unfiltered. The focus distance starts at 35 world units and the maximum blur radius at 3 pixels. Depth of field is initially enabled.

G shows the unblurred base-level G-buffer: diffuse at the top left, specular at the top right, encoded world normals at the bottom left and scaled world positions at the bottom right. The last image shows positions, not depth.

Focus the demo with Tab. F toggles the effect. Left and right arrows move the focus plane nearer and farther; down and up arrows decrease and increase the aperture radius. G toggles the buffer view. R restores the initial camera and effect settings. Hold W/S to fly along the viewing direction and A/D to strafe. Looking around uses left mouse dragging; in the native app the wheel also moves forward or backward.