openxr_hand_tracking_extension.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**************************************************************************/
  2. /* openxr_hand_tracking_extension.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_HAND_TRACKING_EXTENSION_H
  31. #define OPENXR_HAND_TRACKING_EXTENSION_H
  32. #include "../util.h"
  33. #include "core/math/quaternion.h"
  34. #include "openxr_extension_wrapper.h"
  35. #include "servers/xr/xr_hand_tracker.h"
  36. class OpenXRHandTrackingExtension : public OpenXRExtensionWrapper {
  37. public:
  38. enum HandTrackedHands {
  39. OPENXR_TRACKED_LEFT_HAND,
  40. OPENXR_TRACKED_RIGHT_HAND,
  41. OPENXR_MAX_TRACKED_HANDS
  42. };
  43. enum HandTrackedSource {
  44. OPENXR_SOURCE_UNKNOWN,
  45. OPENXR_SOURCE_UNOBSTRUCTED,
  46. OPENXR_SOURCE_CONTROLLER,
  47. OPENXR_SOURCE_NOT_TRACKED,
  48. OPENXR_SOURCE_MAX
  49. };
  50. struct HandTracker {
  51. bool is_initialized = false;
  52. Ref<XRHandTracker> godot_tracker;
  53. XrHandJointsMotionRangeEXT motion_range = XR_HAND_JOINTS_MOTION_RANGE_UNOBSTRUCTED_EXT;
  54. HandTrackedSource source = OPENXR_SOURCE_UNKNOWN;
  55. XrHandTrackerEXT hand_tracker = XR_NULL_HANDLE;
  56. XrHandJointLocationEXT joint_locations[XR_HAND_JOINT_COUNT_EXT];
  57. XrHandJointVelocityEXT joint_velocities[XR_HAND_JOINT_COUNT_EXT];
  58. XrHandJointVelocitiesEXT velocities;
  59. XrHandJointLocationsEXT locations;
  60. XrHandTrackingDataSourceStateEXT data_source;
  61. };
  62. static OpenXRHandTrackingExtension *get_singleton();
  63. OpenXRHandTrackingExtension();
  64. virtual ~OpenXRHandTrackingExtension() override;
  65. virtual HashMap<String, bool *> get_requested_extensions() override;
  66. virtual void on_instance_created(const XrInstance p_instance) override;
  67. virtual void on_instance_destroyed() override;
  68. virtual void on_session_destroyed() override;
  69. virtual void *set_system_properties_and_get_next_pointer(void *p_next_pointer) override;
  70. virtual void on_state_ready() override;
  71. virtual void on_process() override;
  72. virtual void on_state_stopping() override;
  73. bool get_active();
  74. const HandTracker *get_hand_tracker(HandTrackedHands p_hand) const;
  75. XrHandJointsMotionRangeEXT get_motion_range(HandTrackedHands p_hand) const;
  76. void set_motion_range(HandTrackedHands p_hand, XrHandJointsMotionRangeEXT p_motion_range);
  77. HandTrackedSource get_hand_tracking_source(HandTrackedHands p_hand) const;
  78. XrSpaceLocationFlags get_hand_joint_location_flags(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  79. Quaternion get_hand_joint_rotation(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  80. Vector3 get_hand_joint_position(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  81. float get_hand_joint_radius(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  82. XrSpaceVelocityFlags get_hand_joint_velocity_flags(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  83. Vector3 get_hand_joint_linear_velocity(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  84. Vector3 get_hand_joint_angular_velocity(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  85. private:
  86. static OpenXRHandTrackingExtension *singleton;
  87. // state
  88. XrSystemHandTrackingPropertiesEXT handTrackingSystemProperties;
  89. HandTracker hand_trackers[OPENXR_MAX_TRACKED_HANDS]; // Fixed for left and right hand
  90. // related extensions
  91. bool hand_tracking_ext = false;
  92. bool hand_motion_range_ext = false;
  93. bool hand_tracking_source_ext = false;
  94. bool unobstructed_data_source = false;
  95. bool controller_data_source = false;
  96. // functions
  97. void cleanup_hand_tracking();
  98. // OpenXR API call wrappers
  99. EXT_PROTO_XRRESULT_FUNC3(xrCreateHandTrackerEXT, (XrSession), p_session, (const XrHandTrackerCreateInfoEXT *), p_createInfo, (XrHandTrackerEXT *), p_handTracker)
  100. EXT_PROTO_XRRESULT_FUNC1(xrDestroyHandTrackerEXT, (XrHandTrackerEXT), p_handTracker)
  101. EXT_PROTO_XRRESULT_FUNC3(xrLocateHandJointsEXT, (XrHandTrackerEXT), p_handTracker, (const XrHandJointsLocateInfoEXT *), p_locateInfo, (XrHandJointLocationsEXT *), p_locations)
  102. };
  103. #endif // OPENXR_HAND_TRACKING_EXTENSION_H