我正在制作类似于增强现实应用的东西,其中我有一个OpenGL场景,无论iOS设备如何移动,我都希望与重力保持一致。我以为我用CMDeviceMotion.attitude.pitch设置得很好,直到我发现用一个iPhone倚着这个数字。所以我从pARk *示例中获取了一些代码,现在我试图找出围绕垂直访问的旋转。我正在绘制的场景并不关心用户面向哪个方向,图形将始终与观察者绘制一定距离。我认为当我弄清楚垂直轴旋转分量时,我可以将其反转并将其乘以旋转矩阵,以便在用户改变航向时保持OpenGL数字不变。
这是我的代码:
CMDeviceMotion *d = motionManager.deviceMotion;
if (d != nil) {
CMRotationMatrix r = d.attitude.rotationMatrix;
transformFromCMRotationMatrix(cameraTransform, &r);
mat4f_t projectionCameraTransform;
multiplyMatrixAndMatrix(projectionCameraTransform, projectionTransform, cameraTransform);
GLKMatrix4 rotMatrix = GLKMatrix4Make(projectionCameraTransform[0],
projectionCameraTransform[1],
projectionCameraTransform[2],
projectionCameraTransform[3],
projectionCameraTransform[4],
projectionCameraTransform[5],
projectionCameraTransform[6],
projectionCameraTransform[7],
projectionCameraTransform[8],
projectionCameraTransform[9],
projectionCameraTransform[10],
projectionCameraTransform[11],
projectionCameraTransform[12],
projectionCameraTransform[13],
projectionCameraTransform[14],
projectionCameraTransform[15]);
}
然后我像往常一样在OpenGL中使用rotMatrix。
想法,建议?提前谢谢。
* pARk示例代码在实际空间中设置了几个点,计算出用户的位置以及这些点的相对方向并在屏幕上绘制它们,使其看起来漂浮在指向其位置的地平线上。
答案 0 :(得分:3)
我只是根据设备屏幕方向围绕Z轴旋转方向。这不是最漂亮的,但它似乎完全符合我的需要,而不是去往欧拉和背部(因此,避免了旋转锁定问题)
GLKMatrix4 deviceMotionAttitudeMatrix;
if (_cmMotionmanager.deviceMotionActive) {
CMDeviceMotion *deviceMotion = _cmMotionmanager.deviceMotion;
// Correct for the rotation matrix not including the screen orientation:
// TODO: Let the device notify me when the orientation changes instead of querying on each update.
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
float deviceOrientationRadians = 0.0f;
if (orientation == UIDeviceOrientationLandscapeLeft) {
deviceOrientationRadians = M_PI_2;
}
if (orientation == UIDeviceOrientationLandscapeRight) {
deviceOrientationRadians = -M_PI_2;
}
if (orientation == UIDeviceOrientationPortraitUpsideDown) {
deviceOrientationRadians = M_PI;
}
GLKMatrix4 baseRotation = GLKMatrix4MakeRotation(deviceOrientationRadians, 0.0f, 0.0f, 1.0f);
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);
}
else
{
// Look straight forward (we're probably in the simulator, or a device without a gyro)
deviceMotionAttitudeMatrix = GLKMatrix4MakeRotation(-M_PI_2, 1.0f, 0.0f, 0.0f);
}
答案 1 :(得分:2)
这里有一些代码来阐明如何使用attitude.rotationMatrix
// initial model view matrix
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -5.f);
// convert CMRotationMatrix to GLKMatrix4
CMRotationMatrix r = motion.attitude.rotationMatrix;
GLKMatrix4 = 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);
// apply motion rotation matrix
modelViewMatrix = GLKMatrix4Multiply(modelViewMatrix, _motionRotationMatrix);
// apply matrix to effect
self.effect.transform.modelviewMatrix = modelViewMatrix;
答案 2 :(得分:0)
我从这个答案中得到了一些提示并提出了解决方案:
if (d != nil) {
GLKMatrix4 rotMatrix = GLKMatrix4MakeRotation(0, 0, 1, 0);
float pitch = d.attitude.pitch;
if (d.gravity.z > 0)
pitch = -pitch;
rotMatrix = GLKMatrix4Rotate(rotMatrix, pitch, -1, 0, 0);
rotMatrix = GLKMatrix4Rotate(rotMatrix, d.attitude.roll, 0, -1, 0);
rotMatrix = GLKMatrix4Rotate(rotMatrix, d.attitude.yaw, 0, 0, -1);
rotMatrix = GLKMatrix4Multiply(rotMatrix, GLKMatrix4MakeRotation(M_PI/2, 1, 0, 0));
}
然而,当电话接近垂直时,这会失控。所以我还在寻找。