在 OpenGL 中实现固定坐标系
我的项目的简单版本是根据用户输入旋转球体。
然而,由于 openGL 的本地坐标系,在某些情况下多次旋转后我的旋转变得很奇怪。 我真正需要的是基于世界/固定坐标系旋转。这样当我旋转球体时,它的旋转轴也不会旋转。 这个问题在这里解释 http://www.opengl.org/resources/faq /technical/transformations.htm 位于 9.070。
我明白这一切。然而,按照他们的建议进行设置有点超出了我的技能水平。有人有这方面的经验吗?我尝试使用我在这里找到的一些代码 http://chaosengineer.com/?cat=19 但没有运气。
这是我正在使用的基本代码。
glPushMatrix();
//draw in middle of the screen
glTranslatef(ofGetWidth()/2,ofGetHeight()/2,0);
glRotatef(xRot,1,0,0);
glRotatef(zRot,0,0,1);
glTranslatef(-ofGetWidth()/2,-ofGetHeight()/2,0);
ofSetColor(255, 255, 255, 255);
squirrelModel.draw();
glPopMatrix();
The simple version of my project is rotating a sphere based on user input.
However my rotations get weird after multiple rotations in certain situations due to openGL's local coordinate system.
What I really need is to rotate based on a world/fixed coordinate system. Such that when I rotate the sphere once its rotation axes are not rotated as well.
This issue is explained here http://www.opengl.org/resources/faq/technical/transformations.htm at 9.070.
I understand all that. However setting up what they suggest is a bit beyond my skill level. Does anyone have any experience with this? I tried using some code I found here
http:// chaosengineer.com/?cat=19
but had no luck.
Here is the basic code I am using.
glPushMatrix();
//draw in middle of the screen
glTranslatef(ofGetWidth()/2,ofGetHeight()/2,0);
glRotatef(xRot,1,0,0);
glRotatef(zRot,0,0,1);
glTranslatef(-ofGetWidth()/2,-ofGetHeight()/2,0);
ofSetColor(255, 255, 255, 255);
squirrelModel.draw();
glPopMatrix();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将对象方向存储为矩阵。或者作为一组向量(局部 X、局部 Y、局部 Z)。
要稍微旋转对象,请每次将方向矩阵乘以旋转矩阵。只需确保方向矩阵的所有向量保持彼此垂直即可。
在这种情况下,渲染时,您需要使用 glMultMatrix,而不是 glRotate/glTranslate。
或者使用四元数。
我知道这不是一个完整的答案,但是(尽管我已经编写了用于执行此操作的库和代码),解释这一点将花费大量时间。
尝试下载 NVidia OpenGL sdk 并查看他们如何在演示中处理对象旋转。
还可以使用 google 搜索3dorientation。有些论文应该解释如何使用矩阵来存储面向对象等等。
并检查 euclidianspace.com
YOu need to store object orientation as a matrix. Or as a set of vectors (local X, local Y, local Z).
To rotate object a bit, multiply orientation matrix by rotation matrix each time. Just make sure that all vectors of orientation matrix remain perpendicular to each other.
In this case, when rendering, instead of glRotate/glTranslate you'll need to use glMultMatrix.
Or use quaternions.
I understand it is not a complete answer, but (although I have written libraries and code for doing this), explaining this will take a lot of time.
Try downloading NVidia OpenGL sdk and see how they handled object rotation in their demos.
Also search for 3d orientation with google. Some papers should explain how to store object orientation using matrices, and so on.
And check euclidianspace.com