opengl矩阵旋转四元数

发布于 2024-09-11 08:22:35 字数 843 浏览 3 评论 0原文

我正在尝试围绕 x 和 y 轴对立方体进行简单旋转:

我希望始终将立方体在 x 轴上旋转 x 量 并将立方体在 y 轴上旋转一个量 y,与 x 轴旋转无关,

首先我天真地做了:

glRotatef(x,1,0,0);
glRotatef(y,0,1,0);

然后,

但首先在 x 上旋转,然后在 yi 上旋转,想要独立于 x 访问在 y 上旋转。

我开始研究四元数,所以我尝试:

Quaternion Rotation1;
Rotation1.createFromAxisAngle(0,1, 0, globalRotateY);
Rotation1.normalize();

Quaternion Rotation2;
Rotation2.createFromAxisAngle(1,0, 0, globalRotateX);
Rotation2.normalize();

GLfloat Matrix[16];

Quaternion q=Rotation2 * Rotation1;

q.createMatrix(Matrix);
glMultMatrixf(Matrix);

这几乎完全完成了两次连续 glRotates 所完成的工作...所以我认为我错过了一个或两个步骤。

四元数是要走的路还是我应该使用不同的东西?如果四元数是可行的方法,我可以添加哪些步骤来使立方体独立于每个轴旋转。 我认为其他人也有同样的问题: 在 2 个轴上旋转 OpenGL 场景

Im trying to do a simple rotation of a cube about the x and y axis:

I want to always rotate the cube over the x axis by an amount x
and rotate the cube over the yaxis by an amount y independent of the x axis rotation

first i naively did :

glRotatef(x,1,0,0);
glRotatef(y,0,1,0);

then

but that first rotates over x then rotates over y
i want to rotate over the y independently of the x access.

I started looking into quaternions, so i tried :

Quaternion Rotation1;
Rotation1.createFromAxisAngle(0,1, 0, globalRotateY);
Rotation1.normalize();

Quaternion Rotation2;
Rotation2.createFromAxisAngle(1,0, 0, globalRotateX);
Rotation2.normalize();

GLfloat Matrix[16];

Quaternion q=Rotation2 * Rotation1;

q.createMatrix(Matrix);
glMultMatrixf(Matrix);

that just does almost exactly what was accomplished doing 2 consecutive glRotates ...so i think im missing a step or 2.

is quaternions the way to go or should i be using something different? AND if quaternions are the way to go what steps can i add to make the cube rotate independently of each axis.
i think someone else has the same issue:
Rotating OpenGL scene in 2 axes

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

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

发布评论

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

