From quaternions to OpenGL rotations

前端 未结 2 1511
梦毁少年i
梦毁少年i 2020-12-15 10:55

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

相关标签:
2条回答
  • 2020-12-15 11:03

    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);
    }
    
    0 讨论(0)
  • 2020-12-15 11:06

    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:

    The matrix

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

    0 讨论(0)
提交回复
热议问题