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.

[QUOTE=novice01;1246795]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.[/QUOTE]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 ?

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

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

[QUOTE=novice01;1246807]Reading the textbook, it seems that rotate and translate were either model or view transformation. Maybe I got it wrong then. Need to revisit :slight_smile:
And yeah using glPushMatrix() and glPopMatrix() may indeed be the right way to do so. Cheers ;)[/QUOTE]
Maybe it’s me that got it wrong, I’m still learning too :slight_smile: 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!

[QUOTE=Perlice;1246808]Maybe it’s me that got it wrong, I’m still learning too :slight_smile: 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![/QUOTE]

Thanks for your reply Perlice.

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.

[QUOTE=novice01;1246818]Thanks for your reply Perlice.

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.[/QUOTE]

Ok ok, my fault! :slight_smile: 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

[ul]
[li] Think of a moving coordinate system
[/li]> [li] Modeling transformations affect the coordinate system, not the objects
[/li]> [li] All models are drawn relative to the current coordinate system
[/li]> [li] The current coordinate system is represented by the current matrix
[/li]> [li] Push/pop is used to isolate the rest of the program from the effects of a set of transformations.
[/li]> [/ul]

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 :slight_smile:

[QUOTE=Perlice;1246821]Ok ok, my fault! :slight_smile: 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 :)[/QUOTE]

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 :slight_smile: , 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 :wink:

Thanks for the advice :slight_smile:
cheers!

Ga tre my to 3th !