Microsoft DirectX 8.0 (Visual Basic)

Setting Up a View Matrix

Microsoft® Visual Basic® applications use the D3DXMatrixLookAtLH and D3DXMatrixLookAtRH helper functions to create a view matrix based on the camera location, a look-at point, and an up vector (usually 0,1,0).

The following example code, written in Visual Basic, shows how you can use the D3DXMatrixLookAtLH function.

    ' This example assumes that g_D3DDevice is a valid reference 
    ' to a Direct3DDevice8 object. 

    Dim matView As D3DMATRIX

    ' Set the eyepoint five units back along the z-axis 
    ' and up three units.
    D3DXMatrixLookAtLH matView, vec3(0#, 3#, -5#), _
                                 vec3(0#, 0#, 0#), _
                                 vec3(0#, 1#, 0#)
                                 
    g_D3DDevice.SetTransform D3DTS_VIEW, matView

As with the world transformation, you call the Direct3DDevice8.SetTransform method to set the view transformation, specifying the D3DTS_VIEW flag in the first parameter. See Setting Transformations, for more information.

Performance Optimization Note  Microsoft® Direct3D® uses the world and view matrices that you set to configure several internal data structures. Each time you set a new world or view matrix, the system recalculates the associated internal structures. Setting these matrices frequently—for example, 20,000 times per frame—is computationally expensive. You can minimize the number of required calculations by concatenating your world and view matrices into a world-view matrix that you set as the world matrix, and then setting the view matrix to the identity. Keep cached copies of individual world and view matrices that you can modify, concatenate, and reset the world matrix as needed. For clarity, Direct3D samples rarely employ this optimization.