从四元数到 OpenGL 旋转

2021-12-19 00:00:00 opengl linear-algebra c++ quaternions

我有一个要通过按键旋转的对象.物体应该偏航、俯仰和滚动.尝试了很多之后,我发现 glRotate 有它的局限性,用那个函数不可能实现类似的东西.

I have an object which I want to rotate via keys. The object should yaw, pitch and roll. After trying a lot, I figured out that glRotate has its limitations and it won't be possible to implement something like that with that function.

我研究了一下,发现了基于四元数的旋转.也可以通过旋转矩阵进行旋转,但几乎每个人都将四元数描述为有史以来最好的.

I've researched a bit and found out about quaternion-based rotation. It would be also possible to rotate via a rotation matrix, but almost everyone describes the quaternions as the best ever.

我已经阅读了四元数并且对它们相当了解,但是如何在我的 OpenGL 程序中实现它们仍然是一个谜.有谁知道一个小例子?不是如何实现四元数类,而是如何使用它.我不知道如何把这些东西放在一起.

I have read about the quaternions and understood them fairly well, but how to implement them in my OpenGL program is still a mystery. Does anyone know a small example? Not how to implement the quaternion class but how to use it. I don't get how to put the things together.

推荐答案

最重要的是实现从四元数构建旋转矩阵的方法.然后,就像使用普通旋转矩阵一样,只需将其与模型视图矩阵相乘即可??.

The most important is to implement method that will build rotation matrices from quaternion. Then, as you'll do with normal rotation matrix, just multiply it with your modelview matrix.

这是一个简单的例子,用于通过鼠标移动来旋转立方体.如您所见,我需要实现三种方法:toMatrixquaternionFromAxismultiplyWith.假设您了解四元数,这应该很清楚它们的作用.

This is simple example that was used to rotate cube with mouse movement. As you see I've needed to implement three methods: toMatrix, quaternionFromAxis, multiplyWith. Assuming that you understand quaternions, this should be clear what they do.

void display() {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();

    // Multiply quaternion with current modelview matrix    
    glMultMatrixf(cameraQuaternion.toMatrix());

    // Some other transformations
    glTranslatef(-0.5f, -0.5f, -0.5f);

    // Draw something, i.e. cube
    glDrawArrays(GL_TRIANGLES, 0, 36);
    glDrawArrays(GL_TRIANGLES, 0, 36);

    glPopMatrix();
}

void mouseMoved(float dx, float dy) {
    float axisY[] = { 0, 1, 0 },
          axisX[] = { 1, 0, 0 };

    float sensitivity = 0.005f;
    Quaternion *q1 = Quaternion.quaternionFromAxis(axisY, dx * sensitivity];
    Quaternion *q2 = Quaternion.quaternionFromAxis(axisX, dy * sensitivity];

    // Multiply two quaternions with camera
    cameraQuaternion = (q1.multiplyWith(q2)).multiplyWith(cameraQuaternion);
}

相关文章