DirectX12 3D game development and combat of Chapter III

Convert

learning target

  1. Understand how to use a matrix representing a linear transformation and affine
  2. Coordinate transformation learning geometry scaling, rotation and translation of
  3. The nature of the multiplication between the matrix, a plurality of transformation matrices combined into a single net transformation matrix
  4. The method of finding the coordinate conversion between different coordinate systems, and using this coordinate transformation matrix to represent
  5. Familiar with the function DirectXMath library specifically for the construction of the transformation matrix provided

3.1 Linear Transformation

3.1.1 definitions

If there is a function of t can

t(u + v) = t(u) + t(v)
t(ku) = tk(u)

Establishment, the function is called a linear function of t, also called linear transformation. Wherein u and v are 3D vector, k is a scalar. (Non-3D vector may be used as a linear transformation of input and output, but in general we discuss 3D graphics 3D linear transformation vectors)

3.1.2 Matrix notation (focus)

slightly()

3.1.3 Zoom

Scaling (scaling) refers to the size of the object changes, we generally defined scaling

S(x,y,z) = (s1x,s2y,s3z)

s1, s2, s3 represent three component S

This transform relative to the current origin of the coordinate system, respectively, so that the vector coefficients s1, s2, s3 scaling in x, y, z-axis, S is a linear transformation, this is not proven

  • About scaling transformation matrix representation of S

S (i) = (s1 1, s2 0, s3 * 0);

S(j) = (s10,s21,s3*0);

S (k) = (s1 0, s2 0, s3 * 1);

So scaling matrix S matrix is ​​represented as

{
    s1,0,0,
    0,s2,0,
    0,0,s3
}

Example: If you want to refine a square 0.5 x axis, the y-axis method of 2.0 times, unchanged on the z axis, may be employed scaling matrix S

{
    0.5,0,0,
    0,2,0,
    0,0,0
}

The matrix S and the minimum point coordinates are square, the square coordinates by multiplying the maximum

3.1.4 rotation

This section will be described mathematically by the vector v n at an angle theta about axis rotated vector v we will first be decomposed into two parts, one part is parallel to n, n is orthogonal to the other part, during rotation, the parallel n part is to remain unchanged, so we need only consider the normal to the part of n.

Derivation: Slightly

Rotational transformation is also a linear transformation, it may be converted to a matrix representation. Rotational transform matrix is ​​expressed as:

{
    c+(1-c)x^2  (1-c)xy + sz    (1-c)xz-sy
    (1-c)xy-sz  c+(1-c)y^2      (1-c)yz+sx
    (1-c)xz+sy  (1-c)yz-sx      c+(1-c)z^2
}

Wherein x, y, z indicates a rotary shaft of n (x, y, z), s and c respectively represent sin (theta) and cos (theta)

Examples: Slightly

3.2 affine transformation

3.2.1 homogeneous coordinates

We can see in the next section, the affine transformation is a linear transformation and combination of a translation transform , the vector, the shift operation makes no sense, because only describes the direction of the vector size, and position-independent. Therefore, the panning operation can be applied only point (i.e., the position vector). Is the mechanism employed in homogeneous coordinates provided, we can easily point to the vector and uniform treatment, in homogeneous coordinates, the coordinates will be expanded to four-tuple, the fourth coordinate values will be described according to object is a point or vector may be, specifically,

  1. (X, y, z, 0) represents the vector
  2. (X, y, z, 1) represents the point

3.2.2 defines the affine transformation matrix and represents

Shows linear transformation does not transform all we need, so we need to expand it into a broader range of the mapping known as a function of the class of affine transformation, an affine transformation is a linear transformation + i.e. a translation vector

a(u) = t(u) + b

It may also be represented by a matrix notation:

{
    A1  A2  A3  0
    A4  A5  A6  0
    A7  A8  A9  0
    Bx  By  Bz  1
}

Wherein An represents a linear transformation matrix, Bx, By, Bz is a translation vector b

3.2.3 translation

Identity transform is a linear transform which returns directly input parameters, i.e., t (u) = u; difficult to see, t linear transformation matrix is ​​expressed as matrix. Translation transformation will now be defined as affine transformation, which is a linear transformation that is the identity transform, the affine transformation equation is translated

t (u) = uI + b = u + b

