# OpenGL Projection Matrix

Related Topics: OpenGL Transformation

The mathematical expressions in this page are re-written by Jiergir Ogoerg using MathML. This page is intended to promote MathML for better presentation of mathematical notation in HTML page, and to encourage the web browsers for better MathML support.

### Overview

A computer monitor is a 2D surface. A 3D scene rendered by OpenGL must be projected onto the computer screen as a 2D image. GL_PROJECTION matrix is used for this projection transformation. First, it transforms all vertex data from the eye coordinates to the clip coordinates. Then, these clip coordinates are also transformed to the normalized device coordinates (NDC) by dividing with w component of the clip coordinates.

A triangle clipped by frustum

Therefore, we have to keep in mind that both clipping (frustum culling) and NDC transformations are integrated into GL_PROJECTION matrix. The following sections describe how to build the projection matrix from 6 parameters; left, right, bottom, top, near and far boundary values.

Note that the frustum culling (clipping) is performed in the clip coordinates, just before dividing by wc. The clip coordinates, xc, yc and zc are tested by comparing with wc. If each clip coordinate is less than -wc, or greater than wc, then the vertex will be discarded. Then, OpenGL will reconstruct the edges of the polygon where clipping occurs.

### Perspective Projection

Perspective Frustum and Normalized Device Coordinates (NDC)

In perspective projection, a 3D point in a truncated pyramid frustum (eye coordinates) is mapped to a cube (NDC); the range of x-coordinate from [l, r] to [-1, 1], the y-coordinate from [b, t] to [-1, 1] and the z-coordinate from [n, f] to [-1, 1].

Note that the eye coordinates are defined in the right-handed coordinate system, but NDC uses the left-handed coordinate system. That is, the camera at the origin is looking along -Z axis in eye space, but it is looking along +Z axis in NDC. Since glFrustum() accepts only positive values of near and far distances, we need to negate them during the construction of GL_PROJECTION matrix.

In OpenGL, a 3D point in eye space is projected onto the near plane (projection plane). The following diagrams show how a point (xe, ye, ze) in eye space is projected to (xp, yp, zp) on the near plane.

Top View of Frustum

Side View of Frustum

From the top view of the frustum, the x-coordinate of eye space, xe is mapped to xp, which is calculated by using the ratio of similar triangles;
$\frac{{x}_{p}}{{x}_{e}}=\frac{\mathrm{-n}}{{z}_{e}}$
${x}_{p}=\frac{\mathrm{-n}\cdot {x}_{e}}{{z}_{e}}=\frac{n\cdot {x}_{e}}{{\mathrm{-z}}_{e}}$

From the side view of the frustum, yp is also calculated in a similar way;
$\frac{{y}_{p}}{{y}_{e}}=\frac{\mathrm{-n}}{{z}_{e}}$
${y}_{p}=\frac{\mathrm{-n}\cdot {y}_{e}}{{z}_{e}}=\frac{n\cdot {y}_{e}}{{\mathrm{-z}}_{e}}$

Note that both xp and yp depend on ze; they are inversely propotional to -ze. In other words, they are both divided by -ze. It is a very first clue to construct GL_PROJECTION matrix. After the eye coordinates are transformed by multiplying GL_PROJECTION matrix, the clip coordinates are still a homogeneous coordinates. It finally becomes the normalized device coordinates (NDC) by divided by the w-component of the clip coordinates. (See more details on OpenGL Transformation.)
$\left(\begin{array}{c}{x}_{\mathrm{clip}}\\ {y}_{\mathrm{clip}}\\ {z}_{\mathrm{clip}}\\ {w}_{\mathrm{clip}}\end{array}\right)={M}_{\mathrm{projection}}\cdot \left(\begin{array}{c}{x}_{\mathrm{eye}}\\ {y}_{\mathrm{eye}}\\ {z}_{\mathrm{eye}}\\ {w}_{\mathrm{eye}}\end{array}\right)\phantom{\rule{10px}{0ex}},\phantom{\rule{50px}{0ex}}\left(\begin{array}{c}{x}_{\mathrm{ndc}}\\ {y}_{\mathrm{ndc}}\\ {z}_{\mathrm{ndc}}\end{array}\right)=\left(\begin{array}{c}{x}_{\mathrm{clip}}}{{w}_{\mathrm{clip}}}\\ {y}_{\mathrm{clip}}}{{w}_{\mathrm{clip}}}\\ {z}_{\mathrm{clip}}}{{w}_{\mathrm{clip}}}\end{array}\right)$

