CameraUtilities.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 <AzCore/Math/Matrix3x3.h>
  9. #include <AzCore/Math/MatrixUtils.h>
  10. namespace ROS2::CameraUtils
  11. {
  12. float GetAspectRatio(float width, float height)
  13. {
  14. return width / height;
  15. };
  16. AZ::Matrix3x3 MakeCameraIntrinsics(int width, int height, float verticalFieldOfViewDeg)
  17. {
  18. const auto w = static_cast<float>(width);
  19. const auto h = static_cast<float>(height);
  20. const float verticalFieldOfView = AZ::DegToRad(verticalFieldOfViewDeg);
  21. const float horizontalFoV = 2.0 * AZStd::atan(AZStd::tan(verticalFieldOfView / 2.0) * GetAspectRatio(width, height));
  22. const float focalLengthX = w / (2.0 * AZStd::tan(horizontalFoV / 2.0));
  23. const float focalLengthY = h / (2.0 * AZStd::tan(verticalFieldOfView / 2.0));
  24. return AZ::Matrix3x3::CreateFromRows({ focalLengthX, 0.f, w / 2.f }, { 0.f, focalLengthY, h / 2.f }, { 0.f, 0.f, 1.f });
  25. }
  26. AZ::Matrix4x4 MakeClipMatrix(int width, int height, float verticalFieldOfViewDeg, float nearDist, float farDist)
  27. {
  28. AZ::Matrix4x4 localViewToClipMatrix;
  29. AZ::MakePerspectiveFovMatrixRH(
  30. localViewToClipMatrix,
  31. AZ::DegToRad(verticalFieldOfViewDeg),
  32. CameraUtils::GetAspectRatio(width, height),
  33. nearDist,
  34. farDist,
  35. true);
  36. return localViewToClipMatrix;
  37. }
  38. } // namespace ROS2::CameraUtils