Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: A simple Rotation problem

Hybrid View

  1. #1
    Junior Member Newbie novice01's Avatar
    Join Date
    Jan 2013
    Posts
    5

    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.
    Last edited by novice01; 01-05-2013 at 06:51 AM.

  2. #2
    Intern Contributor
    Join Date
    Aug 2011
    Posts
    76
    Quote Originally Posted by novice01 View Post
    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. #3
    Junior Member Newbie novice01's Avatar
    Join Date
    Jan 2013
    Posts
    5
    Quote Originally Posted by newsb View Post
    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. #4
    Junior Member Newbie Perlice's Avatar
    Join Date
    Jan 2013
    Location
    Verona, Italy
    Posts
    10
    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. #5
    Junior Member Newbie novice01's Avatar
    Join Date
    Jan 2013
    Posts
    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. #6
    Junior Member Newbie Perlice's Avatar
    Join Date
    Jan 2013
    Location
    Verona, Italy
    Posts
    10
    Quote Originally Posted by novice01 View Post
    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. #7
    Junior Member Newbie novice01's Avatar
    Join Date
    Jan 2013
    Posts
    5
    Quote Originally Posted by Perlice View Post
    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!
    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.

  8. #8
    Junior Member Newbie Perlice's Avatar
    Join Date
    Jan 2013
    Location
    Verona, Italy
    Posts
    10
    Quote Originally Posted by novice01 View Post
    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.
    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. #9
    Junior Member Newbie novice01's Avatar
    Join Date
    Jan 2013
    Posts
    5
    Quote Originally Posted by Perlice View Post
    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
    Last edited by novice01; 01-06-2013 at 03:29 AM.

  10. #10
    Junior Member Newbie Perlice's Avatar
    Join Date
    Jan 2013
    Location
    Verona, Italy
    Posts
    10
    Quote Originally Posted by novice01 View Post
    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
    Thanks for the advice :-)
    cheers!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •