Frustum.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Math/BoundingBox.h"
  24. #include "../Math/Matrix3x4.h"
  25. #include "../Math/Plane.h"
  26. #include "../Math/Rect.h"
  27. #include "../Math/Sphere.h"
  28. namespace Atomic
  29. {
  30. /// Frustum planes.
  31. enum FrustumPlane
  32. {
  33. PLANE_NEAR = 0,
  34. PLANE_LEFT,
  35. PLANE_RIGHT,
  36. PLANE_UP,
  37. PLANE_DOWN,
  38. PLANE_FAR,
  39. };
  40. static const unsigned NUM_FRUSTUM_PLANES = 6;
  41. static const unsigned NUM_FRUSTUM_VERTICES = 8;
  42. /// Convex constructed of 6 planes.
  43. class ATOMIC_API Frustum
  44. {
  45. public:
  46. /// Construct a degenerate frustum with all points at origin.
  47. Frustum();
  48. /// Copy-construct from another frustum.
  49. Frustum(const Frustum& frustum);
  50. /// Assign from another frustum.
  51. Frustum& operator =(const Frustum& rhs);
  52. /// Define with projection parameters and a transform matrix.
  53. void
  54. Define(float fov, float aspectRatio, float zoom, float nearZ, float farZ, const Matrix3x4& transform = Matrix3x4::IDENTITY);
  55. /// Define with near and far dimension vectors and a transform matrix.
  56. void Define(const Vector3& near, const Vector3& far, const Matrix3x4& transform = Matrix3x4::IDENTITY);
  57. /// Define with a bounding box and a transform matrix.
  58. void Define(const BoundingBox& box, const Matrix3x4& transform = Matrix3x4::IDENTITY);
  59. /// Define with orthographic projection parameters and a transform matrix.
  60. void DefineOrtho
  61. (float orthoSize, float aspectRatio, float zoom, float nearZ, float farZ, const Matrix3x4& transform = Matrix3x4::IDENTITY);
  62. /// Transform by a 3x3 matrix.
  63. void Transform(const Matrix3& transform);
  64. /// Transform by a 3x4 matrix.
  65. void Transform(const Matrix3x4& transform);
  66. /// Test if a point is inside or outside.
  67. Intersection IsInside(const Vector3& point) const
  68. {
  69. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  70. {
  71. if (planes_[i].Distance(point) < 0.0f)
  72. return OUTSIDE;
  73. }
  74. return INSIDE;
  75. }
  76. /// Test if a sphere is inside, outside or intersects.
  77. Intersection IsInside(const Sphere& sphere) const
  78. {
  79. bool allInside = true;
  80. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  81. {
  82. float dist = planes_[i].Distance(sphere.center_);
  83. if (dist < -sphere.radius_)
  84. return OUTSIDE;
  85. else if (dist < sphere.radius_)
  86. allInside = false;
  87. }
  88. return allInside ? INSIDE : INTERSECTS;
  89. }
  90. /// Test if a sphere if (partially) inside or outside.
  91. Intersection IsInsideFast(const Sphere& sphere) const
  92. {
  93. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  94. {
  95. if (planes_[i].Distance(sphere.center_) < -sphere.radius_)
  96. return OUTSIDE;
  97. }
  98. return INSIDE;
  99. }
  100. /// Test if a bounding box is inside, outside or intersects.
  101. Intersection IsInside(const BoundingBox& box) const
  102. {
  103. Vector3 center = box.Center();
  104. Vector3 edge = center - box.min_;
  105. bool allInside = true;
  106. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  107. {
  108. const Plane& plane = planes_[i];
  109. float dist = plane.normal_.DotProduct(center) + plane.d_;
  110. float absDist = plane.absNormal_.DotProduct(edge);
  111. if (dist < -absDist)
  112. return OUTSIDE;
  113. else if (dist < absDist)
  114. allInside = false;
  115. }
  116. return allInside ? INSIDE : INTERSECTS;
  117. }
  118. /// Test if a bounding box is (partially) inside or outside.
  119. Intersection IsInsideFast(const BoundingBox& box) const
  120. {
  121. Vector3 center = box.Center();
  122. Vector3 edge = center - box.min_;
  123. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  124. {
  125. const Plane& plane = planes_[i];
  126. float dist = plane.normal_.DotProduct(center) + plane.d_;
  127. float absDist = plane.absNormal_.DotProduct(edge);
  128. if (dist < -absDist)
  129. return OUTSIDE;
  130. }
  131. return INSIDE;
  132. }
  133. /// Return distance of a point to the frustum, or 0 if inside.
  134. float Distance(const Vector3& point) const
  135. {
  136. float distance = 0.0f;
  137. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  138. distance = Max(-planes_[i].Distance(point), distance);
  139. return distance;
  140. }
  141. /// Return transformed by a 3x3 matrix.
  142. Frustum Transformed(const Matrix3& transform) const;
  143. /// Return transformed by a 3x4 matrix.
  144. Frustum Transformed(const Matrix3x4& transform) const;
  145. /// Return projected by a 4x4 projection matrix.
  146. Rect Projected(const Matrix4& transform) const;
  147. /// Update the planes. Called internally.
  148. void UpdatePlanes();
  149. /// Frustum planes.
  150. Plane planes_[NUM_FRUSTUM_PLANES];
  151. /// Frustum vertices.
  152. Vector3 vertices_[NUM_FRUSTUM_VERTICES];
  153. };
  154. }