The Projection matrix
On a physical camera, you might have a selection of various lenses with different properties – a wide angled lens for wide shots, or a telephoto lens for zooming in close. The Projection
matrix is the way we describe such properties to the XNA rendering system. The Projection
matrix describes to the graphics card how to translate (or project) 3D objects onto the 2D viewing area of the screen. The following image illustrates the parameters used to construct the Projection
matrix:
The Matrix.CreatePerspectiveFieldOfView()
method accepts four parameters that define how our virtual camera will view the 3D scene. The first is the field of view, or viewing angle, that the camera covers. The larger this value is, the wider the angle the camera will display. In this case, we specify MathHelper.PiOver4
, which translates to a 45 degree angle.
A 45 degree field of view is a fairly standard value for 3D games, and represents a realistic view angle. As the angle gets larger, you will begin to notice a fish-eye effect and the rotation of the camera will seem more and more unusual. In fact, some games have used an expanded field of view to distort the player's perspective when their character becomes disoriented or incapacitated in some way. The following image shows the impact of changing only the field of view angle while viewing the same scene:
In all three of the previous images, the player is standing in exactly the same spot in identical mazes. The only difference between each image is the field of view angle specified in the Projection
matrix. As the viewing angle increases, objects become stretched out and distorted the further they are away from the center of the viewing area.
The second parameter, the aspect ratio, determines the shape of the viewing area. The most familiar use of aspect ratios in daily life is in televisions and monitors. A standard-definition television has a 4:3 (or 4 divided by 3, or 1.333) aspect ratio, while high-definition displays often use a 16:9 (1.777) or 16:10 (1.6) aspect ratio. Generally, the aspect ratio of your Projection
matrix should match the aspect ratio of the viewport you are displaying your graphics in (in fact, when we define an instance of our camera, we will simply pass the viewport's aspect ratio property in for this parameter). Using a mismatched value will cause your image to be either stretched out or squashed together. A similar effect can be seen in films, when the ending credits of a wide-screen movie are scaled to display on a standard-definition television. The text looks correct while the actors and scenes in the background become stretched vertically, appearing tall and thin.
The last two parameters define clipping planes associated with the Projection
matrix. A clipping plane defines the point, past which objects in the 3D world will no longer be displayed. Any 3D geometry closer than the near clipping distance will not be drawn to the display, nor will any geometry further away than the far clipping distance. In other words, the only things in our game world that will be drawn to the screen are the items which lie further away than the near distance, but closer than the far distance.
We need the far clipping plane to place reasonable limits on the objects we draw for performance purposes. Some games allow the player to modify the drawing distance for the far clipping plane based on the graphical horsepower of their system.
The reason for the near clipping plane is less obvious, but still important. Let's say, for example, that we are creating a multi-player, first-person shooter style game. In this case, our player is represented to other players by a 3D avatar that exists in the game world just like other players, enemies, and objects. If we define the location of the camera near the avatar's eyes, the movement and animation of the avatar may cause parts of the avatar's model to push their way in front of the camera position. By specifying a near clipping plane, we can prevent these pieces of the player's own avatar from being drawn so that the player's view is not obscured by the inside of their own head!