PDA

View Full Version : Rotations again !

Eric
02-28-2000, 11:23 PM
Hi there.

I am developping some kind of basic 3D modeller which imports various formats and exports to my own format for my own viewer (my format is quite OpenGL-oriented as you can imagine !).

I wanted to implement in the modeller the kind of rotation you have in 3D Studio Max.
Basically, the object will always rotate around the world axes........

But I am stuck on how to reproduce that !

I wanted to keep track of the X_Angle,Y_Angle,Z_Angle and then use the 3 different rotation matrices to obtain the full rotation matrix. Of course it does not work (OK, I should have thought about that before !).

Let me explain... I wanted to use three rotations around X,Y,Z. I had three matrices Rx,Ry,Rz and I thought Rx*Ry*Rz=R would be my final matrix.... The problem is that when I apply Ry, it applies to my world coordinates system transformed by Rz !

Does anyone know how I can rotate my object about any of the X,Y,Z of the WORLD coordinates system ????

Just some precision : at the moment, for each object, I have a GLfloat m_TransformMatrix[16] (loaded with glLoadMatrix when rendering) and I keep track of the m_XRotationAngle, m_YRotationAngle and m_ZRotationAngle......

I am not afraid of complicated maths so you can go !

I would prefer an algorithm on how to build the transformation matrix based on my 3 angles but if I must use Euler angles and/or quaternions, please tell me !

Hope someone can save my life http://www.opengl.org/discussion_boards/ubb/smile.gif !!!

Eric

Eric
02-29-2000, 12:07 AM
I was thinking : could it be possible with the data I have to find the corresponding rotation of an angle a0 around an axis (x0,y0,z0) ???

Eric

Hude
02-29-2000, 02:28 AM
Ummm.. I'm not sure if I got it right, but..

Basically you have object location and rotation in world space and rotation in object space. First you rotate the object in object space. Then you translate the object space coordinates into world space coordinates. After this, the rotations take place in world space.

With 3ds exported models you get the transformation matrices, but you have to notice that the object vertices are in world space.

Hope it helps...

Eric
02-29-2000, 02:41 AM
Yes, I know that (thanks anyway !).

But if we forget that I import 3DS models, I would like to be able to rotate my object around one of the principal axes of my 3D World.... I guess I should transform my array of vertices each time I apply a rotation instead of trying to build a rotation matrix for the object...

Anyone can help ????????????????

Eric

Bob
02-29-2000, 02:51 AM
Hello

First I want to ask you, do you mean to rotate the objects about the world origin just to be able to view the object(s) from different angles?

If so, here is a solution.

First you set up a camera with
gluLookAt(cx, cy, cz, 0, 0, 0, u1, u2, u3);
... where cx,cy,cz is camera position, u1,u2, u3 is upvectors.
If you want to be able to rotate, you can place the camera on a sphere with origin at (0,0,0) in world coordinates, and with radius large enough to enclose all objects.

Then you translate object(s) to their position in worldcoordinates, and then draw them.

If you want to, I can post the equations for setting cameracoordinates on a sphere.

Bob

Eric
02-29-2000, 04:16 AM
Hi Bob !

Actually, I want to rotate the objects around "their" world origin... I mean, when I load them, I center them on their gravity centre. Then, to display them, I just have to FIRST rotate and THEN translate....

I can not use a camera there coz' I have got many objects in the scene and I want to rotate them individually !

Well, I've been thinking of a way to simplify my problem....

Let's take a coordinate system R(i,j,k) where i=(1,0,0) , j=(0,1,0) and k=(0,0,1).

Then, I know that I want to rotate my vertices around i,j, and k with the angle a,b,c.

Basically, my first technique is OK if I do the transformation myself i.e. I transform my vertex (x,y,z) with Rx(i,a) then Ry(j,b) and finally Rz(k,c) (or whatever order coz with this technique, I do not modify the rotation axes !!!!!).

