Forum Discussion
Carandiru
11 years agoHonored Guest
World, View, Projection Matrix DirectX Snippet
I thought I would post a quick code snippet for anyone looking on how to do this with the oculus rift and DirectX with Right-Handed Coordinates. Had to Transpose the Projection Matrix, etc. I hope this helps some one out there looking for solutions.
XMMATRIX xmView, xmProj, xmLocal;
CXMVECTOR qid(XMQuaternionIdentity());
XMVECTORF32 const vScale = { 1.777777777f, 1.0f, 1.0f };
XMVECTORF32 const vTranslate = { 0.0f, 0.0f, -25.0f + m_fScreenScale };
XMVECTORF32 const vUp = { 0.0f, 1.0f, 0.0f };
XMVECTORF32 const vForward = { 0.0f, 0.0f, -1.0f };
OVR::Matrix4f matProj;
ovrPosef eyeRenderPose[2],
worldPose;
ovrHmd const& HMD(cMaxVRSingleton::getOculus().getHMD());
ovrEyeRenderDesc const* const EyeRenderDesc(cMaxVRSingleton::getOculus().getEyeRenderDesc());
ovrRecti const* const EyeRenderViewport(cMaxVRSingleton::getOculus().getEyeRenderViewport());
ovrEyeType eye;
// do 3D rendering on the back buffer here
// Set Oculus RenderTarget Color & Depth
cMaxVRSingleton::getOculus().SetRenderTarget(m_d3dContext);
for (unsigned int iDx = 0; iDx < ovrEye_Count; ++iDx)
{
eye = HMD->EyeRenderOrder[iDx];
eyeRenderPose[eye] = ovrHmd_GetEyePose(HMD, eye);
SetViewport(OVR::Recti(EyeRenderViewport[eye]));
worldPose = eyeRenderPose[eye];
XMVECTOR xmPoseTranslation = XMVectorSet(worldPose.Position.x, worldPose.Position.y, worldPose.Position.z, 0.0f);
XMVECTOR xmPoseOrientation = XMVectorSet(worldPose.Orientation.x, worldPose.Orientation.y, worldPose.Orientation.z, worldPose.Orientation.w);
// Rotate and position View Camera
XMVECTOR xmUp = XMVector3Rotate(vUp, xmPoseOrientation);
XMVECTOR xmForward = XMVector3Rotate(vForward, xmPoseOrientation);
// Initialize the view matrix
XMVECTOR xmAt = XMVectorAdd(xmPoseTranslation, xmForward);
xmView = XMMatrixLookAtRH(xmPoseTranslation, xmAt, xmUp);
xmView = XMMatrixMultiply(OVRMatrix4fTOXMMATRIX(OVR::Matrix4f::Translation(EyeRenderDesc[eye].ViewAdjust)), xmView);
// Initialize the projection matrix
OVR::Matrix4f matProj = ovrMatrix4f_Projection(EyeRenderDesc[eye].Fov, m_fMinZ, m_fMaxZ, true);
xmProj = OVRMatrix4fTOXMMATRIX(matProj);
xmProj = XMMatrixTranspose(xmProj);
xmLocal = XMMatrixMultiply(XMMatrixScalingFromVector(vScale),
XMMatrixMultiply(XMMatrixTranslationFromVector(vTranslate), XMMatrixRotationQuaternion(XMQuaternionRotationRollPitchYaw(0.0f, 0.0f, 0.0f))));
No RepliesBe the first to reply
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 10 months ago
- 2 months ago