I have a scene and an object placed in some coordinates. I can transform the object using
glTranslate(center) and then glRotate…
But how do I rotate an object not using angles but rather directions top and forward?
Thanks
What I’m looking is translation between model coordinate system and global coordinate system.
Say you know 3 axes for your object in object space. For simplicity we’ll assume these are the cartesian axes (if it’s not the case, the process described below can be applied twice to take care of that):
And say you have 3 other orthogonal and normalized axes in world space, indicating the top, forward and side directions of your object [*]:
Then the following (assuming column vectors) is a rotation matrix taking you from object space to world space:
It’s a rotation matrix because the determinant is 1 (othogonal and normalized lines). To verify it goes from world space to object space, just note how
M*wx = (1, 0, 0)etc.Now you want the exact opposite: from object space to world space. Just invert the matrix. In that case, the inverse is the same as the transpose, so your final answer is:
Two things remain:
1) Loading this matrix in OpenGL.
glMultMatrixwill do this for you (be careful,glMultMatrixis column major and needs a 4×4 matrix):2) Translating. This is done just by following this with a call to
glTranslate.[*] If you have only two of the three, say top and forward you can easily compute the side with a cross product. If they’re not normalized, simply normalize them. If they’re not orthogonal it all gets trickier.