When it comes to using the OpenGL MODEL_VIEW matrix, it doesn't work because my first rotation will rotate the axes I use for the other rotations !

So I was wondering if there is a way to find a rotation matrix depending on (a,b,c) that would rotate my object properly if I laod it as my MODEL_VIEW matrix......

Thanks to anyone who can help !

Eric

Hude
02-29-2000, 04:49 AM
You CAN use camera AND rotate objects individually. Use glPushMatrix, glPopMatrix and glLoadIdentity to fiddle around with the current model view matrix. If you need to multiply the current matrix by your own use glMultMatrix. Or then do ALL transformations yourself and use glLoadMatrix.

Hude
02-29-2000, 04:53 AM
Just something else... Are you sure you are multiplying the matrices in correct order?-)

Eric
02-29-2000, 05:19 AM
Well, the order of the matrices isn't so important for that problem : whatever the order, the first one will rotate the axes I want to use for the others rotations !

The thing is, I AM OK TO DO THE ROTATION MATRIX MYSELF ! I am just looking for how to compute it !!!!!

If I build it with multiplying R(i,a),R(j,b) and R(k,c) it won't work ! I am not sure of the english word but in french, it is called "Matrice de Passage". Those matrices are used when you change from one coordinate system to another... I believe I have to use them for my problem but I can't see how...

Well, shame I studied only Maths and Physics for three years plus three others of engineering only and I do not remember anything..........................

By the way, am I wrong or any combination of N rotations is equivalent to one rotation around an axis with a specified angle ?????

I know I learnt to extract this axis and the angle from any rotation matrix but can not remember the method..........

Well, just to let you know, I have implemented the method that consists of modifying my vertex arrays instead of using the modelview matrix and it works....

And the reason is still the same : if you say :

1) I want to rotate around X with angle a
2) I want to rotate around Y with angle b
3) I want to rotate around Z with angle c

When using OpenGL and glRotate or whatever, when you do step 2, YOUR Y AXIS IS NOT THE ONE YOU HAD BEFORE step 1 !!!! Same when you reach step 3.....

In fact I have a solution but it's gonna be quite time-consuming :

