Skip to content

Commit

Permalink
Session: Recenter eye level space on the first frame.
Browse files Browse the repository at this point in the history
Not all runtime have the local space at eye level.
  • Loading branch information
CrossVR committed Jan 21, 2021
1 parent 8cbd07b commit 3cc106b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 55 deletions.
57 changes: 3 additions & 54 deletions ReviveXR/REV_CAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,33 +371,7 @@ OVR_PUBLIC_FUNCTION(ovrResult) ovr_RecenterTrackingOrigin(ovrSession session)
return ovrError_InvalidSession;

ovr_ClearShouldRecenterFlag(session);

std::lock_guard<std::shared_mutex> lk(session->TrackingMutex);
XrSpaceLocation location = XR_TYPE(SPACE_LOCATION);
CHK_XR(xrLocateSpace(session->ViewSpace, session->OriginSpaces[session->TrackingOrigin],
(*session->CurrentFrame).predictedDisplayTime, &location));

if (!(location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT))
return ovrError_InvalidHeadsetOrientation;

// Get the yaw orientation from the view pose
float yaw;
XR::Quatf(location.pose.orientation).GetYawPitchRoll(&yaw, nullptr, nullptr);

// Construct the new origin pose
XR::Posef newOrigin(OVR::Quatf(OVR::Axis_Y, yaw), XR::Vector3f(location.pose.position));

// For floor level spaces we keep the height at the floor
if (session->TrackingOrigin == ovrTrackingOrigin_FloorLevel)
newOrigin.Translation.y = 0.0f;

// Replace the tracking space with the newly calibrated one
XrReferenceSpaceCreateInfo spaceInfo = XR_TYPE(REFERENCE_SPACE_CREATE_INFO);
spaceInfo.referenceSpaceType = static_cast<XrReferenceSpaceType>(XR_REFERENCE_SPACE_TYPE_LOCAL + session->TrackingOrigin);
spaceInfo.poseInReferenceSpace = newOrigin;
CHK_XR(xrDestroySpace(session->TrackingSpaces[session->TrackingOrigin]));
CHK_XR(xrCreateReferenceSpace(session->Session, &spaceInfo, &session->TrackingSpaces[session->TrackingOrigin]));
return ovrSuccess;
return session->RecenterSpace(session->TrackingOrigin, session->ViewSpace);
}

OVR_PUBLIC_FUNCTION(ovrResult) ovr_SpecifyTrackingOrigin(ovrSession session, ovrPosef originPose)
Expand All @@ -406,33 +380,8 @@ OVR_PUBLIC_FUNCTION(ovrResult) ovr_SpecifyTrackingOrigin(ovrSession session, ovr
return ovrError_InvalidSession;

ovr_ClearShouldRecenterFlag(session);

std::lock_guard<std::shared_mutex> lk(session->TrackingMutex);
XrSpaceLocation location = XR_TYPE(SPACE_LOCATION);
CHK_XR(xrLocateSpace(session->TrackingSpaces[session->TrackingOrigin], session->OriginSpaces[session->TrackingOrigin],
(*session->CurrentFrame).predictedDisplayTime, &location));

if (!(location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT))
return ovrError_InvalidParameter;

// Get the yaw orientation from the origin pose
float yaw;
OVR::Quatf(originPose.Orientation).GetYawPitchRoll(&yaw, nullptr, nullptr);

// Construct the new origin pose
XR::Posef newOrigin(OVR::Quatf(OVR::Axis_Y, yaw), originPose.Position);

// For floor level spaces we keep the height at the floor
if (session->TrackingOrigin == ovrTrackingOrigin_FloorLevel)
newOrigin.Translation.y = 0.0f;

// Replace the tracking space with the newly calibrated one
XrReferenceSpaceCreateInfo spaceInfo = XR_TYPE(REFERENCE_SPACE_CREATE_INFO);
spaceInfo.referenceSpaceType = static_cast<XrReferenceSpaceType>(XR_REFERENCE_SPACE_TYPE_LOCAL + session->TrackingOrigin);
spaceInfo.poseInReferenceSpace = XR::Posef(XR::Posef(location.pose) * newOrigin);
CHK_XR(xrDestroySpace(session->TrackingSpaces[session->TrackingOrigin]));
CHK_XR(xrCreateReferenceSpace(session->Session, &spaceInfo, &session->TrackingSpaces[session->TrackingOrigin]));
return ovrSuccess;
XrSpace anchor = session->TrackingSpaces[session->TrackingOrigin];
return session->RecenterSpace(session->TrackingOrigin, anchor, originPose);
}

