Platform SDK: DirectX

Setting the Viewport Clipping Volume

The only thing required to configure the viewport parameters for a rendering device is to 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 area of the render-target surface, but this isn't a requirement.

[C++]

You could use the following settings for the members of the D3DVIEWPORT7 structure to achieve this from C++.

    memset(&viewData, 0, sizeof(D3DVIEWPORT7));
    viewData.dwX = 0;
    viewData.dwY = 0;
    viewData.dwWidth  = width;
    viewData.dwHeight = height;
    viewData.dvMinZ = 0.0f;     
    viewData.dvMaxZ = 1.0f;     

After setting values in the D3DVIEWPORT7 structure, simply apply the viewport parameters to the device by calling its IDirect3DDevice7::SetViewport method. The following examples shows what this call might look like.

    HRESULT hr;
 
    hr = lpD3DDevice->SetViewport(&viewData);
    if(FAILED(hr))
return hr;

If the call succeeds, the viewport parameters are set and will take effect the next time a rendering method is called. If you need to make changes to the viewport parameters, just update the values in the D3DVIEWPORT7 structure and call SetViewport again.

Note  The D3DVIEWPORT7 structure members dvMinZ and dvMaxZ are interpreted in a manner completely different than in previous versions of DirectX. The dvMinZ and dvMaxZ members now indicate the depth-ranges into which the scene will be rendered, and are not used for clipping. Most applications will set these members to 0.0 and 1.0 to enable the system to render to the entire range of depth values in the depth buffer. In some cases, you can achieve special effects by using other depth ranges. For instance, if you wanted to render a heads-up display in a game, you could set both values to 0.0 to force the system to render objects in a scene in the foreground, or you might set them both to 1.0 to render an object that should always be in the background.

[Visual Basic]

The following Visual Basic code creates settings in a D3DVIEWPORT7 type to achieve this.

Dim viewData As D3DVIEWPORT7

With viewData
    .lX = 0: .lY = 0
    .lWidth = Width: .lHeight = Height
    .minz = 0#: .maxz = 1#
End With

After setting values in the D3DVIEWPORT7 type, simply apply the viewport parameters to the device by calling its Direct3DDevice7.SetViewport method.

' The dev variable contains a valid reference to a Direct3DDevice7 object.
Call dev.SetViewport(viewData) 

After the call, the viewport parameters are set and will take effect the next time a rendering method is called. If you need to make changes to the viewport parameters, just update the values in the D3DVIEWPORT7 type and call SetViewport again.