World Transform

The world transform changes coordinates from model space to world space. This can include any combination of translations, rotations, and scalings. For a discussion of the mathematics of transformations, see 3-D Transformations.

You can create a translation using code like this. Notice that here (and in the other transformation samples) the D3D_OVERLOADS form of D3DMATRIX is being used.

D3DMATRIX Translate(const float dx, const float dy, const float dz)
{
    D3DMATRIX ret = IdentityMatrix();
    ret(3, 0) = dx;
    ret(3, 1) = dy;
    ret(3, 2) = dz;
    return ret;
}    // end of Translate()
 

You can create a rotation around an axis using code like this:

D3DMATRIX RotateX(const float rads)
{
    float    cosine, sine;
 
    cosine = cos(rads);
    sine = sin(rads);
    D3DMATRIX ret = IdentityMatrix();
    ret(1,1) = cosine;
    ret(2,2) = cosine;
    ret(1,2) = -sine;
    ret(2,1) = sine;
    return ret;
}   // end of RotateX()
 
D3DMATRIX RotateY(const float rads)
{
    float    cosine, sine;
 
    cosine = cos(rads);
    sine = sin(rads);
    D3DMATRIX ret = IdentityMatrix();
    ret(0,0) = cosine;
    ret(2,2) = cosine;
    ret(0,2) = sine;
    ret(2,0) = -sine;
    return ret;
}   // end of RotateY()
 
D3DMATRIX RotateZ(const float rads)
{
    float    cosine, sine;
 
    cosine = cos(rads);
    sine = sin(rads);
    D3DMATRIX ret = IdentityMatrix();
    ret(0,0) = cosine;
    ret(1,1) = cosine;
    ret(0,1) = -sine;
    ret(1,0) = sine;
    return ret;
}   // end of RotateZ()
 

You can create a scale transform using code like this:

D3DMATRIX Scale(const float size)
{
D3DMATRIX ret = IdentityMatrix();
    ret(0, 0) = size;
    ret(1, 1) = size;
    ret(2, 2) = size;
    return ret;
}   // end of Scale()
 

These basic transformations can be combined to create the final transform. Remember that when you combine them the results are not commutative — the order in which you multiply matrices is important.