OpenGL Projection Matrix

Source: Internet
Author: User
  • Overview
  • Perspective Projection
  • Orthogonal projection
Overview

A computer display is a 2D plane. 3D scenes rendered by OpenGL must be projected onto a computer screen using 2D images. The gl_projection matrix is used for this projection transformation. First, it converts all the fixed point data from the observed coordinate transformation to the cut coordinate. Then, the cut coordinates are converted to the normalized device coordinates (NDC) by dividing by W ).

Therefore, we need to remember that both the cut transform (view cone removal) and the NDC transform are stored in the gl_projection matrix. The following sections describe how to create a projection matrix from six limited parameters (left, right, bottom, top, near plane, and far plane.

Note: The cone removal (CUT) is executed on the cut coordinate and before dividing by WC. The cut coordinates XC, YC, and ZC are compared with WC. If any coordinate is smaller than-WC or greater than WC, the vertex will be discarded.

Then, OpenGL re-constructs the edges of the cut polygon.

 
Triangle cut by Cone Perspective Projection
OpenGL perspective cone and NDC

In Perspective Projection, 3D points in the cube (observed coordinates) are mapped to the cube (NDC. The X coordinate ranges from [L, F] to [-]. The Y coordinate ranges from [B, T] to [-]. The zcoordinate ranges from [n, f] to [-1, 1].

Note: The observed coordinates are the right coordinate system, and NDC uses the left and right coordinate system. That is to say, the camera located at the origin looks at the-Z axis in the observation coordinate, while the camera looks at the + z axis in the NDC. Because glfrustum () only receives the positive distance between the near plane and the far plane, we need to reverse them when constructing the gl_projection matrix.

In OpenGL, 3D points in the observed space are projected to the near plane (projection plane. Displays how points (xe, ye, ze) in the observed space are projected to points near the plane (XP, YP, ZP ).

 
Pyramid View
Lateral view of the Cone

 

From the view of the cone, we can see that the X coordinate Xe of the observed space is mapped to XP using the ratio calculation of similar triangles.

From the lateral view of the cone, YP calculates the result in the same way:

Note that both XP and YP depend on ze, which is in inverse proportion to-Ze. That is to say, they are all divided by-Ze. This is the first prompt to build the gl_projection matrix. After the observed coordinate is multiplied by the gl_projection matrix, the cut coordinate is still the second coordinate. It is divided by the W component of the cut coordinate to become the normalized device coordinate (NDC ). (For more details, see OpenGL transformation .)

,

Therefore, we can set the W component of the cut coordinate to-Ze. In this way, the fourth row of the gl_projection matrix is (0, 0 ).

Next, we map XP and YP to XN and yn: [L, R] => [-], [B, t] => [-1, 1].


Map XP to XN


Map YP to YN

Then, we use the equation above to replace XP and YP.

Note: We divide each equation by-ze for the perspective Division (XC/WC, YC/WC. We have set WC to-Ze, and the items in braces are XC and YC in the cut coordinate.

From this equation, we can find the first and second rows of the gl_projection matrix.

Now, we only solve the three rows of the gl_projection matrix. Since the ze in the observed space is always projected to the-n point on the near plane, the calculation method of Zn is slightly different from that of other coordinates. However, we need a unique Z value for cropping and deep testing. In addition, we also perform Inverse Projection (inverse transformation) operations. We know that Z does not depend on the values of X and Y. We use W to find the relationship between Zn and Ze. Therefore, we can specify the third row of the gl_projection matrix as follows:

In the observed space, we is equal to 1. Therefore, the equation becomes:

To calculate the coefficients A and B, we use the (Ze, Zn) relationship (-N,-1) and (-F, 1) and bring them into the equation above.

To solve a and B, rewrite the equation (1 ):

Bringing equation (1') into equation (2), and then solving:

Add a to equation (1) and obtain B:

Let's work out a and B. Therefore, the relationship between ze and Zn becomes:

Finally, we solve all the elements in the gl_projection matrix. The complete projection matrix is:


OpenGL Perspective Projection Matrix

The projection matrix is a common cross-section. If the cone is symmetric, that is, r =-L and T =-B, the matrix can be simplified:

Before proceeding, review the relationship between ze and Zn: equation (3 ). You will notice that it is a rational number equation and the relationship between ze and Zn is not linear. That is to say, the near plane has a very high precision, while the far plane has a very low precision. If the range of [-N,-F] is large, it will cause the depth precision problem (depth conflict): the small change of ze near the far plane will not affect the Zn value. To minimize the precision of the deep cache, the distance between N and F should be as small as possible.


Comparison of depth cache precision Orthogonal projection
Orthogonal vertebral body and normalized device coordinates (NDC)

Constructing the gl_projection matrix of orthogonal projection is much easier than the Perspective Projection mode.

The Xe, ye, And Ze components of the observed space are linearly mapped to NDC. We only need to scale the cube to the cube, and then move it to the origin. Let's use a linear relationship to derive all elements in gl_projection.


Map Xe to XN

 


Map ye to YN

 


Map Ze to Zn

Because W is not required for orthogonal projection, the 4th rows of the gl_projection matrix are still ). Therefore, the complete gl_projection matrix of orthogonal projection is:


OpenGL orthogonal projection matrix

If the cone is symmetric (r =-L and T =-B), it can be further simplified.

 

Http://www.songho.ca/opengl/gl_projectionmatrix.html.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.