OpenGL旋转问题 [英] opengl rotation problem

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

问题描述

有人可以告诉我如何使模型在其中心旋转而不是默认(0,0,0)轴的重力吗?

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?

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

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

推荐答案

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

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.

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

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();

应用这些矩阵:

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

所以顺序是:旋转,平移.

So the order is: Rotation, Translation.

编辑:对上述方程式的解释.

An explanation for the equation above.

转换旋转,缩放和平移会影响 model-view-matrix .将模型的每个3D点(向量)乘以该矩阵以得到3D空间中的最终点,然后将其乘以投影矩阵以接收一个2D点(在2D屏幕上).

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

忽略投影内容,由model-view-matrix转换的点是:

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

v_t = MV * v

意思是原始点v,乘以model-view-matrix MV.

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

在上面的代码中,我们通过一个单位矩阵I,一个平移T和一个旋转R构造了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

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

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)

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

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

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

这很糟糕:将其转换为3D的某个点,然后绕原点旋转,从而导致模型出现一些奇怪的变形.

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

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

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