Therefore, we can set the w-component of the clip coordinates as -ze. And, the 4th of GL_PROJECTION matrix becomes (0, 0, -1, 0).

Next, we map xp and yp to xn and yn of NDC with linear relationship; [l, r] ⇒ [-1, 1] and [b, t] ⇒ [-1, 1].

Mapping from xp to xn
${x}_{n}=\frac{1-\left(-1\right)}{r-l}\cdot {x}_{p}+\beta$

$\beta =1-\frac{2r}{r-l}=\frac{r-l}{r-l}-\frac{2r}{r-l}=\frac{r-l-2r}{r-l}=\frac{\mathrm{-r}-l}{r-l}$
$\phantom{\rule{0.7em}{0ex}}=-\frac{r+l}{r-l}$

Mapping from yp to yn
${y}_{n}=\frac{1-\left(-1\right)}{t-b}\cdot {y}_{p}+\beta$

$\beta =1-\frac{2t}{t-b}=\frac{t-b}{t-b}-\frac{2t}{t-b}=\frac{t-b-2t}{t-b}=\frac{\mathrm{-t}-b}{t-b}$
$\phantom{\rule{0.7em}{0ex}}=-\frac{t+b}{t-b}$

Then, we substitute xp and yp into the above equations.

$\phantom{\rule{1em}{0ex}}=\frac{2\cdot \frac{n\cdot {x}_{e}}{{\mathrm{-z}}_{e}}}{r-l}-\frac{r+l}{r-l}$

$\phantom{\rule{1em}{0ex}}=\frac{\frac{2n}{r-l}\cdot {x}_{e}}{{\mathrm{-z}}_{e}}-\frac{r+l}{r-l}$
$\phantom{\rule{1em}{0ex}}=\frac{\frac{2n}{r-l}\cdot {x}_{e}}{{\mathrm{-z}}_{e}}+\frac{\frac{r+l}{r-l}\cdot {z}_{e}}{{\mathrm{-z}}_{e}}$
$\phantom{\rule{1em}{0ex}}=\left(\frac{2n}{r-l}\cdot {x}_{e}+\frac{r+l}{r-l}\cdot {z}_{e}\right)}{{\mathrm{-z}}_{e}}$

$\phantom{\rule{1em}{0ex}}=\frac{2\cdot \frac{n\cdot {y}_{e}}{{\mathrm{-z}}_{e}}}{t-b}-\frac{t+b}{t-b}$
$\phantom{\rule{1em}{0ex}}=\frac{2n\cdot {y}_{e}}{\left(t-b\right)\left({\mathrm{-z}}_{e}\right)}-\frac{t+b}{t-b}$
$\phantom{\rule{1em}{0ex}}=\frac{\frac{2n}{t-b}\cdot {y}_{e}}{{\mathrm{-z}}_{e}}-\frac{t+b}{t-b}$
$\phantom{\rule{1em}{0ex}}=\frac{\frac{2n}{t-b}\cdot {y}_{e}}{{\mathrm{-z}}_{e}}+\frac{\frac{t+b}{t-b}\cdot {z}_{e}}{{\mathrm{-z}}_{e}}$
$\phantom{\rule{1em}{0ex}}=\left(\frac{2n}{t-b}\cdot {y}_{e}+\frac{t+b}{t-b}\cdot {z}_{e}\right)}{{\mathrm{-z}}_{e}}$

Note that we make both terms of each equation divisible by -ze for perspective division (xc/wc, yc/wc). And we set wc to -ze earlier, and the terms inside parentheses become xc and yc of the clip coordiantes.

