PDA

View Full Version : Weird thing with glRotatef()



yaro_dup1
07-29-2003, 10:18 AM
Hi,

I've got this piece code:




glTranslatef( -0.5f, 0, -0.5f );
glRotatef( yRot, 0, 1, 0 );
glTranslatef( -0.5f, 0, -0.5f );


and that:




glTranslatef( 0.5f, 0, 0.5f );
glRotatef( yRot, 0, 1, 0 );
glTranslatef( -0.5f, 0, -0.5f );


Those pieces contains rotating two objects aroud their own axises, one object is translated with XZ -0.5f, -0.5f and the second is 0.5f, 0.5f. Everything works fine, but after rotation I always translate them in negative direction with -0.5f, -0.5f. But why? First object is located in different place than the second after first translation ???



[This message has been edited by glYaro (edited 07-29-2003).]

starman
07-29-2003, 10:55 AM
Are you asking why glTranslatef(-0.5, 0.0, -0.5); glRotatef(yRot, 0, 1, 0); puts the object in a different place than glTranslatef(0.5, 0.0, 0.5); glRotatef(yRot, 0, 1, 0);??

What your code is doing in both instances is translating first and then rotating about the global origin. If you want the object to be rotated and then end up at (0.5, 0.0, 0.5), do the rotation first, then the translation.

Sorry if I didn't understand your question.

yaro_dup1
07-29-2003, 11:28 PM
My question contains this that the second translation in both two codes translates object with negative direction. I want to rotate an object around his own axis, and those codes work. But shouldn't it be that (the second code):



glTranslatef( -1, 0, -1 ); // translate in negative direction
glRotatef( yRot, 0, 1, 0 );
glTranslatef( 1, 0, 1 ); // translate in positive direction


Above code doesn't work. In order to make it work OK ( rotate around -1, -1 axis ) I have to change the second call of glTranslatef with negative direction, why? It is not logical, If I won't use glRotatef so the code:



glTranslatef( -1, 0, -1 );
//glRotatef( yRot, 0, 1, 0 );
glTransletef( -1, 0, -1 );

will twice a time translate the object with XZ -1, -1 and the object position will be -2, -2. So object should not return to its position after rotation.

[This message has been edited by glYaro (edited 07-30-2003).]

GlutterFly
07-30-2003, 06:40 AM
Well, let me see if i understand...

You are trying to translate an object, rotate that object, then move back to the origin and do the same for another object. Well, the only thing i can see that might be wrong is that you are not "unrotating" after you rotate the object, you are simply translating back. You might wanna try a line like glRotatef(-yrot,0.0f,1.0f,0.0f). If this isnt the issue, im not sure i understand.

yaro_dup1
07-30-2003, 07:11 AM
OK, let's say we've got two objects:
1. A cube with origin vertex in (5, 0, 5)
2. A buce with origin veretx in (-5, 0, -5)

I want to rotate it around its origin vertex Y axis: (5, 0, 5) and the second (-5, 0, -5) with glRotatef(0, 1, 0).

nexusone
07-30-2003, 07:25 AM
Don't hold me to this I am doing in my head..

should look like this if I undstand the question correctly.

glRotate( A, 0, 1, 0);
glPushMatrix()
glTranslatef( -5, 0, -5);
draw_cube();
glPopMatrix();

glPushMatrix();
glTranslatef(5, 0, 5);
draw_buce();
glPopMatrix();


http://www.opengl.org/discussion_boards/ubb/smile.gif



Originally posted by glYaro:
OK, let's say we've got two objects:
1. A cube with origin vertex in (5, 0, 5)
2. A buce with origin veretx in (-5, 0, -5)

I want to rotate it around its origin vertex Y axis: (5, 0, 5) and the second (-5, 0, -5) with glRotatef(0, 1, 0).

yaro_dup1
07-30-2003, 07:29 AM
Thanks. I forgot about tricks with matrix stack http://www.opengl.org/discussion_boards/ubb/smile.gif

TheAnt
07-31-2003, 02:15 AM
Just for the sake of clearing thigs up, the problem created without using glPushMatrix()-glPopMatrix() seems to be that when you rotate an object with glRotatef() and do not push the previous matrix onto the stack you do get what you want, but when trying to traslate back to the previous position your modelview matrix is now rotated around making the ipression that the negative values compared to your previous translation are not going backwards like they should. Think about what every matrix multiplication is doing to your modelview matrix or you'll find yourself in a virtual mirror house http://www.opengl.org/discussion_boards/ubb/frown.gif

[This message has been edited by TheAnt (edited 07-31-2003).]