在 OpenGL 中相对于视口旋转
我正在尝试在视图中显示一个对象,该对象可以通过拖动光标/触摸屏来自然旋转。目前我已经得到了像这样的物体的 X 和 Y 旋转,
glRotatef(rotateX, 0f, 1f, 0f); // Dragging along X, so spin around Y axis
glRotatef(rotateY, 1f, 0f, 0f);
我明白为什么这不能做我想要它做的事情(例如,如果你将它旋转 180 度,上下旋转就会相反)。我只是想不出一种方法让两个方向相对于观看者保持左右和上下。
我可以假设相机是固定的并且沿着 Z 轴观察。有什么想法吗?
I'm trying to display an object in the view which can be rotated naturally by dragging the cursor/touchscreen. At the moment I've got X and Y rotation of an object like this
glRotatef(rotateX, 0f, 1f, 0f); // Dragging along X, so spin around Y axis
glRotatef(rotateY, 1f, 0f, 0f);
I understand why this doesn't do what I want it to do (e.g. if you spin it right 180 degrees, up and down spinning gets reversed). I just can't figure out a way for both directions to stay left-right and up-down relative to the viewer.
I can assume that the camera is fixed and looking along the Z axis. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最好的选择是实现基于四元数的旋转。在四元数世界中,每次旋转时,它都会与您指定的轴对齐,而不受之前旋转的影响。这也是它不会受到万向节锁定问题的原因。
我发现这些页面有助于实现四元数:
祝你好运。我确信还有其他解决方案,但这是您可以拥有的最干净的解决方案之一。
Your best bet is to implement a Quaternion-based rotation. In the Quaternion world, every time you rotate, it will be axis-aligned to the axis you specify, without being affected by the previous rotations. This is also why it doesn't suffer from Gimbal Lock.
I've found these pages helpful for implementing quaternions:
Good luck. I'm sure there are other solutions, but this one is one of the cleanest you can have.
解决了!将 xAngle 和 yAngle 添加到当前矩阵。
Solved it! Add xAngle and yAngle to the current matrix.
我决定将我的 DragControl 类与四元数支持类一起提供下载。一旦你有了一个沿着 Z 轴查看某个对象的 OpenGL 画布,将其放入应该不费什么力气。目前它只是普通的 .java 文件,没有内置库。
DragControl 几乎可以处理所有事情,包括手指弹动,因此您可以通过轻弹使对象旋转。
http://github.com/halfninja/android-dragcontrol3d
在活动设置中:
更新对象时循环旋转:
如果你做了任何很酷的修改,我希望看到它们。
I decided to put my DragControl class up for download, along with the Quaternion support class. Once you have an OpenGL canvas looking along the Z axis at some object, it should be little effort to drop this in. It's just the plain .java files currently, no built library.
DragControl handles pretty much everything including finger flinging, so you can send your object spinning with a flick.
http://github.com/halfninja/android-dragcontrol3d
In activity setup:
When updating object rotation in a loop:
If you make any cool modifications I'd like to see them.