💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
5.2 KiB

  1. /*
  2. * Copyright (C) 2023 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_MATH_PROJECTION_HPP
  20. #define ANTKEEPER_MATH_PROJECTION_HPP
  21. #include "math/matrix.hpp"
  22. #include <cmath>
  23. namespace math {
  24. /**
  25. * Calculates a horizontal FoV given a vertical FoV and aspect ratio.
  26. *
  27. * @param v Vertical FoV, in radians.
  28. * @param r Ratio of width to height.
  29. *
  30. * @return Horizontal FoV, in radians.
  31. *
  32. * @see https://en.wikipedia.org/wiki/Field_of_view_in_video_games
  33. */
  34. template <class T>
  35. [[nodiscard]] T horizontal_fov(T v, T r)
  36. {
  37. return T{2} * std::atan(std::tan(v * T{0.5}) * r);
  38. }
  39. /**
  40. * Calculates a vertical FoV given a horizontal FoV and aspect ratio.
  41. *
  42. * @param h Horizontal FoV, in radians.
  43. * @param r Ratio of width to height.
  44. *
  45. * @return Vertical FoV, in radians.
  46. *
  47. * @see https://en.wikipedia.org/wiki/Field_of_view_in_video_games
  48. */
  49. template <class T>
  50. [[nodiscard]] T vertical_fov(T h, T r)
  51. {
  52. return T{2} * std::atan(std::tan(h * T{0.5}) / r);
  53. }
  54. /**
  55. * Creates an orthographic projection matrix which will transform the near and far clipping planes to `[-1, 1]`, respectively.
  56. *
  57. * @param left Signed distance to the left clipping plane.
  58. * @param right Signed distance to the right clipping plane.
  59. * @param bottom Signed distance to the bottom clipping plane.
  60. * @param top Signed distance to the top clipping plane.
  61. * @param z_near Signed distance to the near clipping plane.
  62. * @param z_far Signed distance to the far clipping plane.
  63. *
  64. * @return Orthographic projection matrix.
  65. */
  66. template <class T>
  67. [[nodiscard]] constexpr matrix<T, 4, 4> ortho(T left, T right, T bottom, T top, T z_near, T z_far) noexcept
  68. {
  69. return
  70. {{
  71. {T(2) / (right - left), T(0), T(0), T(0)},
  72. {T(0), T(2) / (top - bottom), T(0), T(0)},
  73. {T(0), T(0), T(-2) / (z_far - z_near), T(0)},
  74. {-((right + left) / (right - left)), -((top + bottom) / (top - bottom)), -((z_far + z_near) / (z_far - z_near)), T(1)}
  75. }};
  76. }
  77. /**
  78. * Creates an orthographic projection matrix which will transform the near and far clipping planes to `[0, 1]`, respectively.
  79. *
  80. * @param left Signed distance to the left clipping plane.
  81. * @param right Signed distance to the right clipping plane.
  82. * @param bottom Signed distance to the bottom clipping plane.
  83. * @param top Signed distance to the top clipping plane.
  84. * @param z_near Signed distance to the near clipping plane.
  85. * @param z_far Signed distance to the far clipping plane.
  86. *
  87. * @return Orthographic projection matrix.
  88. */
  89. template <class T>
  90. [[nodiscard]] constexpr matrix<T, 4, 4> ortho_half_z(T left, T right, T bottom, T top, T z_near, T z_far) noexcept
  91. {
  92. return
  93. {{
  94. {T(2) / (right - left), T(0), T(0), T(0)},
  95. {T(0), T(2) / (top - bottom), T(0), T(0)},
  96. {T(0), T(0), T(-1) / (z_far - z_near), T(0)},
  97. {-((right + left) / (right - left)), -((top + bottom) / (top - bottom)), -z_near / (z_far - z_near), T(1)}
  98. }};
  99. }
  100. /**
  101. * Creates a perspective projection matrix which will transform the near and far clipping planes to `[-1, 1]`, respectively.
  102. *
  103. * @param vertical_fov Vertical field of view angle, in radians.
  104. * @param aspect_ratio Aspect ratio which determines the horizontal field of view.
  105. * @param z_near Distance to the near clipping plane.
  106. * @param z_far Distance to the far clipping plane.
  107. *
  108. * @return Perspective projection matrix.
  109. */
  110. template <class T>
  111. [[nodiscard]] matrix<T, 4, 4> perspective(T vertical_fov, T aspect_ratio, T z_near, T z_far)
  112. {
  113. T half_fov = vertical_fov * T(0.5);
  114. T f = std::cos(half_fov) / std::sin(half_fov);
  115. return
  116. {{
  117. {f / aspect_ratio, T(0), T(0), T(0)},
  118. {T(0), f, T(0), T(0)},
  119. {T(0), T(0), (z_far + z_near) / (z_near - z_far), T(-1)},
  120. {T(0), T(0), (T(2) * z_far * z_near) / (z_near - z_far), T(0)}
  121. }};
  122. }
  123. /**
  124. * Creates a perspective projection matrix which will transform the near and far clipping planes to `[0, 1]`, respectively.
  125. *
  126. * @param vertical_fov Vertical field of view angle, in radians.
  127. * @param aspect_ratio Aspect ratio which determines the horizontal field of view.
  128. * @param z_near Distance to the near clipping plane.
  129. * @param z_far Distance to the far clipping plane.
  130. *
  131. * @return Perspective projection matrix.
  132. */
  133. template <class T>
  134. [[nodiscard]] matrix<T, 4, 4> perspective_half_z(T vertical_fov, T aspect_ratio, T z_near, T z_far)
  135. {
  136. T half_fov = vertical_fov * T(0.5);
  137. T f = std::cos(half_fov) / std::sin(half_fov);
  138. return
  139. {{
  140. {f / aspect_ratio, T(0), T(0), T(0)},
  141. {T(0), f, T(0), T(0)},
  142. {T(0), T(0), z_far / (z_near - z_far), T(-1)},
  143. {T(0), T(0), -(z_far * z_near) / (z_far - z_near), T(0)}
  144. }};
  145. }
  146. } // namespace math
  147. #endif // ANTKEEPER_MATH_PROJECTION_HPP