我计算了在Vuforia中渲染的SCNScene
的相机位置。然而,物体并没有固定在标记上,而是在移动时跳跃。场景中的立方体只出现在正确的位置,无论设备如何在两侧移动都看不到。如何从Vuforia GL矩阵计算相机位置?
// Get model view matrix
Vuforia::Matrix44F modelViewMatrix = Vuforia::Tool::convertPose2GLMatrix(result->getPose());
// Convert to extrinsic matrix
SCNMatrix4 extrinsic = [self SCNMatrix4FromVuforiaMatrix44: modelViewMatrix];
SCNMatrix4 inverted = SCNMatrix4Invert(extrinsic);
// Set new position of SCNCamera
cameraNode.transform = inverted;
当相机盯着摄像机投影矩阵计算:
// Get device camera calibration
const Vuforia::CameraCalibration& cameraCalibration = Vuforia::CameraDevice::getInstance().getCameraCalibration();
projectionGLMatrix = Vuforia::Tool::getProjectionGL(cameraCalibration, 2.0f, 5000.0f);
// Convert matrix
GLKMatrix4 glkMatrix;
for(int i=0; i<16; i++) {
glkMatrix.m[i] = projectionGLMatrix.data[i];
}
// Convert matrix
SCNMatrix4 projectionTransform = SCNMatrix4FromGLKMatrix4(glkMatrix);
cameraNode.camera.projectionTransform = projectionTransform;
什么我错在这里做
相机位置与每一帧计算出来的?
更新1
投影矩阵现在计算像这样,当照相机开始:
const Vuforia::CameraCalibration& cameraCalibration = Vuforia::CameraDevice::getInstance().getCameraCalibration();
Vuforia::Matrix44F vuforiaMatrix = Vuforia::Tool::getProjectionGL(cameraCalibration, 2.0f, 5000.0f);
matrix_float4x4 simdMatrix = simdMatrixWithVuforiaMatrix44F(vuforiaMatrix);
cameraNode.camera.projectionTransform = SCNMatrix4FromMat4(simdMatrix);
相机位置被更新与每一帧:
Vuforia::Matrix44F modelViewMatrix = Vuforia::Tool::convertPose2GLMatrix(result->getPose());
matrix_float4x4 simdMatrix = simdMatrixWithVuforiaMatrix44F(modelViewMatrix);
cameraNode.transform = SCNMatrix4FromMat4(simdMatrix);
NSLog(@"camera position: x%lf, y%lf, z%lf, rotation: x%lf, y%lf, z%lf", _cameraNode.position.x, _cameraNode.position.y, _cameraNode.position.z, _cameraNode.rotation.x, _cameraNode.rotation.y, _cameraNode.rotation.z);
通过移动设备并观察相机位置(左图)和旋转(右图)的记录,似乎这些轴是:
旋转轴与位置轴不同。此外,围绕剩余轴的旋转(在下图中未命名)对cameraNode.rotation.x值没有任何影响,该值浮动在0.999左右。
这里有什么问题?
谢谢,相机的x,y,z位置与您的方法相同。但是,我有同样的问题,当我移动设备时,物体不会停留在派克上。将设备移动到右侧(+ x)不会将渲染对象移动到屏幕左侧。这里是一个视频,更好地解释它:https://dl.dropboxusercontent.com/u/71604584/vuforia_1.m4v – Manuel
我刚刚意识到我只更新'renderFrameVuforia'中相机的投影矩阵,但不能转换任何其他节点的场景。这是原因吗?顺便说一句。我正在使用SCNRenderer。 – Manuel
投影矩阵在开始时更新一次,之后您需要在每个刷新/回调中更新表示对象的节点的姿态。 –