Reprinted to: https://blog.csdn.net/cordova/article/details/52558133
background
Following the translation transformation of the previous tutorial, here we begin to learn the rotation transformation, that is, it is possible to rotate a point by a certain angle along a coordinate axis. A rotation transform will always change two of the coordinates of the position, and the third one will remain the same, which means the path of the rotation will remain on one of the planes: the XY plane (rotation around the Z axis), the YZ plane (the rotation around the X axis) rotation) and the XZ plane (rotation around the Y axis). There are also some complex rotation transformations that allow the graph to rotate around arbitrary vectors, but we don't need it at this stage.
Let us define this problem in terms of universal unification. Look at the picture below:
We want to move along the circle from (x1,y1) to (x2,y2), in other words rotate the point (x1,y1) by a2 angle. Assuming the radius of the circle is 1, it has the following formula:
We use the following trigonometric transformation formulas to derive the expressions for x2 and y2:
From the above formula we can get:
In the above picture we are looking at the XY plane, and the Z axis points to the paper. If X and Y are placed in a 4-dimensional matrix, then the above formula can be written in the following matrix form (without affecting the Z and W components):
If we want to achieve rotation around the X or Y axis, the formula is basically similar but the arrangement of the transformation matrix is slightly different.
The following is the rotation transformation matrix for rotation around the Y axis (y does not change):
Rotation transformation matrix for rotation around the X axis (x does not change):
Detailed source code
There are very few changes to this code based on the previous tutorial. We just change the content of the transformation matrix in the code, and replace the translation transformation matrix with a rotation transformation matrix:
World.m[0][0]=cosf(Scale);World.m[0][1]=-sinf(Scale);World.m[0][2]=0.0f; World.m[0][3]=0.0f;
World.m[1][0]=sinf(Scale);World.m[1][1]=cosf(Scale);World.m[1][2]=0.0f; World.m[1][3]=0.0f;
World.m[2][0]=0.0f;World.m[2][1]=0.0f;World.m[2][2]=1.0f;World.m[2][3]=0.0f;
World.m[3][0]=0.0f;World.m[3][1]=0.0f;World.m[3][2]=0.0f;World.m[3][3]=1.0f;
This will see that the graph is rotated around the Z axis. We could also try rotations around other axes, but the other two rotations would look odd without a 3d to 2d projection, and we'll refine it in a full graphics transformation pipeline class in a later tutorial.