PDA

View Full Version : glMultMatrixf and rotations


jSTIN
2003.11.23, 04:46 PM
I want to set a matrix up to store rotations for all my objects. Since if you call glRotate three times for each matrix the result will depend on the order, etc.
So by default each objects rotation matrix is set to the identity matrix. So there should be no change when I multiply the current matrix with glMultMatrixf. But it messes it all up when I do that. Here is some code.

rotMatrix[0] = 1; rotMatrix[4] = 0; rotMatrix[8] = 0; rotMatrix[12] = 0;// Set rot matrix to identity matrix
rotMatrix[1] = 0; rotMatrix[5] = 1; rotMatrix[9] = 0; rotMatrix[13] = 0;
rotMatrix[2] = 0; rotMatrix[6] = 0; rotMatrix[10] = 1; rotMatrix[14] = 0;
rotMatrix[3] = 0; rotMatrix[7] = 0; rotMatrix[11] = 0; rotMatrix[15] = 1;

So each objects rotation is set to that, then the drawing code.

glPushMatrix();
glTranslatef(tempModel->xPos, tempModel->yPos, tempModel->zPos); // Translate To the models position
glMultMatrixf(tempModel->rotMatrix);

tempModel->Draw(); // draw the model

glPopMatrix();


If I comment out the glMultMatrixf it looks fine. Every thing is where it should be. But it should be the same since I multiply it with the identity matrix.
If there is another way to get the rotations I need, that would work too.

OneSadCookie
2003.11.23, 05:14 PM
My guess is that the matrix isn't the identity. Try printing it just before you call multmatrix to check.

Sohta
2003.11.23, 07:03 PM
Technically, the code you wrote works. I actually copied and pasted it in a test project and it works fine. So the matrix you defined IS the identity matrix. So the problem lies somewhere else (uninitialized memory for example).

Make sure the initialization block of code is properly executed.

good luck!

kelvin
2003.11.23, 09:41 PM
Are you in the correct matrix mode? (model view)

Sohta
2003.11.24, 01:52 PM
Come to think of it, glRotatef could pretty well do what you need. It depends what kind of rotation you want. You can define any single 3D rotation with two calls to glRotatef.


glRotatef(xRot,1.0,0.0,0.0);
glRotatef(yRot,0.0,1.0,0.0);


it's just a matter of splitting your rotation along those two components.


Also, if you know the arbitrary axis along wich you want to rotate tyour object, you can simply give that axis to glRotatef. For example, if I want to rotate 90 degrees along the X = Y, Z=0 axis, i would call


glRotatef(90.0,0.7071,0.7071,0.0);


The last three parameters of the glRotatef function represent a normalized (I think) vector along wich to rotate.

Nevertheless, your approach should work too.

jSTIN
2003.11.24, 02:54 PM
Thanks for all the input. I completely got rid of using matrices for rotations. I decided to go with quaternions instead.

It works great since I have fixed rotation coordinates now. If a want to rotate an object relative to its position, I simply take its orientation vectors against the rotation degree before putting it into the quaternion.

I still don't get why the matrix when multiplied got messed up.

jSTIN
2003.11.24, 07:28 PM
Well, I figured out the problem. Everything was fine but the matrix I created was m[15] not m[16]. Very lame.

But I might need to use them, since it would would be faster to go from a quaternion to a matrix and then use glMultMatrix instead going from a quaternion to axis and angles and then using glRotatef. It would be faster since glRotatef will multiply the current matrix anyway, and first it will calculate the rotation matrix, right? glMultMatrix would be more direct, no?

DoG
2003.11.25, 07:56 AM
glMultMatrix does the same work, but it is fast to create a matrix from a quaternion, and you let OpenGL do the matrix multiply, which may be faster than doing it manually.