Construct a projection matrix/cropping Matrix

Source: Internet
Author: User

Matrix changes generally include three parts: WVP. This article is devoted to explaining some principles in the Construction of P Matrix, that is, projection cropping matrix.

Learn more before you start. The role of the projection matrix is to save the objects in the cone and eventually correspond to the viewport. The process is roughly three steps. First, the cone is projected to become a so-called parallel pipeline observer, which can be understood as converting the cone into a cube. Second, the cone is converted into a cube, the three sides of the cube cannot exceed plus or minus 1. The third part is to map the content in the cube to the viewport. The final matrix we get can only complete the first two steps, and must be the value after the homogeneous operation.

The following is an overview of the camera. You can refer to this image for abstract reasoning.

With the previous general process, we can step by step obtain the desired matrix. First, construct a projection matrix. Let's take a look at the general projection principle:

This figure shows the general projection principle. On the way, Z = D is the projection plane. P is the actual point (X, Y, Z, 1), and P is the projection point. It is not difficult to obtain that the coordinates of P are (dx/Z, dy/Z, D ). To convert a 4*4 matrix, we can deduce the projection matrix projected on a certain projection Plane Based on the matrix multiplication principle and the homogeneous principle.

(1 0 0 0

0 1 0 0

0 0 1 1/D

0 0 0)

After the point is multiplied by this matrix, a thinking homogeneous vector is obtained. After dividing by the fourth dimension, it is the vector of the three-dimensional space.

Therefore, we can obtain a real second projection matrix.

For example, after P' is multiplied by this matrix, (X, Y, Z, Z/D) is obtained, and then the coordinates of the projection points are obtained by dividing all the elements by Z/d.

Now we aim to get the parallel pipeline object, and it is not difficult to find that after projection, We Can rectangle the X and Y coordinates. However, after the Z projection, we change it to the uniform coordinates, that is, the final projection result is a plane, not a cube we want. Here we have to change the Z axis, so we cannot let it change in the station on a plane. This will discard the Z axis information, which plays an important role in the subsequent process. Therefore, we should try to change the change of Z while X and Y remain unchanged. We know that our ultimate goal is to get a cube, and we know that the side length of the cube is 2, and the maximum and minimum values are 1 and-1 respectively. At this point, we may want to set the Z value to the value we want. We know that zooming alone cannot achieve this goal, so translation + scaling is required. Assume that the matrix is in the following format:

(1 0 0 0

0 1 0 0

0 0 A 1

0 0 B 0)

In this way, we can see that Az + B is the multiplication value. This value must meet the requirements of the following sub-statements, that is,-1 = <(AZ + B) /z <= 1 (according to OpenGL, dx is 0 ~ 1), the maximum value of Z is f, and the minimum value is N. Through the extreme value, the values of A and B are obtained.

A = (F + N)/(F-N), B = (2nf)/(n-f ).

So far, we have completed the conversion to the cube.

Supplement: It may be strange why the projection plane is not set, but directly one. There are two reasons for this. First, it is not important where the projection plane is located, because the objects that are finally presented on the projection plane are determined by the cone, and setting it to 1 is very convenient for calculation. Second, the projection plane of DX is 1.

Now you can take the second step to obtain a cube. It is still a cube in dx.

Since the value of Z has been processed in the projection matrix, we will not consider it here, mainly considering the zooming ratio of X and Y. We know that the scale ratio of XY must be the same as the zoom ratio of the viewport, otherwise it will stretch. Therefore, you only need to know the angle of view or zoom value to solve the problem. Generally, the user provides the Angle of View Size. The following shows how to get the zoom ratio from the perspective.


We know from the figure that the purpose is to convert X to 1 through trigonometric functions, x Toom = x (1/TAN (angle/2 )) = X × Z/x = z, we know that the value of Z is the value of the projection plane, so Z = 1, so Toom is the zoom value. Toom = 1/TAN (angle/2 ). You can use the zooming value of X or Y to obtain another zooming value.

Finally, we multiply the projection drop matrix and the zoom matrix to get the desired matrix.

(1 0 0 0 (zoomx 0 0 0

0 1 0 0 0 zoomy 0 0

0 0 (F + N)/(F-N) 1*0 0 1 0

0 0 2nf/(n-f) 0) 0 0 0 1)

= (Zooomx 0 0 0

0 zoomy 0 0

0 0 (F + N)/(F-N) 1

0 0 2nf/(n-f) 0)

This is the matrix form in OpenGL. DX is only the difference in Z axis variation. As mentioned above.

After this matrix is obtained, divide by the four-dimensional value to obtain the cube.

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.