在OpenGL中相对于视口旋转 [英] Rotating in OpenGL relative to the viewport
问题描述
我正在尝试在视图中显示一个对象,该对象可以通过拖动光标/触摸屏自然旋转.此刻,我已经对这样的对象进行了X和Y旋转
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);
我了解为什么这样做没有达到我想要的目的(例如,如果将其旋转180度,则上下旋转会相反).我只是想不出办法让两个方向都相对于观看者保持左右和上下.
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.
我可以假设相机是固定的,并且沿Z轴方向看.有任何想法吗?
I can assume that the camera is fixed and looking along the Z axis. Any ideas?
推荐答案
您最好的选择是实现的旋转.在四元数世界中,每次旋转时,它都会与您指定的轴对齐,而不受先前旋转的影响.这也是为什么它不会遭受万向节锁定的困扰.
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:
- http://en.wikipedia.org/wiki/Quaternion
- http://www.cprogramming.com/tutorial/3d/quaternions.html
- http://www.gamedev.net/reference/articles/article1095.asp
- http://en.wikipedia.org/wiki/Quaternion
- http://www.cprogramming.com/tutorial/3d/quaternions.html
- http://www.gamedev.net/reference/articles/article1095.asp
祝你好运.我敢肯定还有其他解决方案,但这是您可以采用的最干净的解决方案之一.
Good luck. I'm sure there are other solutions, but this one is one of the cleanest you can have.
这篇关于在OpenGL中相对于视口旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!