在opengl中围绕感兴趣的点旋转相机

ano*_*les 2 opengl 3d rotation

我想围绕一个兴趣点旋转我的"相机".目前,相机仅围绕原点旋转.

许多教程建议使用以下方案:

translate(-P)
rotate
translate(P) 
Run Code Online (Sandbox Code Playgroud)

不幸的是,这不起作用.我的应用程序使用平移向量(QVector3D)以及四元数(QQuaternion)来保存相机的平移和旋转.

目前,它是这样做的,它始终围绕原点旋转:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(translation.x(),translation.y(), translation.z());
multMatrix(accumulatedQuaternionRotation);
Run Code Online (Sandbox Code Playgroud)

其中mulMatrix使用Quaternion构建一个传递给glMultMatrixf()的4x4矩阵;

使用这样的东西:

glTranslatef(-translation.x(),-translation.y(), -translation.z());
multMatrix(accumulatedQuaternionRotation);
glTranslatef(translation.x(),translation.y(), translation.z());
Run Code Online (Sandbox Code Playgroud)

导致非常奇怪的控制,我无法进一步描述.在我的应用程序中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;
Run Code Online (Sandbox Code Playgroud)

diff只是两个鼠标移动事件点的差异.

小智 5

你要做的是基本上实现gluLookAt,对吗?

这就是gluLookAt所做的,你应该能够从中复制你需要的东西:

http://www.opengl.org/wiki/GluLookAt_code

我不确定你真正试图用向量和四元数做什么,但我想你知道你在做什么,因为对于不熟悉gluLookat的人来说它似乎相当先进.