/* =============== RotatePointAroundVector dst是一个float[3],也就是p’’’ dir相当于a,point就是p,degrees是旋转度数 =============== */ void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees ) { float m[3][3]; float im[3][3]; float zrot[3][3]; float tmpmat[3][3]; float rot[3][3]; int i; vec3_t vr, vup, vf; float rad;
vf[0] = dir[0]; vf[1] = dir[1]; vf[2] = dir[2]; // 首先通过dir得到一个和它垂直的vr // PERPendicularVector()函数用于构造和dir垂直的向量 // 也就是我们上面的第1步 PERPendicularVector( vr, dir ); // 通过cross multiply得到vup // 现在已经构造出坐标轴向量vr, vup, vf CrossProduct( vr, vf, vup ); // 把这三个单位向量放入矩阵中 m[0][0] = vr[0]; m[1][0] = vr[1]; m[2][0] = vr[2];
m[0][1] = vup[0]; m[1][1] = vup[1]; m[2][1] = vup[2];
m[0][2] = vf[0]; m[1][2] = vf[1]; m[2][2] = vf[2]; // 产生转置矩阵im memcpy( im, m, sizeof( im ) );
im[0][1] = m[1][0]; im[0][2] = m[2][0]; im[1][0] = m[0][1]; im[1][2] = m[2][1]; im[2][0] = m[0][2]; im[2][1] = m[1][2]; // 构造旋转矩阵zrot memset( zrot, 0, sizeof( zrot ) ); zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
rad = DEG2RAD( degrees ); zrot[0][0] = cos( rad ); zrot[0][1] = sin( rad ); zrot[1][0] = -sin( rad ); zrot[1][1] = cos( rad ); // 开始构造变换矩阵M // tmpmat = m * zrot MatrixMultiply( m, zrot, tmpmat ); // rot = m * zrot * im MatrixMultiply( tmpmat, im, rot ); // 则 rot = m * zrot * im 和我们上面推出的 // M = |a b c| * RotMatrix * |a b c|^T 一致
// 变换point这个点 // p’’’ = M * p for ( i = 0; i < 3; i++ ) { dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2]; } }
|