Platform SDK: DirectX

Clearing a Viewport

Clearing the viewport resets the contents of the viewport rectangle on the render-target surface as well as the rectangle in the depth and stencil buffer surfaces (if specified). Typically, you will clear the viewport before rendering a new frame to ensure that graphics and other data is ready to accept new rendered objects without displaying artifacts.

[C++]

The IDirect3DDevice7 interface offers C++ developers the IDirect3DDevice7::Clear method to clear the viewport. The method accepts one or more rectangles that define the area or areas on the surfaces being cleared. In cases where the scene being rendered includes motion throughout the entire viewport rectangle—in a first-person perspective game, perhaps—you might want to clear the entire viewport each frame. In this situation, you would set the dwCount parameter to 1, and the lpRects parameter to the address of a single rectangle that covers the entire viewport area. If you find it more convenient, you can set the lpRects parameter to NULL and the dwCount parameter to 0 to indicate that the entire viewport rectangle should be cleared.

The Clear method is flexible, and provides support for clearing stencil bits within a depth buffer. The dwFlags parameter accepts three flags that determine how it clears the render target and any associated depth or stencil buffers. If you include the D3DCLEAR_TARGET flag, the method clears the viewport using an arbitrary RGBA color that you provide in the dwColor parameter (not the material color). If you include the D3DCLEAR_ZBUFFER flag, the method clears the depth buffer to an arbitrary depth you specify in dvZ: 0.0 is the closest distance, and 1.0 is the farthest. Including the D3DCLEAR_STENCIL flag causes the method to reset the stencil bits to the value you provide in the dwStencil parameter. You can use integers that range from 0 to 2n-1, where n is the stencil buffer bit depth.

Note  DirectX 5.0 allowed background materials to have associated textures, making it possible to clear the viewport to a texture, rather than a simple color. This feature was little used, and not particularly efficient. Interfaces for DirectX 6.0 and later do not accept texture handles, meaning that you can no longer clear the viewport to a texture. Rather, applications must now draw backgrounds manually. As a result, there is rarely a need to clear the viewport on the render-target surface. So long as your application clears the depth buffer, all pixels on the render-target surface will be overwritten anyway.

In some situations, you might only be rendering to small portions of the render target and depth buffer surfaces. The clear methods also allow you to clear multiple areas of your surfaces in a single call. Do this by setting the dwCount parameter to the number of rectangles you want cleared, and specify the address of the first rectangle in an array of rectangles in the lpRects parameter.

[Visual Basic]

The Direct3DDevice7 Visual Basic class offers the Direct3DDevice7.Clear method to clear the viewport. The method accepts one or more rectangles that define the area or areas on the surfaces being cleared. In cases where the scene being rendered includes motion throughout the entire viewport rectangle—in a first-person perspective game, perhaps—you might want to clear the entire viewport each frame. In this situation, you would set the count parameter to 1, and the recs() parameter to a single-element array of D3DRECT variables (where the first and only element describes a rectangle that covers the entire area of the render-target surface).

The Clear method is flexible, and provides support for clearing stencil bits within a depth buffer. The flags parameter accepts three flags that determine how it clears the render target and any associated depth or stencil buffers. If you include the D3DCLEAR_TARGET flag, the method clears the viewport using an arbitrary RGBA color that you provide in the color parameter (not the material color). If you include the D3DCLEAR_ZBUFFER flag, the method clears the depth buffer to an arbitrary depth you specify in z: 0.0 is the closest distance, and 1.0 is the farthest. Including the D3DCLEAR_STENCIL flag causes the method to reset the stencil bits to the value you provide in the stencil parameter. You can use integers that range from 0 to 2n-1, where n is the stencil buffer bit depth.

In some situations, you might only be rendering to small portions of the render target and depth buffer surfaces. The clear methods also allow you to clear multiple areas of your surfaces in a single call. Do this by setting the count parameter to the number of rectangles you want cleared, and an array of rectangles in the recs() parameter.