Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/openxr/extensions/openxr_hand_tracking_extension.h
10278 views
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
#pragma once
32
33
#include "../util.h"
34
#include "core/math/quaternion.h"
35
#include "openxr_extension_wrapper.h"
36
#include "servers/xr/xr_hand_tracker.h"
37
38
class OpenXRHandTrackingExtension : public OpenXRExtensionWrapper {
39
GDCLASS(OpenXRHandTrackingExtension, OpenXRExtensionWrapper);
40
41
protected:
42
static void _bind_methods() {}
43
44
public:
45
enum HandTrackedHands {
46
OPENXR_TRACKED_LEFT_HAND,
47
OPENXR_TRACKED_RIGHT_HAND,
48
OPENXR_MAX_TRACKED_HANDS
49
};
50
51
enum HandTrackedSource {
52
OPENXR_SOURCE_UNKNOWN,
53
OPENXR_SOURCE_UNOBSTRUCTED,
54
OPENXR_SOURCE_CONTROLLER,
55
OPENXR_SOURCE_NOT_TRACKED,
56
OPENXR_SOURCE_MAX
57
};
58
59
struct HandTracker {
60
bool is_initialized = false;
61
Ref<XRHandTracker> godot_tracker;
62
XrHandJointsMotionRangeEXT motion_range = XR_HAND_JOINTS_MOTION_RANGE_UNOBSTRUCTED_EXT;
63
HandTrackedSource source = OPENXR_SOURCE_UNKNOWN;
64
65
XrHandTrackerEXT hand_tracker = XR_NULL_HANDLE;
66
XrHandJointLocationEXT joint_locations[XR_HAND_JOINT_COUNT_EXT];
67
XrHandJointVelocityEXT joint_velocities[XR_HAND_JOINT_COUNT_EXT];
68
69
XrHandJointVelocitiesEXT velocities;
70
XrHandJointLocationsEXT locations;
71
XrHandTrackingDataSourceStateEXT data_source;
72
};
73
74
static OpenXRHandTrackingExtension *get_singleton();
75
76
OpenXRHandTrackingExtension();
77
virtual ~OpenXRHandTrackingExtension() override;
78
79
virtual HashMap<String, bool *> get_requested_extensions() override;
80
81
virtual void on_instance_created(const XrInstance p_instance) override;
82
virtual void on_instance_destroyed() override;
83
virtual void on_session_destroyed() override;
84
85
virtual void *set_system_properties_and_get_next_pointer(void *p_next_pointer) override;
86
virtual void on_state_ready() override;
87
virtual void on_process() override;
88
virtual void on_state_stopping() override;
89
90
bool get_active();
91
const HandTracker *get_hand_tracker(HandTrackedHands p_hand) const;
92
93
XrHandJointsMotionRangeEXT get_motion_range(HandTrackedHands p_hand) const;
94
void set_motion_range(HandTrackedHands p_hand, XrHandJointsMotionRangeEXT p_motion_range);
95
96
HandTrackedSource get_hand_tracking_source(HandTrackedHands p_hand) const;
97
98
XrSpaceLocationFlags get_hand_joint_location_flags(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
99
Quaternion get_hand_joint_rotation(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
100
Vector3 get_hand_joint_position(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
101
float get_hand_joint_radius(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
102
103
XrSpaceVelocityFlags get_hand_joint_velocity_flags(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
104
Vector3 get_hand_joint_linear_velocity(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
105
Vector3 get_hand_joint_angular_velocity(HandTrackedHands p_hand, XrHandJointEXT p_joint) const;
106
107
private:
108
static OpenXRHandTrackingExtension *singleton;
109
110
// state
111
XrSystemHandTrackingPropertiesEXT handTrackingSystemProperties;
112
HandTracker hand_trackers[OPENXR_MAX_TRACKED_HANDS]; // Fixed for left and right hand
113
114
// related extensions
115
bool hand_tracking_ext = false;
116
bool hand_motion_range_ext = false;
117
bool hand_tracking_source_ext = false;
118
bool unobstructed_data_source = false;
119
bool controller_data_source = false;
120
121
// functions
122
void cleanup_hand_tracking();
123
124
// OpenXR API call wrappers
125
EXT_PROTO_XRRESULT_FUNC3(xrCreateHandTrackerEXT, (XrSession), p_session, (const XrHandTrackerCreateInfoEXT *), p_createInfo, (XrHandTrackerEXT *), p_handTracker)
126
EXT_PROTO_XRRESULT_FUNC1(xrDestroyHandTrackerEXT, (XrHandTrackerEXT), p_handTracker)
127
EXT_PROTO_XRRESULT_FUNC3(xrLocateHandJointsEXT, (XrHandTrackerEXT), p_handTracker, (const XrHandJointsLocateInfoEXT *), p_locateInfo, (XrHandJointLocationsEXT *), p_locations)
128
};
129
130