# Thread: A simple Rotation problem

1. ## A simple Rotation problem

Hi,

I am new to OpenGL programming and this is my first post in the forum, so hello everyone.

I have been having this problem with this successive translation and rotation thing. Suppose I want to move a cube 5 units down the +x axis and then rotate this 45 degrees about the initial origin. So the cube would be midway between x and y axis. But the code snippet I am writing in a display function is able to translate it 5 units but the rotation is about the cube's coordinate system so the cube remains in the x -axis only. Where could I be going wrong?

gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glPushMatrix();
glTranslatef(5.0, 0.0, 0.0);
glRotatef (45.0, 0.0, 0.0, 1.0);
glutWireCube (1.0);
glPopMatrix();

Please let me know if the question is unclear.

2. Originally Posted by novice01
Hi,

I am new to OpenGL programming and this is my first post in the forum, so hello everyone.

I have been having this problem with this successive translation and rotation thing. Suppose I want to move a cube 5 units down the +x axis and then rotate this 45 degrees about the initial origin. So the cube would be midway between x and y axis. But the code snippet I am writing in a display function is able to translate it 5 units but the rotation is about the cube's coordinate system so the cube remains in the x -axis only. Where could I be going wrong?

gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glPushMatrix();
glTranslatef(5.0, 0.0, 0.0);
glRotatef (45.0, 0.0, 0.0, 1.0);
glutWireCube (1.0);
glPopMatrix();

Please let me know if the question is unclear.
I've had a lot of these problems myself too, when I started programming using opengl. You're not doing it right - if you want the cube to be at a line 45 degrees ("halfways" of x/y-axis at all times) then you should FIRST rotate your coordinate system and LATER THEN you translate. So just swap your translate and rotate calls - I think that should work for you.

3. Originally Posted by newsb
I've had a lot of these problems myself too, when I started programming using opengl. You're not doing it right - if you want the cube to be at a line 45 degrees ("halfways" of x/y-axis at all times) then you should FIRST rotate your coordinate system and LATER THEN you translate. So just swap your translate and rotate calls - I think that should work for you.
Thanks for the reply, newsb. Yes it did work when I swapped the translate and rotate commands. Many thanks. Cheers.

Were both the Rotate and translate part of viewing transformation or rotate was a viewing transformation and translate was the modelling transformation. Pardon me but I get very confused when distinguishing between these two. Since I am reading the textbook now and these terms come up quite frequently.

Also always it may not be possible to rotate the coordinate system first and then translate. Consider an example where suppose you need to draw multiple such cubes in x-y plane and based on some keyboard inputs, you may need to rotate them about a fixed point. So in that case will the best approach be to draw the rotated cubes one by one clearing the screen each time for successive rotations ?

4. I'm a newbie too but from what I understood rotate and translate are both projections. You do the trick of rotate, translate and then go back to the original coordinate system by using glPushMatrix() and glPopMatrix()

5. Reading the textbook, it seems that rotate and translate were either model or view transformation. Maybe I got it wrong then. Need to revisit
And yeah using glPushMatrix() and glPopMatrix() may indeed be the right way to do so. Cheers

6. Originally Posted by novice01
Reading the textbook, it seems that rotate and translate were either model or view transformation. Maybe I got it wrong then. Need to revisit
And yeah using glPushMatrix() and glPopMatrix() may indeed be the right way to do so. Cheers
Maybe it's me that got it wrong, I'm still learning too :-) and I am studying from the slides that my professor gave us(they are written in English and I'm Italian so it's possible that I have got it wrong)
Bye!

7. Originally Posted by Perlice
Maybe it's me that got it wrong, I'm still learning too :-) and I am studying from the slides that my professor gave us(they are written in English and I'm Italian so it's possible that I have got it wrong)
Bye!

But I am still confused whether translate and rotate are both model transformation or view transformation? I need to clear this doubt before proceeding further else I am afraid I might be writing code without much understanding which is not a good thing.

Cheers.

8. Originally Posted by novice01

But I am still confused whether translate and rotate are both model transformation or view transformation? I need to clear this doubt before proceeding further else I am afraid I might be writing code without much understanding which is not a good thing.

Cheers.
Ok ok, my fault! I think I messed up things, I misunderstood your answer too..sorry!
Maybe this time I can halp you, I only copied the little part of the slides from where I'm studying that writes about transformations:

Compositing Modeling Transformations
• Think of a moving coordinate system
• Modeling transformations affect the coordinate system, not the objects
• All models are drawn relative to the current coordinate system
• The current coordinate system is represented by the current matrix
• Push/pop is used to isolate the rest of the program from the effects of a set of transformations.
From what I understand translate, rotate and scale are all modeling transformation, since they affect the way a scene is drawn; on the other hand lookAt is a matrix used to change the point of view so I suppose it's a view transformation.
Looking at your textbook, does it make any sense? Sorry but without a textbook I always fear to make mistakes

9. Originally Posted by Perlice
Ok ok, my fault! I think I messed up things, I misunderstood your answer too..sorry!
Maybe this time I can halp you, I only copied the little part of the slides from where I'm studying that writes about transformations:

From what I understand translate, rotate and scale are all modeling transformation, since they affect the way a scene is drawn; on the other hand lookAt is a matrix used to change the point of view so I suppose it's a view transformation.
Looking at your textbook, does it make any sense? Sorry but without a textbook I always fear to make mistakes
Yes this seems to be a simpler way to view at. The glulookAt as a view transformation and the rotation, translation as a model transform.
Nice Perlice , Cheers.

You can try reading the OpenGL red book which I am referring to. In my humble opinion, you must have a text book when you are trying to learn a new subject. And then the forums for posting doubts

10. Originally Posted by novice01
You can try reading the OpenGL red book which I am referring to. In my humble opinion, you must have a text book when you are trying to learn a new subject. And then the forums for posting doubts