Preparing a perspective projection matrix
3D applications usually try to simulate the effect of how we perceive the world around us--objects in the distance seem smaller than the objects that are closer to us. To achieve this effect, we need to use a perspective projection matrix.
How to do it...
- Prepare a variable of type
float
namedaspect_ratio
that will hold an aspect ratio of a renderable area (image's width divided by its height). - Create a variable of type
float
namedfield_of_view
. Initialize it with an angle (in radians) of a vertical field of view of a camera. - Create a variable of type
float
namednear_plane
and initialize it with the distance from the camera's position to the near clipping plane. - Create a variable of type
float
namedfar_plane
. Store the distance between a camera and the far clipping plane in the variable. - Calculate a value of
1.0f
divided by a tangent of the half of thefield_of_view
(1.0f / tan(Deg2Rad(0.5f * field_of_view))
) and store the result in a variable of type...