What is Projection Transformation?

The projection transformation converts the viewing frustum into a cuboid shape. The near end of the viewing frustum is smaller than the far end, which has the effect of expanding objects that are near to the camera. The viewing matrix translates the camera to the origin by translating in the z-direction by -D.
It is the process of converting a 3D object into a 2D object. It is also defined as mapping or transformation of the object in projection plane or view plane. The view plane is displayed surface.

In the viewing frustum, the distance between the camera and the origin of the viewing transformation space is defined arbitrarily as D, so the projection matrix looks like the following illustration.

Projection matrix 1

The viewing matrix translates the camera to the origin by translating in the z-direction by -D. The translation matrix is as follows.

Projection matrix 2

Multiplying the translation matrix by the projection matrix (T*P) gives the composite projection matrix. It looks like the following illustration.

Projection matrix 3

The following illustration shows how the perspective transformation converts a viewing frustum to a new coordinate space. Notice that the frustum becomes cuboid and also that the origin moves from the upper-right corner of the scene to the center.

Cuboid

In the perspective transformation, the limits of the x- and y-directions are -1 and 1. The limits of the z-direction are 0 for the front plane and 1 for the back plane.

This matrix translates and scales objects based on a specified distance from the camera to the near clipping plane, but it does not consider the field of view (fov), and the z-values that it produces for objects in the distance can be nearly identical, making depth comparisons difficult. The following matrix addresses these issues, and it adjusts vertices to account for the aspect ratio of the viewport, making it a good choice for the perspective projection.

Projection matrix X1

In the following matrix, Zn is the z-value of the near clipping plane. The variables w, h, and Q have the following meanings. Note that fovw and fovk represent the viewport’s horizontal and vertical fields of view, in radians.

Projection matrix X2

For your application, using field-of-view angles to define the x- and y-scaling coefficients might not be as convenient as using the viewport’s horizontal and vertical dimensions (in camera space). As the math works out, the following two formulas for w and h use the viewport’s dimensions, and are equivalent to the preceding formulas.

Projection matrix X3

In these formulas, Zn represents the position of the near clipping plane, and the Vw and Vh variables represent the width and height of the viewport, in camera space.

For a managed application, these two dimensions correspond directly to the Width and Height members of the Viewport structure.

Whatever formula you decide to use, be sure to set Z to as large a value as possible, because z-values extremely close to the camera do not vary by much. This makes depth comparisons using 16-bit z-buffers somewhat complicated.