Sheet 08: Occlusion Culling

Exercise: Hierarchical Z-Buffer

The scene is a city of buildings assembled from colored bricks, arranged in 4×4 street blocks with raised sidewalks and broad roads between them. Frustum culling alone cannot remove buildings hidden behind other buildings. In this exercise, implement the two functions in HZB.h to construct a hierarchical Z-buffer (HZB) and test projected brick bounds against it.

The rendering, camera, and conservative projection of world-space AABBs into pixel rectangles are provided. At the start of each frame, the previous frame's winners are rendered into a new depth buffer using the current camera. The first frame uses all bricks. After building the HZB, test every frustum candidate, including previous losers, and use the new winners for the main image and the next frame's depth pass.

The teaching implementation reads the depth pass back to the CPU so both tasks work on ordinary arrays. It uses standard OpenGL depth: zero is near, one is far, and uncovered pixels contain one. The HZB has the same base resolution as the camera image. This makes the algorithm easy to inspect, but GPU readback and CPU processing are not intended as a production performance benchmark.

Do not expect a speedup in this demo: the buildings consist of very simple box geometry and use inexpensive shading. Rendering a brick is therefore cheap compared with the depth prepass, GPU readback, pyramid construction, and visibility tests. In a real scene, objects can have far more complex geometry and much more expensive materials. Avoiding those draw calls can save substantially more work and outweigh the culling overhead, depending on how much of the scene is hidden.

Task 1: Build the Depth Pyramid

Implement buildHZB. Keep the input depth buffer as level zero, then reduce each 2×2 block into one texel. With this depth convention, each parent must store the maximum of its children. Continue until the last level is 1×1.

Use ceiling division for odd widths and heights. Include every valid child at the image edges. A background pixel with depth one must not disappear during reduction: otherwise a hole between buildings could incorrectly become an occluder.

Task 2: Test a Brick Against the HZB

Implement isVisible. The supplied ScreenBounds contains an inclusive pixel rectangle and the nearest possible depth of the brick. Choose a mip level at which the rectangle overlaps at most 2×2 texels. Query every overlapped texel and calculate their maximum depth.

Return false only when the brick's nearest depth is greater than that maximum plus depthBias. Equality must remain visible, including when testing an occluder against its own depth. Keep near-plane-crossing boxes and boxes tested against an empty HZB visible. False positives are acceptable; false negatives remove geometry that should appear.

Reference Solution

Loading

W/S: fly forward/backward; A/D: strafe. Drag with the left mouse button to look around; the mouse wheel moves forward/backward. C: toggle occlusion culling; O: toggle overview; R: reset camera and visibility history.

Text description and demo controls

The camera looks down marked streets between 4 by 4 building blocks on raised sidewalks. The buildings are made from stacked, differently colored rectangular bricks. Nearby buildings can hide bricks farther down the street. The main view draws the selected bricks in their material colors. The upper-left overview always draws all buildings. A building is red if at least one of its bricks was selected for rendering, otherwise white. Yellow lines show the camera frustum projected onto the ground.

The overview colors indicate conservative rendering decisions, not exact per-pixel visibility. A building can remain red even if the test could not prove that it was hidden. Disabling occlusion culling leaves the provided frustum rejection active.

Focus the demo with Tab. Hold W or S to fly forward or backward along the viewing direction; A and D move sideways. C toggles occlusion culling. O shows or hides the overview, which is initially visible. R resets the camera and seeds the next depth pass with all bricks. Changing the viewing direction requires dragging with the left mouse button. The mouse wheel also moves forward or backward.