[3D Basics] derivation of projection matrix (1)

Source: Internet
Author: User

In the twinkling of an eye, I have been playing the game industry for eight months, and the gaming industry has a low entry threshold. Therefore, it is easy to learn about computing and learning. I have summarized some of my confused knowledge points and I would like to write them out to beginners to solve their puzzles, rogue I am a lazy person who has been dragging on to the present and finally is determined to move to blog in black and white. I hope everyone will like it.
 
Projection Transformation: I think this is the most disturbing problem for beginners in 3D to 2D transformation, but it is also the most important.

Please refer to the figure above that I caught with my claws. This coordinate system is DX's left-hand coordinate system, y up, X to the right, Z inward, the geometric coordinate has been transformed by the camera coordinate system, the camera position is (0, 0 ), assume that the distance between the distant cut surface is f, the distance between the near cut surface is N, the left side of the near cut surface is l, the right side is R, the distance is t, and the bottom side is B. The coordinates of vertex A and vertex B to be projected are a (XA, ya, za), B (XB, Yb, ZB ), now we need their projection point coordinates A0 and B0. Here I will use point B as an example to explain the simplest geometric knowledge of projection.

First, we can calculate the X coordinates of B0 points (dotted lines are all auxiliary lines). We can see That Triangle B C O and triangle B0 K O are similar to each other by using Junior High School ry knowledge, the similarity triangle theorem shows that B0 K = B c * (N/c o), B C is XB, and C O is ZB; therefore, xb0 = XB * n/ZB; similarly, yb0 = Yb * n/ZB; we know that DX maps 3D coordinates to an X (-) y (-) Z () Cube Body. Now we need to perform one-dimensional ing on the projected coordinates. (In fact, projection is a ing. The mathematical form is a function that maps one value to another ), now on the X axis, we need to map the values in the value range of l-R to-1-1 one by one. What should we do? Obviously, we have an equation (X-l)/(R-l) = (x0-(-1)/(1-(-1 )); X is a value between L-R, and x0 is a value between-1-1. We get X0 = (2X-(R + l )) /(R-l); replace X with xb0: X0 = (2N * xb)/(zb * (R-l)-(R + l) /(R-l); similarly: Y0 = (2N * Yb)/(zb * (t-B)-(t + B)/(t-B ); ZB ing is simplest. It maps the value between N-F to 0-1, z0 = ZB/(F-N)-N/(F-N ); now we use the matrix form to represent these four arithmetic operations:
[X, y, z, 1] * [2n/(R-l), 0, 0, 0]
[0, 2n/(t-B), 0, 0]
[-(R + l)/(R-l),-(t + B)/(t-B), Z/(F-N), 1]
[0, 0,-z * n/(F-N), 0]
The resulting structure should be [x0, y0, z0, w]-> [X0/W, Y0/W, z0/W, 1]

However, to facilitate linear interpolation of the Z value of the intermediate pixel when the 3D engine is raster later, it is best to save the 1/z value of the vertex directly, and then embed 1/Z into 0 ~ Between 1.

[X, y, z, 1] * [2n/(R-l), 0, 0, 0]
[0, 2n/(t-B), 0, 0]
[-(R + l)/(R-l),-(t + B)/(t-B), 0, 1]
[0, 0, 1, 0] // save 1/Z directly

Embed 1/Z into 0 ~ Zing between 1 (linear) z0 = (1/BZ-1/N)/(1/f-1/n); [1/n-> 0, 1/F-> 1]
Convert the first form to make the matrix clearer: z0 = f/(F-N)-f * n/(f-N) * z)
[X, y, z, 1] * [2n/(R-l), 0, 0, 0]
[0, 2n/(t-B), 0, 0]
[ -(R + l)/(R-l),-(t + B)/(t-B ), F/(F-N), 1]
[0, 0,-f * n/(F-N), 0]

The answer I wrote in real-time computer graphics is a little different from the one I deduced. The red symbol is the opposite. I don't know where I am wrong.

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.