PDA

View Full Version : glRotetef & 4x4 Matrix



wererabit
8th February 2011, 01:45
Hi,

Sorry in advance if this is in the wrong place!

I just wonder if anyone know how can I decompose a 4x4 transformation matrix (Rotation & translation) into angles & other thing so I can use with glRotatef, glScalef & glTranslationf.

Thank alot!

agarny
8th February 2011, 08:32
That's a pure OpenGL question, so I highly doubt a Qt forum would be the right place for it...

JohannesMunk
8th February 2011, 10:39
Hi!

I would suggest to do it the other way around. Use OpenGL to setup your matrix!



// use gl to perform the matrix multiplications. using modelview matrix as temp.. Start fresh
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glScaled(...);
glTranslated(...);
if (Angle().x() != 0) glRotatef(Angle().x(),1,0,0);
if (Angle().y() != 0) glRotatef(Angle().y(),0,1,0);
if (Angle().z() != 0) glRotatef(Angle().z(),0,0,1);

GLfloat itemtrans[16];
glGetFloatv(GL_MODELVIEW_MATRIX , itemtrans);

// Restore previous gl matrix
glPopMatrix();
afterwards you can transform this to a QTransform like this:



QTransform GetQTrans(GLfloat* m)
{
// In QTransform the dx and dy are stored in m31 and m32. which is nuts.
// In 4x4 GL they are stored in 03 and 13, so that when you multiply
// with a homogenous (x,y,z,1) vector you get them added automatically.

// Build QTransform of this matrix. QTransform 3x3 recieves the values per row,
// whereas GL saves the 4x4 matrix per column.
// m11 m12 m13 1 2 3 m00 m10 m30 0 1 3
// QT: m21 m22 m23 4 5 6 GL: m01 m11 m31 4 5 7
// dx dy m33 7 8 9 m03 m13 m33 12 13 15

// AAH! QTransform does not use m33, unless the transformation is not affine, m13<>0 and m23 <> 0
QTransform trans;
if (qFuzzyCompare(m[3] + 1, 1) && qFuzzyCompare(m[7] + 1, 1))
trans = QTransform(m[0]/m[15], m[1]/m[15], m[4]/m[15], m[5]/m[15], m[12]/m[15], m[13]/m[15]);
else
trans = QTransform(m[0], m[1], m[3], m[4], m[5], m[7], m[12], m[13], m[15]);
trans.scale(1,-1);
return trans;
}
For a project sample have a look at this thread http://www.qtcentre.org/threads/20059-QGraphicsScene-Widgets-in-OpenGL-Projection-with-only-a-tiny-problem

HIH

Johannes