opengl旋转问题

发布于 2024-12-15 12:35:15 字数 80 浏览 4 评论 0原文

谁能告诉我如何让我的模型以自己的重力中心而不是默认的 (0,0,0) 轴旋转?

而且我的旋转似乎只是左右旋转而不是 360 度..

can anyone tell me how to make my model rotate at its own center go gravity in stead of the default (0,0,0) axis?

and my rotation seems to be only going left and right not 360 degree..

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

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

发布评论

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

评论(1

顾北清歌寒 2024-12-22 12:35:16

如果要绕其中心旋转对象,首先必须将其平移到原点,然后旋转并平移回来。由于变换矩阵从右到左影响向量,因此您必须以相反的顺序对这些步骤进行编码。

这是一些伪代码,因为我不熟悉 OpenGL 例程:

PushMatrix();
LoadIdentity();  // Start with a fresh matrix
Translate();     // Move your object to its final destination
Rotate();        // Apply rotations
Draw();          // Draw your object using coordinates relative to the object center
PopMatrix();

应用这些矩阵:

v_t = (I * T * R) * v = (I * (T * (R * v)))

所以顺序是:旋转、平移。


编辑:对上面等式的解释。

旋转、缩放和平移变换会影响模型-视图矩阵。模型的每个 3D 点(向量)都乘以该矩阵,以获得其在 3D 空间中的最终点,然后乘以投影矩阵以接收 2D 点(在 2D 屏幕上)。

忽略投影内容,模型视图矩阵转换后的点是:

v_t = MV * v

意思是原始点 v 乘以模型视图矩阵 MV

在上面的代码中,我们通过单位矩阵I、平移T和旋转R构造了MV

MV = I * T * R

将所有内容放在一起,您会发现您的点 v 首先受到旋转 R 的影响,然后受到平移 T 的影响,因此您的点是在平移之前旋转,就像我们希望的那样:

v_t = MV * v = (I * T * R) * v = T * (R * v)

在之前调用 Rotate() Translate() 会导致:

v_t = (I * R * T) * v = R * (T * v)

这会很糟糕:平移到 3D 中的某个点,然后绕原点旋转,导致模型中出现一些奇怪的变形。

If you want to rotate an object around its center, you first have to translate it to the origin, then rotate and translate it back. Since transformation matrices affect your vectors from right to left, you have to code these steps in opposite order.

Here is some pseudocode since I don't know OpenGL routines by heart:

PushMatrix();
LoadIdentity();  // Start with a fresh matrix
Translate();     // Move your object to its final destination
Rotate();        // Apply rotations
Draw();          // Draw your object using coordinates relative to the object center
PopMatrix();

These matrices get applied:

v_t = (I * T * R) * v = (I * (T * (R * v)))

So the order is: Rotation, Translation.


EDIT: An explanation for the equation above.

The transformations rotation, scale and translation affect the model-view-matrix. Every 3D point (vector) of your model is multiplied by this matrix to get its final point in 3D space, then it gets multiplied by the projection matrix to receive a 2D point (on your 2D screen).

Ignoring the projection stuff, your point transformed by the model-view-matrix is:

v_t = MV * v

Meaning the original point v, multiplied by the model-view-matrix MV.

In the code above, we have constructed MV by an identity matrix I, a translation T and a rotation R:

MV = I * T * R

Putting everything together, you see that your point v is first affected by the rotation R, then the translation T, so that your point is rotated before it is translated, just as we wanted it to be:

v_t = MV * v = (I * T * R) * v = T * (R * v)

Calling Rotate() prior to Translate() would result in:

v_t = (I * R * T) * v = R * (T * v)

which would be bad: Translated to some point in 3D, then rotated around the origin, leading to some strange distortion in your model.

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