绕任意点旋转的矩阵

三维空间中有时候需要计算绕任意点旋转的矩阵,假设绕点P(x1,y1)旋转α角度,则步骤分为三步:

1.计算将P点平移到原点的矩阵T1。

2.计算旋转α角度的旋转矩阵R1。

3.计算将从原点平移到P点的平移矩阵T2。

最终的结果矩阵matrix = T1 * R1 * T2,旋转后的顶点坐标P' = matrix * P。

代码如下:

 1 vec4 getRoateByPoint(vec3 point,vec3 roatePoint,float angleZ)
 2 {
 3     
 4     //=================得到变换矩阵  start====================
 5     //平移到原点的平移矩阵
 6     mat4 firstransfromMat = mat4(1.0, 0.0, 0.0, -roatePoint.x,
 7                                  0.0, 1.0, 0.0, -roatePoint.y,
 8                                  0.0, 0.0, 1.0, -roatePoint.z,
 9                                  0.0, 0.0, 0.0, 1.0);
10                                  0.0, 0.0, 0.0, 1.0);
11   
12     
13     mat4 RoateMatZ = mat4(cos(angleZ), -sin(angleZ), 0.0, 0.0,
14                           sin(angleZ), cos(angleZ), 0.0, 0.0,
15                           0.0, 0.0, 1.0, 0.0,
16                           0.0, 0.0, 0.0, 1.0);
17     
18     //平移到目标点的平移矩阵
19     mat4 lasttransformat = mat4(1.0, 0.0, 0.0, roatePoint.x,
20                                 0.0, 1.0, 0.0, roatePoint.y,
21                                 0.0, 0.0, 1.0, roatePoint.z,
22                                 0.0, 0.0, 0.0, 1.0);
23     //=================得到变换矩阵  end====================
24     return firstransfromMat * RoateMatZ * lasttransformat * vec4(point,1.0);//
25 }
原文地址:https://www.cnblogs.com/calence/p/6207569.html