Microsoft DirectX 8.1 (Visual Basic)

World Transformation

The discussion of the world transformation introduces basic concepts and provides details on how to set up a world transformation matrix in a Microsoft® Direct3D® application.

What Is a World Transformation?

A world transformation changes coordinates from model space, where vertices are defined relative to a model's local origin, to world space, where vertices are defined relative to an origin common to all the objects in a scene. In essence, the world transformation places a model into the world; hence its name. The following diagram illustrates the relationship between the world coordinate system and a model's local coordinate system.

The world transformation can include any combination of translations, rotations, and scalings. For a discussion of the mathematics of transformations, see 3-D Transformations.

Setting Up a World Matrix

As with any other transformation, you create the world transformation by concatenating a series of transformation matrices into a single matrix that contains the sum total of their effects. In the simplest case, when a model is at the world origin and its local coordinate axes are oriented the same as world space, the world matrix is the identity matrix. More commonly, the world matrix is a combination of a translation into world space and possibly one or more rotations to turn the model as needed.

The following example, from a fictitious 3-D model class written in Microsoft ® Visual Basic® uses the helper functions included in the Direct3DX utility library to create a world matrix that includes three rotations to orient a model and a translation to relocate it relative to its position in world space.

'
' For the purposes of this example, the following variables
' are assumed to be valid and initialized.
'
' The m_xPos, m_yPos, m_zPos variables contain the model's
' location in world coordinates.
'
' The m_sPitch, m_sYaw, and m_sRoll variables are Singles that
' contain the model's orientation in terms of pitch, yaw, and roll
' angles, in radians.
'
Public Function MakeWorldMatrix() As D3DMATRIX
    Dim matWorld As D3DMATRIX   ' World matrix to return.
    Dim matTemp As D3DMATRIX    ' Temp matrix to hold rotations.
    Dim matRot As D3DMATRIX     ' Temp rotation matrix.
 
    ' Modify matWorld to create a translation matrix.
    D3DXMatrixIdentity matWorld
    matWorld.m41 = m_xPos
    matWorld.m42 = m_yPos
    matWorld.m43 = m_zPos
    
    D3DXMatrixIdentity matRot ' Sets up the rotation matrix.
    
    '
    ' Using the left-to-right order of matrix concatenation,
    ' apply the translation to the object's world position
    ' before applying the rotations.
    '
    
    ' Produce and combine the rotation matrices.
    If (m_sPitch <> 0) Or (m_sYaw <> 0) Or (m_sRoll <> 0) Then
       ' First, pitch.
        D3DXMatrixRotationX matTemp, m_sPitch
        D3DXMatrixMultiply matRot, matRot, matTemp

       ' Then, yaw.
        D3DXMatrixRotationY matTemp, m_sYaw
        D3DXMatrixMultiply matRot, matRot, matTemp

        ' Finally, roll.
        D3DXMatrixRotationZ matTemp, m_sRoll
        D3DXMatrixMultiply matRot, matRot, matTemp
 
        ' Apply the rotation matrices to the translation already in
        ' matWorld to complete the world matrix.
        D3DXMatrixMultiply matWorld, matRot, matWorld
    End If
    
    MakeWorldMatrix = matWorld
End Function

After you prepare the world transformation matrix, call the Direct3DDevice8.SetTransform method to set it, specifying the D3DTS_WORLD flag in the first parameter. For more information, see Setting Transformations.

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, thousands of 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 so that you can modify, concatenate, and reset the world matrix as needed. For clarity, in this documentation Direct3D samples rarely employ this optimization.