/**************************************************************************/1/* openxr_util.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "openxr_util.h"3132#include "core/math/math_funcs.h"3334#include <openxr/openxr_reflection.h>3536#define XR_ENUM_CASE_STR(name, val) \37case name: \38return #name;39#define XR_ENUM_SWITCH(enumType, var) \40switch (var) { \41XR_LIST_ENUM_##enumType(XR_ENUM_CASE_STR) default : return "Unknown " #enumType ": " + String::num_int64(int64_t(var)); \42}4344String OpenXRUtil::get_view_configuration_name(XrViewConfigurationType p_view_configuration){45XR_ENUM_SWITCH(XrViewConfigurationType, p_view_configuration)46}4748String OpenXRUtil::get_reference_space_name(XrReferenceSpaceType p_reference_space){49XR_ENUM_SWITCH(XrReferenceSpaceType, p_reference_space)50}5152String OpenXRUtil::get_structure_type_name(XrStructureType p_structure_type){53XR_ENUM_SWITCH(XrStructureType, p_structure_type)54}5556String OpenXRUtil::get_session_state_name(XrSessionState p_session_state){57XR_ENUM_SWITCH(XrSessionState, p_session_state)58}5960String OpenXRUtil::get_action_type_name(XrActionType p_action_type){61XR_ENUM_SWITCH(XrActionType, p_action_type)62}6364String OpenXRUtil::get_environment_blend_mode_name(XrEnvironmentBlendMode p_blend_mode) {65XR_ENUM_SWITCH(XrEnvironmentBlendMode, p_blend_mode);66}6768String OpenXRUtil::make_xr_version_string(XrVersion p_version) {69String version;7071version += String::num_int64(XR_VERSION_MAJOR(p_version));72version += String(".");73version += String::num_int64(XR_VERSION_MINOR(p_version));74version += String(".");75version += String::num_int64(XR_VERSION_PATCH(p_version));7677return version;78}7980// Copied from OpenXR xr_linear.h private header, so we can still link against81// system-provided packages without relying on our `thirdparty` code.8283// Copyright (c) 2017 The Khronos Group Inc.84// Copyright (c) 2016 Oculus VR, LLC.85//86// SPDX-License-Identifier: Apache-2.08788// Creates a projection matrix based on the specified dimensions.89// The projection matrix transforms -Z=forward, +Y=up, +X=right to the appropriate clip space for the graphics API.90// The far plane is placed at infinity if farZ <= nearZ.91// An infinite projection matrix is preferred for rasterization because, except for92// things *right* up against the near plane, it always provides better precision:93// "Tightening the Precision of Perspective Rendering"94// Paul Upchurch, Mathieu Desbrun95// Journal of Graphics Tools, Volume 16, Issue 1, 201296void OpenXRUtil::XrMatrix4x4f_CreateProjection(XrMatrix4x4f *result, GraphicsAPI graphicsApi, const float tanAngleLeft,97const float tanAngleRight, const float tanAngleUp, float const tanAngleDown,98const float nearZ, const float farZ) {99const float tanAngleWidth = tanAngleRight - tanAngleLeft;100101// Set to tanAngleDown - tanAngleUp for a clip space with positive Y down (Vulkan).102// Set to tanAngleUp - tanAngleDown for a clip space with positive Y up (OpenGL / D3D / Metal).103const float tanAngleHeight = graphicsApi == GRAPHICS_VULKAN ? (tanAngleDown - tanAngleUp) : (tanAngleUp - tanAngleDown);104105// Set to nearZ for a [-1,1] Z clip space (OpenGL / OpenGL ES).106// Set to zero for a [0,1] Z clip space (Vulkan / D3D / Metal).107const float offsetZ = (graphicsApi == GRAPHICS_OPENGL || graphicsApi == GRAPHICS_OPENGL_ES) ? nearZ : 0;108109if (farZ <= nearZ) {110// place the far plane at infinity111result->m[0] = 2.0f / tanAngleWidth;112result->m[4] = 0.0f;113result->m[8] = (tanAngleRight + tanAngleLeft) / tanAngleWidth;114result->m[12] = 0.0f;115116result->m[1] = 0.0f;117result->m[5] = 2.0f / tanAngleHeight;118result->m[9] = (tanAngleUp + tanAngleDown) / tanAngleHeight;119result->m[13] = 0.0f;120121result->m[2] = 0.0f;122result->m[6] = 0.0f;123result->m[10] = -1.0f;124result->m[14] = -(nearZ + offsetZ);125126result->m[3] = 0.0f;127result->m[7] = 0.0f;128result->m[11] = -1.0f;129result->m[15] = 0.0f;130} else {131// normal projection132result->m[0] = 2.0f / tanAngleWidth;133result->m[4] = 0.0f;134result->m[8] = (tanAngleRight + tanAngleLeft) / tanAngleWidth;135result->m[12] = 0.0f;136137result->m[1] = 0.0f;138result->m[5] = 2.0f / tanAngleHeight;139result->m[9] = (tanAngleUp + tanAngleDown) / tanAngleHeight;140result->m[13] = 0.0f;141142result->m[2] = 0.0f;143result->m[6] = 0.0f;144result->m[10] = -(farZ + offsetZ) / (farZ - nearZ);145result->m[14] = -(farZ * (nearZ + offsetZ)) / (farZ - nearZ);146147result->m[3] = 0.0f;148result->m[7] = 0.0f;149result->m[11] = -1.0f;150result->m[15] = 0.0f;151}152}153154// Creates a projection matrix based on the specified FOV.155void OpenXRUtil::XrMatrix4x4f_CreateProjectionFov(XrMatrix4x4f *result, GraphicsAPI graphicsApi, const XrFovf fov,156const float nearZ, const float farZ) {157const float tanLeft = std::tan(fov.angleLeft);158const float tanRight = std::tan(fov.angleRight);159160const float tanDown = std::tan(fov.angleDown);161const float tanUp = std::tan(fov.angleUp);162163XrMatrix4x4f_CreateProjection(result, graphicsApi, tanLeft, tanRight, tanUp, tanDown, nearZ, farZ);164}165166167