[Translation]-Derivation of Projection Matrix

Source: Internet
Author: User

[Translation]-Derivation of Projection Matrix

Original post address:

Http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__1/Deriving-Projection-Matrices.htm

Translation:

Matrix transformation is the basic knowledge of 3D graphics programmers. Projection matrices are complex content. Translation and scaling are easy to understand. The rotation matrix only requires basic triangular ry knowledge, but the projection matrix is different. If you have read the form of the projection matrix, you will find it hard to quickly know how it came from. Moreover, I have not found many resources on the Internet to deduce the projection matrix. This article describes how to deduce the projection matrix.

For new users who are new to 3D graphics, deriving the projection matrix requires a certain mathematical foundation, but it is not necessary. You can directly use the final formula. If you use a graphical API, such as direct3d, you do not need to worry about it because the API already provides the projection matrix computing function. Once you understand how to deduce it, there is no harm to you. This article is intended for those who need to understand the details of the derivation projection matrix.

Introduction: What is projection?

A computer display is a 2D surface. To display 3D objects, You need to convert 3D objects into 2D images. This process is projection. For example, the simplest way to change a 3D object to a 2D surface is to remove the zcoordinate. For 1 cube, such as 1.

 

Figure 1: Remove zcoordinate from projection to xy plane

Of course, the method shown in Figure 1 is very simple and not applicable in most cases. The projection will not be projected on a plane. On the contrary, the projection mentioned here will transform the object to a standard view space (what is canonical view volume ?). Different image APIs may vary depending on the coordinates of the standard space. For the sake of discussion, direct3d is used here, that is, (-1,-1, 0) to (1, 1, 1 ). Once the coordinates of an object are converted to the standard space, the X and Y coordinates are used to map to the screen space. The zcoordinates are generally used for Z-buffer.

Note that figure 1 uses the left-hand coordinate system. This is also the form of direct3d. This article will always use the left-hand coordinate system. For the right-hand coordinate system, the knowledge described in this article is applicable.

Now we can start to talk about projection transformation. The most common form of projection is normal projection and Perspective Projection.

Forward projection

Normal projection is a simple projection form that requires that all projection rays be perpendicular to the projection plane. The final criterion of positive projection is an AAB (axis-aligned box), such as 2.

 

Figure 2: positive projection

It can be seen that the standard visual space is composed of six faces:

Left: x = L

Right: x = r

Below: Y = B

Upper: Y = T

Near: z = N

Far: z = f

Because the view volume and canonical view volume of the positive projection are both AAB, they do not have the characteristics that change with distance as in Perspective Projection. For normal projection, the size of all objects does not change and does not change with distance.

The forward projection matrix is derived below. The X coordinate of a point in view volume is in [L, R], which needs to be transformed to [-1, 1] In canonical view volume.

L <= x <= r

0 <= x-L <= R-l

0 <= (X-l)/(R-l) <= 1

0 <= 2 (x-l)/(R-l) <= 2

-1 <= 2 (x-l)/(R-l)-1 <= 1

-1 <= (2x-r-l)/(R-l) <= 1

Finally, you need to get the form of PX + Q, so it is decomposed:

-1 <= 2x/(R-l)-(R + l)/(R-l) <= 1

So the X coordinates in Canonical view volume are obtained:

X' = 2x/(R-l)-(R + l)/(R-l)

Likewise, the Y coordinates in Canonical view volume are obtained:

Y' = 2y/(t-B)-(t + B)/(t-B)

Finally, we will deduce the Z' formula. The zcoordinate of vertices in view volume is in [N, F] and needs to be transformed to [0, 1] In canonical view volume.

N <= z <= f

0 <= z-n <= f-n

0 <= (Z-N)/(F-N) <= 1

0 <= z/(F-N)-N/(F-N) <= 1

Get the Z' expression:

Z' = z/(F-N)-N/(F-N)

Summarize the above x', y' and Z ',

X' = 2x/(R-l)-(R + l)/(R-l)

Y' = 2y/(t-B)-(t + B)/(t-B)

Z' = z/(F-N)-N/(F-N)

If the format is matrix:

 

In direct3d, a function d3dxmatrixorthooffcenterlh () provides this function (note that the form is somewhat different, but the line/class matrix is different ). "LH" indicates the left-hand coordinate system, but what does "offcenter" mean?

First, in the camera space, if camera is placed at the origin and along the Z axis, and second, if r =-L, t =-B, and define the width W on the X axis and the height H on the Y axis.

 

The above matrix is consistent with the calculation result of d3dxmatrixortholh () in direct3d.

If the matrix of positive projection is divided into the following:

 

Note that the conversion format is: P' (canonical view volume) = P0 * P (view volume ).

Through this decomposition, we can understand the positive projection matrix. First, view volume moves the near plane along the Z axis to the origin. Second, the view volume is scaled to the canonical view volume.

Perspective Projection

Perspective Projection is widely used because it can produce the illusion of distance (distant objects look smaller), so it can produce more real results. Unlike normal projection, view volume in perspective projection is a cone, such as 4:

 

Figure 4: Perspective Projection

We can see that the near plane of view frustum extends from (L, B, n) to (R, t, n ).

Step 1: For a point (x, y, z) In view frustum, project it to near-plane Z = n. Because the projection point is on the near plane, the X coordinate ranges from [L, R] to [B, T]. Figure 5

Step 2: Use the knowledge learned in positive projection to hide [L, R] of X on the near plane to [-1, 1], [B, y, t] to [-1, 1].

 

Figure 5

Step 1: Get the projection point

 

 

Therefore, the projection points (x * n/Z, y * n/Z, n) are obtained ).

Step 2: Apply the projection point obtained in step 1 to the knowledge learned in positive projection.

 

X = x * n/Z and Y = y * n/Z,

 

Multiply by Z,

 

Apply (x, y, z) to (x 'z, y 'z, Z 'Z ).

Suppose z'z = PZ + Q, And the zcoordinate's implicit shot is [N, F] to [0, 1], so we get:

0 = pN + q

F = PF + q

Equation solving:

P = f/(F-N)

Q =-FN/(F-N)

So,

Z'z = FZ/(F-N)-FN/(F-N)

Summarized together,

 

Matrix format:

 

The matrix above can make P * (X, Y, Z, 1) = (x 'z, y 'z, Z' Z, Z ). This is also the same as d3dxmatrixperspectiveoffcenterlh () in direct3d (note the differences between rows and columns ). In normal projection, if certain conditions are met, r =-L, t =-B, the following matrix form is available:

 

This is the same as the d3dxmatrixperspectivelh () result in direct3d.

Finally, it is necessary to talk about the Matrix Representation of the aspect ratio.

6:

 

Figure 6

Cot (A/2) = N/(h/2) = 2n/h

Suppose R = W/H, that is, aspect ratio.

2n/W = 2n/(RH) = (1/R) * Cot (A/2)

There are the following forms:

 

This form is the same as the d3dxmatrixperspectivefovlh () result in direct3d.

Note:

This article was originally intended for translation, but later I was impatient. I have never been patient in my work recently, so I did not translate it in the original text, but listed the important ones.

Some of the formulas mentioned above do not need to be so complicated, but in general, they are worth reading.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/G_cofa/archive/2010/10/08/5926092.aspx

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.