Frustum.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "Base.h"
  2. #include "Frustum.h"
  3. #include "BoundingSphere.h"
  4. #include "BoundingBox.h"
  5. namespace gameplay
  6. {
  7. Frustum::Frustum()
  8. {
  9. set(Matrix::identity());
  10. }
  11. Frustum::Frustum(const Matrix& matrix)
  12. {
  13. set(matrix);
  14. }
  15. Frustum::Frustum(const Frustum& frustum)
  16. {
  17. set(frustum);
  18. }
  19. Frustum::~Frustum()
  20. {
  21. }
  22. const Plane& Frustum::getNear() const
  23. {
  24. return _near;
  25. }
  26. const Plane& Frustum::getFar() const
  27. {
  28. return _far;
  29. }
  30. const Plane& Frustum::getLeft() const
  31. {
  32. return _left;
  33. }
  34. const Plane& Frustum::getRight() const
  35. {
  36. return _right;
  37. }
  38. const Plane& Frustum::getBottom() const
  39. {
  40. return _bottom;
  41. }
  42. const Plane& Frustum::getTop() const
  43. {
  44. return _top;
  45. }
  46. void Frustum::getMatrix(Matrix* dst) const
  47. {
  48. GP_ASSERT(dst);
  49. dst->set(_matrix);
  50. }
  51. void Frustum::getCorners(Vector3* corners) const
  52. {
  53. getNearCorners(corners);
  54. getFarCorners(corners + 4);
  55. }
  56. void Frustum::getNearCorners(Vector3* corners) const
  57. {
  58. GP_ASSERT(corners);
  59. Plane::intersection(_near, _left, _top, &corners[0]);
  60. Plane::intersection(_near, _left, _bottom, &corners[1]);
  61. Plane::intersection(_near, _right, _bottom, &corners[2]);
  62. Plane::intersection(_near, _right, _top, &corners[3]);
  63. }
  64. void Frustum::getFarCorners(Vector3* corners) const
  65. {
  66. GP_ASSERT(corners);
  67. Plane::intersection(_far, _right, _top, &corners[0]);
  68. Plane::intersection(_far, _right, _bottom, &corners[1]);
  69. Plane::intersection(_far, _left, _bottom, &corners[2]);
  70. Plane::intersection(_far, _left, _top, &corners[3]);
  71. }
  72. bool Frustum::intersects(const Vector3& point) const
  73. {
  74. if (_near.distance(point) <= 0)
  75. return false;
  76. if (_far.distance(point) <= 0)
  77. return false;
  78. if (_left.distance(point) <= 0)
  79. return false;
  80. if (_right.distance(point) <= 0)
  81. return false;
  82. if (_top.distance(point) <= 0)
  83. return false;
  84. if (_bottom.distance(point) <= 0)
  85. return false;
  86. return true;
  87. }
  88. bool Frustum::intersects(float x, float y, float z) const
  89. {
  90. return intersects(Vector3(x, y, z));
  91. }
  92. bool Frustum::intersects(const BoundingSphere& sphere) const
  93. {
  94. return sphere.intersects(*this);
  95. }
  96. bool Frustum::intersects(const BoundingBox& box) const
  97. {
  98. return box.intersects(*this);
  99. }
  100. float Frustum::intersects(const Plane& plane) const
  101. {
  102. return plane.intersects(*this);
  103. }
  104. float Frustum::intersects(const Ray& ray) const
  105. {
  106. return ray.intersects(*this);
  107. }
  108. void Frustum::set(const Frustum& frustum)
  109. {
  110. _near = frustum._near;
  111. _far = frustum._far;
  112. _bottom = frustum._bottom;
  113. _top = frustum._top;
  114. _left = frustum._left;
  115. _right = frustum._right;
  116. _matrix.set(frustum._matrix);
  117. }
  118. void Frustum::updatePlanes()
  119. {
  120. _near.set(Vector3(_matrix.m[3] + _matrix.m[2], _matrix.m[7] + _matrix.m[6], _matrix.m[11] + _matrix.m[10]), _matrix.m[15] + _matrix.m[14]);
  121. _far.set(Vector3(_matrix.m[3] - _matrix.m[2], _matrix.m[7] - _matrix.m[6], _matrix.m[11] - _matrix.m[10]), _matrix.m[15] - _matrix.m[14]);
  122. _bottom.set(Vector3(_matrix.m[3] + _matrix.m[1], _matrix.m[7] + _matrix.m[5], _matrix.m[11] + _matrix.m[9]), _matrix.m[15] + _matrix.m[13]);
  123. _top.set(Vector3(_matrix.m[3] - _matrix.m[1], _matrix.m[7] - _matrix.m[5], _matrix.m[11] - _matrix.m[9]), _matrix.m[15] - _matrix.m[13]);
  124. _left.set(Vector3(_matrix.m[3] + _matrix.m[0], _matrix.m[7] + _matrix.m[4], _matrix.m[11] + _matrix.m[8]), _matrix.m[15] + _matrix.m[12]);
  125. _right.set(Vector3(_matrix.m[3] - _matrix.m[0], _matrix.m[7] - _matrix.m[4], _matrix.m[11] - _matrix.m[8]), _matrix.m[15] - _matrix.m[12]);
  126. }
  127. void Frustum::set(const Matrix& matrix)
  128. {
  129. _matrix.set(matrix);
  130. // Update the planes.
  131. updatePlanes();
  132. }
  133. }