From quaternions to OpenGL rotations

牧云@^-^@ 提交于 2019-11-27 14:58:45

问题


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.

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.


回答1:


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.

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);
}



回答2:


You can easily build rotation matrices out of unit quaternions.

Given a unit quaternion a + bi + cj + dk, you can build the following 3x3 matrix:

Add the last line and column taken from the identity 4x4 matrix, glMultMatrix and you're done :)



来源:https://stackoverflow.com/questions/7938373/from-quaternions-to-opengl-rotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!