I have an opengl arbitrary rotation matrix and would like to remove the X & Y axis, leaving开发者_运维百科 me with only the Z axis?
Is this possible? Any pointers on how to do it?
Just thinking out loud here, but can't you use the matrix to rotate a vector like (1,0,0), then do atan2(y,x) to see how much it's rotated and then build a new matrix to rotate through the Z axis by that much?
In a rotation that is only around the z-axis, the z axis should remain unchanged. So the above recommendation is sort of the reverse of what you want.
Let's assume you have an arbitrary OpenGL matrix:
| r_xx r_xy r_xz t_x |
| r_yx r_yy r_yz t_y |
M = | r_zx r_zy r_zz t_z |
| 0 0 0 1 |
Where the t_i elements are translations and the r_jk elements are components of rotation. You want a matrix that looks like this:
| cos(th) sin(th) 0 t_x |
|-sin(th) cos(th) 0 t_y |
| 0 0 1 t_z |
| 0 0 0 1 |
Unless the matrix has scaling factors or is close to a singularity, you should be able to get this by just zeroing out the z parts of the matrix and then re-normalizing the columns. Since an OpenGL matrix is column major order:
double xLen = sqrt(M[0]*M[0] + M[1]*M[1]); // Singularity if either of these
double yLen = sqrt(M[4]*M[4] + M[5]*M[5]); // is equal to zero.
M[0]/=xLen; M[1]/=xLen; M[2]=0; // Set the x column
M[4]/=yLen; M[5]/=yLen; M[6]=0; // Set the y column
M[8]=0; M[9]=0; M[10]=1; // Set the z column
//Don't change the translation column
精彩评论