Setting Up a Projection Matrix

The following ProjectionMatrix sample function takes four input parameters that set the front and back clipping planes, as well as the horizontal and vertical field of view angles. (This code parallels the approach discussed in What Is the Projection Transformation?.) The fields-of-view should be less than pi radians.

D3DMATRIX 
ProjectionMatrix(const float near_plane,
                             // distance to near clipping plane
                 const float far_plane,
                             // distance to far clipping plane
                 const float fov_horiz,
                             // horizontal field of view angle, in radians
                 const float fov_vert)
                             // vertical field of view angle, in radians
{
    float    h, w, Q;
 
    w = (float)cot(fov_horiz*0.5);
    h = (float)cot(fov_vert*0.5);
    Q = far_plane/(far_plane - near_plane);
 
    D3DMATRIX ret = ZeroMatrix();
    ret(0, 0) = w;
    ret(1, 1) = h;
    ret(2, 2) = Q;
    ret(3, 2) = -Q*near_plane;
    ret(2, 3) = 1;
    return ret;
}   // end of ProjectionMatrix()
 

When you have created the matrix, you need set it in a call to the IDirect3DDevice3::SetTransform method, specifying D3DTRANSFORMSTATE_PROJECTION in the first parameter. For details, see Setting Transformations.