Notes:
OpenGL is right handed in eye space but left-handed in normalized device coordinates (cvv).
OpenGL is right handed in object space and world space.
But in window space (aka screen space) we are suddenly left handed.
http://stackoverflow.com/questions/4124041/is-opengl-coordinate-system-left-handed-or-right-handed
http://www.songho.ca/opengl/gl_projectionmatrix.html
OpenGL Projection Matrix
Related Topics: OpenGL Transformation
Updates: The MathML version is available here.
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 projectiontransformation. 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 withw component of the clip coordinates.
![A triangle clipped by frustum](https://i-blog.csdnimg.cn/blog_migrate/f2be4efdca7b98459a70be3058022f3f.png)
A triangle clipped by frustum
Therefore, we have to keep in mind that both clipping (frustum culling) and NDC transformations are integrated intoGL_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 any 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
![OpenGL Perspective Frustum and NDC](https://i-blog.csdnimg.cn/blog_migrate/81ee78b0ef2b14c60ec8ba79bf2fff86.png)
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. SinceglFrustum() accepts only positive values ofnear 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 thenear plane.
![Top View of Frustum](https://i-blog.csdnimg.cn/blog_migrate/af3e06bdb57f2b8b1f34b78c91cb8c3f.png)
Top View of Frustum
![Side View of Frustum](https://i-blog.csdnimg.cn/blog_migrate/0f2c701377ed7fe19286f5ea64a3772e.png)
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;
From the side view of the frustum, yp is also calculated in a similar way;
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 onOpenGL Transformation.)
,
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].
![](https://i-blog.csdnimg.cn/blog_migrate/eea3b930a51f254b83442fd426be872a.png)
Mapping from xp to xn
![](https://i-blog.csdnimg.cn/blog_migrate/570b04f6c4a497f5e12e97215bf82513.png)
Mapping from yp to yn
Then, we substitute xp and yp into the above equations.
![](https://i-blog.csdnimg.cn/blog_migrate/1e2d20cfd2155550bfc9332dd96768fd.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e15dc12d799310d529954624c595d58f.png)
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.
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.
In eye space, we equals to 1. Therefore, the equation becomes;
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.
To solve the equations for A and B, rewrite eq.(1) for B;
Substitute eq.(1') to B in eq.(2), then solve for A;
Put A into eq.(1) to find B;
We found A and B. Therefore, the relation between ze and zn becomes;
Finally, we found all entries of GL_PROJECTION matrix. The complete projection matrix is;
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;
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 thenear plane, but very little precision at thefar plane. If the range [-n, -f] is getting larger, it causes a depth precision problem (z-fighting); a small change of ze around thefar plane does not affect on zn value. The distance betweenn and f should be short as possible to minimize the depth buffer precision problem.
![Comparison of depth precision](https://i-blog.csdnimg.cn/blog_migrate/ad1c28f83d45ca66cf92ca43db964ee5.png)
Comparison of Depth Buffer Precisions
Orthographic Projection
![OpenGL Orthographic Volume and NDC](https://i-blog.csdnimg.cn/blog_migrate/3851f571323e5a619cfb0a63cc598feb.png)
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.
![](https://i-blog.csdnimg.cn/blog_migrate/a00356d19dd001ee3e56bf167534d4b7.png)
Mapping from xe to xn
![](https://i-blog.csdnimg.cn/blog_migrate/2299cd0c4272bde75600b8a2916cec07.png)
Mapping from ye to yn
![](https://i-blog.csdnimg.cn/blog_migrate/b02497afffb7916ea91f6c8c0b9ac8a9.png)
Mapping from ze to zn
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;
OpenGL Orthographic Projection Matrix
It can be further simplified if the viewing volume is symmetrical, and
.