Debian Codegraph @

modules/openxr/extensions/spatial_entities/openxr_spatial_entities.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* openxr_spatial_entities.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_spatial_entities.h"
32
33 #include "../../openxr_api.h"
34 #include "core/variant/native_ptr.h"
35 #include "openxr_spatial_entity_extension.h"
36
37 ////////////////////////////////////////////////////////////////////////////
38 // OpenXRSpatialCapabilityConfigurationBaseHeader
39
40 void OpenXRSpatialCapabilityConfigurationBaseHeader::_bind_methods() {
41 ClassDB::bind_method(D_METHOD("has_valid_configuration"), &OpenXRSpatialCapabilityConfigurationBaseHeader::has_valid_configuration);
42
43 GDVIRTUAL_BIND(_has_valid_configuration);
44 GDVIRTUAL_BIND(_get_configuration);
45 }
46
47 bool OpenXRSpatialCapabilityConfigurationBaseHeader::has_valid_configuration() const {
48 bool is_valid = false;
49
50 if (GDVIRTUAL_CALL(_has_valid_configuration, is_valid)) {
51 return is_valid;
52 }
53
54 return false;
55 }
56
57 XrSpatialCapabilityConfigurationBaseHeaderEXT *OpenXRSpatialCapabilityConfigurationBaseHeader::get_configuration() {
58 uint64_t pointer = 0;
59
60 if (GDVIRTUAL_CALL(_get_configuration, pointer)) {
61 return reinterpret_cast<XrSpatialCapabilityConfigurationBaseHeaderEXT *>(pointer);
62 }
63
64 return nullptr;
65 }
66
67 ////////////////////////////////////////////////////////////////////////////
68 // OpenXRSpatialEntityTracker
69
70 void OpenXRSpatialEntityTracker::_bind_methods() {
71 ClassDB::bind_method(D_METHOD("set_entity", "entity"), &OpenXRSpatialEntityTracker::set_entity);
72 ClassDB::bind_method(D_METHOD("get_entity"), &OpenXRSpatialEntityTracker::get_entity);
73
74 ADD_PROPERTY(PropertyInfo(Variant::RID, "entity"), "set_entity", "get_entity");
75
76 ClassDB::bind_method(D_METHOD("set_spatial_tracking_state", "spatial_tracking_state"), &OpenXRSpatialEntityTracker::_set_spatial_tracking_state);
77 ClassDB::bind_method(D_METHOD("get_spatial_tracking_state"), &OpenXRSpatialEntityTracker::_get_spatial_tracking_state);
78 ADD_PROPERTY(PropertyInfo(Variant::INT, "spatial_tracking_state"), "set_spatial_tracking_state", "get_spatial_tracking_state");
79
80 ADD_SIGNAL(MethodInfo("spatial_tracking_state_changed", PropertyInfo(Variant::INT, "spatial_tracking_state")));
81
82 BIND_ENUM_CONSTANT(ENTITY_TRACKING_STATE_STOPPED);
83 BIND_ENUM_CONSTANT(ENTITY_TRACKING_STATE_PAUSED);
84 BIND_ENUM_CONSTANT(ENTITY_TRACKING_STATE_TRACKING);
85 }
86
87 OpenXRSpatialEntityTracker::OpenXRSpatialEntityTracker() {
88 set_tracker_type(XRServer::TrackerType::TRACKER_ANCHOR);
89 }
90
91 OpenXRSpatialEntityTracker::~OpenXRSpatialEntityTracker() {
92 if (spatial_entity.is_valid()) {
93 OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton();
94 if (se_extension) {
95 se_extension->free_spatial_entity(spatial_entity);
96 spatial_entity = RID();
97 }
98 }
99 }
100
101 void OpenXRSpatialEntityTracker::set_entity(const RID &p_entity) {
102 if (spatial_entity.is_valid()) {
103 if (spatial_entity == p_entity) {
104 return;
105 }
106
107 OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton();
108 if (se_extension) {
109 se_extension->free_spatial_entity(spatial_entity);
110 spatial_entity = RID();
111 }
112 }
113
114 spatial_entity = p_entity;
115
116 if (p_entity.is_valid()) {
117 OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton();
118 ERR_FAIL_NULL(se_extension);
119
120 XrSpatialEntityIdEXT entity_id = se_extension->get_spatial_entity_id(p_entity);
121
122 String tracker_name = String("openxr/spatial_entity/") + String::num_int64(entity_id);
123 set_tracker_name(tracker_name);
124 } else {
125 set_tracker_name("openxr/spatial_entity/null");
126 }
127 }
128
129 RID OpenXRSpatialEntityTracker::get_entity() const {
130 return spatial_entity;
131 }
132
133 void OpenXRSpatialEntityTracker::set_spatial_tracking_state(const XrSpatialEntityTrackingStateEXT p_state) {
134 if (spatial_tracking_state != p_state) {
135 spatial_tracking_state = p_state;
136
137 emit_signal(SNAME("spatial_tracking_state_changed"), spatial_tracking_state);
138 }
139 }
140
141 void OpenXRSpatialEntityTracker::_set_spatial_tracking_state(const EntityTrackingState p_state) {
142 set_spatial_tracking_state((XrSpatialEntityTrackingStateEXT)p_state);
143 }
144
145 XrSpatialEntityTrackingStateEXT OpenXRSpatialEntityTracker::get_spatial_tracking_state() const {
146 return spatial_tracking_state;
147 }
148
149 OpenXRSpatialEntityTracker::EntityTrackingState OpenXRSpatialEntityTracker::_get_spatial_tracking_state() const {
150 return (EntityTrackingState)get_spatial_tracking_state();
151 }
152
153 ////////////////////////////////////////////////////////////////////////////
154 // OpenXRSpatialComponentData
155
156 void OpenXRSpatialComponentData::_bind_methods() {
157 ClassDB::bind_method(D_METHOD("set_capacity", "capacity"), &OpenXRSpatialComponentData::set_capacity);
158
159 GDVIRTUAL_BIND(_set_capacity, "capacity");
160 GDVIRTUAL_BIND(_get_component_type);
161 GDVIRTUAL_BIND(_get_structure_data, "next");
162 }
163
164 void OpenXRSpatialComponentData::set_capacity(uint32_t p_capacity) {
165 GDVIRTUAL_CALL(_set_capacity, p_capacity);
166 }
167
168 XrSpatialComponentTypeEXT OpenXRSpatialComponentData::get_component_type() const {
169 uint64_t component_type = XR_SPATIAL_COMPONENT_TYPE_MAX_ENUM_EXT;
170
171 if (GDVIRTUAL_CALL(_get_component_type, component_type)) {
172 return (XrSpatialComponentTypeEXT)component_type;
173 }
174
175 return XR_SPATIAL_COMPONENT_TYPE_MAX_ENUM_EXT;
176 }
177
178 void *OpenXRSpatialComponentData::get_structure_data(void *p_next) {
179 uint64_t pointer = 0;
180
181 if (GDVIRTUAL_CALL(_get_structure_data, (uint64_t)p_next, pointer)) {
182 return reinterpret_cast<void *>(pointer);
183 }
184
185 return p_next;
186 }
187
188 ////////////////////////////////////////////////////////////////////////////
189 // Spatial component bounded2d list
190
191 void OpenXRSpatialComponentBounded2DList::_bind_methods() {
192 ClassDB::bind_method(D_METHOD("get_center_pose", "index"), &OpenXRSpatialComponentBounded2DList::get_center_pose);
193 ClassDB::bind_method(D_METHOD("get_size", "index"), &OpenXRSpatialComponentBounded2DList::get_size);
194 }
195
196 void OpenXRSpatialComponentBounded2DList::set_capacity(uint32_t p_capacity) {
197 bounded2d_data.resize(p_capacity);
198
199 bounded2d_list.boundCount = uint32_t(bounded2d_data.size());
200 bounded2d_list.bounds = bounded2d_data.ptrw();
201 }
202
203 XrSpatialComponentTypeEXT OpenXRSpatialComponentBounded2DList::get_component_type() const {
204 return XR_SPATIAL_COMPONENT_TYPE_BOUNDED_2D_EXT;
205 }
206
207 void *OpenXRSpatialComponentBounded2DList::get_structure_data(void *p_next) {
208 bounded2d_list.next = p_next;
209 return &bounded2d_list;
210 }
211
212 Transform3D OpenXRSpatialComponentBounded2DList::get_center_pose(int64_t p_index) const {
213 ERR_FAIL_INDEX_V(p_index, bounded2d_data.size(), Transform3D());
214
215 OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
216 ERR_FAIL_NULL_V(openxr_api, Transform3D());
217
218 return openxr_api->transform_from_pose(bounded2d_data[p_index].center);
219 }
220
221 Vector2 OpenXRSpatialComponentBounded2DList::get_size(int64_t p_index) const {
222 ERR_FAIL_INDEX_V(p_index, bounded2d_data.size(), Vector2());
223
224 OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
225 ERR_FAIL_NULL_V(openxr_api, Vector2());
226
227 const XrExtent2Df &extents = bounded2d_data[p_index].extents;
228 return Vector2(extents.width, extents.height);
229 }
230
231 ////////////////////////////////////////////////////////////////////////////
232 // Spatial component bounded3d list
233
234 void OpenXRSpatialComponentBounded3DList::_bind_methods() {
235 ClassDB::bind_method(D_METHOD("get_center_pose", "index"), &OpenXRSpatialComponentBounded3DList::get_center_pose);
236 ClassDB::bind_method(D_METHOD("get_size", "index"), &OpenXRSpatialComponentBounded3DList::get_size);
237 }
238
239 void OpenXRSpatialComponentBounded3DList::set_capacity(uint32_t p_capacity) {
240 bounded3d_data.resize(p_capacity);
241
242 bounded3d_list.boundCount = uint32_t(bounded3d_data.size());
243 bounded3d_list.bounds = bounded3d_data.ptrw();
244 }
245
246 XrSpatialComponentTypeEXT OpenXRSpatialComponentBounded3DList::get_component_type() const {
247 return XR_SPATIAL_COMPONENT_TYPE_BOUNDED_3D_EXT;
248 }
249
250 void *OpenXRSpatialComponentBounded3DList::get_structure_data(void *p_next) {
251 bounded3d_list.next = p_next;
252 return &bounded3d_list;
253 }
254
255 Transform3D OpenXRSpatialComponentBounded3DList::get_center_pose(int64_t p_index) const {
256 ERR_FAIL_INDEX_V(p_index, bounded3d_data.size(), Transform3D());
257
258 OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
259 ERR_FAIL_NULL_V(openxr_api, Transform3D());
260
261 return openxr_api->transform_from_pose(bounded3d_data[p_index].center);
262 }
263
264 Vector3 OpenXRSpatialComponentBounded3DList::get_size(int64_t p_index) const {
265 ERR_FAIL_INDEX_V(p_index, bounded3d_data.size(), Vector3());
266
267 OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
268 ERR_FAIL_NULL_V(openxr_api, Vector3());
269
270 const XrExtent3Df &extents = bounded3d_data[p_index].extents;
271 return Vector3(extents.width, extents.height, extents.depth);
272 }
273
274 ////////////////////////////////////////////////////////////////////////////
275 // Spatial component parent list
276
277 void OpenXRSpatialComponentParentList::_bind_methods() {
278 ClassDB::bind_method(D_METHOD("get_parent", "index"), &OpenXRSpatialComponentParentList::get_parent);
279 }
280
281 void OpenXRSpatialComponentParentList::set_capacity(uint32_t p_capacity) {
282 parent_data.resize(p_capacity);
283
284 parent_list.parentCount = uint32_t(parent_data.size());
285 parent_list.parents = parent_data.ptrw();
286 }
287
288 XrSpatialComponentTypeEXT OpenXRSpatialComponentParentList::get_component_type() const {
289 return XR_SPATIAL_COMPONENT_TYPE_PARENT_EXT;
290 }
291
292 void *OpenXRSpatialComponentParentList::get_structure_data(void *p_next) {
293 parent_list.next = p_next;
294 return &parent_list;
295 }
296
297 RID OpenXRSpatialComponentParentList::get_parent(int64_t p_index) const {
298 ERR_FAIL_INDEX_V(p_index, parent_data.size(), RID());
299
300 OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton();
301 ERR_FAIL_NULL_V(se_extension, RID());
302
303 return se_extension->find_spatial_entity(parent_data[p_index]);
304 }
305
306 ////////////////////////////////////////////////////////////////////////////
307 // Spatial component mesh2d list
308
309 void OpenXRSpatialComponentMesh2DList::_bind_methods() {
310 ClassDB::bind_method(D_METHOD("get_transform", "index"), &OpenXRSpatialComponentMesh2DList::get_transform);
311 ClassDB::bind_method(D_METHOD("get_vertices", "snapshot", "index"), &OpenXRSpatialComponentMesh2DList::get_vertices);
312 ClassDB::bind_method(D_METHOD("get_indices", "snapshot", "index"), &OpenXRSpatialComponentMesh2DList::get_indices);
313 }
314
315 void OpenXRSpatialComponentMesh2DList::set_capacity(uint32_t p_capacity) {
316 mesh2d_data.resize(p_capacity);
317
318 mesh2d_list.meshCount = uint32_t(mesh2d_data.size());
319 mesh2d_list.meshes = mesh2d_data.ptrw();
320 }
321
322 XrSpatialComponentTypeEXT OpenXRSpatialComponentMesh2DList::get_component_type() const {
323 return XR_SPATIAL_COMPONENT_TYPE_MESH_2D_EXT;
324 }
325
326 void *OpenXRSpatialComponentMesh2DList::get_structure_data(void *p_next) {
327 mesh2d_list.next = p_next;
328 return &mesh2d_list;
329 }
330
331 Transform3D OpenXRSpatialComponentMesh2DList::get_transform(int64_t p_index) const {
332 ERR_FAIL_INDEX_V(p_index, mesh2d_data.size(), Transform3D());
333
334 OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
335 ERR_FAIL_NULL_V(openxr_api, Transform3D());
336
337 return openxr_api->transform_from_pose(mesh2d_data[p_index].origin);
338 }
339
340 PackedVector2Array OpenXRSpatialComponentMesh2DList::get_vertices(RID p_snapshot, int64_t p_index) const {
341 ERR_FAIL_INDEX_V(p_index, mesh2d_data.size(), PackedVector2Array());
342
343 const XrSpatialBufferEXT &buffer = mesh2d_data[p_index].vertexBuffer;
344 if (buffer.bufferId == XR_NULL_SPATIAL_BUFFER_ID_EXT) {
345 // We don't have data (yet).
346 return PackedVector2Array();
347 }
348
349 ERR_FAIL_COND_V(buffer.bufferType != XR_SPATIAL_BUFFER_TYPE_VECTOR2F_EXT, PackedVector2Array());
350
351 OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton();
352 ERR_FAIL_NULL_V(se_extension, PackedVector2Array());
353
354 return se_extension->get_vector2_buffer(p_snapshot, buffer.bufferId);
355 }
356
357 PackedInt32Array OpenXRSpatialComponentMesh2DList::get_indices(RID p_snapshot, int64_t p_index) const {
358 ERR_FAIL_INDEX_V(p_index, mesh2d_data.size(), PackedInt32Array());
359
360 const XrSpatialBufferEXT &buffer = mesh2d_data[p_index].indexBuffer;
361 if (buffer.bufferId == XR_NULL_SPATIAL_BUFFER_ID_EXT) {
362 // We don't have data (yet).
363 return PackedInt32Array();
364 }
365
366 OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton();
367 ERR_FAIL_NULL_V(se_extension, PackedInt32Array());
368
369 PackedInt32Array ret;
370
371 switch (buffer.bufferType) {
372 case XR_SPATIAL_BUFFER_TYPE_UINT8_EXT: {
373 PackedByteArray data = se_extension->get_uint8_buffer(p_snapshot, buffer.bufferId);
374
375 ret.resize(data.size());
376 int count = ret.size();
377 int32_t *ptr = ret.ptrw();
378 for (int i = 0; i < count; i++) {
379 ptr[i] = data[i];
380 }
381 } break;
382 case XR_SPATIAL_BUFFER_TYPE_UINT16_EXT: {
383 Vector<uint16_t> data = se_extension->get_uint16_buffer(p_snapshot, buffer.bufferId);
384
385 ret.resize(data.size());
386 int count = ret.size();
387 int32_t *ptr = ret.ptrw();
388 for (int i = 0; i < count; i++) {
389 ptr[i] = data[i];
390 }
391 } break;
392 case XR_SPATIAL_BUFFER_TYPE_UINT32_EXT: {
393 Vector<uint32_t> data = se_extension->get_uint32_buffer(p_snapshot, buffer.bufferId);
394
395 ret.resize(data.size());
396 int count = ret.size();
397 int32_t *ptr = ret.ptrw();
398 for (int i = 0; i < count; i++) {
399 ptr[i] = data[i];
400 }
401 } break;
402 default: {
403 ERR_FAIL_V_MSG(PackedInt32Array(), "OpenXR: Unsupported buffer type for indices.");
404 } break;
405 }
406
407 return ret;
408 }
409
410 ////////////////////////////////////////////////////////////////////////////
411 // Spatial component mesh3d list
412
413 void OpenXRSpatialComponentMesh3DList::_bind_methods() {
414 ClassDB::bind_method(D_METHOD("get_transform", "index"), &OpenXRSpatialComponentMesh3DList::get_transform);
415 ClassDB::bind_method(D_METHOD("get_mesh", "index"), &OpenXRSpatialComponentMesh3DList::get_mesh);
416 }
417
418 void OpenXRSpatialComponentMesh3DList::set_capacity(uint32_t p_capacity) {
419 mesh3d_data.resize(p_capacity);
420
421 mesh3d_list.meshCount = uint32_t(mesh3d_data.size());
422 mesh3d_list.meshes = mesh3d_data.ptrw();
423 }
424
425 XrSpatialComponentTypeEXT OpenXRSpatialComponentMesh3DList::get_component_type() const {
426 return XR_SPATIAL_COMPONENT_TYPE_MESH_3D_EXT;
427 }
428
429 void *OpenXRSpatialComponentMesh3DList::get_structure_data(void *p_next) {
430 mesh3d_list.next = p_next;
431 return &mesh3d_list;
432 }
433
434 Transform3D OpenXRSpatialComponentMesh3DList::get_transform(int64_t p_index) const {
435 ERR_FAIL_INDEX_V(p_index, mesh3d_data.size(), Transform3D());
436
437 OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
438 ERR_FAIL_NULL_V(openxr_api, Transform3D());
439
440 return openxr_api->transform_from_pose(mesh3d_data[p_index].origin);
441 }
442
443 Ref<Mesh> OpenXRSpatialComponentMesh3DList::get_mesh(int64_t p_index) const {
444 ERR_FAIL_INDEX_V(p_index, mesh3d_data.size(), nullptr);
445
446 // TODO implement, need to convert mesh data to Godot mesh resource
447
448 return nullptr;
449 }
450
451 ////////////////////////////////////////////////////////////////////////////
452 // OpenXRSpatialQueryResultData
453
454 void OpenXRSpatialQueryResultData::_bind_methods() {
455 ClassDB::bind_method(D_METHOD("get_capacity"), &OpenXRSpatialQueryResultData::get_capacity);
456 ClassDB::bind_method(D_METHOD("get_entity_id", "index"), &OpenXRSpatialQueryResultData::_get_entity_id);
457 ClassDB::bind_method(D_METHOD("get_entity_state", "index"), &OpenXRSpatialQueryResultData::_get_entity_state);
458 }
459
460 void OpenXRSpatialQueryResultData::set_capacity(uint32_t p_capacity) {
461 entity_ids.resize(p_capacity);
462 entity_states.resize(p_capacity);
463
464 query_result.entityIdCapacityInput = entity_ids.size();
465 query_result.entityIds = entity_ids.ptrw();
466 query_result.entityStateCapacityInput = entity_states.size();
467 query_result.entityStates = entity_states.ptrw();
468 }
469
470 XrSpatialComponentTypeEXT OpenXRSpatialQueryResultData::get_component_type() const {
471 // This component is always included and has no type.
472 return XR_SPATIAL_COMPONENT_TYPE_MAX_ENUM_EXT;
473 }
474
475 void *OpenXRSpatialQueryResultData::get_structure_data(void *p_next) {
476 query_result.next = p_next;
477 query_result.entityIdCountOutput = 0;
478 query_result.entityStateCountOutput = 0;
479 return &query_result;
480 }
481
482 XrSpatialEntityIdEXT OpenXRSpatialQueryResultData::get_entity_id(int64_t p_index) const {
483 ERR_FAIL_INDEX_V(p_index, entity_ids.size(), XR_NULL_ENTITY);
484
485 return entity_ids[p_index];
486 }
487
488 uint64_t OpenXRSpatialQueryResultData::_get_entity_id(int64_t p_index) const {
489 return (uint64_t)get_entity_id(p_index);
490 }
491
492 XrSpatialEntityTrackingStateEXT OpenXRSpatialQueryResultData::get_entity_state(int64_t p_index) const {
493 ERR_FAIL_INDEX_V(p_index, entity_states.size(), XR_SPATIAL_ENTITY_TRACKING_STATE_MAX_ENUM_EXT);
494
495 return entity_states[p_index];
496 }
497
498 OpenXRSpatialEntityTracker::EntityTrackingState OpenXRSpatialQueryResultData::_get_entity_state(int64_t p_index) const {
499 return (OpenXRSpatialEntityTracker::EntityTrackingState)get_entity_state(p_index);
500 }
501
502 String OpenXRSpatialQueryResultData::get_entity_tracking_state_name(XrSpatialEntityTrackingStateEXT p_tracking_state) {
503 XR_ENUM_SWITCH(XrSpatialEntityTrackingStateEXT, p_tracking_state)
504 }