💿🐜 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.

188 lines
5.4 KiB

  1. /*
  2. * Copyright (C) 2021 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_GEOM_VIEW_FRUSTUM_HPP
  20. #define ANTKEEPER_GEOM_VIEW_FRUSTUM_HPP
  21. #include "geom/convex-hull.hpp"
  22. #include "math/math.hpp"
  23. #include <array>
  24. namespace geom {
  25. /**
  26. * View frustum.
  27. */
  28. template <class T>
  29. class view_frustum
  30. {
  31. public:
  32. typedef math::vector<T, 3> vector_type;
  33. typedef math::matrix<T, 4, 4> matrix_type;
  34. typedef plane<T> plane_type;
  35. /**
  36. * Creates a view frustum from a view-projection matrix.
  37. *
  38. * @param view_projection View-projection matrix.
  39. */
  40. view_frustum(const matrix_type& view_projection);
  41. /// Creates an uninitialized view frustum.
  42. view_frustum();
  43. /**
  44. * Recalculates the view frustum from a view-projection matrix.
  45. *
  46. * @param view_projection View-projection matrix.
  47. */
  48. void set_matrix(const matrix_type& view_projection);
  49. /// Returns a convex hull which describes the bounds of the view frustum.
  50. const convex_hull<T>& get_bounds() const;
  51. /// Returns the left clipping plane.
  52. const plane_type& get_left() const;
  53. /// Returns the right clipping plane.
  54. const plane_type& get_right() const;
  55. /// Returns the bottom clipping plane.
  56. const plane_type& get_bottom() const;
  57. /// Returns the top clipping plane.
  58. const plane_type& get_top() const;
  59. /// Returns the near clipping plane.
  60. const plane_type& get_near() const;
  61. /// Returns the far clipping plane.
  62. const plane_type& get_far() const;
  63. /**
  64. * Returns an array containing the corners of the view frustum bounds.
  65. *
  66. * @return Array containing the corners of the view frustum bounds. Corners are stored in the following order: NTL, NTR, NBL, NBR, FTL, FTR, FBL, FBR; where N is near, F is far, T is top, B is bottom, L is left, and R is right, therefore NTL refers to the corner shared between the near, top, and left clipping planes.
  67. */
  68. const std::array<vector_type, 8>& get_corners() const;
  69. private:
  70. void recalculate_planes(const matrix_type& view_projection);
  71. void recalculate_corners();
  72. convex_hull<T> bounds;
  73. std::array<vector_type, 8> corners;
  74. };
  75. template <class T>
  76. view_frustum<T>::view_frustum(const matrix_type& view_projection):
  77. bounds(6)
  78. {
  79. set_matrix(view_projection);
  80. }
  81. template <class T>
  82. view_frustum<T>::view_frustum():
  83. view_frustum(math::identity4x4<T>)
  84. {}
  85. template <class T>
  86. void view_frustum<T>::set_matrix(const matrix_type& view_projection)
  87. {
  88. recalculate_planes(view_projection);
  89. recalculate_corners();
  90. }
  91. template <class T>
  92. inline const convex_hull<T>& view_frustum<T>::get_bounds() const
  93. {
  94. return bounds;
  95. }
  96. template <class T>
  97. inline const typename view_frustum<T>::plane_type& view_frustum<T>::get_left() const
  98. {
  99. return bounds.planes[0];
  100. }
  101. template <class T>
  102. inline const typename view_frustum<T>::plane_type& view_frustum<T>::get_right() const
  103. {
  104. return bounds.planes[1];
  105. }
  106. template <class T>
  107. inline const typename view_frustum<T>::plane_type& view_frustum<T>::get_bottom() const
  108. {
  109. return bounds.planes[2];
  110. }
  111. template <class T>
  112. inline const typename view_frustum<T>::plane_type& view_frustum<T>::get_top() const
  113. {
  114. return bounds.planes[3];
  115. }
  116. template <class T>
  117. inline const typename view_frustum<T>::plane_type& view_frustum<T>::get_near() const
  118. {
  119. return bounds.planes[4];
  120. }
  121. template <class T>
  122. inline const typename view_frustum<T>::plane_type& view_frustum<T>::get_far() const
  123. {
  124. return bounds.planes[5];
  125. }
  126. template <class T>
  127. inline const std::array<typename view_frustum<T>::vector_type, 8>& view_frustum<T>::get_corners() const
  128. {
  129. return corners;
  130. }
  131. template <class T>
  132. void view_frustum<T>::recalculate_planes(const matrix_type& view_projection)
  133. {
  134. matrix_type transpose = math::transpose(view_projection);
  135. bounds.planes[0] = plane_type(transpose[3] + transpose[0]);
  136. bounds.planes[1] = plane_type(transpose[3] - transpose[0]);
  137. bounds.planes[2] = plane_type(transpose[3] + transpose[1]);
  138. bounds.planes[3] = plane_type(transpose[3] - transpose[1]);
  139. bounds.planes[4] = plane_type(transpose[3] + transpose[2]);
  140. bounds.planes[5] = plane_type(transpose[3] - transpose[2]);
  141. }
  142. template <class T>
  143. void view_frustum<T>::recalculate_corners()
  144. {
  145. corners[0] = plane_type::intersection(get_near(), get_top(), get_left());
  146. corners[1] = plane_type::intersection(get_near(), get_top(), get_right());
  147. corners[2] = plane_type::intersection(get_near(), get_bottom(), get_left());
  148. corners[3] = plane_type::intersection(get_near(), get_bottom(), get_right());
  149. corners[4] = plane_type::intersection(get_far(), get_top(), get_left());
  150. corners[5] = plane_type::intersection(get_far(), get_top(), get_right());
  151. corners[6] = plane_type::intersection(get_far(), get_bottom(), get_left());
  152. corners[7] = plane_type::intersection(get_far(), get_bottom(), get_right());
  153. }
  154. } // namespace geom
  155. #endif // ANTKEEPER_GEOM_VIEW_FRUSTUM_HPP