Platform SDK: DirectX

Tutorial 2: Adding a Depth Buffer

[Visual Basic]

The information in this section pertains only to applications written in C and C++. See Direct3D Immediate Mode Visual Basic Tutorials.

[C++]

Direct3D applications often rely on depth buffers to properly display objects in a scene. For a conceptual overview, see Depth Buffers. To use depth buffering, you must enumerate supported depth-buffer formats, create a depth-buffer surface, attach it to a render-target surface, and enable depth buffering for the rendering device.

This tutorial parallels the code in the Triangle sample project which uses the most commonly supported type of depth buffer, a z-buffer. The ZBuffer sample performs the following steps to use a z-buffer:

Note  The code in the ZBuffer sample is nearly identical to the code in Triangle. This tutorial focuses only on the depth-buffer code unique to ZBuffer, and does not cover setting up Direct3D, rendering, shutting down, or handling Windows messages. For information on these tasks, see Tutorial 1: Rendering a Single Triangle.

Because some rendering devices require depth buffers to be located in particular places in memory, the system requires that you create and attach the depth-buffer surface to the render-target surface before you create a rendering device.