?/TD> |
Microsoft DirectX 9.0 |
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.
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.
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 C++, uses the helper functions included in the Direct3D extensions (D3DX) 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_fPitch, m_fYaw, and m_fRoll variables are floats that * contain the model's orientation in terms of pitch, yaw, and roll * angles, in radians. */ void C3DModel::MakeWorldMatrix( D3DXMATRIX* pMatWorld ) { D3DXMATRIX MatTemp; // Temp matrix for rotations. D3DXMATRIX MatRot; // Final rotation matrix, applied to // pMatWorld. // Using the left-to-right order of matrix concatenation, // apply the translation to the object's world position // before applying the rotations. D3DXMatrixTranslation(pMatWorld, m_xPos, m_yPos, m_zPos); D3DXMatrixIdentity(&MatRot); // Now, apply the orientation variables to the world matrix if(m_fPitch || m_fYaw || m_fRoll) { // Produce and combine the rotation matrices. D3DXMatrixRotationX(&MatTemp, m_fPitch); // Pitch D3DXMatrixMultiply(&MatRot, &MatRot, &MatTemp); D3DXMatrixRotationY(&MatTemp, m_fYaw); // Yaw D3DXMatrixMultiply(&MatRot, &MatRot, &MatTemp); D3DXMatrixRotationZ(&MatTemp, m_fRoll); // Roll D3DXMatrixMultiply(&MatRot, &MatRot, &MatTemp); // Apply the rotation matrices to complete the world matrix. D3DXMatrixMultiply(pMatWorld, &MatRot, pMatWorld); } }
After you prepare the world transformation matrix, call the IDirect3DDevice9::SetTransform method to set it, specifying the D3DTS_WORLD macro for the first parameter.