Perspective projection detailed "Go"

Source: Internet
Author: User

Overview

The projection transformation is to show how to display the three-dimensional model to the two-dimensional viewport, which is a three-dimensional to two-dimensional process. You can consider a projection transformation as the focal length of the camera, which simulates the process of selecting a lens for a camera. A projection transformation is the most complex of all transformations.

Cone of sight

The view cone is a three-dimensional body whose position is related to the camera, and the shape of the cone determines how the model is projected from camera space onto the screen. The most common type of projection-the perspective projection-makes the object close to the camera larger, while the object farther away from the camera is projected smaller. The perspective projection uses a pyramid as the cone, and the camera is located on top of the pyramid. The pyramid is truncated to the front and back two planes, forming a bevel called the View Frustum, and only the model inside the Frustum is visible.

Purpose of Perspective projection

The goal of perspective projection is to convert the above bevel into a cube (cuboid), and after conversion, the upper-right corner of the front clipping plane of the bevel becomes the center of the front plane of the cube (shown in the middle arc). It is shown from the graph that the process of this transformation is to enlarge the smaller part of the bevel and to narrow the larger part to form the final cube. This is the reason that the projection transformation produces near-large, far-smaller effects. The transformed x-coordinate range is [-1, the 1],y coordinate range is [-1, the 1],z coordinate range is [0, 1] (OpenGL is slightly different, the z-value range is [-1, 1]).

Perspective projection Matrix Derivation

Let's deduce the perspective projection matrix so that we can set the projection matrix ourselves, and we can simulate the function of the magical d3dxmatrixperspectivelh function. So what exactly does perspective projection work on? This is a difficult part, whether it is the DX SDK help document, or most of the graphics books, this is all around, rarely discussed in detail, the Early DX SDK documentation also discussed a little more, and the new document completely cancels the projection matrix deduction process.

We can divide the whole projection process into two parts, the first part is the process of projecting from frustum to the near clipping plane, and the second part is the process of scaling by the near clipping plane. Suppose that the projection of a point P (x, Y, z) on the near clipping plane of the frustum is P ' (× ', S ', '), and the final coordinate of P ' after scaling is set to P ' (x ", y", z "). Assuming that the projection matrix is m, then according to matrix multiplication, the following equation is established.

Pm=p ", i.e.

First, in order to simplify the problem, we consider the projection on the yoz plane, see. Set P (x, Y, z) is a point inside the frustum, and its projection on the near clipping plane is P ' (x ', y ', Z '). ( Note: The D3D has a near clipping plane as the projection plane ), and the angle of the cones in the y direction is θ.

It is known that the triangular op ' Q ' is similar to the triangular OPQ, so the following equation is established.

In the second part, the process of scaling p ', assuming that the projection plane has a height of H, since the converted cuboid height is 2. So there are

And because the aspect ratio of the projection plane is aspect,

At the end of the Z ', when the point in the frustum is projected to the near clipping plane, the z ' value is actually meaningless because all the points on the near clipping plane, whose z ' value is N, looks like we can even discard this z ' value, can we? Of course not! Don't forget that there's a depth test in the back. The first picture shows that all points on the P ' p of the line segment will eventually be projected to the P ' point, so if there is really more than one point on the segment, how do you determine which one is ultimately retained? This is, of course, the closest to the observation, which is the lowest depth value (z-value). So z ' coordinates can hold the z-value of the P-point directly. Because before rasterization, we need to interpolate the reciprocal of the z-coordinate (see Mathematics for 3D Game programming and Computer grahpics 3rd section 5.4), so you can put Z ' The form of an expression written as Z, as follows

Before mapping, the range of z is [N,f], where N and F are nearly two of the distance from the clipping plane to the origin, after the mapping, the range of z "is [0,1], the data is placed in the above one-time, the following equations can be

The solution to this equation group is to get

So

Tidy it up a bit.

Substituting x ', y ', Z ' into the first matrix multiplication equation.

The above is visible, X ', y ', Z ' are divided by the PZ, so we multiply them by the PZ (which is not the size of the coordinates) and get the following equation.

Note here that X is px,y i.e. py,z is PZ, and each column of the solution matrix gets

So the matrix is

Code

Generally speaking, in the program we usually given four parameters to find the perspective projection matrix, respectively, the angle of view of the y direction, aspect ratio, near the clipping plane to the origin of the distance and the distance from the far clipping plane to the origin, through the four parameters can be obtained above the matrix, the code is as follows.

D3dxmatrix Buildprojectionmatrix (Float FOV,float aspect,Float Zn,FloatZF) {D3dxmatrix proj; ZeroMemory (&proj,sizeof(proj)); proj.m[0] [0] = 1/(Tan (FOV * 0.5f) *aspect); Proj.m[1][1] = 1/tan ( FOV * 0.5f2][  Zn); proj.m[ 2] [3] = 1.0f3][2] = (ZN * Zf)/(Zn- ZF); return Proj;}           

After the matrix is solved, it is now possible to try the results with the following code, which is consistent with the effect of using the D3D function D3dxmatrixperspectivefovlh.

G_pd3ddevice->settransform (d3dts_projection, &proj) ;

Happy Coding!!!

Perspective projection detailed "Go"

Related Article

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.