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