Where I is the identity matrix.

Translation transformation matrix expressed as follows:

{
    1   0   0   0
    0   1   0   0
    0   0   1   0
    Bx  By  Bz  1
}

Wherein Bx, By, Bz are each component of a translation vector b

3.2.4 affine scaling and rotation matrix

The rotation matrix and a scaling matrix expanded to a 4x4 matrix, i.e. a w value of each row vector is set to 0, while adding a translation vector b (0,0,0,1) to

Geometric meaning affine transformation matrix

slightly

Composite (combined transformation) conversion

Question: Suppose there is a scaling matrix S, the rotation matrix R, the translation matrix T, there is now an eight vertices of the cube, I would like to transform these 3 sequentially applied to each vertex of a cube, can be

((Vi S) R) T        //其中Vi为正方体的每一个顶点

Since the matrix multiplication is associative, it is possible to make the matrix C = SRT; i.e. before these packages 3 into a net transformation matrix, and allows us to operate, but also can improve the performance of

3.4 coordinate transformation (coordinate transformation)

  • In a subsequent study, we have to face different frame to convert coordinate points or vectors, we convert different frame key is called a coordinate transformation

  • In the coordinate transformation process, the geometric transformation itself does not occur, but the reference coordinate system transformation object changes. In contrast, we can consider scaling, rotation, translation of these operations was the geometry undergone substantial changes

  • In computer graphics, we will use many different coordinate systems, and therefore need to learn how to convert between them, since the position is the attribute points, regardless of the vector, so different when the coordinates of the point and vector conversion, below we will separate introduction.

3.4.1 coordinate transformation vector

Consideration: wherein a vector p are positioned frames 1 and 2 in the frame, the coordinate set vector p in frame 1 as P1 = (x1, y1); then how to obtain the vector p corresponding coordinates in the frame 2 ?

Solution process: a little (method main application translation)

The answer: P2 = xu + yv + zw

u, v, w respectively represent a unit vector pointing in the frame 1 x axis, y axis and z-axis direction (represented performed in frame 2)

3.4.2 point coordinate transformation

Bit different point coordinate transformation vector, because the position of the point is an important property, and therefore can not be confused with the points and vectors, the vectors can not be simply translated onto a point Methods

Solution process: a little

Conversion formula: P2 = xu + yv + zw + Q

u, v, w represent beacon frame in an X-axis unit vector in the positive direction of the y-axis and z-axis (in 2 frames represented), Q represents the coordinates 1 frame origin in the frame 2

3.4.3 coordinate transformation matrix representation

In the two previous paragraphs, we have introduced a coordinate transformation vectors and points

