Introduction to Direct3D rendering Pipelines

Source: Internet
Author: User

The rendering pipeline is responsible for performing a series of necessary steps to convert 3D scenes into 2D images that can be displayed on the display. In Direct3D, the steps for rendering pipelines are roughly as follows:

(1) local coordinate system to world coordinate system

Suppose we are creating a game. Now, we need to build a blacksmith shop for storing in the game scene. It is impossible for us to build a blacksmith shop in a game scenario (World coordinate system), because we do not know where it will be placed, how big it will be, and where it will be oriented. Therefore, we construct the blacksmith shop in the local coordinate system. The so-called local coordinate system is the coordinate system centered on the object itself. In this way, we do not need to consider the factors of the blacksmith shop in the game scenario. Displays the description of a cube in a local coordinate system:

Once we build an object in the local coordinate system, we place it in the world coordinate system. To complete this step, we need to build a world transformation matrix, we can directly build a world transformation matrix using the interface provided by D3DX. Here, I will introduce another method for building the world transformation matrix, because it will be used in view transformation later.

First look at the figure:

Assume that Frame A is A local coordinate system, Frame B is A world coordinate system, and B is A point in Frame A. How can we convert B from A local coordinate system to A world coordinate system? It is very simple. Assume that coordinate point B in the local coordinate system is described as (x, y), and the two coordinate axes and origin points of the local coordinate system Frame A are described as u in the world coordinate system Frame B, v and o, we can see from the figure that o + xu + yv is the description of B in the world coordinate system. It is the same principle to convert to a 3D coordinate system, except that there is an extra z axis. If the z axis is described as w in the world coordinate system, a point (x, y, z) in the local coordinate system is) you can write o + xu + yv + zw to the world coordinate system.

As a result, we construct a world transformation matrix. Assume that the origin of the local coordinate system and the three coordinate axes are described as p, r, u, and f in the world coordinate system, the world transformation matrix can be constructed as follows:

(2) World coordinate system to view Coordinate System

The view coordinate system is the local coordinate system of the camera. We can think of the camera as an object in 3D scenarios. In the View coordinate system, the camera is in the center of the coordinate system and faces the positive direction of the Z axis. It is very difficult and inefficient to perform the projection transformation of objects at any position. Therefore, we need to transform the objects to the view coordinate system before performing the projection transformation. Why do we need to explain the above method for building the world transformation matrix? Because it constructs the world transformation matrix with three axes and the origin. Now, assume that the three coordinate axes and origin of the view coordinate system are described as r, u, f, and p in the world coordinate system, so we know that the transformation matrix of the object in the view coordinate system to the World transformation matrix under the world coordinate system is that we only need to build its inverse matrix to convert the object in the world coordinate system to the view coordinate system.

Since the transformation matrix can be decomposed into the product of the translation matrix and the rotation matrix, we can build the inverse matrix as follows:

(3) Projection Transformation

Before a 3D scene is finally converted to a 2D image and displayed on the display, it also needs to undergo projection transformation. Direct3D expects the points after the projection transformation to be in the standard sight body (x, the range of y is [-1, 1], and the range of z is [0, 1]).

In order to perform projection transformation, we need to define the ratio of the head-to-head intercept and the horizontal-vertical ratio. The flattened part is similar to the flattened pyramid. Therefore, we need to define a near-Plane n, a far-flattened original f, and a visual angle of the vertical range.α. The vertical/horizontal ratio is usually defined as the width/height of the backend buffer.

Then how can we determine the visual angle of the vertical range?αObtain the visual angle of the horizontal range.βWhat about it? According to the triangle formula, we have tan (α/2) =D/nAnd tan (β/2) =C/nAnd R (horizontal/vertical ratio) = 2c/2d 1_c = Rd. So we can get tan (β/2) = c/n = Rd/n = Rtan (α/2 ).

After defining the data, we need to perform the actual projection transformation. First, we need to transform the x and y coordinates:

According to the similarity Triangle Theory, x/x' = z/n then x' = xn/z. Similarly, y/y' = z/n then y' = yn/z. Because x' must be in the range [-c, c] And y' must be in the range [-d, d], and Direct3D expects x of the point after the projection transformation, the range of y is [-1, 1], so we need to normalize them to the range [-1, 1].

-CX'CMemory-1 ≤X'/c≤ 1

-DY' <dMemory-1 ≤Y'/d≤ 1

Then we obtain the x and y coordinates in the range [-1, 1:

Next, we transform the zcoordinate. Direct3D expects that the z range of the point after the projection transformation is [0, 1], so we need to normalize it to the range [0, 1].

Since the normalized x and y coordinates contain zcoordinates, we want to multiply them by z. In this way, zcoordinates are written as follows, in this way, you can write the form of a matrix.

Now the problem is how to calculate the u and v. We know that the near Plane n is converted to 0 after the projection transformation, and the far plane f is converted to 1 after the projection transformation, we haveU + v/n =0,U + v/f= 1, we can obtain:

U+ (-Un)/F= 1

Uf-Un=F

U=F/(F-N)

V=-Un=-Fn/(F-N)

Finally, we get

Then, we can write a matrix:

Note: (x, y, z, 1) after the transformation, it is not in the standard horizon, and the homogeneous transformation in the rendering pipeline is also required.

(4) view Transformation

The view transform function is used to convert the points in the standard view area to the background buffer area (the view area ). Generally speaking, the viewport is the rectangle of the entire background buffer, but it can also be part of the background buffer. The display defines four views:

It is quite easy to define the start x, y coordinates, width and height, and the minimum and maximum z values of the view.

How can we convert the points in the standard sight to the viewport? Assume that the start coordinate of the view is X, Y, the Width and Height of the view are Width, Height, and the minimum and maximum z values are MinZ and MaxZ.

We know that the range of x and y in the standard horizon is [-1, 1]. We need to convert it to the range [X, X + Width] and [Y, Y + Height]. as follows:

-1 ≤ x' ≤ 1

0 ≤ x' + 1 ≤ 2

0 ≤ (x' + 1)/2 ≤ 1

0 ≤ Width * (x' + 1)/2 ≤ Width

X ≤ Width * (x' + 1)/2 + x ≤ X + Width

X ≤ Width * x'/2 + Width/2 + x ≤ X + Width

 

The y coordinate conversion is the same, but you only need to note that the y axis is vertical up, so the-1 of Y should be y + Height after conversion to the viewport, as shown below:

-1 ≤ y' ≤ 1

-2 ≤ y'-1 ≤ 0

-1 ≤ (y'-1)/2 ≤ 0

-Height ≤ Height * (y'-1)/2 ≤ 0

(Multiplied by-1) 0 ≤-Height * (y'-1)/2 ≤ Height

Y ≤-Height * (y'-1)/2 + y ≤ Y + Height

Y ≤-Height * y'/2 + Height/2 + y ≤ Y + Height

 

In the standard field of view, the z range is [0, 1]. We need to convert it to [MinZ, MaxZ], as shown below:

0 ≤ Z' ≤ 1

0 ≤ Z' * (MaxZ-MinZ) ≤ MaxZ-MinZ

MinZ ≤ Z' * (MaxZ-MinZ) + MinZ ≤ MaxZ

 

At this point, we have constructed a matrix for the transformation of the viewport:

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.