OpenGL旋转-局部轴与全局轴
OpenGL Rotation - Local vs Global Axes
所以我有一个对象,我试图根据偏航、俯仰和滚转方案,相对于对象自己的局部轴而不是全局空间的轴旋转。根据这个,我需要按照这个顺序进行旋转。我把它解释为:
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,但在其他方面会造成混乱。
可以肯定的是,我也尝试了相反的顺序,但也不起作用。我想我也试过所有其他的订单,所以问题一定是其他的。可能是吗?
这就是我获得旋转的方式:
///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中,如下所示:
//Rotation
m_Rotation.x += Angle.x;
m_Rotation.y += Angle.y;
m_Rotation.z += Angle.z;
(它们被传递给被移动对象内部的函数,但不会对它们执行任何其他操作)。
想法?还有什么我应该调用的东西来确保所有旋转的轴都是局部轴吗?
Garrick,
当调用glRotate(角度,x,y,z)时,它将围绕传递到glRotate中的向量旋转。矢量从(0,0,0)到(x,y,z)。
如果要绕对象的局部轴旋转对象,则需要将对象glTranslate到原点,执行旋转,然后将其平移回其来源。
这里有一个例子:
//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();
}
您的问题是存储x、y、z旋转并累积添加。然后在渲染时,对单位矩阵执行总累积旋转(全局执行所有旋转)。注释掉渲染循环中的身份调用。并确保在初始化函数中设置了标识。然后
rotate as normal
m_Rotation.x = m_Rotation.y = m_Rotation.z = 0.0f;
//clear your rotations you don't want them to accumulate between frames
glpush
translate as normal
(all other stuff)
glpop
//you should be back to just the rotations
//glclear and start next frame as usual
我相信你是在接受了原来的答案后才发现的。旋转或平移的顺序不会影响旋转发生在哪个轴上,而是影响执行旋转的点。例如,将行星旋转15度将使其在全球轴上旋转15度。将其从原点平移,然后旋转,将使其以平移的距离环绕原点(如果与旋转在同一轴上,则在x轴上平移,然后在y轴上旋转不会产生任何混淆效应)。
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 当vector是tje全局变量时,c++中vector的内存管理
- 与互斥锁相比,旋转锁可以保证上下文切换
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 如何创建一个空的全局类并在启动时实例化它
- 内联函数中具有内部链接的全局变量
- 绘制旋转的三角形
- 为什么虚函数不能是静态的和全局的?
- 旋转模型矩阵时的形状失真
- 四边形的 2D 旋转
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 垂直方向的 Gtk3+ 旋转按钮 (c/c++)
- 全局作用域中函数指针的赋值
- 发布旋转矩阵(openGL/glm)
- 如何在 openGL 中围绕全局轴旋转模型?
- 从全局位置/旋转/缩放更新局部位置/旋转/缩放
- OpenGL旋转-局部轴与全局轴
- 使对象围绕全局轴Directx旋转
- 如何使用 glRotatef 围绕对象的局部(非全局)轴执行旋转?