World Transformation

This 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 of 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.

World 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 C# code example, from a fictitious 3-D model class written in C#, creates a world matrix that includes three rotations to orient a model and a translation to relocate it relative to its position in world space.

          [C#]
          

public class ModelClass
{
   private float xPos=0;
   private float yPos=0;
   private float zPos=0;

   private float Pitch=0;
   private float Yaw=0;
   private float Roll=0;

   //...Other model properties and methods
   
   public Matrix MakeWorldMatrix(Matrix worldMatrix)
   {
      worldMatrix.Translate(xPos,yPos,zPos);

      Matrix matRot = Matrix.Identity;

      matRot.RotateYawPitchRoll(Yaw,Pitch,Roll);
      
      worldMatrix = Matrix.Multiply(matRot, worldMatrix);

      return worldMatrix;

   }
}

After you prepare the world transformation matrix, call the SetTransform method to set it, specifying the TransformType.World value for the first parameter. Alternately, you can pass the world transformation matrix to the Device.Transform.World property.

Note: 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 time-consuming. 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, Direct3D samples in this documentation rarely employ this optimization.