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

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