OpenGL乘法矩阵

时间:2014-02-01 23:31:37

标签: ios matrix opengl-es-2.0 glkit

我正在制作一款3D第一人称游戏,使用手机中的陀螺仪,人们可以上下左右看,等等。我还想添加手动控制来添加。

此代码处理陀螺仪

GLKMatrix4 deviceMotionAttitudeMatrix;
    if (_cmMotionmanager.deviceMotionActive) {
        CMDeviceMotion *deviceMotion = _cmMotionmanager.deviceMotion;

        GLKMatrix4 baseRotation = GLKMatrix4MakeRotation(M_PI_2, 0.0f , 0.0f , 1.0f );

        // Note: in the simulator this comes back as the zero matrix.
        // on device, this doesn't include the changes required to match screen rotation.
        CMRotationMatrix a = deviceMotion.attitude.rotationMatrix;
        deviceMotionAttitudeMatrix
        = GLKMatrix4Make(a.m11, a.m21, a.m31, 0.0f,
                         a.m12 , a.m22, a.m32, 0.0f,
                         a.m13 , a.m23 , a.m33 , 0.0f,
                         0.0f, 0.0f, 0.0f, 1.0f);
        deviceMotionAttitudeMatrix = GLKMatrix4Multiply(baseRotation, deviceMotionAttitudeMatrix);

//        NSLog(@"%f %f %f %f %f %f %f %f", a.m11, a.m21, a.m31,
//              a.m12 , a.m22, a.m32, a.m23, a.m33);
    }

然后我想添加上/下左/上等的手动值。

我可以轻松添加左侧

deviceMotionAttitudeMatrix = GLKMatrix4RotateZ(deviceMotionAttitudeMatrix, (self.rotateLeft / 20));

然而,当我加起来时,它在北/南方向工作,但东/西方向然后在不同的轴上旋转

deviceMotionAttitudeMatrix = GLKMatrix4RotateY(deviceMotionAttitudeMatrix, (self.rotateTop / 20));

我试图对矩阵进行多重处理,但也有同样的问题。感觉我需要合并它们而不是多个它们

GLKMatrix4 leftRotation = GLKMatrix4MakeRotation((self.rotateLeft / 20), 0.0f, 0.0f, 1.0f);
 GLKMatrix4 TopRotation = GLKMatrix4MakeRotation(-(self.rotateTop / 20), 0.0f, 1.0f, 0.0f);

    GLKMatrix4 complete = GLKMatrix4Multiply(TopRotation, leftRotation);
    deviceMotionAttitudeMatrix = GLKMatrix4Multiply(deviceMotionAttitudeMatrix,complete);

0 个答案:

没有答案