Sheet 10: Deferred Shading
Exercise: Deferred Lighting in the City
Use the provided starter, which reuses the city layout of exercise 08. It supplies the scene, camera, 400 street lamps, four floating-point G-buffer textures, their shared framebuffer and depth attachment, and a fullscreen compositing pass.
The complete city is uploaded once and submitted each frame. The GPU's normal clipping and depth testing determine which surfaces appear in the image.
The starter is deliberately not yet deferred shading: the geometry
shader still computes the complete illumination and writes that same shaded result
into all four targets. The compositing shader simply displays the first texture.
Your task is to replace the stored values and move lighting into the compositing
pass. Work in GBufferShaders.h; the framebuffer setup is already provided.
Four hundred local street lamps are distributed along the roads, with visible poles and
warm or cool light colors. The supplied illuminate() function in
Lighting.h loops over all 400 lights and evaluates distance attenuation,
diffuse reflection, Phong highlights, and a downward lighting weight. A weak
directional term provides fill light; tone mapping and gamma encoding
prepare the result for display.
The ambient weight is zero and the directional fill weight is 0.01. Each lamp's
range fades smoothly between 6 and 9 world units. The lamps stand 4 world units
above the street, leaving the city dark outside their compact pools of light. The 400-light loop intentionally increases
the cost of shading a fragment.
Task 1: Store Surface Attributes
Change gbufferFragmentShader so the geometry pass stores the following
attributes instead of the shaded scene:
outDiffuse: unlit surface color fromsurfaceMaterial(); alpha one marks a surface.outSpecular: specular RGB reflectance and shininess divided by 128 in alpha. UsesurfaceSpecular(): window pixels have reflectance 0.6, while walls, roofs, streets, and lamp geometry have zero specular reflectance. Use shininess 24.outNormal: normalized world-space normal, encoded asN * 0.5 + 0.5.outPosition: world-space position in RGB and alpha one.
The framebuffer is cleared with alpha zero, so background pixels can be distinguished
from surfaces. Keep the procedural window material, but do not evaluate illumination
in the geometry pass. The supplied windowMask() identifies the glass
on building facades; an instance flag prevents windows from appearing on lamp posts
or other scene geometry. Only these glass pixels receive specular highlights.
Task 2: Illuminate the G-Buffer
Change compositeFragmentShader to sample the G-buffer and compute
diffuse and Phong specular illumination. Decode and normalize the stored normal.
Use the world-space position and the supplied cameraPosition to obtain
the viewing direction.
Reuse the supplied cityLightingShader string from Lighting.h
in the compositing fragment shader and call illuminate() with the decoded
attributes and reconstructed shininess. Remove it from the geometry fragment shader.
The application already supplies the same 400 lamp positions and color selections through
lampBuffer, a one-row RGBA32F data texture bound to texture unit 4, to the lighting
pass in both variants. Preserve the lighting calculation so the starter and solution
produce the same final image. Pixels without a surface should retain the original
background color. All positions and directions must use world coordinates.
Use G to inspect all four buffers together in a 2×2 view. Initially all four views are identical;
after completing the tasks they should show distinct material, normal, and position
information. Keep the camera and framebuffer setup unchanged.
Reference Solution
G: toggle the 2×2 G-buffer view. Top left: diffuse; top right: specular; bottom left: normals; bottom right: positions. R: reset camera. W/S: forward/backward; A/D: strafe; left mouse drag: look around; mouse wheel: move forward/backward.
Text description and demo controls
A city of colored buildings is rendered in two stages. First, visible surfaces write unlit material colors, specular properties, normals, and positions into four images called the G-buffer. A fullscreen pass reads these images and computes illumination from 400 local street lamps, including diffuse reflection, highlights on the windows, and distance attenuation. Building walls remain matte and diffusely lit.
The diagnostic mode shows four images at once: diffuse color at the top left, specular reflectance at the top right, encoded normals at the bottom left, and a scaled visualization of world-space positions at the bottom right. Lighting is applied only in the final-image mode.
Focus the demo with Tab. G toggles the G-buffer view; R resets the camera. Hold W/S to fly along the viewing direction and A/D to move sideways. Looking around requires left mouse dragging; the mouse wheel also moves forward or backward.