Derivation of the [3D base] projection matrix (1)

Source: Internet
Author: User
Tags range

I've been playing the game industry for eight months now, the entry threshold of the game industry is low, so it is relatively easy to learn, summed up their own confused several points of knowledge, originally wanted to write out to beginners, rogue I was a lazy person, has been dragged to the present, finally resolved to move to the blog in black and white, I hope you like.

Projection transformation: I think this is the 3D to 2D transformation of the most let beginners dizzy problem, but it is the most important.

Please look at the picture above which I scratched out with my paw. This coordinate system is the left-hand coordinate system of DX, y up, X to the right, z inward, geometric coordinates have been transformed by the camera coordinate system, the camera position is (0,0,0), assuming that the distance from the cut surface is F, near cut surface distance of N, near cut surface to the left of the L, R on the right, on the T, under B. The 2 vertices A and b coordinates to be projected are a (Xa,ya,za), B (XB,YB,ZB), and now we ask for their projection point coordinates A0 and B0, where I take B as an example to illustrate the simplest geometric knowledge of projection.

First we'll compute the x-coordinate of the B0 point (dotted lines are guides), we look at the triangle B c o and triangle B0 k O, using the middle school geometry knowledge can be similar to them, by the similarity triangle theorem, B0 K = b c * (N/c O), and b C equals XB, C O equals ZB; so XB 0 = Xb * N/ZB; similarly Yb0 = Yb * N/ZB; we know that DX maps 3D coordinates to a cube in X ( -1,1) Y ( -1,1) Z (0,1). So now we need to do a one-dimensional mapping of the projected coordinates, (in fact, projections are maps, mathematical forms are functions, and a range of one by one is mapped to another range), and now on the x-axis, we're going to map the value one by one in the L-r range to 1-1, what should we do? Obviously we have an equation (x-l)/(r-l) = (x0-(-1))/(1-(-1)); X is a value between the L-r, x0 is a value between 1-1, we get x0 = (2x-(R + L))/(r-l); Here the x is replaced by Xb0: x0 = (2n * Xb)/(zb* (R-l))-(R + L )/(R-l); similarly: y0 = (2n * Yb)/(zb* (T-b))-(T + B)/(T-B); Zb mapping is the simplest, mapping values between N-f to 0-1, z0 = Zb/(f-n)-N/(f-n); now we're going to use these arithmetic in matrix form:

[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, in order to facilitate the linear interpolation of the z value of the middle pixel in the late grating of the 3D engine, it is best to save the 1/z value of the vertex directly and then 1/z the 0~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 ]  //直接保存1/Z

The 1/z is nested to the 0~1 (linear mapping) Z0 = (1/bz-1/n)/(1/f-1/n); [1/n->0, 1/f->1]

Transformation first form makes matrix understanding more clear: 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 ]

I see real-time computer graphics written on the answer and I deduced a bit different, red part of the symbol on the contrary, rather puzzled, do not know where I was wrong, I hope to understand the friend guidance maze.

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.