读书人

OpenGL Projection Matrix(投影矩阵推

发布时间: 2012-07-02 17:46:22 作者: rapoo

OpenGL Projection Matrix(投影矩阵推导) .
OpenGL Projection Matrix

Related Topics: OpenGL Transformation

Overview Perspective Projection Orthographic Projection Overview

A computer monitor is a 2D surface. We need to transform 3D scene into 2D image in order to display it. GL_PROJECTION matrix is for this projection transformation . This matrix is used for converting from the eye coordinates to the clip coordinates. Then, this clip coordinates are also transformed to the normalized device coordinates (NDC) by divided with w component of the clip coordinates.

Therefore, we have to keep in mind that both clipping 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.

Perspective ProjectionOpenGL Projection Matrix(投影矩阵推导)


From the side view of the projection, yp is also calculated in a similar way;
OpenGL Projection Matrix(投影矩阵推导)

Note that both xp and yp depend on ze ; they are inversely propotional to -ze . It is an important fact to construct GL_PROJECTION matrix. After an eye coordinates are transformed by multiplying GL_PROJECTION matrix, the clip coordinates are still a homogeneous coordinates . It finally becomes normalized device coordinates (NDC) divided by the w-component of the clip coordinates. (See more details on OpenGL Transformation . )
OpenGL Projection Matrix(投影矩阵推导),? OpenGL Projection Matrix(投影矩阵推导)

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).
OpenGL Projection Matrix(投影矩阵推导)

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

OpenGL Projection Matrix(投影矩阵推导)

?

OpenGL Projection Matrix(投影矩阵推导)

?

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

OpenGL Projection Matrix(投影矩阵推导)OpenGL Projection Matrix(投影矩阵推导)

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 clip coordiantes.

From these equations, we can find the 1st and 2nd rows of GL_PROJECTION matrix.
OpenGL 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 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.
OpenGL Projection Matrix(投影矩阵推导)

In eye space, we equals to 1. Therefore, the equation becomes;
OpenGL Projection Matrix(投影矩阵推导)

To find the coefficients, A and B , we use (ze , zn ) relation; (-n, -1) and (-f, 1), and put them into the above equation.
OpenGL Projection Matrix(投影矩阵推导)

To solve the equations for A and B , rewrite eq.(1) for B;
OpenGL Projection Matrix(投影矩阵推导)

Substitute eq.(1') to B in eq.(2), then solve for A;
OpenGL Projection Matrix(投影矩阵推导)

Put A into eq.(1) to find B ;
OpenGL Projection Matrix(投影矩阵推导)

We found A and B . Therefore, the relation between ze and zn becomes;
OpenGL Projection Matrix(投影矩阵推导)

Finally, we found all entries of GL_PROJECTION matrix. The complete projection matrix is;
OpenGL Projection Matrix(投影矩阵推导)
and OpenGL Projection Matrix(投影矩阵推导) ,.then it can be simplified as;
OpenGL Projection Matrix(投影矩阵推导)

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.

OpenGL Projection Matrix(投影矩阵推导)


?

OpenGL Projection Matrix(投影矩阵推导)

?

OpenGL Projection Matrix(投影矩阵推导)

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 Projection Matrix(投影矩阵推导)
and OpenGL Projection Matrix(投影矩阵推导) .
OpenGL Projection Matrix(投影矩阵推导)

读书人网 >开源软件

热点推荐