opengl旋转问题
谁能告诉我如何让我的模型以自己的重力中心而不是默认的 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要绕其中心旋转对象,首先必须将其平移到原点,然后旋转并平移回来。由于变换矩阵从右到左影响向量,因此您必须以相反的顺序对这些步骤进行编码。
这是一些伪代码,因为我不熟悉 OpenGL 例程:
应用这些矩阵:
所以顺序是:旋转、平移。
编辑:对上面等式的解释。
旋转、缩放和平移变换会影响模型-视图矩阵。模型的每个 3D 点(向量)都乘以该矩阵,以获得其在 3D 空间中的最终点,然后乘以投影矩阵以接收 2D 点(在 2D 屏幕上)。
忽略投影内容,模型视图矩阵转换后的点是:
意思是原始点
v
乘以模型视图矩阵MV
。在上面的代码中,我们通过单位矩阵
I
、平移T
和旋转R
构造了MV
:将所有内容放在一起,您会发现您的点
v
首先受到旋转R
的影响,然后受到平移T
的影响,因此您的点是在平移之前旋转,就像我们希望的那样:在之前调用 Rotate() Translate() 会导致:
这会很糟糕:平移到 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:
These matrices get applied:
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:
Meaning the original point
v
, multiplied by the model-view-matrixMV
.In the code above, we have constructed
MV
by an identity matrixI
, a translationT
and a rotationR
:Putting everything together, you see that your point
v
is first affected by the rotationR
, then the translationT
, so that your point is rotated before it is translated, just as we wanted it to be:Calling Rotate() prior to Translate() would result in:
which would be bad: Translated to some point in 3D, then rotated around the origin, leading to some strange distortion in your model.