四元数转化为矩阵

 1 /**
 2  *   /  R[ 0]   R[ 1]   R[ 2]   0  \
 3  *   |  R[ 4]   R[ 5]   R[ 6]   0  |
 4  *   |  R[ 8]   R[ 9]   R[10]   0  |
 5  *   \  0       0       0       1  /
 6  */
 7 + (GLKMatrix4)getRotationMatrixFromQuaternion:(CMQuaternion*)quaternion{
 8     float xx = quaternion->x * quaternion->x;
 9     float yy = quaternion->y * quaternion->y;
10     float zz = quaternion->z * quaternion->z;
11     float xy = quaternion->x * quaternion->y;
12     float wz = quaternion->w * quaternion->z;
13     float wy = quaternion->w * quaternion->y;
14     float xz = quaternion->x * quaternion->z;
15     float yz = quaternion->y * quaternion->z;
16     float wx = quaternion->w * quaternion->x;
17     
18     float r00 = 1 - 2 * (yy + zz);
19     float r01 = 2 * (xy - wz);
20     float r02 = 2 * (wy + xz);
21     float r03 = 0;
22     
23     float r10 = 2 * (xy + wz);
24     float r11 = 1 - 2 * (xx + zz);
25     float r12 = 2 * (yz - wx);
26     float r13 = 0;
27     
28     float r20 = 2 * (xz - wy);//不是 xy - wy
29     float r21 = 2 * (yz + wx);
30     float r22 = 1 - 2 * (xx + yy);
31     float r23 = 0;
32     
33     float r30 = 0;
34     float r31 = 0;
35     float r32 = 0;
36     float r33 = 1;
37     
38     return GLKMatrix4Make(r00,r01,r02,r03,
39                       r10,r11,r12,r13,
40                       r20,r21,r22,r23,
41                       r30,r31,r32,r33);
42 }

本方法是基于Objective-C实现。

posted @ 2016-11-22 21:02  bky2016  阅读(1317)  评论(0编辑  收藏  举报