OpenGL 旋转 - 局部与全局轴 [英] OpenGL Rotation - Local vs Global Axes

查看:42
本文介绍了OpenGL 旋转 - 局部与全局轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个对象,我试图根据偏航、俯仰和滚动方案相对于对象自己的局部轴而不是全局空间的轴进行旋转.根据this,我需要执行旋转以该顺序.我已经将其解释为:

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. According to this, I need to perform the rotations in that order. I've interpreted that to mean this:

glRotatef(m_Rotation.y, 0.0, 1.0, 0.0);
glRotatef(m_Rotation.z, 0.0, 0.0, 1.0);
glRotatef(m_Rotation.x, 1.0, 0.0, 0.0);

但是,围绕 Y 轴和 Z 轴的旋转不起作用.绕Y轴旋转总是相对于全局空间,绕Z轴旋转的作用是绕X轴旋转的0,否则就乱了.

However, rotation around the Y and Z axes don't work. Rotation around the Y axis is always relative to the global space, and rotation around the z axis works of the rotation around the X axis is 0, but otherwise messes up.

可以肯定的是,我也尝试了相反的顺序,但这也不起作用.我想我也尝试了所有其他命令,所以问题一定是其他问题.可能吗?

Just to be sure, I tried the reverse order as well, but that doesn't work either. I think I've tried all the other orders as well, so the problem must be something else. might it be?

这就是我获得旋转的方式:

This is how I obtain the rotations:

    ///ROTATIONS
    sf::Vector3<float> Rotation;
    Rotation.x = 0;
    Rotation.y = 0;
    Rotation.z = 0;
    //ROLL
    if (m_pApp->GetInput().IsKeyDown(sf::Key::Up) == true)
    {
        Rotation.x -= TurnSpeed;
    }
    if (m_pApp->GetInput().IsKeyDown(sf::Key::Down) == true)
    {
        Rotation.x += TurnSpeed;
    }
    //YAW
    if (m_pApp->GetInput().IsKeyDown(sf::Key::Left) == true)
    {
        Rotation.y -= TurnSpeed;
    }
    if (m_pApp->GetInput().IsKeyDown(sf::Key::Right) == true)
    {
        Rotation.y += TurnSpeed;
    }
    //PITCH
    if (m_pApp->GetInput().IsKeyDown(sf::Key::Q) == true)
    {
        Rotation.z -= TurnSpeed;
    }
    if (m_pApp->GetInput().IsKeyDown(sf::Key::E) == true)
    {
        Rotation.z += TurnSpeed;
    }

然后将它们添加到 m_Rotation 中:

They are then added to m_Rotation as such:

//Rotation
m_Rotation.x += Angle.x;
m_Rotation.y += Angle.y;
m_Rotation.z += Angle.z;

(它们被传递给正在移动的东西的内部函数,但没有对它们做任何其他事情).

(They are passed to a function internal to the thing being moved around, but nothing else is done with them).

想法?还有什么我应该打电话来确保所有旋转的轴都是局部轴的吗?

Thoughts? Is there something else I should call to make sure all the axes being rotated around are local axes?

推荐答案

Garrick,

当您调用 glRotate(angle, x, y, z) 时,它会围绕您传递给 glRotate 的向量旋转.向量从 (0,0,0) 到 (x,y,z).

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

如果要绕对象的局部轴旋转对象,则需要将对象 glTranslate 到原点,执行旋转,然后将其平移回原来的位置.

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.

这是一个例子:

//Assume your object has the following properties
sf::Vector3<float> m_rotation;
sf::Vector3<float> m_center;

//Here would be the rotate method
public void DrawRotated(sf::Vector<float> 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();
}

这篇关于OpenGL 旋转 - 局部与全局轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