Platform SDK: DirectX

Step 4: Enable Depth Buffering

[C++]

This section pertains only to application development in Visual Basic. See Direct3D Immediate Mode C/C++ Tutorials.

[Visual Basic]

After attaching the depth buffer to the render-target surface, you can create a rendering device from the render target.

If your application uses a depth buffer you will need to clear both the viewport and the depth buffer. To clear the depth buffer, specify the D3DCLEAR_ZBUFFER flag in the Direct3DDevice7.Clear method.

    g_d3dDevice.Clear 1, g_d3drcViewport(), D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, &HFF, 1, 0

Given a rendering device, you enable depth buffering by setting the D3DRENDERSTATE_ZENABLE render state for the device. The CONST_D3DZBUFFERTYPE enumerated type includes members to set the depth-buffering render state. The D3DZB_TRUE member (or True), enables z-buffering. The ZBuffer tutorial application enables z-buffering during scene rendering in the RenderScene application-defined subroutine. The following is the appropriate excerpt from RenderScene:

    g_d3dDevice.SetRenderState D3DRENDERSTATE_ZENABLE, D3DZB_TRUE

Although this tutorial enables depth-buffering for each frame, it is not necessary to do so. A real-world application would likely set the D3DRENDERSTATE_ZENABLE render state during scene initialization, only changing to disable depth buffering or to choose another type of depth buffering.

Note  The CONST_D3DZBUFFERTYPE enumerated type includes the D3DZB_USEW value to enable w-based depth comparisons on compliant hardware. For more information, see Depth Buffers.