Rotate camera around point

I want to rotate my "camera" around a point of interest. Currently the camera only rotates around the origin.

A lot of tutorials suggest to use following scheme:

translate(-P)
rotate
translate(P) 

Unfortunately, this doesn't work. My application uses a translation vector (QVector3D) as well as a quaternion (QQuaternion) to save translation and rotation of the camera.

Currently, it is done like this, which always rotates around the origin:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(translation.x(),translation.y(), translation.z());
multMatrix(accumulatedQuaternionRotation);

where mulMatrix uses the Quaternion to build a 4x4 matrix which is passed to glMultMatrixf();

Using something like this:

glTranslatef(-translation.x(),-translation.y(), -translation.z());
multMatrix(accumulatedQuaternionRotation);
glTranslatef(translation.x(),translation.y(), translation.z());

results in very weird controls which I'm unable to describe further. In my application translation.z() means: move the camera forward. Changing x() and y() issues a pan like operation.

I suspect that I'm using the translation vector wrong, resulting in the above sequence of operations to fail.

Is there any thing I can else check?

Edit: This is how the quaternion rotation works:

        // 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 is simply the difference of two mouse move event points.


What you're trying to do is basically implement gluLookAt, right?

Here's what gluLookAt does, you should be able to copy what you need from it:

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

I'm not sure what you're really trying to do with the vector and the quaternion, but I assume you know what you're doing there as it seems rather advanced for someone who is unfamiliar with gluLookat.

链接地址: http://www.djcxy.com/p/81836.html

上一篇: 围绕opengl中的一个固定点旋转对象

下一篇: 围绕点旋转相机