OpenGL Rotation - Local vs Global Axes

前端 未结 2 530
走了就别回头了
走了就别回头了 2021-01-20 16:13

So I\'ve got an object I\'m trying to rotate according to a Yaw, Pitch and Roll scheme, relative to the object\'s own local axes rather than the global space\'s axes. Accor

2条回答
  •  庸人自扰
    2021-01-20 16:26

    Garrick,

    When you call glRotate(angle, x, y, z), it is rotating around the vector that you are passing into glRotate. The vector goes from (0,0,0) to (x,y,z).

    If you want to rotate an object around the object's local axis, you need to glTranslate the object to origin, perform your rotation, and then translate it back to where it came from.

    Here is an example:

    //Assume your object has the following properties
    sf::Vector3 m_rotation;
    sf::Vector3 m_center;
    
    //Here would be the rotate method
    public void DrawRotated(sf::Vector degrees) {
      //Store our current matrix 
      glPushMatrix();
    
      //Everything will happen in the reverse order...
      //Step 3: Translate back to where this object came from
      glTranslatef(m_center.x, m_center.y, m_center.z);
    
      //Step 2: Rotate this object about it's local axis
      glRotatef(degrees.y, 0, 1.0, 0);
      glRotatef(degrees.z, 0, 0, 1.0);
      glRotatef(degrees.x, 1.0, 0, 0);
    
      //Step 1: Translate this object to the origin
      glTranslatef(-1*m_center.x, -1*m_center.y, -1*m_center.z);
    
      //Render this object rotated by degrees
      Render();
    
      //Pop this matrix so that everything we render after this
      // is not also rotated
      glPopMatrix();
    }
    

提交回复
热议问题