Implementing the 3D image engine from scratch :( 11) explaining the derivation of the 3D Transformation Matrix

Source: Internet
Author: User

1. Mathematical Analysis

In the previous article, I directly used the Rotation Transformation Matrix during rotation. At that time, I felt very embarrassed because I didn't say how to generate the matrix before.

 

1) the subtle relationship between matrices and Vectors

If you still remember the geometric meaning of vector addition, it is not difficult to understand the following equation:

[X] [x] [0] [0] [1] [0] [0]

V = [y] = [0] + [y] + [0] = x * [0] + y * [1] + z * [0]

[Z] [0] [0] [Z] [0] [0] [1]

 

We can see a vector or a point that represents the sum of the product of each component of the vector and the unit vector of a coordinate axis.

We set P, Q, and r to represent the three unit vectors, which can be converted to this form:

V = XP + yq + Zr

 

Now we generalize P, Q, R, so that p = [px py pz], q = [qx qy qz], r = [RX ry RZ], we make them into a matrix.

[Px py pz]

M = [qx qy qz]

[RX ry RZ]

 

Then multiply the vector [x y z] by the matrix to obtain

[X y z] × m = [(x * px + y * qx + z * RX) (x * py + y * Qy + z * ry) (x * PZ + y * qz + z * rZ)] = XP + yq + Zr

 

Haha, have you seen the role of M? He performed a matrix transformation on the vector [x y z] and changed it to v. This is the source of the transformation matrix. What can he do? The following is the focus.

 

We use the unit vector of the coordinate axis (we call it the base vector) to multiply them by M:

[1 0 0] x m = P

[0 1 0] x m = Q

[0 0 1] x m = r

Haha, this property is too important: for a coordinate axis (the preceding [1 0 0] [0 1 0] [0 0 1] is the unit vector of a coordinate axis ), we can use a vector to transform it. For example, for the X axis, we only need to provide a transformation vector p to convert the X axis according to the P vector. In addition, we can give P, Q, R at the same time, and transform the three axes X, Y, and Z at the same time.

 

The figure below shows how we can use this nature:

We can regard the green and red arrows in the graph as the unit vector of the coordinate axis, which corresponds to the X axis [1 0] and the Y axis [0 1] respectively. What will happen to the transformation?

For example, if we convert the X axis from the vector [1 0] to [2 1] and the Y axis from [0 1] to [-1 2], the effect is as follows:

After this transformation, we found that the flight attendant's photos were not only enlarged, but also rotated around the Z axis. This is the matrix we just built.

Role of M = [2 1]

[-1 2]

 

Now let's figure out the relationship between the transformation matrix and the vector. We can build the matrix m and use the first line of m to transform the x-axis vector of the object coordinate system, use the second line of m to transform the y-axis vector of the object coordinate system, and use the third line of m to transform the z-axis vector of the object, just like controlling the operation handle of 3ds max. Now we can deduce the rotation matrix.

 

2) Derivation of the 3D Rotation Transformation Matrix.

First, we also use a top view to deduce. We look down along the Z axis and now let the object rotate around the Z axis. How should we change the X axis control handle vector and Y axis control handle vector of the object? Figure:

Because it is rotated around the Z axis, the X axis and Y axis should be rotated at the same angle and set to Theta. For the X axis unit vector (), after Theta is rotated, the coordinates become cos (theta) and sin (theta ). After the Y axis unit vector () rotates Theta, the coordinates become-sin (theta) and cos (theta ). Then we begin to organize the matrix:

For the X control handle, the transformation vector is: [cos (theta) sin (theta) 0]

For the y Control handle, the transformation vector is: [-sin (theta) Cos (theta) 0]

For the Z control handle, the transformation vector is: [0 0 1] (because the Z control handle is not changed, it is thrown as the Z axis unit vector)

Now, we have obtained the transformation matrix of a point rotating around the Z axis:

M =

[Cos (theta) sin (theta) 0]

[-Sin (theta) Cos (theta) 0]

[0 0 1]

 

 

 

2. Code Implementation

According to the above derivation, we can also introduce the transformation matrix around X and Y axes. The following function is to achieve a comprehensive rotation, so that we can get a point, round x, y, z transformation matrix at the same time:

Void _ cppyin_3dlib: buildrotatematrix (double anglex_du, double angley_du, double anglez_du, matrix4x4_ptr m) // create and simultaneously enclose x, y, Z rotates the transformation matrix of different degrees <br/>{< br/> // initializes the three transformation matrices for rotation around X, Y, and Z axes, and copies them to the unit matrix, if the axis is not rotated, the unit matrix does not affect the result. <br/> matrix4x4 MX, my, Mz; <br/> memcpy (void *) (& mx ), (void *) & matrixi_4x4, sizeof (matrix4x4); <br/> memcpy (void *) (& my), (void *) & matrixi_4x4, sizeof (matrix4x4); <br/> memcpy (void *) (& MZ), (void *) & matrixi_4x4, sizeof (matrix4x4 )); </P> <p> double sin_theta, cos_theta; // cache calculation results to reduce CPU overhead </P> <p> If (anglex_du> epsilon) <br/>{< br/> cos_theta = fastcos (anglex_du); <br/> sin_theta = fastsin (anglex_du); </P> <p> matrixcreate (& MX, <br/> 1, 0, 0, 0, <br/> 0, cos_theta, sin_theta, 0, <br/> 0,-sin_theta, cos_theta, 0, <br/> 0, 0, 0, 1); <br/>}</P> <p> If (angley_du> epsilon) <br/>{< br/> cos_theta = fastcos (angley_du); <br/> sin_theta = fastsin (angley_du); </P> <p> matrixcreate (& my, <br/> cos_theta, 0,-sin_theta, 0, <br/> 0, 1, 0, 0, <br/> sin_theta, 0, cos_theta, 0, <br/> 0, 0, 0, 1); <br/>}</P> <p> If (anglez_du> epsilon) <br/>{< br/> cos_theta = fastcos (anglez_du); <br/> sin_theta = fastsin (anglez_du); </P> <p> matrixcreate (& Mz, <br/> cos_theta, sin_theta, 0, 0, <br/>-sin_theta, cos_theta, 0, 0, <br/> 0, 0, 1, 0, <br/> 0, 0, 0, 1); <br/>}</P> <p> // transform matrix multiplication, obtain the final transformation matrix <br/> matrix4x4 mtemp; <br/> matrixmul (& MX, & my, & mtemp); <br/> matrixmul (& mtemp, & Mz, m); <br/>}

 

Using this function simplifies some of the functions in the previous article. You don't need to write these matrices every time. This demo executes the X and Z axes transformation for the last pyramid at the same time.

Note that I have modified the sequence of world transformations and rotating objects, and the storage location of the rotated points is different.

:

 

 

 

3. Download Code

Download complete project source code:> click to enter the download page <

 

 

 

4. Additional content

In fact, this time we only talked about the derivation of the rotating matrix, but through this process, we know the subtle relationship between the vector and the Matrix. For other transformations, such: scaling, projection, image, and shear can all be derived from this relationship. There is no extra charge here.

 

A special case is translation. If you read the article carefully, you will find that the transformation of the three axes X, Y, and Z cannot achieve the goal of moving objects, that's why we need to use 4D vectors to represent and transform. How to use these 4th dimensions? If we have time, let's study the principle of 4D homogeneous coordinates, if you are interested, you can send a link to share it.

 

This guest flight attendant is purely interesting. All the rights of ownership, issuance, and publication belong to you. If you think your rights have been violated, I will change the picture immediately ~~

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.