Model View Projection Matrix
The Model-View-Projection (MVP) matrix encodes the transformation pipeline of every vertex in a 3D scene. More specifically, the MVP matrix consists of three separate 4x4 matrices that each perform their own specific transformation.
Model Matrix
This matrix transforms model-local object coordinates to world coordinates. It is used to place, rotate and scale e.g. a mesh in the 3D world.
Output of this transformation is in world space. The input is in local space.
C code to specify a model matrix:
// column-major (used by opengl) float view_matrix[16] = { };
View Matrix
This matrix transforms world coordinates (output by the model matrix) to camera (or view) coordinates.
It does this by positioning and orienting the camera in the world. Technically, the entire scene is moved and rotated around the camera to create the illusion of a moving observer.
Output of this transformation is in eye space. In this space, the xyz axes line up to the camera's orientation.
C code to specify a view matrix:
// column-major (used by opengl) float view_matrix[16] = { };
Projection Matrix
This matrix defines how the scene is projected onto the 2D screen.
More specifically, the projection matrix is used to transform the camera coordinates (output by the view matrix) to normalized device coordinates (NDC). These coordinates are then transformed to the viewport space, where the z component is used to sort. However, these last two transformation are usually performed in a fixed part of the rendering API.
The used projection is usually either an orthographic (objects appear to be the same size regardless of how far away they are) or perspective projection (far-away objects appear smaller).
The projection matrix also contains…
- aspect ratio of the viewport
- the near and far clipping planes of the camera frustum
C code to specify an orthographic projection matrix:
float left = 0.0f; float right = SCREEN_WIDTH; float bottom = 0.0f; float top = SCREEN_HEIGHT; float near = -1.0f; float far = 1.0f; // column-major (used by opengl) float orthographic_projection_matrix[16] = { 2.0f / (right - left), 0.0f, 0.0f, 0.0f, 0.0f, 2.0f / (top - bottom), 0.0f, 0.0f, 0.0f, 0.0f, -2.0f / (far - near), 0.0f, -(right + left) / (right - left), -(top + bottom) / (top - bottom), -(far + near) / (far - near), 1.0f };
C code to specify a perspective projection matrix:
// column-major (used by opengl) float perspective_projection_matrix[16] = { };