From these equations, we can find the 1st and 2nd rows of GL_PROJECTION matrix.
$\left(\begin{array}{c}{x}_{c}\\ {y}_{c}\\ {z}_{c}\\ {w}_{c}\end{array}\right)=\left(\begin{array}{cccc}\frac{2n}{r-l}& 0& \frac{r+l}{r-l}& 0\\ 0& \frac{2n}{t-b}& \frac{t+b}{t-b}& 0\\ \cdot & \cdot & \cdot & \cdot \\ 0& 0& -1& 0\end{array}\right)\phantom{\rule{10px}{0ex}}\left(\begin{array}{c}{x}_{e}\\ {y}_{e}\\ {z}_{e}\\ {w}_{e}\end{array}\right)$

Now, we only have the 3rd row of GL_PROJECTION matrix to solve. Finding zn is a little different from others because ze in eye space is always projected to -n on the near plane. But we need unique z value for the clipping and depth test. Plus, we should be able to unproject (inverse transform) it. Since we know z does not depend on x or y value, we borrow w-component to find the relationship between zn and ze. Therefore, we can specify the 3rd row of GL_PROJECTION matrix like this.
$\left(\begin{array}{c}{x}_{c}\\ {y}_{c}\\ {z}_{c}\\ {w}_{c}\end{array}\right)=\left(\begin{array}{cccc}\frac{2n}{r-l}& 0& \frac{r+l}{r-l}& 0\\ 0& \frac{2n}{t-b}& \frac{t+b}{t-b}& 0\\ 0& 0& A& B\\ 0& 0& -1& 0\end{array}\right)\phantom{\rule{10px}{0ex}}\left(\begin{array}{c}{x}_{e}\\ {y}_{e}\\ {z}_{e}\\ {w}_{e}\end{array}\right)\phantom{\rule{10px}{0ex}}\text{,}\phantom{\rule{30px}{0ex}}{z}_{n}=\frac{{z}_{c}}{{w}_{c}}=\frac{A{z}_{e}+B{w}_{e}}{{\mathrm{-z}}_{e}}$

In eye space, we equals to 1. Therefore, the equation becomes;
${z}_{n}=\frac{A{z}_{e}+B}{{\mathrm{-z}}_{e}}$

