Weird thing with glRotatef()

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).]

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.

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).]

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.

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).

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();

Originally posted by glYaro:
[b]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).[/b]

Thanks. I forgot about tricks with matrix stack

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

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