From 5b3f405c9a3ba58a2bcbbccaa7897e3dd82d601f Mon Sep 17 00:00:00 2001 From: amwatson Date: Thu, 15 Feb 2024 23:14:08 -0600 Subject: [PATCH] [vr_main] add sep method for creating runtime-initated reference spaces --- src/android/app/src/main/jni/vr/vr_main.cpp | 69 +++++++++++---------- 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/src/android/app/src/main/jni/vr/vr_main.cpp b/src/android/app/src/main/jni/vr/vr_main.cpp index 2c94b518e..789f4399f 100644 --- a/src/android/app/src/main/jni/vr/vr_main.cpp +++ b/src/android/app/src/main/jni/vr/vr_main.cpp @@ -352,38 +352,7 @@ private: // Re-initialize the reference spaces on the first frame so // it is in-sync with user if (mFrameIndex == 1) { - - // Create a reference space with the forward direction from the - // starting frame. - { - const XrReferenceSpaceCreateInfo sci = {XR_TYPE_REFERENCE_SPACE_CREATE_INFO, - nullptr, XR_REFERENCE_SPACE_TYPE_LOCAL, - XrMath::Posef::Identity()}; - OXR(xrCreateReferenceSpace(gOpenXr->mSession, &sci, - &gOpenXr->mForwardDirectionSpace)); - } - - { - const XrReferenceSpaceCreateInfo sci = {XR_TYPE_REFERENCE_SPACE_CREATE_INFO, - nullptr, XR_REFERENCE_SPACE_TYPE_VIEW, - XrMath::Posef::Identity()}; - OXR(xrCreateReferenceSpace(gOpenXr->mSession, &sci, &gOpenXr->mViewSpace)); - } - - // Get the pose of the local space. - XrSpaceLocation lsl = {XR_TYPE_SPACE_LOCATION}; - OXR(xrLocateSpace(gOpenXr->mForwardDirectionSpace, gOpenXr->mLocalSpace, - frameState.predictedDisplayTime, &lsl)); - - // Set the forward direction of the new space. - const XrPosef forwardDirectionPose = lsl.pose; - - // Create a reference space with the same position and rotation as - // local. - const XrReferenceSpaceCreateInfo sci = {XR_TYPE_REFERENCE_SPACE_CREATE_INFO, nullptr, - XR_REFERENCE_SPACE_TYPE_LOCAL, - forwardDirectionPose}; - OXR(xrCreateReferenceSpace(gOpenXr->mSession, &sci, &gOpenXr->mHeadSpace)); + CreateRuntimeInitatedReferenceSpaces(frameState.predictedDisplayTime); } gOpenXr->headLocation = {XR_TYPE_SPACE_LOCATION}; @@ -806,6 +775,42 @@ private: } } + // Called whenever a session is started/resumed. Creates the head space based on the + // current pose of the HMD. + void CreateRuntimeInitatedReferenceSpaces(const XrTime predictedDisplayTime) const { + // Create a reference space with the forward direction from the + // starting frame. + { + const XrReferenceSpaceCreateInfo sci = {XR_TYPE_REFERENCE_SPACE_CREATE_INFO, + nullptr, XR_REFERENCE_SPACE_TYPE_LOCAL, + XrMath::Posef::Identity()}; + OXR(xrCreateReferenceSpace(gOpenXr->mSession, &sci, + &gOpenXr->mForwardDirectionSpace)); + } + + { + const XrReferenceSpaceCreateInfo sci = {XR_TYPE_REFERENCE_SPACE_CREATE_INFO, + nullptr, XR_REFERENCE_SPACE_TYPE_VIEW, + XrMath::Posef::Identity()}; + OXR(xrCreateReferenceSpace(gOpenXr->mSession, &sci, &gOpenXr->mViewSpace)); + } + + // Get the pose of the local space. + XrSpaceLocation lsl = {XR_TYPE_SPACE_LOCATION}; + OXR(xrLocateSpace(gOpenXr->mForwardDirectionSpace, gOpenXr->mLocalSpace, + predictedDisplayTime, &lsl)); + + // Set the forward direction of the new space. + const XrPosef forwardDirectionPose = lsl.pose; + + // Create a reference space with the same position and rotation as + // local. + const XrReferenceSpaceCreateInfo sci = {XR_TYPE_REFERENCE_SPACE_CREATE_INFO, nullptr, + XR_REFERENCE_SPACE_TYPE_LOCAL, + forwardDirectionPose}; + OXR(xrCreateReferenceSpace(gOpenXr->mSession, &sci, &gOpenXr->mHeadSpace)); + } + void HandleSessionStateChanges(const XrSessionState state) { if (state == XR_SESSION_STATE_READY) { assert(mIsXrSessionActive == false);