openxr_interface.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**************************************************************************/
  2. /* openxr_interface.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef OPENXR_INTERFACE_H
  31. #define OPENXR_INTERFACE_H
  32. #include "action_map/openxr_action_map.h"
  33. #include "extensions/openxr_fb_passthrough_extension_wrapper.h"
  34. #include "extensions/openxr_hand_tracking_extension.h"
  35. #include "openxr_api.h"
  36. #include "servers/xr/xr_interface.h"
  37. #include "servers/xr/xr_positional_tracker.h"
  38. // declare some default strings
  39. #define INTERACTION_PROFILE_NONE "/interaction_profiles/none"
  40. class OpenXRInterface : public XRInterface {
  41. GDCLASS(OpenXRInterface, XRInterface);
  42. private:
  43. OpenXRAPI *openxr_api = nullptr;
  44. bool initialized = false;
  45. XRInterface::TrackingStatus tracking_state;
  46. OpenXRFbPassthroughExtensionWrapper *passthrough_wrapper = nullptr;
  47. // At a minimum we need a tracker for our head
  48. Ref<XRPositionalTracker> head;
  49. Transform3D head_transform;
  50. Vector3 head_linear_velocity;
  51. Vector3 head_angular_velocity;
  52. XRPose::TrackingConfidence head_confidence;
  53. Transform3D transform_for_view[2]; // We currently assume 2, but could be 4 for VARJO which we do not support yet
  54. void _load_action_map();
  55. struct Action { // An action we've registered with OpenXR
  56. String action_name; // Name of our action as presented to Godot (can be altered from the action map)
  57. OpenXRAction::ActionType action_type; // The action type of this action
  58. RID action_rid; // RID of the action registered with our OpenXR API
  59. };
  60. struct ActionSet { // An action set we've registered with OpenXR
  61. String action_set_name; // Name of our action set
  62. bool is_active; // If true this action set is active and we will sync it
  63. Vector<Action *> actions; // List of actions in this action set
  64. RID action_set_rid; // RID of the action registered with our OpenXR API
  65. };
  66. struct Tracker { // A tracker we've registered with OpenXR
  67. String tracker_name; // Name of our tracker (can be altered from the action map)
  68. Vector<Action *> actions; // Actions related to this tracker
  69. Ref<XRPositionalTracker> positional_tracker; // Our positional tracker object that holds our tracker state
  70. RID tracker_rid; // RID of the tracker registered with our OpenXR API
  71. RID interaction_profile; // RID of the interaction profile bound to this tracker (can be null)
  72. };
  73. Vector<ActionSet *> action_sets;
  74. Vector<RID> interaction_profiles;
  75. Vector<Tracker *> trackers;
  76. ActionSet *create_action_set(const String &p_action_set_name, const String &p_localized_name, const int p_priority);
  77. void free_action_sets();
  78. Action *create_action(ActionSet *p_action_set, const String &p_action_name, const String &p_localized_name, OpenXRAction::ActionType p_action_type, const Vector<Tracker *> p_trackers);
  79. Action *find_action(const String &p_action_name);
  80. void free_actions(ActionSet *p_action_set);
  81. Tracker *find_tracker(const String &p_tracker_name, bool p_create = false);
  82. void handle_tracker(Tracker *p_tracker);
  83. void free_trackers();
  84. void free_interaction_profiles();
  85. void _set_default_pos(Transform3D &p_transform, double p_world_scale, uint64_t p_eye);
  86. void handle_hand_tracking(const String &p_path, OpenXRHandTrackingExtension::HandTrackedHands p_hand);
  87. protected:
  88. static void _bind_methods();
  89. public:
  90. virtual StringName get_name() const override;
  91. virtual uint32_t get_capabilities() const override;
  92. virtual PackedStringArray get_suggested_tracker_names() const override;
  93. virtual TrackingStatus get_tracking_status() const override;
  94. bool is_hand_tracking_supported();
  95. bool is_eye_gaze_interaction_supported();
  96. bool initialize_on_startup() const;
  97. virtual bool is_initialized() const override;
  98. virtual bool initialize() override;
  99. virtual void uninitialize() override;
  100. virtual Dictionary get_system_info() override;
  101. virtual void trigger_haptic_pulse(const String &p_action_name, const StringName &p_tracker_name, double p_frequency, double p_amplitude, double p_duration_sec, double p_delay_sec = 0) override;
  102. virtual bool supports_play_area_mode(XRInterface::PlayAreaMode p_mode) override;
  103. virtual XRInterface::PlayAreaMode get_play_area_mode() const override;
  104. virtual bool set_play_area_mode(XRInterface::PlayAreaMode p_mode) override;
  105. float get_display_refresh_rate() const;
  106. void set_display_refresh_rate(float p_refresh_rate);
  107. Array get_available_display_refresh_rates() const;
  108. bool is_action_set_active(const String &p_action_set) const;
  109. void set_action_set_active(const String &p_action_set, bool p_active);
  110. Array get_action_sets() const;
  111. double get_render_target_size_multiplier() const;
  112. void set_render_target_size_multiplier(double multiplier);
  113. bool is_foveation_supported() const;
  114. int get_foveation_level() const;
  115. void set_foveation_level(int p_foveation_level);
  116. bool get_foveation_dynamic() const;
  117. void set_foveation_dynamic(bool p_foveation_dynamic);
  118. virtual Size2 get_render_target_size() override;
  119. virtual uint32_t get_view_count() override;
  120. virtual Transform3D get_camera_transform() override;
  121. virtual Transform3D get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) override;
  122. virtual Projection get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) override;
  123. virtual RID get_color_texture() override;
  124. virtual RID get_depth_texture() override;
  125. virtual void process() override;
  126. virtual void pre_render() override;
  127. bool pre_draw_viewport(RID p_render_target) override;
  128. virtual Vector<BlitToScreen> post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) override;
  129. virtual void end_frame() override;
  130. virtual bool is_passthrough_supported() override;
  131. virtual bool is_passthrough_enabled() override;
  132. virtual bool start_passthrough() override;
  133. virtual void stop_passthrough() override;
  134. /** environment blend mode. */
  135. virtual Array get_supported_environment_blend_modes() override;
  136. virtual XRInterface::EnvironmentBlendMode get_environment_blend_mode() const override;
  137. virtual bool set_environment_blend_mode(XRInterface::EnvironmentBlendMode mode) override;
  138. void on_state_ready();
  139. void on_state_visible();
  140. void on_state_focused();
  141. void on_state_stopping();
  142. void on_pose_recentered();
  143. void tracker_profile_changed(RID p_tracker, RID p_interaction_profile);
  144. /** Hand tracking. */
  145. enum Hand {
  146. HAND_LEFT,
  147. HAND_RIGHT,
  148. HAND_MAX,
  149. };
  150. enum HandMotionRange {
  151. HAND_MOTION_RANGE_UNOBSTRUCTED,
  152. HAND_MOTION_RANGE_CONFORM_TO_CONTROLLER,
  153. HAND_MOTION_RANGE_MAX
  154. };
  155. void set_motion_range(const Hand p_hand, const HandMotionRange p_motion_range);
  156. HandMotionRange get_motion_range(const Hand p_hand) const;
  157. enum HandJoints {
  158. HAND_JOINT_PALM = 0,
  159. HAND_JOINT_WRIST = 1,
  160. HAND_JOINT_THUMB_METACARPAL = 2,
  161. HAND_JOINT_THUMB_PROXIMAL = 3,
  162. HAND_JOINT_THUMB_DISTAL = 4,
  163. HAND_JOINT_THUMB_TIP = 5,
  164. HAND_JOINT_INDEX_METACARPAL = 6,
  165. HAND_JOINT_INDEX_PROXIMAL = 7,
  166. HAND_JOINT_INDEX_INTERMEDIATE = 8,
  167. HAND_JOINT_INDEX_DISTAL = 9,
  168. HAND_JOINT_INDEX_TIP = 10,
  169. HAND_JOINT_MIDDLE_METACARPAL = 11,
  170. HAND_JOINT_MIDDLE_PROXIMAL = 12,
  171. HAND_JOINT_MIDDLE_INTERMEDIATE = 13,
  172. HAND_JOINT_MIDDLE_DISTAL = 14,
  173. HAND_JOINT_MIDDLE_TIP = 15,
  174. HAND_JOINT_RING_METACARPAL = 16,
  175. HAND_JOINT_RING_PROXIMAL = 17,
  176. HAND_JOINT_RING_INTERMEDIATE = 18,
  177. HAND_JOINT_RING_DISTAL = 19,
  178. HAND_JOINT_RING_TIP = 20,
  179. HAND_JOINT_LITTLE_METACARPAL = 21,
  180. HAND_JOINT_LITTLE_PROXIMAL = 22,
  181. HAND_JOINT_LITTLE_INTERMEDIATE = 23,
  182. HAND_JOINT_LITTLE_DISTAL = 24,
  183. HAND_JOINT_LITTLE_TIP = 25,
  184. HAND_JOINT_MAX = 26,
  185. };
  186. enum HandJointFlags {
  187. HAND_JOINT_NONE = 0,
  188. HAND_JOINT_ORIENTATION_VALID = 1,
  189. HAND_JOINT_ORIENTATION_TRACKED = 2,
  190. HAND_JOINT_POSITION_VALID = 4,
  191. HAND_JOINT_POSITION_TRACKED = 8,
  192. HAND_JOINT_LINEAR_VELOCITY_VALID = 16,
  193. HAND_JOINT_ANGULAR_VELOCITY_VALID = 32,
  194. };
  195. BitField<HandJointFlags> get_hand_joint_flags(Hand p_hand, HandJoints p_joint) const;
  196. Quaternion get_hand_joint_rotation(Hand p_hand, HandJoints p_joint) const;
  197. Vector3 get_hand_joint_position(Hand p_hand, HandJoints p_joint) const;
  198. float get_hand_joint_radius(Hand p_hand, HandJoints p_joint) const;
  199. Vector3 get_hand_joint_linear_velocity(Hand p_hand, HandJoints p_joint) const;
  200. Vector3 get_hand_joint_angular_velocity(Hand p_hand, HandJoints p_joint) const;
  201. OpenXRInterface();
  202. ~OpenXRInterface();
  203. };
  204. VARIANT_ENUM_CAST(OpenXRInterface::Hand)
  205. VARIANT_ENUM_CAST(OpenXRInterface::HandMotionRange)
  206. VARIANT_ENUM_CAST(OpenXRInterface::HandJoints)
  207. VARIANT_BITFIELD_CAST(OpenXRInterface::HandJointFlags)
  208. #endif // OPENXR_INTERFACE_H