Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: How to rotate objects around global axis not local axis

  1. #1
    Newbie Newbie
    Join Date
    Nov 2017
    Posts
    2

    How to rotate objects around global axis not local axis

    I'm newbie in openGL.
    I'v searched for an examples from google, stackoverflow and opengl.org but I don't found an exactly result yet.

    My issue is
    1. I'v rotated a box in x axis by 45 degrees, the result is OK.
    2. I'v rotated same box in y axis by 45 degrees, the result is weird.
    It's not rotated in global y axis as my expect but rotated in own y axis.

    How should I do? Are there any examples?

    This is my code

    Code :
    ...
    model = glm::rotate(model, glm::radians(45.0f), glm::vec3(1.0, 0.0, 0.0));
    model = glm::rotate(model, glm::radians(45.0f), glm::vec3(0.0, 1.0, 0.0));
     
    myShader.setMat4("model", model);
    myModel.Draw(myShader);
    ...

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,476
    Try reversing the order of the rotations.

    The first rotation is rotates everything (including the axes) about the initial (global) axes, the second rotation rotates about the axes established by the first rotation.

    So in your case, the second rotation is rotating about a Y axis which has itself been rotated by 45 degrees about the global X axis. If you change the order, the first rotation will rotate about the global Y axis, the second about an X axis which has itself been rotated by 45 degrees about the global Y axis.

    If that isn't what you want, another option is to use rotation vectors: represent each rotation as a vector whose direction is the rotation axis and the length proportional to the rotation angle. Sum the vectors, then convert the final result to a length and a normalised vector, then pass those to glm::rotate(), e.g.

    Code :
        glm::vec3 x = glm::vec3(1,0,0)*glm::radians(45.0f);
        glm::vec3 y = glm::vec3(0,1,0)*glm::radians(45.0f);
        glm::vec3 r = x+y;
        glm::mat4 m;
        m = glm::rotate(m, glm::length(r), glm::normalize(r));

  3. #3
    Newbie Newbie
    Join Date
    Nov 2017
    Posts
    2
    Quote Originally Posted by GClements View Post
    Try reversing the order of the rotations.

    The first rotation is rotates everything (including the axes) about the initial (global) axes, the second rotation rotates about the axes established by the first rotation.

    So in your case, the second rotation is rotating about a Y axis which has itself been rotated by 45 degrees about the global X axis. If you change the order, the first rotation will rotate about the global Y axis, the second about an X axis which has itself been rotated by 45 degrees about the global Y axis.

    If that isn't what you want, another option is to use rotation vectors: represent each rotation as a vector whose direction is the rotation axis and the length proportional to the rotation angle. Sum the vectors, then convert the final result to a length and a normalised vector, then pass those to glm::rotate(), e.g.

    Code :
        glm::vec3 x = glm::vec3(1,0,0)*glm::radians(45.0f);
        glm::vec3 y = glm::vec3(0,1,0)*glm::radians(45.0f);
        glm::vec3 r = x+y;
        glm::mat4 m;
        m = glm::rotate(m, glm::length(r), glm::normalize(r));

    Thanks for your suggestion, it seems worked at 45 degrees of both x and y axes.
    but when I changed a rotation degree of some axis ex. x=45 deg. , y=80 deg.
    the given result is still tilted. Have any ideas?

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,476
    In that case, you'll need to explain exactly what you mean.

Posting Permissions

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