Setting the Viewport Clipping Volume

After you add a viewport to a device, you can set the viewport's clipping volume. To do this, you initialize and set clipping values for the clipping volume and for the render target surface. Viewports are commonly set up to render to the full surface and to compensate for the aspect ratio. You could use the following settings for the members of the D3DVIEWPORT2 structure to achieve this:

    memset(&viewData, 0, sizeof(D3DVIEWPORT2));
    viewData.dwSize = sizeof(D3DVIEWPORT2);
    viewData.dwX = 0;
    viewData.dwY = 0;
    viewData.dwWidth  = width;
    viewData.dwHeight = height;
    viewData.dvClipX  = -1.0f;
    viewData.dvClipY  =  1.0;
    viewData.dvClipWidth  = 2.0f;
    viewData.dvClipHeight = 2.0f;
    viewData.dvMinZ = 0.0f;
    viewData.dvMaxZ = 1.0f;
 

After setting values in the D3DVIEWPORT2 structure, you apply the structure to the viewport object by calling its IDirect3DViewport3::SetViewport2 method. The following examples shows what this call might look like:

    HRESULT hr;
 
    hr = lpD3DViewport3->SetViewport2(&viewData);
    if(FAILED(hr))
        return hr;
 

If the call succeeds, you have a working viewport. If you need to make changes to the viewport values, simply update the values in the D3DVIEWPORT2 structure and call SetViewport2 again.

The IDirect3DViewport3 interface has two ways of specifying a viewport clipping volume. For more information, see Clipping Volumes.