我想围绕一个兴趣点旋转我的“相机”。目前相机仅绕原点旋转。
很多教程建议使用以下方案:
translate(-P)
rotate
translate(P)
不幸的是,这不起作用。我的应用程序使用平移向量 (QVector3D) 以及四元数 (QQaternion) 来保存相机的平移和旋转。
目前是这样完成的,始终绕原点旋转:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(translation.x(),translation.y(), translation.z());
multMatrix(accumulatedQuaternionRotation);
其中 mulMatrix 使用四元数构建一个 4x4 矩阵,并将其传递给 glMultMatrixf();
使用这样的东西:
glTranslatef(-translation.x(),-translation.y(), -translation.z());
multMatrix(accumulatedQuaternionRotation);
glTranslatef(translation.x(),translation.y(), translation.z());
导致非常奇怪的控件,我无法进一步描述。在我的应用程序中,translation.z() 的意思是:向前移动相机。更改 x() 和 y() 会发出类似平移的操作。
我怀疑我使用了错误的平移向量,导致上述操作序列失败。
还有什么我可以检查的吗?
编辑:这就是四元数旋转的工作原理:
// calculate rotation axis
QVector3D rotationAxis = QVector3D(diff.y(), diff.x(), 0.0).normalized();
// update rotation see http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
accumulatedQuaternionRotation = QQuaternion::fromAxisAndAngle(rotationAxis, diff.length()/4.0f) * accumulatedQuaternionRotation;
diff 只是两个鼠标移动事件点的差异。
最佳答案
您想要做的基本上是实现 gluLookAt,对吧?
这是 gluLookAt 的作用,您应该能够从中复制您需要的内容:
http://www.opengl.org/wiki/GluLookAt_code
我不确定你真正想用向量和四元数做什么,但我假设你知道你在那里做什么,因为对于不熟悉 gluLookat 的人来说这似乎相当先进。
关于opengl - 在opengl中围绕兴趣点旋转相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14308595/