openxr_util.cpp 8.3 KB

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