Frustum.cpp 5.9 KB

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