3D games in a variety of spatial transformation in the end is how is it

Each game can be presented behind the dazzling effect, it requires a series of complex calculations, but also accompanied by a variety of vertex space transformation. The rendering process of the game is to be understood as a vertex through layers of processing after the final conversion process to on the screen, this paper aims to explain, through which vertex is the coordinate space, eventually painted on our screen.

Principle space conversion

First, let's look at a simple question: which when given a little space, and coordinate (a, b, c), how we know the location of the point of it?

  1. From the origin of the coordinate space
  2. A mobile unit in the x-axis direction,
  3. Mobile units b y-axis direction
  4. C units move in the z-axis direction,

Transformation coordinate space are begun in the step 4 above. Now, we know three coordinate space C axis at coordinate space P represents X- C , the Y C , the Z C , and an origin position O C . When a given point in the coordinate space C A C = (A, B, C), we can also determine its position P A in the coordinate space in accordance with the above four steps p

  1. From the origin of the coordinate space, i.e., O C
  2. A mobile unit to the x-axis direction, i.e., O C + aX C
  3. Mobile units b y-axis direction, i.e., O C + aX C + bY in C
  4. C units move in the z-axis direction, i.e., O c + aX c + bY in c + cZ c

To transform the expression obtained as follows, where "|" symbol indicates expansion in columns

Plus expression continues to transform them to do, that extended to homogeneous coordinate space to do translation transformation

Now, we obtain the spatial coordinates of the coordinate transformation matrix C to the space P M the C-> P

It can be seen M the C-> P is actually represented by a vector constructed out of the C coordinate space P in the coordinate space and the origin of the coordinate axes: the axis 3 successively before the matrix into three, the origin of this vector discharge to the last column, then 0 and 1 to the last row is filled.

We can use reverse thinking, this transformation matrix is ​​extracted from the origin and the coordinate axis C coordinate space P in the coordinate space of the representation. For example, when we know from the 4 × 4 model space to world space transform matrix, we can extract its first column, and then normalized (to eliminate the effect of scaling) to obtain x-axis in the world space model It represents a unit vector in space. The same method can extract the y and z axes.

When the orientation vector transformation coordinate space, because there is no position of the vector, thus the origin of the coordinate space transformation it is negligible. Then the coordinate space direction vector transformation can use a 3 × 3 matrix to indicate that the

In Shader, we often see three to normal direction, the direction of illumination spatial change 3 lines intercepted transform matrix before, this is the reason.

Once the seek out M the C-> P , M p-> C can find out by means of inverse matrix, because the coordinate transformation from the coordinate space to the space C P P transformed from the coordinate space to coordinate space and C is the reciprocal of two a process. When M the C-> P is an orthogonal matrix when, M the C-> P is equal to its inverse matrix transpose matrix, i.e.,

In this case, we can also M > the C-P anti-Release axis represents the X coordinate space P in the coordinate space C is P , the Y P , the Z P , which is the axis corresponding M the C-> P each row .

Model Space

Model space, and is said to be a model or an object related, also known as model space object space or local space.
Each model has its own independent coordinate space when it moves or rotates when the model space will follow the movement and rotation.
Unity used in the model space is left-handed, so in the model space, + x-axis, + y-axis, + z-axis corresponding to each of the model is a right, upper, forward.
Origin and axis model space is usually determined by the art staff in the modeling software was good. When introduced into the Unity, we can access the vertex shader vertex information model, which contains the coordinates of each vertex. These coordinates are relative to the model space origin (usually located in the center of gravity Model) defined.

World Space

Is a special world space coordinate system, because it established the largest space we are concerned that the whole game space
in Unity, the world space using the same left-handed coordinate system. Its x axis, y axis, z-axis is fixed.
The first step in the transformed vertices is vertex coordinate transformation from model space to world space, this transformation is often called model transformation
in Unity, we can transform what had been known by the model worth Transform component. This value is based on the definition of origin of the model coordinate space of the parent node in the Transform, Transform this if there is no parent, then this value is relative to the world coordinate space defined.

To model a point in space converted to its parent space, need to acquire M child -> parent , through this matrix can be worth to Transform the model. Transform included rotation, scaling and translation values, then the M sub -> Parent = M Translation M Rotate M Scale . The transition from model space to world space transformation matrix M model -> the world through subspace to the parent space conversion matrix, the father of space to grandfather space conversion matrix, even by until a world space so far.

Space observation

Also known as space observation camera space, it can be considered a special case of the model space, that model space camera.
In Unity observed space using a right-handed coordinate system, i.e. + x-axis points to the right, + y axis pointing upward, + z axis pointing rear camera
transformed vertices second step is to transform the vertex coordinates from world space to viewing space in. This transformation is often called observation transformation .

From the observation space to world space transformation matrix we can pass worth Transform in to, then the inverse matrix of the transformation matrix obtained from observation space to world space. We can also use another method, the value of the direct component Transform negated (do an inverse transform), and then the transformation matrix obtained from world space to the viewing space. Note that, since the observation space is right-handed use, it is also necessary to perform the z-component of the transformation matrix inversion operation.

Clip space

