diff --git a/client/application.cpp b/client/application.cpp index 51437b43..83f567d0 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -1841,6 +1841,59 @@ void application::loop() } } +std::optional application::locate_gaze(XrTime time) +{ + XrSpaceLocation res{ + .type = XR_TYPE_SPACE_LOCATION, + }; + if (get_hmd_traits().view_locate) + { + xrLocateSpace(spaces[size_t(xr::spaces::eye_gaze)], + spaces[size_t(xr::spaces::view)], + time, + &res); + } + else + { + XrSpaceLocation tmp{ + .type = XR_TYPE_SPACE_LOCATION, + }; + auto xr_res = xrLocateSpace(spaces[size_t(xr::spaces::view)], + spaces[size_t(xr::spaces::world)], + time, + &tmp); + if (XR_FAILED(xr_res)) + return std::nullopt; + xr_res = xrLocateSpace(spaces[size_t(xr::spaces::eye_gaze)], + spaces[size_t(xr::spaces::world)], + time, + &res); + if (XR_FAILED(xr_res)) + return std::nullopt; + + glm::quat target_quat( + res.pose.orientation.w, + res.pose.orientation.x, + res.pose.orientation.y, + res.pose.orientation.z); + const glm::quat view_quat( + tmp.pose.orientation.w, + tmp.pose.orientation.x, + tmp.pose.orientation.y, + tmp.pose.orientation.z); + target_quat = glm::conjugate(view_quat) * target_quat; + res.pose.orientation = { + .x = target_quat.x, + .y = target_quat.y, + .z = target_quat.z, + .w = target_quat.w}; + res.locationFlags &= tmp.locationFlags; + } + if (res.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) + return res.pose.orientation; + return std::nullopt; +} + #ifdef __ANDROID__ void application::run() { diff --git a/client/application.h b/client/application.h index 11305d8a..f6c1b828 100644 --- a/client/application.h +++ b/client/application.h @@ -278,6 +278,8 @@ public: return std::make_pair(position, orientation); } + std::optional locate_gaze(XrTime time); + void run(); static void push_scene(std::shared_ptr); diff --git a/client/foveation.cpp b/client/foveation.cpp index 81a216f7..26634174 100644 --- a/client/foveation.cpp +++ b/client/foveation.cpp @@ -104,29 +104,18 @@ client_foveation::angles target_angles( } std::optional eye_gaze_angles( - XrSpace eye_gaze_space, - XrSpace view_space, XrTime time, XrViewStateFlags view_flags, std::span views) { - if (not eye_gaze_space or not view_space or views.size() != 2) - return {}; - - XrSpaceLocation location{ - .type = XR_TYPE_SPACE_LOCATION, - }; - - XrResult res = xrLocateSpace(eye_gaze_space, view_space, time, &location); - if (XR_FAILED(res) or - not(location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) or - not(location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT)) - return {}; - if (not(view_flags & XR_VIEW_STATE_POSITION_VALID_BIT)) - return {}; + return std::nullopt; - const XrVector2f gaze = yaw_pitch(location.pose.orientation); + auto gaze_quat = application::instance().locate_gaze(time); + if (not gaze_quat) + return std::nullopt; + + const XrVector2f gaze = yaw_pitch(*gaze_quat); return target_angles(-gaze.x, gaze.y, client_foveation::default_convergence_distance, views); } @@ -362,9 +351,7 @@ void center::update(std::span submitted_fov, std::span } if (not angles and application::get_eye_gaze_supported()) { - if (auto a = eye_gaze_angles(application::space(xr::spaces::eye_gaze), - view_space, - display_time, + if (auto a = eye_gaze_angles(display_time, view_flags, views)) angles = *a;