OpenGL中相对于相机旋转对象

发布于 2024-12-21 11:59:21 字数 1138 浏览 1 评论 0原文

我在 OpenGL 让我的对象(行星)相对于当前相机旋转旋转时遇到问题。一开始似乎可以工作,但旋转了一下后,旋转不再正确/相对于相机。

我正在计算屏幕上 mouseX 和 mouseY 移动的增量(差异)。旋转存储在名为“planetRotation”的 Vector3D 中。

这是我计算相对于 PlanetRotation 的旋转的代码:

Vector3D rotateAmount;
rotateAmount.x = deltaY;
rotateAmount.y = deltaX;
rotateAmount.z = 0.0;

glPushMatrix();
    glLoadIdentity();
    glRotatef(-planetRotation.z, 0.0, 0.0, 1.0);
    glRotatef(-planetRotation.y, 0.0, 1.0, 0.0);
    glRotatef(-planetRotation.x, 1.0, 0.0, 0.0);
    GLfloat rotMatrix[16];
    glGetFloatv(GL_MODELVIEW_MATRIX, rotMatrix);
glPopMatrix();

Vector3D transformedRot = vectorMultiplyWithMatrix(rotateAmount, rotMatrix);
planetRotation = vectorAdd(planetRotation, transformedRot);

理论上 - 其作用是在“rotateAmount”变量中设置旋转。然后,通过将该向量与逆模型变换矩阵 (rotMatrix) 相乘,将其放入模型空间。

然后将转换后的旋转添加到当前旋转中。

要渲染这是正在设置的变换:

glPushMatrix();
    glRotatef(planetRotation.x, 1.0, 0.0, 0.0);
    glRotatef(planetRotation.y, 0.0, 1.0, 0.0);
    glRotatef(planetRotation.z, 0.0, 0.0, 1.0);
    //render stuff here
glPopMatrix();

相机有点摇晃,我试图执行的旋转似乎与当前变换无关。

我做错了什么?

I'm having problems in OpenGL getting my object (a planet) to rotate relative to the current camera rotation. It seems to work at first, but then after rotating a bit, the rotations are no longer correct/relative to the camera.

I'm calculating a delta (difference) in mouseX and mouseY movements on the screen. The rotation is stored in a Vector3D called 'planetRotation'.

Here is my code to calculate the rotation relative to the planetRotation:

Vector3D rotateAmount;
rotateAmount.x = deltaY;
rotateAmount.y = deltaX;
rotateAmount.z = 0.0;

glPushMatrix();
    glLoadIdentity();
    glRotatef(-planetRotation.z, 0.0, 0.0, 1.0);
    glRotatef(-planetRotation.y, 0.0, 1.0, 0.0);
    glRotatef(-planetRotation.x, 1.0, 0.0, 0.0);
    GLfloat rotMatrix[16];
    glGetFloatv(GL_MODELVIEW_MATRIX, rotMatrix);
glPopMatrix();

Vector3D transformedRot = vectorMultiplyWithMatrix(rotateAmount, rotMatrix);
planetRotation = vectorAdd(planetRotation, transformedRot);

In theory - what this does is, sets up a rotation in the 'rotateAmount' variable. It then gets this into model space, by multiplying this vector with the inverse model transform matrix (rotMatrix).

This transformed rotation is then added to the current rotation.

To render this is the transform being setup:

glPushMatrix();
    glRotatef(planetRotation.x, 1.0, 0.0, 0.0);
    glRotatef(planetRotation.y, 0.0, 1.0, 0.0);
    glRotatef(planetRotation.z, 0.0, 0.0, 1.0);
    //render stuff here
glPopMatrix();

The camera sort of wobbles around, the rotation I'm trying to perform, doesn't seem relative to the current transform.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞花火° 2024-12-28 11:59:21

GAH! 不要这样做:

glPushMatrix();
    glLoadIdentity();
    glRotatef(-planetRotation.z, 0.0, 0.0, 1.0);
    glRotatef(-planetRotation.y, 0.0, 1.0, 0.0);
    glRotatef(-planetRotation.x, 1.0, 0.0, 0.0);
    GLfloat rotMatrix[16];
    glGetFloatv(GL_MODELVIEW_MATRIX, rotMatrix);
glPopMatrix();

OpenGL 不是一个数学库。有适合此类工作的线性代数库。


至于你的问题。 向量不适合存储旋转。您至少需要一个向量(旋转轴)和角度本身,或者更好的是一个四元数。

旋转也不会相加。它们不可交换,但加法是可交换运算。事实上,旋转成倍

如何修复代码:使用正确的数学方法从头开始重写代码。为此,请阅读“旋转矩阵”和“四元数”主题(维基百科有)。

GAH! Don't do that:

glPushMatrix();
    glLoadIdentity();
    glRotatef(-planetRotation.z, 0.0, 0.0, 1.0);
    glRotatef(-planetRotation.y, 0.0, 1.0, 0.0);
    glRotatef(-planetRotation.x, 1.0, 0.0, 0.0);
    GLfloat rotMatrix[16];
    glGetFloatv(GL_MODELVIEW_MATRIX, rotMatrix);
glPopMatrix();

OpenGL is not a math library. There are proper linear algebra libraries for that kind of job.


As for your problems. A vector is not fit to store a rotation. You need at least a Vector (axis of rotation) and the angle itself, or better yet a Quaternion.

Also rotations don't add. They're no commutative, however addition is a commutative operation. Rotations in fact multiply.

How to fix your code: Rewrite it from scratch using the proper mathematical methods. For this please read up the topics of "Rotation matrices" and "Quaternions" (Wikipedia has them).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文