问题
I am displaying an object in OpenGL using a model matrix for the object, which I build from my pre-stored object location AND a quaternion applied to the rotation. I need to find the final cartesian coordinates in 3D of my object AFTER the rotations and transformations applied (the coordinates that the object appears at on the screen). How can I get the plain coordinates?
回答1:
If I understand correctly, you have an object; if you rendered it without applying any transformation, its center would be at [0,0,0].
You have a point, [a,b,c], in 3D space. You apply a translation to the modelview matrix. Now, if you rendered the object, its center would be at [a,b,c] in world space coordinates.
You have a quaternion, [qw,qx,qy,qz]. You create a rotation matrix, M, from this and apply it to the modelview matrix. Now you want to know the new coordinates, [a',b',c'], of the object's center in world space.
If this is true, then the easiest way is to just do the matrix multiplication yourself:
a' = m11*a + m12*b + m13*c
b' = m21*a + m22*b + m23*c
c' = m31*a + m32*b + m33*c
where
[m11 m12 m13]
M = [m21 m22 m23]
[m31 m32 m33]
But perhaps you're not actually building M. Another way would be to use the quaternion directly, although that essentially involves building the rotation matrix and then using it.
There should be no need to actually use gluProject
. When you apply the rotation to the modelview matrix, the matrix multiply is done there. So you could just get the values from the matrix itself:
double mv[16];
glGetDoublev(GL_MODELVIEW_MATRIX,mv);
a' = mv[13];
b' = mv[14];
c' = mv[15];
This tells you where the modelview matrix is moving the model's origin.
回答2:
Re-implement gluProject() and apply everything but the viewport transform.
来源:https://stackoverflow.com/questions/9912357/find-final-world-coordinates-from-model-matrix-or-quaternion