openxr_util.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 <openxr/openxr_reflection.h>
  32. #include <math.h>
  33. #define XR_ENUM_CASE_STR(name, val) \
  34. case name: \
  35. return #name;
  36. #define XR_ENUM_SWITCH(enumType, var) \
  37. switch (var) { \
  38. XR_LIST_ENUM_##enumType(XR_ENUM_CASE_STR) default : return "Unknown " #enumType ": " + String::num_int64(int64_t(var)); \
  39. }
  40. String OpenXRUtil::get_view_configuration_name(XrViewConfigurationType p_view_configuration){
  41. XR_ENUM_SWITCH(XrViewConfigurationType, p_view_configuration)
  42. }
  43. String OpenXRUtil::get_reference_space_name(XrReferenceSpaceType p_reference_space){
  44. XR_ENUM_SWITCH(XrReferenceSpaceType, p_reference_space)
  45. }
  46. String OpenXRUtil::get_structure_type_name(XrStructureType p_structure_type){
  47. XR_ENUM_SWITCH(XrStructureType, p_structure_type)
  48. }
  49. String OpenXRUtil::get_session_state_name(XrSessionState p_session_state){
  50. XR_ENUM_SWITCH(XrSessionState, p_session_state)
  51. }
  52. String OpenXRUtil::get_action_type_name(XrActionType p_action_type){
  53. XR_ENUM_SWITCH(XrActionType, p_action_type)
  54. }
  55. String OpenXRUtil::get_environment_blend_mode_name(XrEnvironmentBlendMode p_blend_mode) {
  56. XR_ENUM_SWITCH(XrEnvironmentBlendMode, p_blend_mode);
  57. }
  58. String OpenXRUtil::make_xr_version_string(XrVersion p_version) {
  59. String version;
  60. version += String::num_int64(XR_VERSION_MAJOR(p_version));
  61. version += String(".");
  62. version += String::num_int64(XR_VERSION_MINOR(p_version));
  63. version += String(".");
  64. version += String::num_int64(XR_VERSION_PATCH(p_version));
  65. return version;
  66. }
  67. // Copied from OpenXR xr_linear.h private header, so we can still link against
  68. // system-provided packages without relying on our `thirdparty` code.
  69. // Copyright (c) 2017 The Khronos Group Inc.
  70. // Copyright (c) 2016 Oculus VR, LLC.
  71. //
  72. // SPDX-License-Identifier: Apache-2.0
  73. // Creates a projection matrix based on the specified dimensions.
  74. // The projection matrix transforms -Z=forward, +Y=up, +X=right to the appropriate clip space for the graphics API.
  75. // The far plane is placed at infinity if farZ <= nearZ.
  76. // An infinite projection matrix is preferred for rasterization because, except for
  77. // things *right* up against the near plane, it always provides better precision:
  78. // "Tightening the Precision of Perspective Rendering"
  79. // Paul Upchurch, Mathieu Desbrun
  80. // Journal of Graphics Tools, Volume 16, Issue 1, 2012
  81. void OpenXRUtil::XrMatrix4x4f_CreateProjection(XrMatrix4x4f *result, GraphicsAPI graphicsApi, const float tanAngleLeft,
  82. const float tanAngleRight, const float tanAngleUp, float const tanAngleDown,
  83. const float nearZ, const float farZ) {
  84. const float tanAngleWidth = tanAngleRight - tanAngleLeft;
  85. // Set to tanAngleDown - tanAngleUp for a clip space with positive Y down (Vulkan).
  86. // Set to tanAngleUp - tanAngleDown for a clip space with positive Y up (OpenGL / D3D / Metal).
  87. const float tanAngleHeight = graphicsApi == GRAPHICS_VULKAN ? (tanAngleDown - tanAngleUp) : (tanAngleUp - tanAngleDown);
  88. // Set to nearZ for a [-1,1] Z clip space (OpenGL / OpenGL ES).
  89. // Set to zero for a [0,1] Z clip space (Vulkan / D3D / Metal).
  90. const float offsetZ = (graphicsApi == GRAPHICS_OPENGL || graphicsApi == GRAPHICS_OPENGL_ES) ? nearZ : 0;
  91. if (farZ <= nearZ) {
  92. // place the far plane at infinity
  93. result->m[0] = 2.0f / tanAngleWidth;
  94. result->m[4] = 0.0f;
  95. result->m[8] = (tanAngleRight + tanAngleLeft) / tanAngleWidth;
  96. result->m[12] = 0.0f;
  97. result->m[1] = 0.0f;
  98. result->m[5] = 2.0f / tanAngleHeight;
  99. result->m[9] = (tanAngleUp + tanAngleDown) / tanAngleHeight;
  100. result->m[13] = 0.0f;
  101. result->m[2] = 0.0f;
  102. result->m[6] = 0.0f;
  103. result->m[10] = -1.0f;
  104. result->m[14] = -(nearZ + offsetZ);
  105. result->m[3] = 0.0f;
  106. result->m[7] = 0.0f;
  107. result->m[11] = -1.0f;
  108. result->m[15] = 0.0f;
  109. } else {
  110. // normal projection
  111. result->m[0] = 2.0f / tanAngleWidth;
  112. result->m[4] = 0.0f;
  113. result->m[8] = (tanAngleRight + tanAngleLeft) / tanAngleWidth;
  114. result->m[12] = 0.0f;
  115. result->m[1] = 0.0f;
  116. result->m[5] = 2.0f / tanAngleHeight;
  117. result->m[9] = (tanAngleUp + tanAngleDown) / tanAngleHeight;
  118. result->m[13] = 0.0f;
  119. result->m[2] = 0.0f;
  120. result->m[6] = 0.0f;
  121. result->m[10] = -(farZ + offsetZ) / (farZ - nearZ);
  122. result->m[14] = -(farZ * (nearZ + offsetZ)) / (farZ - nearZ);
  123. result->m[3] = 0.0f;
  124. result->m[7] = 0.0f;
  125. result->m[11] = -1.0f;
  126. result->m[15] = 0.0f;
  127. }
  128. }
  129. // Creates a projection matrix based on the specified FOV.
  130. void OpenXRUtil::XrMatrix4x4f_CreateProjectionFov(XrMatrix4x4f *result, GraphicsAPI graphicsApi, const XrFovf fov,
  131. const float nearZ, const float farZ) {
  132. const float tanLeft = tanf(fov.angleLeft);
  133. const float tanRight = tanf(fov.angleRight);
  134. const float tanDown = tanf(fov.angleDown);
  135. const float tanUp = tanf(fov.angleUp);
  136. XrMatrix4x4f_CreateProjection(result, graphicsApi, tanLeft, tanRight, tanUp, tanDown, nearZ, farZ);
  137. }