You define a clipping volume inside the coordinate system of the viewport. The best way to define a clipping volume in Direct3D is by using the D3DVIEWPORT2 structure—the structure that is specified by the methods in IDirect3DViewport3. (Another way is by using the D3DVIEWPORT structure. The D3DVIEWPORT2 structure enables a better clip-volume definition than D3DVIEWPORT and is recommended for all DirectX 5.0 and newer applications.)
The dvClipX, dvClipY, dvClipWidth, and dvClipHeight members of the D3DVIEWPORT2 structure specify the region inside of which vertices will be visible. This region corresponds to the destination rectangle, specified by the dwX, dwY, dwWidth, and dwHeight members of D3DVIEWPORT2.
Direct3D uses the values you set in the D3DVIEWPORT2 structure to construct a matrix that it internally applies to all vertices before performing clipping tests. The matrix looks like this:
In the preceding matrix all variables are taken directly from the D3DVIEWPORT2 structure: Cx and Cy are the values in the dvClipX and dvClipY members, Cw and Ch are values in dvClipWidth and dvClipHeight, and the Zmin and Zmax variables are values taken from the dvMinZ and dvMaxZ members. The matrix effectively scales vertices according to the proportions of the clipping volume you define and translates them to position them around the volume's origin.
The values produced by the clipping matrix are tested by using the following formulas, and any vertices that fail the tests are clipped:
In the preceding formulas, Xc, Yc, Zc, and Wc represent the vertex coordinates after the clipping matrix is applied.
In most cases, you will define a clipping volume that extends from -1.0 to 1.0 in the x- and y-directions, and 0.0 to 1.0 in the z-direction, and doesn't scale vertices. The significant D3DVIEWPORT2 structure settings for such a volume are as follows:
dvClipX = -1.0;
dvClipY = 1.0;
dvClipWidth = 2.0;
dvClipHeight = 2.0;
dvMinZ = 0.0;
dvMaxZ = 1.0;
It is important that neither dvClipWidth nor dvClipHeight be zero. Also, dvMinZ cannot equal dvMaxZ.
This clipping volume corresponds to the destination rectangle, as shown in the following illustration.
The values you specify for the dwX, dwY, dwWidth, and dwHeight members of the D3DVIEWPORT2 structure are screen coordinates relative to the upper-left corner of the render target surface.