diff --git a/client/constants.h b/client/constants.h index 1bc17fd0..7b58c775 100644 --- a/client/constants.h +++ b/client/constants.h @@ -85,7 +85,8 @@ constexpr glm::vec3 keyboard_position = {0, -0.3, 0.1}; constexpr float near_plane = 0.02; // Recenter gesture thresholds -constexpr float recenter_cosangle_min = 0.8; +constexpr float recenter_cos_palm_angle_min = 0.7; +constexpr float recenter_cos_fingertip_angle_max = 0.3; constexpr float recenter_distance_up = 0.3; constexpr float recenter_distance_front = 0.2; diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index bdd089da..cc75b002 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -31,6 +31,7 @@ #include "render/scene_components.h" #include "stream.h" #include "utils/files.h" +#include "utils/glm_cast.h" #include "utils/i18n.h" #include "wivrn_client.h" #include "wivrn_discover.h" @@ -427,27 +428,59 @@ void scenes::lobby::connect(const configuration::server_data & data) data.manual); } -std::optional scenes::lobby::check_recenter_gesture(xr::spaces space, const std::optional> & joints) +std::optional scenes::lobby::check_recenter_gesture( + xr::spaces space, + const std::optional> & joints, + const std::pair & head_pose) { if (recentering_context and std::get<0>(*recentering_context) != space) return std::nullopt; - if (not joints) + if (joints) { - recentering_context.reset(); - return std::nullopt; - } + const auto & palm = (*joints)[XR_HAND_JOINT_PALM_EXT].first; + const auto palm_y = glm::rotate(glm_cast(palm.pose.orientation), glm::vec3(0, 1, 0)); - const auto & palm = (*joints)[XR_HAND_JOINT_PALM_EXT].first; - const auto & o = palm.pose.orientation; - const auto & p = palm.pose.position; - glm::quat q{o.w, o.x, o.y, o.z}; - glm::vec3 v{p.x, p.y, p.z}; + for (XrHandJointEXT index: { + XR_HAND_JOINT_INDEX_PROXIMAL_EXT, + XR_HAND_JOINT_INDEX_INTERMEDIATE_EXT, + XR_HAND_JOINT_INDEX_DISTAL_EXT, - if (glm::dot(q * glm::vec3(0, 1, 0), glm::vec3(0, -1, 0)) > constants::lobby::recenter_cosangle_min) - { - recentering_context.emplace(space, glm::vec3{}, 0); - return v + glm::vec3(0, constants::lobby::recenter_distance_up, 0) + q * glm::vec3(0, 0, -constants::lobby::recenter_distance_front); + XR_HAND_JOINT_MIDDLE_PROXIMAL_EXT, + XR_HAND_JOINT_MIDDLE_INTERMEDIATE_EXT, + XR_HAND_JOINT_MIDDLE_DISTAL_EXT, + + XR_HAND_JOINT_RING_PROXIMAL_EXT, + XR_HAND_JOINT_RING_INTERMEDIATE_EXT, + XR_HAND_JOINT_RING_DISTAL_EXT, + + XR_HAND_JOINT_LITTLE_PROXIMAL_EXT, + XR_HAND_JOINT_LITTLE_INTERMEDIATE_EXT, + XR_HAND_JOINT_LITTLE_DISTAL_EXT, + }) + { + const auto fingertip_y = glm::rotate(glm_cast((*joints)[index].first.pose.orientation), glm::vec3(0, 1, 0)); + + if (glm::dot(palm_y, fingertip_y) < constants::lobby::recenter_cos_fingertip_angle_max) + { + recentering_context.reset(); + return std::nullopt; + } + } + + const auto q = glm_cast(palm.pose.orientation); + const auto x = glm_cast(palm.pose.position); + + // Make the up vector orthogonal to forward + const glm::vec3 forward = glm::rotate(head_pose.second, glm::vec3{0, 0, -1}); + const glm::vec3 up = glm::normalize(glm::vec3{0, 1, 0} - forward * glm::dot(forward, {0, 1, 0})); + + // Y is pointing to the back of the hand + if (glm::dot(q * glm::vec3(0, -1, 0), up) > constants::lobby::recenter_cos_palm_angle_min) + { + recentering_context.emplace(space, glm::vec3{}, 0); + return x + glm::rotate(q, glm::vec3{0, -constants::lobby::recenter_distance_up, -constants::lobby::recenter_distance_front}); + } } recentering_context.reset(); @@ -959,14 +992,14 @@ void scenes::lobby::render(const XrFrameState & frame_state) hand_model::apply(world, left, right); - if (not new_gui_position) - new_gui_position = check_recenter_gesture(xr::spaces::palm_left, left); + if (not new_gui_position and head_position) + new_gui_position = check_recenter_gesture(xr::spaces::palm_left, left, *head_position); - if (not new_gui_position) - new_gui_position = check_recenter_gesture(xr::spaces::palm_right, right); + if (not new_gui_position and head_position) + new_gui_position = check_recenter_gesture(xr::spaces::palm_right, right, *head_position); } - if (head_position && new_gui_position) + if (head_position and new_gui_position) { move_gui(head_position->first, *new_gui_position); } diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index 6250909a..39b9b975 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -238,7 +238,7 @@ class lobby : public scene_impl void connect(const configuration::server_data & data); std::unique_ptr connect_to_session(wivrn_discover::service service, bool manual_connection); - std::optional check_recenter_gesture(xr::spaces space, const std::optional> & joints); + std::optional check_recenter_gesture(xr::spaces space, const std::optional> & joints, const std::pair & head_pose); std::optional check_recenter_action(XrTime predicted_display_time, glm::vec3 head_position); std::optional check_recenter_gui(glm::vec3 head_position, glm::quat head_orientation); diff --git a/client/utils/glm_cast.h b/client/utils/glm_cast.h new file mode 100644 index 00000000..c11e732a --- /dev/null +++ b/client/utils/glm_cast.h @@ -0,0 +1,42 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Guillaume Meunier + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include +#include + +inline glm::quat glm_cast(const XrQuaternionf & q) +{ + return glm::quat::wxyz(q.w, q.x, q.y, q.z); +} + +inline XrQuaternionf glm_cast(const glm::quat & q) +{ + return XrQuaternionf(q.w, q.x, q.y, q.z); +} + +inline glm::vec3 glm_cast(const XrVector3f & v) +{ + return glm::vec3(v.x, v.y, v.z); +} + +inline XrVector3f glm_cast(const glm::vec3 & v) +{ + return XrVector3f(v.x, v.y, v.z); +}