openxr_hand_tracking_extension.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_MAX
  48. };
  49. struct HandTracker {
  50. bool is_initialized = false;
  51. Ref<XRHandTracker> godot_tracker;
  52. XrHandJointsMotionRangeEXT motion_range = XR_HAND_JOINTS_MOTION_RANGE_UNOBSTRUCTED_EXT;
  53. HandTrackedSource source = OPENXR_SOURCE_UNKNOWN;
  54. XrHandTrackerEXT hand_tracker = XR_NULL_HANDLE;
  55. XrHandJointLocationEXT joint_locations[XR_HAND_JOINT_COUNT_EXT];
  56. XrHandJointVelocityEXT joint_velocities[XR_HAND_JOINT_COUNT_EXT];
  57. XrHandJointVelocitiesEXT velocities;
  58. XrHandJointLocationsEXT locations;
  59. XrHandTrackingDataSourceStateEXT data_source;
  60. };
  61. static OpenXRHandTrackingExtension *get_singleton();
  62. OpenXRHandTrackingExtension();
  63. virtual ~OpenXRHandTrackingExtension() override;
  64. virtual HashMap<String, bool *> get_requested_extensions() override;
  65. virtual void on_instance_created(const XrInstance p_instance) override;
  66. virtual void on_instance_destroyed() override;
  67. virtual void on_session_destroyed() override;
  68. virtual void *set_system_properties_and_get_next_pointer(void *p_next_pointer) override;
  69. virtual void on_state_ready() override;
  70. virtual void on_process() override;
  71. virtual void on_state_stopping() override;
  72. bool get_active();
  73. const HandTracker *get_hand_tracker(HandTrackedHands p_hand) const;
  74. XrHandJointsMotionRangeEXT get_motion_range(HandTrackedHands p_hand) const;
  75. void set_motion_range(HandTrackedHands p_hand, XrHandJointsMotionRangeEXT p_motion_range);
  76. HandTrackedSource get_hand_tracking_source(HandTrackedHands p_hand) const;
  77. XrSpaceLocationFlags get_hand_joint_location_flags(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  78. Quaternion get_hand_joint_rotation(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  79. Vector3 get_hand_joint_position(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  80. float get_hand_joint_radius(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  81. XrSpaceVelocityFlags get_hand_joint_velocity_flags(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  82. Vector3 get_hand_joint_linear_velocity(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  83. Vector3 get_hand_joint_angular_velocity(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  84. private:
  85. static OpenXRHandTrackingExtension *singleton;
  86. // state
  87. XrSystemHandTrackingPropertiesEXT handTrackingSystemProperties;
  88. HandTracker hand_trackers[OPENXR_MAX_TRACKED_HANDS]; // Fixed for left and right hand
  89. // related extensions
  90. bool hand_tracking_ext = false;
  91. bool hand_motion_range_ext = false;
  92. bool hand_tracking_source_ext = false;
  93. // functions
  94. void cleanup_hand_tracking();
  95. // OpenXR API call wrappers
  96. EXT_PROTO_XRRESULT_FUNC3(xrCreateHandTrackerEXT, (XrSession), p_session, (const XrHandTrackerCreateInfoEXT *), p_createInfo, (XrHandTrackerEXT *), p_handTracker)
  97. EXT_PROTO_XRRESULT_FUNC1(xrDestroyHandTrackerEXT, (XrHandTrackerEXT), p_handTracker)
  98. EXT_PROTO_XRRESULT_FUNC3(xrLocateHandJointsEXT, (XrHandTrackerEXT), p_handTracker, (const XrHandJointsLocateInfoEXT *), p_locateInfo, (XrHandJointLocationsEXT *), p_locations)
  99. };
  100. #endif // OPENXR_HAND_TRACKING_EXTENSION_H