openxr_hand_tracking_extension.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. class OpenXRHandTrackingExtension : public OpenXRExtensionWrapper {
  36. public:
  37. enum HandTrackedHands {
  38. OPENXR_TRACKED_LEFT_HAND,
  39. OPENXR_TRACKED_RIGHT_HAND,
  40. OPENXR_MAX_TRACKED_HANDS
  41. };
  42. struct HandTracker {
  43. bool is_initialized = false;
  44. XrHandJointsMotionRangeEXT motion_range = XR_HAND_JOINTS_MOTION_RANGE_UNOBSTRUCTED_EXT;
  45. XrHandTrackerEXT hand_tracker = XR_NULL_HANDLE;
  46. XrHandJointLocationEXT joint_locations[XR_HAND_JOINT_COUNT_EXT];
  47. XrHandJointVelocityEXT joint_velocities[XR_HAND_JOINT_COUNT_EXT];
  48. XrHandJointVelocitiesEXT velocities;
  49. XrHandJointLocationsEXT locations;
  50. };
  51. static OpenXRHandTrackingExtension *get_singleton();
  52. OpenXRHandTrackingExtension();
  53. virtual ~OpenXRHandTrackingExtension() override;
  54. virtual HashMap<String, bool *> get_requested_extensions() override;
  55. virtual void on_instance_created(const XrInstance p_instance) override;
  56. virtual void on_instance_destroyed() override;
  57. virtual void on_session_destroyed() override;
  58. virtual void *set_system_properties_and_get_next_pointer(void *p_next_pointer) override;
  59. virtual void on_state_ready() override;
  60. virtual void on_process() override;
  61. virtual void on_state_stopping() override;
  62. bool get_active();
  63. const HandTracker *get_hand_tracker(HandTrackedHands p_hand) const;
  64. XrHandJointsMotionRangeEXT get_motion_range(HandTrackedHands p_hand) const;
  65. void set_motion_range(HandTrackedHands p_hand, XrHandJointsMotionRangeEXT p_motion_range);
  66. XrSpaceLocationFlags get_hand_joint_location_flags(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  67. Quaternion get_hand_joint_rotation(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  68. Vector3 get_hand_joint_position(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  69. float get_hand_joint_radius(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  70. XrSpaceVelocityFlags get_hand_joint_velocity_flags(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  71. Vector3 get_hand_joint_linear_velocity(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  72. Vector3 get_hand_joint_angular_velocity(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
  73. private:
  74. static OpenXRHandTrackingExtension *singleton;
  75. // state
  76. XrSystemHandTrackingPropertiesEXT handTrackingSystemProperties;
  77. HandTracker hand_trackers[OPENXR_MAX_TRACKED_HANDS]; // Fixed for left and right hand
  78. // related extensions
  79. bool hand_tracking_ext = false;
  80. bool hand_motion_range_ext = false;
  81. // functions
  82. void cleanup_hand_tracking();
  83. // OpenXR API call wrappers
  84. EXT_PROTO_XRRESULT_FUNC3(xrCreateHandTrackerEXT, (XrSession), p_session, (const XrHandTrackerCreateInfoEXT *), p_createInfo, (XrHandTrackerEXT *), p_handTracker)
  85. EXT_PROTO_XRRESULT_FUNC1(xrDestroyHandTrackerEXT, (XrHandTrackerEXT), p_handTracker)
  86. EXT_PROTO_XRRESULT_FUNC3(xrLocateHandJointsEXT, (XrHandTrackerEXT), p_handTracker, (const XrHandJointsLocateInfoEXT *), p_locateInfo, (XrHandJointLocationsEXT *), p_locations)
  87. };
  88. #endif // OPENXR_HAND_TRACKING_EXTENSION_H