XRUtils.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <XR/XRUtils.h>
  9. #include <AzFramework/CommandLine/CommandLine.h>
  10. #include <AzFramework/API/ApplicationAPI.h>
  11. #include <AzFramework/StringFunc/StringFunc.h>
  12. #include <AzCore/Settings/SettingsRegistry.h>
  13. static constexpr char OpenXREnableSetting[] = "/O3DE/Atom/OpenXR/Enable";
  14. namespace XR
  15. {
  16. AZ::Matrix4x4 CreateStereoscopicProjection(float angleLeft, float angleRight, float angleBottom, float angleTop, float nearDist, float farDist, bool reverseDepth)
  17. {
  18. AZ::Matrix4x4 result;
  19. AZ_MATH_ASSERT(nearDist > 0.0f, "Near plane distance must be greater than 0");
  20. const float left = tanf(angleLeft);
  21. const float right = tanf(angleRight);
  22. const float bottom = tanf(angleBottom);
  23. const float top = tanf(angleTop);
  24. const float invfn = 1.0f / (farDist - nearDist);
  25. const float tanAngleWidth = right - left;
  26. const float tanAngleHeight = (top - bottom);
  27. //Stretch more horizontally and vertically
  28. //Generate asymmetric or off-center projection matrix
  29. //Right handed coord system as Openxr provides data in that system
  30. //Handle the case where farDist is less than nearDist
  31. if(farDist > nearDist)
  32. {
  33. result.SetRow(0, 2.0f / tanAngleWidth, 0.0f, (left + right) / tanAngleWidth, 0.0f);
  34. result.SetRow(1, 0.0f, 2.0f / tanAngleHeight, (top + bottom) / tanAngleHeight, 0.0f);
  35. if(reverseDepth)
  36. {
  37. result.SetRow(2, 0.0f, 0.0f, 2.0f * nearDist * invfn, 2.0f * farDist * nearDist * invfn);
  38. }
  39. else
  40. {
  41. result.SetRow(2, 0.0f, 0.0f, -1.0f * (farDist + nearDist) * invfn, -2.0f * farDist * nearDist * invfn);
  42. }
  43. result.SetRow(3, 0.0f, 0.0f, -1.0f, 0.0f);
  44. }
  45. else
  46. {
  47. // place the far plane at infinity
  48. result.SetRow(0, 2.0f / tanAngleWidth, 0.0f, (left + right) / tanAngleWidth, 0.0f);
  49. result.SetRow(1, 0.0f, 2.0f / tanAngleHeight, (top + bottom) / tanAngleHeight, 0.0f);
  50. if (reverseDepth)
  51. {
  52. result.SetRow(2, 0.0f, 0.0f, 0.0f, 2.0f * nearDist);
  53. }
  54. else
  55. {
  56. result.SetRow(2, 0.0f, 0.0f, -1.0f, -2.0f * nearDist);
  57. }
  58. result.SetRow(3, 0.0f, 0.0f, -1.0f, 0.0f);
  59. }
  60. return result;
  61. }
  62. bool IsOpenXREnabled()
  63. {
  64. const AzFramework::CommandLine* commandLine = nullptr;
  65. AZStd::string commandLineValue;
  66. //Check command line option(-openxr=enable)
  67. AzFramework::ApplicationRequests::Bus::BroadcastResult(commandLine, &AzFramework::ApplicationRequests::GetApplicationCommandLine);
  68. if (commandLine)
  69. {
  70. if (size_t switchCount = commandLine->GetNumSwitchValues("openxr"); switchCount > 0)
  71. {
  72. commandLineValue = commandLine->GetSwitchValue("openxr", switchCount - 1);
  73. }
  74. }
  75. bool isOpenXREnabledViaCL = AzFramework::StringFunc::Equal(commandLineValue.c_str(), "enable");
  76. //Check settings registry
  77. AZ::SettingsRegistryInterface* settingsRegistry = AZ::SettingsRegistry::Get();
  78. bool isOpenXREnabledViaSR = false;
  79. if (settingsRegistry)
  80. {
  81. settingsRegistry->Get(isOpenXREnabledViaSR, OpenXREnableSetting);
  82. }
  83. return isOpenXREnabledViaSR || isOpenXREnabledViaCL;
  84. }
  85. }