frustum.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "types.h"
  25. #include "math_types.h"
  26. #include "vector3.h"
  27. #include "plane.h"
  28. #include "intersection.h"
  29. namespace crown
  30. {
  31. /// Functions to manipulate Frustum.
  32. ///
  33. /// @ingroup Math
  34. namespace frustum
  35. {
  36. /// Returns whether the frustum @a f contains the point @a p.
  37. bool contains_point(const Frustum& f, const Vector3& p);
  38. /// Returns the corner @a index of the frustum @a f.
  39. /// @note
  40. /// Index to corner table:
  41. /// 0 = Near bottom left
  42. /// 1 = Near bottom right
  43. /// 2 = Near top right
  44. /// 3 = Near top left
  45. /// 4 = Far bottom left
  46. /// 5 = Far bottom right
  47. /// 6 = Far top right
  48. /// 7 = Far top left
  49. Vector3 vertex(const Frustum& f, uint32_t index);
  50. /// Builds the frustum @a f from the view matrix @a m.
  51. void from_matrix(Frustum& f, const Matrix4x4& m);
  52. /// Returns the AABB enclosing the frustum @a f.
  53. AABB to_aabb(const Frustum& f);
  54. } // namespace frustum
  55. namespace frustum
  56. {
  57. //-----------------------------------------------------------------------------
  58. inline bool contains_point(const Frustum& f, const Vector3& p)
  59. {
  60. if (plane::distance_to_point(f.left, p) < 0.0) return false;
  61. if (plane::distance_to_point(f.right, p) < 0.0) return false;
  62. if (plane::distance_to_point(f.bottom, p) < 0.0) return false;
  63. if (plane::distance_to_point(f.top, p) < 0.0) return false;
  64. if (plane::distance_to_point(f.near, p) < 0.0) return false;
  65. if (plane::distance_to_point(f.far, p) < 0.0) return false;
  66. return true;
  67. }
  68. //-----------------------------------------------------------------------------
  69. inline Vector3 vertex(const Frustum& f, uint32_t index)
  70. {
  71. CE_ASSERT(index < 8, "Index must be < 8");
  72. // 0 = Near bottom left
  73. // 1 = Near bottom right
  74. // 2 = Near top right
  75. // 3 = Near top left
  76. // 4 = Far bottom left
  77. // 5 = Far bottom right
  78. // 6 = Far top right
  79. // 7 = Far top left
  80. const Plane* side = &f.left;
  81. Vector3 ip;
  82. switch (index)
  83. {
  84. case 0: return Intersection::test_plane_3(side[4], side[0], side[2], ip);
  85. case 1: return Intersection::test_plane_3(side[4], side[1], side[2], ip);
  86. case 2: return Intersection::test_plane_3(side[4], side[1], side[3], ip);
  87. case 3: return Intersection::test_plane_3(side[4], side[0], side[3], ip);
  88. case 4: return Intersection::test_plane_3(side[5], side[0], side[2], ip);
  89. case 5: return Intersection::test_plane_3(side[5], side[1], side[2], ip);
  90. case 6: return Intersection::test_plane_3(side[5], side[1], side[3], ip);
  91. case 7: return Intersection::test_plane_3(side[5], side[0], side[3], ip);
  92. default: break;
  93. }
  94. return ip;
  95. }
  96. //-----------------------------------------------------------------------------
  97. inline void from_matrix(Frustum& f, const Matrix4x4& m)
  98. {
  99. f.left.n.x = m[3] + m[0];
  100. f.left.n.y = m[7] + m[4];
  101. f.left.n.z = m[11] + m[8];
  102. f.left.d = m[15] + m[12];
  103. f.right.n.x = m[3] - m[0];
  104. f.right.n.y = m[7] - m[4];
  105. f.right.n.z = m[11] - m[8];
  106. f.right.d = m[15] - m[12];
  107. f.bottom.n.x = m[3] + m[1];
  108. f.bottom.n.y = m[7] + m[5];
  109. f.bottom.n.z = m[11] + m[9];
  110. f.bottom.d = m[15] + m[13];
  111. f.top.n.x = m[3] - m[1];
  112. f.top.n.y = m[7] - m[5];
  113. f.top.n.z = m[11] - m[9];
  114. f.top.d = m[15] - m[13];
  115. f.near.n.x = m[3] + m[2];
  116. f.near.n.y = m[7] + m[6];
  117. f.near.n.z = m[11] + m[10];
  118. f.near.d = m[15] + m[14];
  119. f.far.n.x = m[3] - m[2];
  120. f.far.n.y = m[7] - m[6];
  121. f.far.n.z = m[11] - m[10];
  122. f.far.d = m[15] - m[14];
  123. plane::normalize(f.left);
  124. plane::normalize(f.right);
  125. plane::normalize(f.bottom);
  126. plane::normalize(f.top);
  127. plane::normalize(f.near);
  128. plane::normalize(f.far);
  129. }
  130. //-----------------------------------------------------------------------------
  131. inline AABB to_aabb(const Frustum& f)
  132. {
  133. AABB tmp;
  134. aabb::reset(tmp);
  135. Vector3 vertices[8];
  136. vertices[0] = vertex(f, 0);
  137. vertices[1] = vertex(f, 1);
  138. vertices[2] = vertex(f, 2);
  139. vertices[3] = vertex(f, 3);
  140. vertices[4] = vertex(f, 4);
  141. vertices[5] = vertex(f, 5);
  142. vertices[6] = vertex(f, 6);
  143. vertices[7] = vertex(f, 7);
  144. aabb::add_points(tmp, 8, vertices);
  145. return tmp;
  146. }
  147. } // namespace frustum
  148. //-----------------------------------------------------------------------------
  149. inline Frustum::Frustum()
  150. {
  151. // Do not initialize
  152. }
  153. } // namespace crown