View Full Version : How to extract a 3d rotation from a matrix?
11-30-2000, 06:00 AM
And by the way.. how can i get the current Matrix.
11-30-2000, 06:42 AM
finally one /I/ can answer!
okay, first you gotta call
glGetFloatfv( GL_MODELVIEW_MATRIX,&Matrix );
that'll get you your 4x4 matrix, set up like this (in hex offsets)
0 1 2 3
4 5 6 7
8 9 A B
C D E F
soo, that means that 5 is also (1,1), k? 6 is (1,2), alright?
to factor as a product of RxRyRz, heres what you do
it come from the fact that a rotation matrix
composed of an Rx x Ry x Rz rotations has the form
sx = sin x
sy = sin y
sz = sin z
cx = cos x
cy = cos y
cz = cos z
the matrix is
[ cy*cz -cy*sz sy 0 ]
[ cz*sx*sy + cx*sz cx*cz - sx*sy*sz -cy*sx 0 ]
[ -cx*cz*sy + sx*sz cz*sx + cx*sy*sz cx*cy 0 ]
[ 0 0 0 1 ]
sure, the matrix is different if you use Rx*Rz*Ry, etc, but it's all the same idea.
basically, you just look in there for ways of canceling out products, and using inverse trig functions.
like for instance, Ry = ArcSin( sy );, right?
well, given that, here's the code:
Ry = ArcSin( Matrix );
if( Ry < Pi/2 )
if( Ry > -Pi/2 )
Rx = ArcTan( -Matrix/Matrix );
Rz = ArcTan( -Matrix/Matrix[0 ] );
Rx = -ArcTan( Matrix/Matrix );
RZ = 0;
Rx = ArcTan( Matrix/Matrix );
Rz = 0;
it's some pretty neat math stuff.
remember, this only works if the matrix is only an Rx*Ry*Rz matrix. it won't work if there's scaling, i don't think, but translations are ok.
if you know what the scaling is, you can also just divide it out.
hope that helps
[This message has been edited by Succinct (edited 11-30-2000).]
11-30-2000, 08:02 AM
What a reply!
Tanks for this wonderfull answer.
It helps me a lot, and i'll be able to to what i want now http://www.opengl.org/discussion_boards/ubb/smile.gif
11-30-2000, 09:03 AM
Well.. all your stuff is working! I thought it was my solution to correct my problem. But it seems not. (it was a good formatio though http://www.opengl.org/discussion_boards/ubb/smile.gif)
Well.. here is my probleme just in case that someone will say "Hey! i know what it is!"
My real problem is that i have a 3d objet, that i draw on a opengl view. This object get transformed with the following matrix transformation: glRotated and glTranslate.
I have a point, in that mesh, that i must calculate mannualy. So i though i just get the rotation of my mesh, and rotate my point with that rotation to get the position, but it is not working. It work only if i rotate only on one axis. And it also work when i rotate it over the XY, and XZ. But XYZ and YZ rotation is not working.
So i though it was something in the Matrix that i do not understand, so i asked how to get the rotation of a matrix, and use this rotation to rotate my point, but it do the same thing.
I am desesperate.. Tanks in advance
12-01-2000, 05:48 AM
although i don't understand the exact meaning
of "I have a point, in that mesh, that i must calculate mannualy...", what i suppose is that u want to find the position of a specific point in the modelview space after the transformation, if u have its position before...
If that is the case, then the answer is simple: u just have to multiple the modelview matrix with (x y z 1) where x,y,z are the coords of the point before the transform.
But if you want to calculate the position of the point after rendering - in 2D coords, then you must multiple the result with projection matrix, and the to apply the viewport transform (see glViewport for more info). Another solution is to use feedback mode in OpenGL (see glRenderMode()) - but this is too complicated for just one point http://www.opengl.org/discussion_boards/ubb/smile.gif
[This message has been edited by martin_marinov (edited 12-01-2000).]
12-01-2000, 05:59 AM
Yes your understood my problem.
Sorry for my bad english!
So, i must multiply my matrix with x,y,z,1
I am not a hard math one, hm... the result will be another matrix yeah? So.. how do i get my x,y,z point back from the matrix?
I am missing something i think..
12-01-2000, 07:21 AM
My linear algebra is pretty rusty so I can't give you exact formulas for the multiplication, but if you take a 4x4 matrix and multiply it by a 4x1 vector, the result is a 4x1 vector. (Or maybe that was 1x4 vector, I never remember.)
12-01-2000, 07:29 AM
You could have been sure when you said the result would be a vector with 4 components... http://www.opengl.org/discussion_boards/ubb/smile.gif
Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.