我有一个关于 OpenVR api 中矩阵转换的问题。
m_compositor->WaitGetPoses(m_rTrackedDevicePose, vr::k_unMaxTrackedDeviceCount, nullptr, 0);
在openvr给出的demo中:
const Matrix4 & matDeviceToTracking = m_rmat4DevicePose[ unTrackedDevice ];
Matrix4 matMVP = GetCurrentViewProjectionMatrix( nEye ) * matDeviceToTracking;
glUniformMatrix4fv( m_nRenderModelMatrixLocation, 1, GL_FALSE, matMVP.get() );
其中 GetCurrentViewProjectionMatrix 的计算方法为
Matrix4 CMainApplication::GetCurrentViewProjectionMatrix( vr::Hmd_Eye nEye )
{
Matrix4 matMVP;
if( nEye == vr::Eye_Left )
{
matMVP = m_mat4ProjectionLeft * m_mat4eyePosLeft * m_mat4HMDPose;
}
else if( nEye == vr::Eye_Right )
{
matMVP = m_mat4ProjectionRight * m_mat4eyePosRight * m_mat4HMDPose;
}
return matMVP;
}
问题是:
1、matDeviceToTracking是从哪个空间变换到哪个空间的?
2,如果我已经有模型 View 矩阵,并且已经可以使用 hmd 旋转,我怎样才能正确渲染渲染模型?我尝试使用 projection*modelview*m_rmat4DevicePose[ unTrackedDevice ]
但没有效果。
最佳答案
1.
在示例代码中,matDeviceToTracking
是对 m_rmat4DevicePose[unTrackedDevice]
的引用,它是从 TrackedDevicePose_t::mDeviceToAbsoluteTracking
复制的。这是从模型空间到世界空间的模型矩阵映射。
不过,有一个陷阱。如果您在示例中包含了 UpdateHMMDAtrixPose()
函数,则此函数会在更新 m_mat4HMDPose
值时反转 m_rmat4DevicePose[vr::k_unTrackedDeviceIndex_Hmd]
,留下从世界空间到模型/HMD View 空间的 m_rmat4DevicePose[0]
映射,与数组中的其他矩阵完全相反。
2.
如果你已经有了模型- View 矩阵,那么你只需要将投影矩阵乘以它就可以得到MVP矩阵。要渲染到 HMD 中,请分别对左眼和右眼使用 m_mat4ProjectionLeft * m_mat4eyePosLeft * modelview
和 m_mat4ProjectionRight * m_mat4eyePosRight * modelview
。为了在监视器上渲染,您可以生成自己的视锥体并将其乘以模型 View 矩阵。以下网站是有关如何创建投影矩阵的很好的引用:
http://www.songho.ca/opengl/gl_projectionmatrix.html
关于matrix - VR中变换矩阵的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38910609/