The next vertex from the viewing space to clip space converter, this conversion may be referred to projection transformation . This is called a matrix for converting clip matrix or the projection matrix
to tailor the space to be able to easily cut rendering primitives: FIG located entirely inside this space element will be retained, completely outside of this space in FIG. yuan will be removed, primitive intersects the boundary of this space will be cut. And this space is made depending on the vertebral body to decide.
There are two types depending on the vertebral body, respectively, corresponding to two kinds of projections: perspective projection (lower left) and vertical projection (lower right). Perspective projection simulated human eyes at the world, and is fully retained the orthogonal projection angle and object distance.

Although the projection matrix is called the projection matrix, but did not really projected, but prepare for the projection. Object is x, y, z components is scaled, after scaled projection matrix, we can directly use the w component as a range of values, only the x, y, z components are positioned at a vertex within this range was considered within the clip space . And w components also used in real time projection.
Orthogonal projection and a perspective projection corresponding to a different projection matrix. It is also noted that the handedness of the projection will change the spatial matrix: space conversion from right-handed to left-handed

Perspective projection

Wherein the opening angle of view FOV represents vertebral vertical direction, and Near and Far control the near clipping plane and the far clipping plane distance from the camera. In this way we can find the height of the near clipping plane, as shown below. Far clipping plane similar.

According to the aspect ratio of the camera information, what can I get the width of the near clipping plane

A result of perspective projection vertices and multiplying the projection matrix as follows

Changes depending on the vertebral body below

At this point we can vertices the following inequality is transformed to determine a view of the vertebral body is located within the

Orthogonal projection

Size wherein represents half the height of the top view of the vertebral body in the vertical direction, the Near and Far similarly control the distance of the near clipping plane and the far clipping distance from the camera. The height of the near clipping plane is shown below. Far clipping plane similar.

The height of the near clipping plane can also be obtained by the aspect ratio of the camera

A result of the orthogonal projection of the projection vertices and multiplying the projection matrix as follows

Changes depending on the vertebral body below

After determining a vertex is located at the same conversion and perspective projection inequality used in the view of the vertebral body, this versatility is why one of the reasons to use a projection matrix.

Screen space

Upon the completion of all the cutting work, on the need for a real projection, depending on the vertebral body that is projected onto the screen space, this process can be called screen map . After this step transformation, we'll get a real pixel location corresponding 2D coordinates, instead of a virtual three-dimensional coordinates. This process can be understood as two steps:

  1. Standard homogeneous division , also called perspective division. Is the homogeneous coordinates x, y, z component of the component divided by w. In OpenGL this step to get the coordinates is called normalized device coordinates (NDC). After the clip-space perspective projection conversion will be converted into a cube, the orthogonal projection of a cube cut space itself (its components are w 1, homogeneous division does not affect it). In Unity of the cube X,, y z component of the range is [-1, 1], and OpenGL consistent.

  2. After homogeneous division, and orthogonal projection of the perspective projection view of a vertebral body are transformed to the same cube. Now, we can map the output pixel coordinates corresponding to the window according to the x, y coordinates after conversion.

In Unity, lower left corner of screen space pixels is (0, 0), the pixel coordinates of the upper right corner is (pixelWidth, pixelHeight). Homogeneous division process and screen mapping can be used represented by the following formula

\[screen_x = \frac{clip_x * pixelWidth}{2 * clip_w} + \frac{pixelWidth}{2} \]

\[screen_y = \frac{clip_y * pixelHeight}{2 * clip_w} + \frac{pixelHeight}{2} \]

In Unity, the transition from clip space to screen space is done from the bottom to help us. We just need the vertex shader vertex can be converted to clip space (model space - world space - space observation - clip space, the corresponding series into a matrix usually MVP matrix).

The normal conversion

Finally, we look at a special kind of transformation: the normal conversion. In the game, a vertex models tend to carry additional information, vertex normals and tangents is one of the two types of information, tangents and normals are perpendicular to each other.
Since the tangent is calculated from the difference between the obtained two vertices, so we can directly use the transformed vertex matrix M A-> B transformed tangent. However, if the direct use of M A-> B transformed normal, obtained new law may not line and the perpendicular to the tangent. For example, as shown in FIG.

So which matrix should be used to transform line on the road? We can deduce this matrix by mathematical constraints. Since the vertex normal N A and the tangent T A vertical, the T A N A = 0. Given the transformation matrix M A-> B , we know T B = M A-> B T A . Now we want to find a transformation matrix G normal N A normal, so that the transformed and remains perpendicular to the tangent, i.e.,

Available through some derivation

Since T A N A = 0, thus obtained

which is

This shows that using reversed matrix of the transformation matrix transforming the original normal correct result can be obtained.
It is noted that, if the matrix M A-> B is an orthogonal matrix, then we can use directly the original transformation matrix as the transformation matrix is normal. If the transformation contains only the rotation and uniform scale, we can use the uniform scaling factor k to obtain the transformation matrix M A-> B reversal of a matrix, so that the process of calculating the inverse matrix can be avoided.

Viewport space

Viewport coordinate space is referred to as the viewport coordinates, the normalization is to the screen, so that the lower left corner of the screen is (0, 0), the upper right corner is (1, 1). If you know the screen coordinates, we just need x, y component of the screen resolution can be obtained by dividing the viewport coordinates. If the clip space coordinates are known, the coordinates of the viewport may be obtained by the following formula

Guess you like

Origin www.cnblogs.com/iwiniwin/p/12547800.html