PDA

View Full Version : glMultMatrixf(): rotation order?


Feanor
2002.07.11, 05:50 PM
If you create a matrix of your own with rotations in it and then multiply the transformation matrix, what order does opengl apply the transformations? My experiments have found that it applies x, y and z in that order, although I really wish it were Y, X, Z (heading/yaw, pitch, roll). I just wanted to confirm/deny my findings. B

Jeff Binder
2002.07.11, 06:07 PM
When you multiply transformation matrices together, the new matrix does the transformations in the order that you multiplied them. So if you have three rotation matrices (let's call them mx, my, and mz), and you want to transform by them, just send them to glMultMatrix() in the order you want. i.e.

glMultMatrixf(my);
glMultMatrixf(mx);
glMultMatrixf(mz);

If you're doing the matrix multiplication yourself, you just need to do the multiplications in this order. So (assuming you've got a C++ matrix class with overloaded multiplication):

//This should be an identity matrix.
Matrix concat();

//Concatenate y, x and z in that order.
concat *= my;
concat *= mx;
concat *= mz;

Now concat is a matrix that applies the three rotations in the order you want. This is pretty much the way OpenGL does things, too. It's one of the best features of using matrices, because one matrix can represent any number of transformations in a specific order.

monteboyd
2002.07.11, 07:12 PM
Just to put confirmation to Jeff's comment, in my snowboarding game I multiply the Y rotation matrix by the X and Z and then transform the snowboarder model by the final matrix and it performs it in that order.

OneSadCookie
2002.07.11, 07:54 PM
One trap for the unwary -- OpenGL uses its matrices transposed from the way you would have learnt about them in math class. That means that if your math text says to do A * B * C, you really need to do C^T * B^T * A^T in OpenGL.

codemattic
2002.07.13, 12:19 PM
Check out "Viewing and Modeling Transformations" in chapt 3 of the redbook. Basically says:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
glMultMatrixf(N);
glMultMatrixf(M);
glMultMatrixf(L);
glBegin(GL_POINTS);
glVertex3f(v);
glEnd();

which will do N * (M * (L * v)) - that is the point v transformed by matrix L, then transformed by matrix M, then transformed by matrix N. That is if you conceptually think about it as being a point which is being transformed. However - you can also conceptualize it as having your base coordinate system which is what is being transformed - in which case you have your identity (base) which is then transformed by matrix N, which is then transformed by matrix M, which is then transformed by matrix L, and then point v is drawn in that new coordinate system. The code is the same obviously - its just two ways of thinking about it.

Also - while this article isnt specific to OpenGL's matrices (matrixes??), <http://www.gamasutra.com/features/20020510/johnson_pfv.htm> is a great article on getting a handle on taking the guesswork out of using transformation matrixes (matrices??).

hth,
Codemattic