Frustum.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #include "Frustum.h"
  24. #include "Types.h"
  25. #include "Intersection.h"
  26. #include "Mat4.h"
  27. namespace crown
  28. {
  29. //-----------------------------------------------------------------------------
  30. Frustum::Frustum()
  31. {
  32. }
  33. //-----------------------------------------------------------------------------
  34. Frustum::Frustum(const Frustum& frustum)
  35. {
  36. m_planes[FP_LEFT] = frustum.m_planes[FP_LEFT];
  37. m_planes[FP_RIGHT] = frustum.m_planes[FP_RIGHT];
  38. m_planes[FP_BOTTOM] = frustum.m_planes[FP_BOTTOM];
  39. m_planes[FP_TOP] = frustum.m_planes[FP_TOP];
  40. m_planes[FP_NEAR] = frustum.m_planes[FP_NEAR];
  41. m_planes[FP_FAR] = frustum.m_planes[FP_FAR];
  42. }
  43. //-----------------------------------------------------------------------------
  44. bool Frustum::contains_point(const Vec3& point) const
  45. {
  46. if (m_planes[FP_LEFT].distance_to_point(point) < 0.0) return false;
  47. if (m_planes[FP_RIGHT].distance_to_point(point) < 0.0) return false;
  48. if (m_planes[FP_BOTTOM].distance_to_point(point) < 0.0) return false;
  49. if (m_planes[FP_TOP].distance_to_point(point) < 0.0) return false;
  50. if (m_planes[FP_NEAR].distance_to_point(point) < 0.0) return false;
  51. if (m_planes[FP_FAR].distance_to_point(point) < 0.0) return false;
  52. return true;
  53. }
  54. //-----------------------------------------------------------------------------
  55. Vec3 Frustum::vertex(uint32_t index) const
  56. {
  57. CE_ASSERT(index < 8, "Index must be < 8");
  58. // 0 = Near bottom left
  59. // 1 = Near bottom right
  60. // 2 = Near top right
  61. // 3 = Near top left
  62. // 4 = Far bottom left
  63. // 5 = Far bottom right
  64. // 6 = Far top right
  65. // 7 = Far top left
  66. Vec3 ip;
  67. switch (index)
  68. {
  69. case 0:
  70. return Intersection::test_plane_3(m_planes[4], m_planes[0], m_planes[2], ip);
  71. case 1:
  72. return Intersection::test_plane_3(m_planes[4], m_planes[1], m_planes[2], ip);
  73. case 2:
  74. return Intersection::test_plane_3(m_planes[4], m_planes[1], m_planes[3], ip);
  75. case 3:
  76. return Intersection::test_plane_3(m_planes[4], m_planes[0], m_planes[3], ip);
  77. case 4:
  78. return Intersection::test_plane_3(m_planes[5], m_planes[0], m_planes[2], ip);
  79. case 5:
  80. return Intersection::test_plane_3(m_planes[5], m_planes[1], m_planes[2], ip);
  81. case 6:
  82. return Intersection::test_plane_3(m_planes[5], m_planes[1], m_planes[3], ip);
  83. case 7:
  84. return Intersection::test_plane_3(m_planes[5], m_planes[0], m_planes[3], ip);
  85. default:
  86. break;
  87. }
  88. return ip;
  89. }
  90. //-----------------------------------------------------------------------------
  91. void Frustum::from_matrix(const Mat4& m)
  92. {
  93. // Left plane
  94. m_planes[FP_LEFT].n.x = m.m[3] + m.m[0];
  95. m_planes[FP_LEFT].n.y = m.m[7] + m.m[4];
  96. m_planes[FP_LEFT].n.z = m.m[11] + m.m[8];
  97. m_planes[FP_LEFT].d = m.m[15] + m.m[12];
  98. // Right plane
  99. m_planes[FP_RIGHT].n.x = m.m[3] - m.m[0];
  100. m_planes[FP_RIGHT].n.y = m.m[7] - m.m[4];
  101. m_planes[FP_RIGHT].n.z = m.m[11] - m.m[8];
  102. m_planes[FP_RIGHT].d = m.m[15] - m.m[12];
  103. // Bottom plane
  104. m_planes[FP_BOTTOM].n.x = m.m[3] + m.m[1];
  105. m_planes[FP_BOTTOM].n.y = m.m[7] + m.m[5];
  106. m_planes[FP_BOTTOM].n.z = m.m[11] + m.m[9];
  107. m_planes[FP_BOTTOM].d = m.m[15] + m.m[13];
  108. // Top plane
  109. m_planes[FP_TOP].n.x = m.m[3] - m.m[1];
  110. m_planes[FP_TOP].n.y = m.m[7] - m.m[5];
  111. m_planes[FP_TOP].n.z = m.m[11] - m.m[9];
  112. m_planes[FP_TOP].d = m.m[15] - m.m[13];
  113. // Near plane
  114. m_planes[FP_NEAR].n.x = m.m[3] + m.m[2];
  115. m_planes[FP_NEAR].n.y = m.m[7] + m.m[6];
  116. m_planes[FP_NEAR].n.z = m.m[11] + m.m[10];
  117. m_planes[FP_NEAR].d = m.m[15] + m.m[14];
  118. // Far plane
  119. m_planes[FP_FAR].n.x = m.m[3] - m.m[2];
  120. m_planes[FP_FAR].n.y = m.m[7] - m.m[6];
  121. m_planes[FP_FAR].n.z = m.m[11] - m.m[10];
  122. m_planes[FP_FAR].d = m.m[15] - m.m[14];
  123. m_planes[FP_LEFT].normalize();
  124. m_planes[FP_RIGHT].normalize();
  125. m_planes[FP_BOTTOM].normalize();
  126. m_planes[FP_TOP].normalize();
  127. m_planes[FP_NEAR].normalize();
  128. m_planes[FP_FAR].normalize();
  129. }
  130. //-----------------------------------------------------------------------------
  131. Box Frustum::to_box() const
  132. {
  133. Box tmp;
  134. tmp.zero();
  135. Vec3 vertices[8];
  136. vertices[0] = vertex(0);
  137. vertices[1] = vertex(1);
  138. vertices[2] = vertex(2);
  139. vertices[3] = vertex(3);
  140. vertices[4] = vertex(4);
  141. vertices[5] = vertex(5);
  142. vertices[6] = vertex(6);
  143. vertices[7] = vertex(7);
  144. tmp.add_points(vertices, 8);
  145. return tmp;
  146. }
  147. } // namespace crown