Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/openxr/openxr_util.cpp
11351 views
1
/**************************************************************************/
2
/* openxr_util.cpp */
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
#include "openxr_util.h"
32
33
#include "core/math/math_funcs.h"
34
35
#include <openxr/openxr_reflection.h>
36
37
String OpenXRUtil::get_result_string(XrResult p_result){
38
XR_ENUM_SWITCH(XrResult, p_result)
39
}
40
41
String OpenXRUtil::get_view_configuration_name(XrViewConfigurationType p_view_configuration){
42
XR_ENUM_SWITCH(XrViewConfigurationType, p_view_configuration)
43
}
44
45
String OpenXRUtil::get_reference_space_name(XrReferenceSpaceType p_reference_space){
46
XR_ENUM_SWITCH(XrReferenceSpaceType, p_reference_space)
47
}
48
49
String OpenXRUtil::get_structure_type_name(XrStructureType p_structure_type){
50
XR_ENUM_SWITCH(XrStructureType, p_structure_type)
51
}
52
53
String OpenXRUtil::get_session_state_name(XrSessionState p_session_state){
54
XR_ENUM_SWITCH(XrSessionState, p_session_state)
55
}
56
57
String OpenXRUtil::get_action_type_name(XrActionType p_action_type){
58
XR_ENUM_SWITCH(XrActionType, p_action_type)
59
}
60
61
String OpenXRUtil::get_environment_blend_mode_name(XrEnvironmentBlendMode p_blend_mode) {
62
XR_ENUM_SWITCH(XrEnvironmentBlendMode, p_blend_mode);
63
}
64
65
String OpenXRUtil::make_xr_version_string(XrVersion p_version) {
66
String version;
67
68
version += String::num_int64(XR_VERSION_MAJOR(p_version));
69
version += String(".");
70
version += String::num_int64(XR_VERSION_MINOR(p_version));
71
version += String(".");
72
version += String::num_int64(XR_VERSION_PATCH(p_version));
73
74
return version;
75
}
76
77
String OpenXRUtil::get_handle_as_hex_string(void *p_handle) {
78
String hex;
79
80
if (p_handle == XR_NULL_HANDLE) {
81
return "null";
82
}
83
84
uint64_t handle = (uint64_t)p_handle;
85
86
while (handle != 0) {
87
uint8_t a = handle & 0x0F;
88
uint8_t b = (handle & 0xF0) >> 4;
89
handle = handle >> 8;
90
91
if (a < 10) {
92
hex = (a + '0') + hex;
93
} else {
94
hex = (a + 'a' - 10) + hex;
95
}
96
97
if (b < 10) {
98
hex = (b + '0') + hex;
99
} else {
100
hex = (b + 'a' - 10) + hex;
101
}
102
}
103
104
return "0x" + hex;
105
}
106
107
String OpenXRUtil::string_from_xruuid(const XrUuid &xr_uuid) {
108
String ret;
109
bool non_zero = false;
110
111
for (int i = 0; i < XR_UUID_SIZE; i++) {
112
non_zero |= xr_uuid.data[i] != 0;
113
114
char a = xr_uuid.data[i] & 0xF0 >> 4;
115
char b = xr_uuid.data[i] & 0x0F;
116
117
if (a < 10) {
118
ret += '0' + a;
119
} else {
120
ret += 'a' + a - 10;
121
}
122
123
if (b < 10) {
124
ret += '0' + b;
125
} else {
126
ret += 'a' + b - 10;
127
}
128
}
129
130
if (non_zero) {
131
return ret;
132
} else {
133
return "";
134
}
135
}
136
137
XrUuid OpenXRUtil::xruuid_from_string(const String &p_uuid) {
138
XrUuid new_uuid = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
139
140
int len = p_uuid.length();
141
if (len == 0) {
142
return new_uuid;
143
} else if (len != (2 * XR_UUID_SIZE)) {
144
WARN_PRINT("OpenXR: Unexpected UUID length: " + String::num_int64(len) + " != " + String::num_int64(2 * XR_UUID_SIZE));
145
}
146
147
int j = 0;
148
for (int i = 0; i < XR_UUID_SIZE; i++) {
149
uint8_t val = 0;
150
151
// 2 chars per byte.
152
for (int k = 0; k < 2; k++) {
153
if (j < len) {
154
val <<= 4;
155
156
char32_t c = p_uuid[j++];
157
if (c >= '0' && c <= '9') {
158
val += uint8_t(c - '0');
159
} else if (c >= 'a' && c <= 'f') {
160
val += uint8_t(10 + c - 'a');
161
} else if (c >= 'A' && c <= 'F') {
162
val += uint8_t(10 + c - 'A');
163
} else {
164
WARN_PRINT("OpenXR: Unexpected character in UUID: " + String::num_int64(c));
165
}
166
}
167
}
168
169
new_uuid.data[i] = val;
170
}
171
172
return new_uuid;
173
}
174
175
// Copied from OpenXR xr_linear.h private header, so we can still link against
176
// system-provided packages without relying on our `thirdparty` code.
177
178
// Copyright (c) 2017 The Khronos Group Inc.
179
// Copyright (c) 2016 Oculus VR, LLC.
180
//
181
// SPDX-License-Identifier: Apache-2.0
182
183
// Creates a projection matrix based on the specified dimensions.
184
// The projection matrix transforms -Z=forward, +Y=up, +X=right to the appropriate clip space for Godot (OpenGL convention).
185
// The far plane is placed at infinity if farZ <= nearZ.
186
// An infinite projection matrix is preferred for rasterization because, except for
187
// things *right* up against the near plane, it always provides better precision:
188
// "Tightening the Precision of Perspective Rendering"
189
// Paul Upchurch, Mathieu Desbrun
190
// Journal of Graphics Tools, Volume 16, Issue 1, 2012
191
void OpenXRUtil::XrMatrix4x4f_CreateProjection(XrMatrix4x4f *result, const float tanAngleLeft, const float tanAngleRight,
192
const float tanAngleUp, float const tanAngleDown, const float nearZ, const float farZ) {
193
const float tanAngleWidth = tanAngleRight - tanAngleLeft;
194
195
// Set to tanAngleUp - tanAngleDown for a clip space with positive Y up.
196
const float tanAngleHeight = (tanAngleUp - tanAngleDown);
197
198
// Set to nearZ for a [-1,1] Z clip space.
199
const float offsetZ = nearZ;
200
201
if (farZ <= nearZ) {
202
// place the far plane at infinity
203
result->m[0] = 2.0f / tanAngleWidth;
204
result->m[4] = 0.0f;
205
result->m[8] = (tanAngleRight + tanAngleLeft) / tanAngleWidth;
206
result->m[12] = 0.0f;
207
208
result->m[1] = 0.0f;
209
result->m[5] = 2.0f / tanAngleHeight;
210
result->m[9] = (tanAngleUp + tanAngleDown) / tanAngleHeight;
211
result->m[13] = 0.0f;
212
213
result->m[2] = 0.0f;
214
result->m[6] = 0.0f;
215
result->m[10] = -1.0f;
216
result->m[14] = -(nearZ + offsetZ);
217
218
result->m[3] = 0.0f;
219
result->m[7] = 0.0f;
220
result->m[11] = -1.0f;
221
result->m[15] = 0.0f;
222
} else {
223
// normal projection
224
result->m[0] = 2.0f / tanAngleWidth;
225
result->m[4] = 0.0f;
226
result->m[8] = (tanAngleRight + tanAngleLeft) / tanAngleWidth;
227
result->m[12] = 0.0f;
228
229
result->m[1] = 0.0f;
230
result->m[5] = 2.0f / tanAngleHeight;
231
result->m[9] = (tanAngleUp + tanAngleDown) / tanAngleHeight;
232
result->m[13] = 0.0f;
233
234
result->m[2] = 0.0f;
235
result->m[6] = 0.0f;
236
result->m[10] = -(farZ + offsetZ) / (farZ - nearZ);
237
result->m[14] = -(farZ * (nearZ + offsetZ)) / (farZ - nearZ);
238
239
result->m[3] = 0.0f;
240
result->m[7] = 0.0f;
241
result->m[11] = -1.0f;
242
result->m[15] = 0.0f;
243
}
244
}
245
246
// Creates a projection matrix based on the specified FOV.
247
void OpenXRUtil::XrMatrix4x4f_CreateProjectionFov(XrMatrix4x4f *result, const XrFovf fov, const float nearZ, const float farZ) {
248
const float tanLeft = std::tan(fov.angleLeft);
249
const float tanRight = std::tan(fov.angleRight);
250
251
const float tanDown = std::tan(fov.angleDown);
252
const float tanUp = std::tan(fov.angleUp);
253
254
XrMatrix4x4f_CreateProjection(result, tanLeft, tanRight, tanUp, tanDown, nearZ, farZ);
255
}
256
257