XRUtils.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. namespace XR
  10. {
  11. AZ::Matrix4x4 CreateProjectionOffset(float angleLeft, float angleRight, float angleBottom, float angleTop, float nearDist, float farDist)
  12. {
  13. AZ::Matrix4x4 result;
  14. AZ_MATH_ASSERT(nearDist > 0.0f, "Near plane distance must be greater than 0");
  15. const float left = tanf(angleLeft);
  16. const float right = tanf(angleRight);
  17. const float bottom = tanf(angleBottom);
  18. const float top = tanf(angleTop);
  19. const float invfn = 1.0f / (farDist - nearDist);
  20. const float tanAngleWidth = right - left;
  21. const float tanAngleHeight = (top - bottom);
  22. //Stretch more horizontally and vertically
  23. //Generate asymmetric or off-center projection matrix
  24. //Right handed coord system as Openxr provides data in that system
  25. //Handle the case where farDist is less than nearDist
  26. if(farDist > nearDist)
  27. {
  28. result.SetRow(0, 2.0f / tanAngleWidth, 0.0f, (left + right) / tanAngleWidth, 0.0f);
  29. result.SetRow(1, 0.0f, 2.0f / tanAngleHeight, (top + bottom) / tanAngleHeight, 0.0f);
  30. result.SetRow(2, 0.0f, 0.0f, -1 * (farDist + nearDist) * invfn, -2.0f * farDist * nearDist * invfn);
  31. result.SetRow(3, 0.0f, 0.0f, -1.0f, 0.0f);
  32. }
  33. else
  34. {
  35. result.SetRow(0, 2.0f / tanAngleWidth, 0.0f, (left + right) / tanAngleWidth, 0.0f);
  36. result.SetRow(1, 0.0f, 2.0f / tanAngleHeight, (top + bottom) / tanAngleHeight, 0.0f);
  37. result.SetRow(2, 0.0f, 0.0f, -1.0f, -2.0f * nearDist);
  38. result.SetRow(3, 0.0f, 0.0f, -1.0f, 0.0f);
  39. }
  40. return result;
  41. }
  42. }