To find the coefficients, A and B, we use the (ze, zn) relation; (-n, -1) and (-f, 1), and put them into the above equation.
$\left\{\begin{array}{c}\frac{\mathrm{-A}n+B}{n}=-1\\ \frac{\mathrm{-A}f+B}{f}=1\end{array}\right\\phantom{\rule{30px}{0ex}}\text{→}\phantom{\rule{30px}{0ex}}\left\{\begin{array}{cc}\mathrm{-A}n+B=\mathrm{-n}& {\text{(1)}}\\ \mathrm{-A}f+B=f& {\text{(2)}}\end{array}\right\$

To solve the equations for A and B, rewrite eq.(1) for B;
$B=An-n\phantom{\rule{30px}{0ex}}{\text{(1')}}$

Substitute eq.(1') to B in eq.(2), then solve for A;
$\mathrm{-A}f+\left(An-n\right)=f\phantom{\rule{30px}{0ex}}{\text{(2)}}$
$-\left(f-n\right)A=f+n$
$A=-\frac{f+n}{f-n}$

Put A into eq.(1) to find B;
$\left(\frac{f+n}{f-n}\right)n+B=\mathrm{-n}\phantom{\rule{30px}{0ex}}{\text{(1)}}$
$B=\mathrm{-n}-\left(\frac{f+n}{f-n}\right)n=-\left(1+\frac{f+n}{f-n}\right)n=-\left(\frac{f-n+f+n}{f-n}\right)n=-\frac{2fn}{f-n}$

We found A and B. Therefore, the relation between ze and zn becomes;
${z}_{n}=\frac{-\frac{f+n}{f-n}{z}_{e}-\frac{2fn}{f-n}}{{\mathrm{-z}}_{e}}\phantom{\rule{30px}{0ex}}{\text{(3)}}$

Finally, we found all entries of GL_PROJECTION matrix. The complete projection matrix is;
$\left(\begin{array}{cccc}\frac{2n}{r-l}& 0& \frac{r+l}{r-l}& 0\\ 0& \frac{2n}{t-b}& \frac{t+b}{t-b}& 0\\ 0& 0& \frac{-\left(f+n\right)}{f-n}& \frac{-2fn}{f-n}\\ 0& 0& -1& 0\end{array}\right)$
OpenGL Perspective Projection Matrix

This projection matrix is for a general frustum. If the viewing volume is symmetric, which is and , then it can be simplified as;

$\left(\begin{array}{cccc}\frac{n}{r}& 0& 0& 0\\ 0& \frac{n}{t}& 0& 0\\ 0& 0& \frac{-\left(f+n\right)}{f-n}& \frac{-2fn}{f-n}\\ 0& 0& -1& 0\end{array}\right)$

Before we move on, please take a look at the relation between ze and zn, eq.(3) once again. You notice it is a rational function and is non-linear relationship between ze and zn. It means there is very high precision at the near plane, but very little precision at the far plane. If the range [-n, -f] is getting larger, it causes a depth precision problem (z-fighting); a small change of ze around the far plane does not affect on zn value. The distance between n and f should be short as possible to minimize the depth buffer precision problem.

Comparison of Depth Buffer Precisions

### Orthographic Projection

Orthographic Volume and Normalized Device Coordinates (NDC)

Constructing GL_PROJECTION matrix for orthographic projection is much simpler than perspective mode.

All xe, ye and ze components in eye space are linearly mapped to NDC. We just need to scale a rectangular volume to a cube, then move it to the origin. Let's find out the elements of GL_PROJECTION using linear relationship.

Mapping from xe to xn
${x}_{n}=\frac{1-\left(-1\right)}{r-l}\cdot {x}_{e}+\beta$

$\beta =1-\frac{2r}{r-l}=-\frac{r+l}{r-l}$

$\therefore {x}_{n}=\frac{2}{r-l}\cdot {x}_{e}-\frac{r+l}{r-l}$

Mapping from ye to yn
${y}_{n}=\frac{1-\left(-1\right)}{t-b}\cdot {y}_{e}+\beta$

$\beta =1-\frac{2t}{t-b}=-\frac{t+b}{t-b}$

$\therefore {y}_{n}=\frac{2}{t-b}\cdot {y}_{e}-\frac{t+b}{t-b}$

Mapping from ze to zn
${z}_{n}=\frac{1-\left(-1\right)}{\mathrm{-f}-\left(\mathrm{-n}\right)}\cdot {z}_{e}+\beta$

$\beta =1-\frac{2f}{f-n}=-\frac{f+n}{f-n}$

$\therefore {z}_{n}=\frac{-2}{f-n}\cdot {z}_{e}-\frac{f+n}{f-n}$

Since w-component is not necessary for orthographic projection, the 4th row of GL_PROJECTION matrix remains as (0, 0, 0, 1). Therefore, the complete GL_PROJECTION matrix for orthographic projection is;
$\left(\begin{array}{cccc}\frac{2}{r-l}& 0& 0& -\frac{r+l}{r-l}\\ 0& \frac{2}{t-b}& 0& -\frac{t+b}{t-b}\\ 0& 0& \frac{-2}{f-n}& -\frac{f+n}{f-n}\\ 0& 0& 0& 1\end{array}\right)$
OpenGL Orthographic Projection Matrix

It can be further simplified if the viewing volume is symmetrical, and
$\begin{array}{cc}\left\{\begin{array}{c}\phantom{\rule{5px}{0ex}}r+l=0\hfill \\ \phantom{\rule{5px}{0ex}}r-l=2r\phantom{\rule{10px}{0ex}}{\text{(width)}}\hfill \end{array}\right\& \phantom{\rule{30px}{0ex}}\text{,}\phantom{\rule{10px}{0ex}}\left\{\begin{array}{c}\phantom{\rule{5px}{0ex}}t+b=0\hfill \\ \phantom{\rule{5px}{0ex}}t-b=2t\phantom{\rule{10px}{0ex}}{\text{(height)}}\hfill \end{array}\right\\end{array}$

$\left(\begin{array}{cccc}\frac{1}{r}& 0& 0& 0\\ 0& \frac{1}{t}& 0& 0\\ 0& 0& \frac{-2}{f-n}& -\frac{f+n}{f-n}\\ 0& 0& 0& 1\end{array}\right)$

### Credits

Jiergir Ogoerg: MathML Conversion