When reaching step 2, I can find the coordinates of my Y axis in the new coordinates system.
Then in step 3, I can find the coordinates of my Z axis in the new new ( http://www.opengl.org/discussion_boards/ubb/smile.gif) coordinate system....

The only thing that bothers me is : HOW YOU GUYS DO ??? Even if this problem doesn't arise for games, I have seen many people here saying they were developing some kind of CAD tool. So they must have ran into the same problem than me ! Do they rotate their vertices as I am doing now ? Or did they find the magic formula for building this bloody matrix from a,b and c ?????????

Well, thanks anyway to all those who try to help me !!!

Eric

Eric
03-01-2000, 01:21 AM
Sorry Hude, I think I did not understand what you said quite right !

I thought you were asking if I was using glRotate in the correct order while I believe now that you were talking about my own matrix multiplications... And I must confess you were right...

When doing my matrix product Rx*Ry*Rz, I calculated (Rx*Ry)*Rz while it must be Rx*(Ry*Rz) (Calculate Ry*Rx and then Rx*Result)........

Basically I have the solution to my problem : instead of trying to use glRotate with X,Y and Z successively, I build my own Rotation / Translation matrix and use glMultMatrix to upload it to OpenGL....

Thanks to everyone who helped me and sorry again Hude, I should have thought about what you said sooner (or at least understand what you meant !!!).

Eric

Bob
03-01-2000, 02:23 AM
Hello again...

Ok, want to ask you one thing Eric. With "object", do you mean several "subobjects" put together to act as one object? Like, a box and four cylinders to build a car. And you want to rotate the car (with it's five objects) about its own origin, and not the world origin?

Heh, getting the feeling that noone knows what the other ones are talking about... :P

Bob

Hude
03-01-2000, 02:34 AM
Yeah, that's what I meant. I should have been a bit more specific when I asked the question.

Don't you have a working solution with glRotate functions then? Shouldn't it be pretty straightforward conversion from your own transformation routines into OpenGL functions? Would be less messy code maybe http://www.opengl.org/discussion_boards/ubb/smile.gif

Aaa.. or then I'm just too confused and don't know what I'm thinking http://www.opengl.org/discussion_boards/ubb/smile.gif

[This message has been edited by Hude (edited 03-01-2000).]

Eric
03-01-2000, 07:56 AM
Hi there !

Bob : when I say object, I just mean one object... Let's say a box in the world (yeah, I know, sound pretty simple but as soon as you know how to do one thing correctly, you can build on it !).

Hude : my "own transformation matrix" is a big word for something simple... The rotation matrix I use is basically the result of the following :

R(x,a)*R(y,b)*R(z,c)

Where R(u,d) is the rotation around axis u by angle d.....

The thing is, I don't see how to do that with OpenGL calls only.If I wanted to follow the same idea with OpenGL, I would do :

glRotated(c,0,0,1);
glRotated(b,0,1,0);
glRotated(a,1,0,0);

The problem is that here, when I call glRotated for the second time, the axis (0,1,0) is not anymore my world Y-Axis coz' I have already changed my coordinates system (with the first glRotate)...

The only thing I can think about is to find the coordinate of my world Y-Axis in the new coordinates system. Then, I can replace (0,1,0) with the new coordinates.... Well, I have tried loads of things but I could not replace my own transformations...

See ya !

Eric

Eric
03-01-2000, 08:39 AM
That's me again...

I've been thinking about replacing my matrix calculations with gl Calls...

In fact, I think I know why I can not manage it :

Let's call M the Model view matrix and Rx,Ry,Rz my rotations around x,y and z.
Let's call R the rotation matrix I need to multiply M with.

In my calculations, I calculate :

R=Rx*(Ry*Rz). Which means :
R1=Ry*Rz and THEN R=Rx*R1.

When you use OpenGL for multiplying M you basically do a glMultMatrix(K) and OpenGL will do :

M=M*K.

I need to do :

M=M*(Rx*(Ry*Rz));

While OpenGL will only let me do :

M=((M*Rx)*Ry))*Rz;

Basically, I can't see a way of avoiding doing the R=(Rx*(Ry*Rz)) myself and then doing M=M*R.

If anyone has suggestions....

Thanks.

Eric

Hude
03-01-2000, 09:48 PM
Originally posted by Eric:
I need to do :

M=M*(Rx*(Ry*Rz));

While OpenGL will only let me do :

M=((M*Rx)*Ry))*Rz;

Basically, I can't see a way of avoiding doing the R=(Rx*(Ry*Rz)) myself and then doing M=M*R.

I don't know if it is possible, but I would try to change the order of the multiplications by transposing some of the rotation matrices.

You know like,
A*B=B^T*A^T

Marc
03-01-2000, 11:01 PM
I had a similar problem and I think you can manage it in this way:

push current matrix
do Ry*Rz (glRotate...)
store current matrix with:
glGetDoublev(GL_MODELVIEW_MATRIX, PointerToArrayWith16GLdouble)
or
glGetDoublev(GL_PROJECTION_MATRIX, PointerToArrayWith16GLdouble)
(just found yesterday http://www.opengl.org/discussion_boards/ubb/smile.gif)
do Rx (glRotate...)
glMultMatrix(WhatYouHaveStoredBefore)
store current matrix
pop matrix
glMultMatrix(WhatYouHaveStoredTheSecondTime)

This should do your equation M=M*(Rx*(Ry*Rz)).

Hope it helps...

Eric
03-01-2000, 11:08 PM
Wow guys !

We've got a hot topic on rotations ! Incredible isn't it ??????

Marc, Hude, I am going to try your ideas as soon as I have some time (which means probably tomorrow !).

I am sure there's a way of doing what I want with only gl calls.... When I manage to do it, I'll post the result here !