如何使用OpenGL从偏航,俯仰和滚动中获得旋转矩阵?

时间:2014-08-10 19:13:38

标签: ios objective-c opengl matrix augmented-reality

我尝试了多种方法,但预期的矩阵不是我想要的。我希望产生由姿态给出的相同旋转矩阵,但是从偏航,俯仰和滚转值产生它。如果有一个我不知道的内置函数,我很乐意使用它。

    [self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical toQueue:[NSOperationQueue currentQueue] withHandler:^ (CMDeviceMotion *motionData, NSError *error) {
        CMRotationMatrix r = motionData.attitude.rotationMatrix;

        GLKMatrix4 baseModelViewMatrix;
        baseModelViewMatrix = GLKMatrix4Make(r.m11, r.m21, r.m31, 0.0f,
                                             r.m12, r.m22, r.m32, 0.0f,
                                             r.m13, r.m23, r.m33, 0.0f,
                                             0.0f,  0.0f,  0.0f,  1.0f);



        GLKMatrix4 baseModelViewMatrix2 = GLKMatrix4Identity;

        double yaw = motionData.attitude.yaw;
        double pitch = motionData.attitude.pitch;
        double roll = motionData.attitude.roll;
        baseModelViewMatrix2 = GLKMatrix4RotateZ(baseModelViewMatrix2, roll);
        baseModelViewMatrix2 = GLKMatrix4RotateX(baseModelViewMatrix2, pitch);
        baseModelViewMatrix2 = GLKMatrix4RotateY(baseModelViewMatrix2, yaw);
        //baseModelViewMatrix = GLKMatrix4Transpose(baseModelViewMatrix);



        _scene.rotationMatrix = baseModelViewMatrix2;


    }];

1 个答案:

答案 0 :(得分:0)

我最终想出了一个解决方案。我写了这个函数。

GLKMatrix4 rotationMatrix(double yaw, double pitch, double roll){
    GLKMatrix4 rotationMat = GLKMatrix4Identity;

    rotationMat = GLKMatrix4RotateZ(rotationMat, roll);
    rotationMat = GLKMatrix4RotateX(rotationMat, pitch);
    rotationMat = GLKMatrix4RotateY(rotationMat, yaw);
    rotationMat = GLKMatrix4Make(rotationMat.m00, rotationMat.m02, rotationMat.m01, 0.0f,
                                 rotationMat.m20, rotationMat.m22, rotationMat.m21, 0.0f,
                                 rotationMat.m10, rotationMat.m12, rotationMat.m11, 0.0f,
                                 0.0f,  0.0f,  0.0f,  1.0f);
    return rotationMat;
}