PDA

View Full Version : QT & OpenGL transformation matrices



Alezhi
07-03-2003, 09:00 PM
Hi! Is here anybody who knows how to correctly convert QT transformation matrix to OpenGL one for 2D case? QT uses following format:
http://doc.trolltech.com/3.2/qwmatrix.html#details (http:// <a href=) " target="_blank"> http://doc.trolltech.com/3.2/qwmatrix.html#details </a>
Thank for any hints.
I've posted this question to beginners forum, but have now answer.

john
07-03-2003, 09:26 PM
Hello,

well, you could just set the z transform to 0. For example, if you had the 2D homogeneonous transform matrix



0 1 2
1 0 3
0 0 1

(which flips the X/Y axis and transforms the origin to [2, 3]), then you could come up with a 3D transform



0 1 _0_ 2
1 0 _0_ 3
_0__0___1___0_
0 0 _0_ 1

the row and coluns have been added to map z<-z. (The _?_ numbers have been added to the matrix)

make sense?

cheers
John

Alezhi
07-03-2003, 11:27 PM
In Qt matrix format is following:
m11 m12 0
m21 m22 0
dx dy 1
If I set identity Qt matrix and translate it dx=10 dy=20 the result will be (M1)
1 0 0
0 1 0
10 20 1
Now I try to prepare similar Gl matrix (M2)
1 0 0 10
0 1 0 20
0 0 1 0
0 0 0 1
and load it then drawing isn't valid. If I load GL identity matrix and translate it (10,20), I'll get the result matrix (M3) which will differ from M2 and drawing will correct.
And what about shearing? I haven't found a shearing function for matrices.
The problem is that my Qt matrix may be result of many transformations, and I can't find out kx,ky, and angle to modify Gl matrix by glRotate, glScale.. So it will be better if I can generate GL matrix myself by Qt matrix. But I don't know how.
Alex

john
07-04-2003, 04:26 AM
Hello,

your confusion lies in getting your head around different conventions. OpenGL *pre* multiplies a vector by the transformation matrix, but Qt (it seems) wants to *post* multiply the matrix. All this means is that verticies in OpenGL are column vectors whereas they're row vectors in Qt and the multiplicaiton order is different, like so:



1 0 0 x [ a ] [ a + x ]
0 1 0 y [ b ] [ b + y ]
0 0 1 z [ c ] = [ c + z ]
0 0 0 1 [ 1 ] = [ 1 ]

[ 1 0 0 0 ]
[ 0 1 0 0 ]
[ a b c 1 ] [ 0 0 1 0 ] = [ a + x, b + y, c +z, 1]
[ x y z 1 ]

... but you get back the same result at the end of the day, so all is well. So, you could get your 4x4 matrix (or your 3x3... i think you're talking about 2D operations before) and compute the transpose and THEN turn the 2D into 3D like my earlier post suggested. (The transpose is just flipping the matrix by the diagonal; check out http://mathworld.wolfram.com/MatrixTranspose.html)


And what about shearing? I haven't found a shearing function for matrices.

shearing is just a bias of one axis as a function of another, like so:



1 s 0 0
0 1 0 0
0 0 1 0
0 0 0 1

where s is some non-zero number. There isn't an opengl function to make this matrix for you, so you have to define it and glMultMatrix() yourself.


The problem is that my Qt matrix may be result of many transformations, and I can't find out kx,ky, and angle to modify Gl matrix by glRotate, glScale.. So it will be better if I can generate GL matrix myself by Qt matrix. But I don't know how.

decompsing a matrix into constituent parts is tricky, but you don't need to do that when you know where the elements sit. In your QT matrix form then your parts are



[ r11 r12 0 ]
[ r21 r22 0 ]
[ t1 t2 1 ]

where [t1 t2] is your translation vector and r[11-22] are your rotation/scale elements. You can just grab your Qt matrix and form an OpenGL matrix like so



[ r11 r21 0 t1 ]
[ r12 r22 0 t2 ]
[ 0 0 1 0 ]
[ 0 0 0 1 ]

but note that the r?? elements have been changed because its now a transposed matrix.

I hope this helps

cheers,
John

Alezhi
07-07-2003, 12:28 AM
John, thank you for the answers. Empirically I've correct this problem - now I translate gl matrix by at first, then form matrix for scaling and rotating (directly from qt matrix) and call glMultMatrixd. All work correctly.
Alex