评论(4

维持三分热 2024-09-18 08:22:36

我使用四元数使其正常工作:我确信还有其他方法,但经过一些研究后,这对我来说非常有效。我在另一个论坛上发布了类似的版本。 http://www.opengl.org/discussion_boards /ubbthreads.php?ubb=showflat&Number=280859&#Post280859

首先创建 x/y 角度变化的四元数表示
然后每一帧将变化的角度四元数乘以累积四元数,最后将该四元数转换为矩阵形式以乘以当前矩阵。这是循环的主要代码:

Quaternion3D Rotation1=Quaternion3DMakeWithAxisAndAngle(Vector3DMake(-1.0f,0,0), DEGREES_TO_RADIANS(globalRotateX));
Quaternion3DNormalize(&Rotation1);

Quaternion3D Rotation2=Quaternion3DMakeWithAxisAndAngle(Vector3DMake(0.0f,-1.0f,0), DEGREES_TO_RADIANS(globalRotateY));
Quaternion3DNormalize(&Rotation2);


Matrix3D Mat;
Matrix3DSetIdentity(Mat);
Quaternion3DMultiply(&QAccum, &Rotation1);

Quaternion3DMultiply(&QAccum, &Rotation2);

Matrix3DSetUsingQuaternion3D(Mat, QAccum);
globalRotateX=0;
globalRotateY=0;

glMultMatrixf(Mat);

然后绘制立方体

I got this to work correctly using quaternions: Im sure there are other ways, but afeter some reseatch , this worked perfectly for me. I posted a similar version on another forum. http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=280859&#Post280859

first create the quaternion representation of the angles of change x/y
then each frame multiply the changing angles quaternions to an accumulating quaternion , then finally convert that quaternion to matrix form to multiply the current matrix. Here is the main code of the loop:

Quaternion3D Rotation1=Quaternion3DMakeWithAxisAndAngle(Vector3DMake(-1.0f,0,0), DEGREES_TO_RADIANS(globalRotateX));
Quaternion3DNormalize(&Rotation1);

Quaternion3D Rotation2=Quaternion3DMakeWithAxisAndAngle(Vector3DMake(0.0f,-1.0f,0), DEGREES_TO_RADIANS(globalRotateY));
Quaternion3DNormalize(&Rotation2);


Matrix3D Mat;
Matrix3DSetIdentity(Mat);
Quaternion3DMultiply(&QAccum, &Rotation1);

Quaternion3DMultiply(&QAccum, &Rotation2);

Matrix3DSetUsingQuaternion3D(Mat, QAccum);
globalRotateX=0;
globalRotateY=0;

glMultMatrixf(Mat);

then draw cube

长梦不多时 2024-09-18 08:22:36

如果您能够更详细地解释您正在尝试做什么以及您得到的结果与您想要的结果有何不同,那将会有很大帮助。但一般来说,使用欧拉角进行旋转会存在一些问题,因为组合旋转可能会导致不直观的行为(并且在最坏的情况下会失去一定的自由度)。

如果您能找到单个轴,那么四元数 slerp 可能是您的最佳选择和一个代表您想要的旋转的角度。但是使用四元数围绕 X 和 Y 轴进行连续旋转并不能帮助您避免构成欧拉旋转所固有的问题。

不过,您链接到的帖子似乎涉及另一个问题。海报似乎一直在平移他的物体,然后进行旋转,而他应该先旋转然后平移。

It would help a lot if you could give a more detailed explanation of what you are trying to do and how the results you are getting differ from the results you want. But in general using Euler angles for rotation has some problems, as combining rotations can result in unintuitive behavior (and in the worst case losing a degree of freedom.)

Quaternion slerp might be the way to go for you if you can find a single axis and a single angle that represent the rotation you want. But doing successive rotations around the X and Y axis using quaternions won't help you avoid the problems inherent in composing Euler rotations.

The post you link to seems to involve another problem though. The poster seems to have been translating his object and then doing his rotations, when he should have been rotating first and then translating.

坠似风落 2024-09-18 08:22:36

目前尚不清楚您想要实现什么目标。也许您应该考虑一些点以及您希望它们旋转到的位置 - 例如顶点 (1,1,1) 应该映射到 (0,1,0)。然后,根据该信息,您可以计算所需的旋转。

四元数通常用于在两个旋转“位置”之间进行插值。因此,第一步是确定您还没有的开始和结束“位置”。一旦你有了它,你就可以使用四元数进行插值。听起来你这里没有任何随时间变化的方面。

It is not clear what you want to achieve. Perhaps you should think about some points and where you want them to rotate to -- e.g. vertex (1,1,1) should map to (0,1,0). Then, from that information, you can calculate the required rotation.

Quaternions are generally used to interpolate between two rotational 'positions'. So step one is identifying your start and end 'positions', which you don't have yet. Once you have that, you use quaternions to interpolate. It doesn't sound like you have any time-varying aspect here.

困倦 2024-09-18 08:22:36

你的问题不是万向节锁。实际上,您的四元数版本没有理由比您的矩阵(glRotate)版本更好,因为您使用的四元数在数学上与您的旋转矩阵相同。

如果您想要的是鼠标控制,您可能需要查看 arcballs

Your problem is not the gimbal lock. And effectively, there is no reason why your quaternion version would work better than your matrix (glRotate) version because the quaternions you are using are mathematically identical to your rotation matrices.

If what you want is a mouse control, you probably want to check out arcballs.

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