OVR_PUBLIC_FUNCTION(void) ovr_ClearShouldRecenterFlag(ovrSession session)
Expand Down
30 changes: 30 additions & 0 deletions ReviveXR/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ ovrResult ovrHmdStruct::BeginSession(void* graphicsBinding, bool beginFrame)
if (beginFrame)
{
CHK_OVR(ovr_WaitToBeginFrame(this, 0));
CHK_OVR(RecenterSpace(ovrTrackingOrigin_EyeLevel, ViewSpace));
CHK_OVR(ovr_BeginFrame(this, 0));
}
return ovrSuccess;
Expand Down Expand Up @@ -240,6 +241,35 @@ ovrResult ovrHmdStruct::UpdateStencil(ovrEyeType view, XrVisibilityMaskTypeKHR t
return ovrSuccess;
}

ovrResult ovrHmdStruct::RecenterSpace(ovrTrackingOrigin origin, XrSpace anchor, ovrPosef offset)
{
std::lock_guard<std::shared_mutex> lk(TrackingMutex);
XrSpaceLocation location = XR_TYPE(SPACE_LOCATION);
CHK_XR(xrLocateSpace(anchor, OriginSpaces[origin], (*CurrentFrame).predictedDisplayTime, &location));

if (!(location.locationFlags & (XR_SPACE_LOCATION_ORIENTATION_VALID_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT)))
return ovrError_InvalidHeadsetOrientation;

// Get the yaw orientation from the view pose
float yaw;
XR::Quatf(location.pose.orientation).GetYawPitchRoll(&yaw, nullptr, nullptr);

// Construct the new origin pose
XR::Posef newOrigin(OVR::Quatf(OVR::Axis_Y, yaw), XR::Vector3f(location.pose.position));

// For floor level spaces we keep the height at the floor
if (origin == ovrTrackingOrigin_FloorLevel)
newOrigin.Translation.y = 0.0f;

// Replace the tracking space with the newly calibrated one
XrReferenceSpaceCreateInfo spaceInfo = XR_TYPE(REFERENCE_SPACE_CREATE_INFO);
spaceInfo.referenceSpaceType = static_cast<XrReferenceSpaceType>(XR_REFERENCE_SPACE_TYPE_LOCAL + origin);
spaceInfo.poseInReferenceSpace = XR::Posef(newOrigin * offset);
CHK_XR(xrDestroySpace(TrackingSpaces[origin]));
CHK_XR(xrCreateReferenceSpace(Session, &spaceInfo, &TrackingSpaces[origin]));
return ovrSuccess;
}

bool ovrHmdStruct::SupportsFormat(int64_t format) const
{
return std::find(SupportedFormats.begin(), SupportedFormats.end(), format) != SupportedFormats.end();
Expand Down
5 changes: 4 additions & 1 deletion ReviveXR/Session.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <OVR_CAPI.h>
#include "OVR_CAPI.h"
#include "Extras/OVR_Math.h"

#include <openxr/openxr.h>
#include <atomic>
#include <condition_variable>
Expand Down Expand Up @@ -82,5 +84,6 @@ struct ovrHmdStruct

ovrResult UpdateStencil(ovrEyeType view, XrVisibilityMaskTypeKHR type);
ovrResult LocateViews(XrView out_Views[ovrEye_Count], XrViewStateFlags* out_Flags = nullptr) const;
ovrResult RecenterSpace(ovrTrackingOrigin origin, XrSpace anchor, ovrPosef offset = OVR::Posef::Identity());
bool SupportsFormat(int64_t format) const;
};

0 comments on commit 3cc106b

Please sign in to comment.