(x',y',z') = xu + yv + zw           //对于向量而言
(x',y',z') = xu + yv + zw + Q       //对于点而言

If using homogeneous coordinates, the above two formulas may be combined into a single formula, namely:

(x',y',z',w) = xu + yv + zw + Q

In the above formula of coordinate transformation, if w = 0; said vector, w = 1 is represented by point, so that we can remember only the one formula. Matrix above formula is expressed as:

{
    u1  u2  u3  0
    v1  v2  v3  0
    w1  w2  w3  0
    Q1  Q2  Q3  1
}

This matrix is ​​called a frame coordinate transformation matrix or the transformation matrix

3.4.4 coordinate transformation matrix and associativity

Question: Suppose there are three frames, respectively 1,2,3, and known three frames of frame transformation matrix are A, B, C, there is a vector of coordinates in the frame P 1, to this vector required the corresponding coordinates in the frame 3, can be

(PB)C = p';

However, such calculation is not efficient, because the matrix multiplication is associative, we can make H = BC; can then use PH = P '; can complete conversion

3.4.5 coordinate transformation matrix and its inverse matrix

slightly

3.5 coordinate transformation matrix and the transformation matrix

In the preceding summary, we have emphasized "the geometry itself change" transformation (scaling, rotation and translation) and distinguish coordinate transformation, however, in this section, we will prove that: From a mathematical point of view, both It is equivalent.

Proof: Slightly

Transform function library provides 3.6DirectXMath

In this section we will DirectXMath function library and transform relevant summarize, for a reference to the future.

//构建一个缩放矩阵
XMMATRIX XM_CALLCONV XMMatrixScaling(
    float ScaleX,       //缩放系数
    float ScaleY,       //缩放系数
    flaot ScaleZ        //缩放系数
)
//用一个3D向量中的分量来构建一个缩放矩阵
XMMATRIX XM_CALLCONV XMMatrixScalingFromVector(
    FXMVECTOR Scale;    //缩放系数(Sx,Sy,Sz)
);
//构建一个绕x轴旋转的旋转矩阵
XMMATRIX XM_CALLCONV XMMatrixRotationX(
    float Angle         //以顺时间方向旋转Angle弧度
);
//构建一个绕y轴旋转的旋转矩阵
XMMATRIX XM_CALLCONV XMMatrixRotationY(
    float Angle         //以顺时间方向旋转Angle弧度
);
//构建一个绕z轴旋转的旋转矩阵
XMMATRIX XM_CALLCONV XMMatrixRotationZ(
    float Angle         //以顺时间方向旋转Angle弧度
);
//构建一个绕任意轴旋转的矩阵
XMMATRIX XM_CALLCONV XMMatrixRotationAxis(
    FXMVECTOR Axis,     //旋转轴n
    float Angle         //沿n轴正方向看,以顺时针方向按弧度Angle进行旋转
);
//构建一个平移矩阵
XMMATRIX XM_CALLCONV XMMatrixTranslation(
    float OffsetX,      //平移系数 
    float OffsetY,      //平移系数
    float OffsetZ       //平移系数
);
//用一个3D向量中的分量来构建平移矩阵
XMMATRIX XM_CALLCONV XMMatrixTranslationFromVector(
    FXMVECTOR Offset    //平移系数(Sx,Sy,Sz)
);
//计算向量和矩阵的乘积VM,此函数默认w = 1;即针对点计算
XMVECTOR XM_CALLCONV XMVector3TransformCoord(
    FXMVECTOR V,        //输入向量V
    CXMMATRIX M         //输入矩阵M
);
//计算向量和矩阵的乘积VM,此函数默认w = 0;即针对向量计算
XMVECTOR XM_CALLCONV XMVector3TransformNormal(
    FXMVECTOR V,        //输入向量V
    CXMMATRIX M         //输入矩阵M
);

summary

1, the scaling, translation and rotation transformation matrix that three basic operations are:

S = 
{
    Sx  0   0   0
    0   Sy  0   0
    0   0   Sz  0
    0   0   0   1
};

T = 
{
    1   0   0   0
    0   1   0   0
    0   0   1   0
    b1  b2  b3  1
};

R = 
{
    c+(1-c)x^2  (1-c)xy + sz    (1-c)xz-sy
    (1-c)xy-sz  c+(1-c)y^2      (1-c)yz+sx
    (1-c)xz+sy  (1-c)yz-sx      c+(1-c)z^2
}

2, when represented by using the homogeneous coordinate transformation, we used 1 x 4 homogeneous coordinates describe points and vectors. When the fourth component w is set to 0, the vector, w = 1, then the point. This allows translation only applies to the point, without affecting the vector

3, if a row vector in the matrix are used and are orthogonal to each unit length, the matrix is ​​called orthogonal matrix, the inverse matrix of the orthogonal matrix and the transposed matrix are equal, the inverse matrix of the orthogonal matrix corresponds to easily calculation. All of the rotation matrix are orthogonal matrix

4, since the matrix multiplication satisfies the associative law, so we generally several transformation matrices into a single matrix, so that the operation efficiency can be improved

5, set Q, u, v, w represent 1 frame origin, x-axis, y-axis, z-axis relative to the coordinate frame 2, if a vector p corresponding coordinates in the frame 1 is (x coordinate, y, z), the vector with respect to the frame 2 is

p' = xu + yv + zw;              //针对向量
p' = xu + yv + zw + Q;          //针对点(位置向量)

6, since the matrix multiplication satisfies the associative law, so for switching between a plurality of frames, we can merge the plurality of frame transform matrix into a matrix, the operation efficiency can be improved

7, if the matrix A coordinate frame may be mapped to 1 from the frame 2, then the inverse matrix A may coordinate mapping from the frame 2 to frame 1

Guess you like

Origin www.cnblogs.com/yaya12138/p/11458617.html