Frustum.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // Copyright (c) 2008-2020 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. /// \file
  23. #pragma once
  24. #include "../Math/BoundingBox.h"
  25. #include "../Math/Matrix3x4.h"
  26. #include "../Math/Plane.h"
  27. #include "../Math/Rect.h"
  28. #include "../Math/Sphere.h"
  29. namespace Urho3D
  30. {
  31. /// Frustum planes.
  32. enum FrustumPlane
  33. {
  34. PLANE_NEAR = 0,
  35. PLANE_LEFT,
  36. PLANE_RIGHT,
  37. PLANE_UP,
  38. PLANE_DOWN,
  39. PLANE_FAR,
  40. };
  41. static const unsigned NUM_FRUSTUM_PLANES = 6;
  42. static const unsigned NUM_FRUSTUM_VERTICES = 8;
  43. /// Convex constructed of 6 planes.
  44. class URHO3D_API Frustum
  45. {
  46. public:
  47. /// Construct a degenerate frustum with all points at origin.
  48. Frustum() noexcept = default;
  49. /// Copy-construct from another frustum.
  50. Frustum(const Frustum& frustum) noexcept;
  51. /// Assign from another frustum.
  52. Frustum& operator =(const Frustum& rhs) noexcept;
  53. /// Define with projection parameters and a transform matrix.
  54. void
  55. Define(float fov, float aspectRatio, float zoom, float nearZ, float farZ, const Matrix3x4& transform = Matrix3x4::IDENTITY);
  56. /// Define with near and far dimension vectors and a transform matrix.
  57. void Define(const Vector3& near, const Vector3& far, const Matrix3x4& transform = Matrix3x4::IDENTITY);
  58. /// Define with a bounding box and a transform matrix.
  59. void Define(const BoundingBox& box, const Matrix3x4& transform = Matrix3x4::IDENTITY);
  60. /// Define from a projection or view-projection matrix.
  61. void Define(const Matrix4& projection);
  62. /// Define with orthographic projection parameters and a transform matrix.
  63. void DefineOrtho
  64. (float orthoSize, float aspectRatio, float zoom, float nearZ, float farZ, const Matrix3x4& transform = Matrix3x4::IDENTITY);
  65. /// Define a split (limited) frustum from a projection matrix, with near & far distances specified.
  66. void DefineSplit(const Matrix4& projection, float near, float far);
  67. /// Transform by a 3x3 matrix.
  68. void Transform(const Matrix3& transform);
  69. /// Transform by a 3x4 matrix.
  70. void Transform(const Matrix3x4& transform);
  71. /// Test if a point is inside or outside.
  72. Intersection IsInside(const Vector3& point) const
  73. {
  74. for (const auto& plane : planes_)
  75. {
  76. if (plane.Distance(point) < 0.0f)
  77. return OUTSIDE;
  78. }
  79. return INSIDE;
  80. }
  81. /// Test if a sphere is inside, outside or intersects.
  82. Intersection IsInside(const Sphere& sphere) const
  83. {
  84. bool allInside = true;
  85. for (const auto& plane : planes_)
  86. {
  87. float dist = plane.Distance(sphere.center_);
  88. if (dist < -sphere.radius_)
  89. return OUTSIDE;
  90. else if (dist < sphere.radius_)
  91. allInside = false;
  92. }
  93. return allInside ? INSIDE : INTERSECTS;
  94. }
  95. /// Test if a sphere if (partially) inside or outside.
  96. Intersection IsInsideFast(const Sphere& sphere) const
  97. {
  98. for (const auto& plane : planes_)
  99. {
  100. if (plane.Distance(sphere.center_) < -sphere.radius_)
  101. return OUTSIDE;
  102. }
  103. return INSIDE;
  104. }
  105. /// Test if a bounding box is inside, outside or intersects.
  106. Intersection IsInside(const BoundingBox& box) const
  107. {
  108. Vector3 center = box.Center();
  109. Vector3 edge = center - box.min_;
  110. bool allInside = true;
  111. for (const auto& plane : planes_)
  112. {
  113. float dist = plane.normal_.DotProduct(center) + plane.d_;
  114. float absDist = plane.absNormal_.DotProduct(edge);
  115. if (dist < -absDist)
  116. return OUTSIDE;
  117. else if (dist < absDist)
  118. allInside = false;
  119. }
  120. return allInside ? INSIDE : INTERSECTS;
  121. }
  122. /// Test if a bounding box is (partially) inside or outside.
  123. Intersection IsInsideFast(const BoundingBox& box) const
  124. {
  125. Vector3 center = box.Center();
  126. Vector3 edge = center - box.min_;
  127. for (const auto& plane : planes_)
  128. {
  129. float dist = plane.normal_.DotProduct(center) + plane.d_;
  130. float absDist = plane.absNormal_.DotProduct(edge);
  131. if (dist < -absDist)
  132. return OUTSIDE;
  133. }
  134. return INSIDE;
  135. }
  136. /// Return distance of a point to the frustum, or 0 if inside.
  137. float Distance(const Vector3& point) const
  138. {
  139. float distance = 0.0f;
  140. for (const auto& plane : planes_)
  141. distance = Max(-plane.Distance(point), distance);
  142. return distance;
  143. }
  144. /// Return transformed by a 3x3 matrix.
  145. Frustum Transformed(const Matrix3& transform) const;
  146. /// Return transformed by a 3x4 matrix.
  147. Frustum Transformed(const Matrix3x4& transform) const;
  148. /// Return projected by a 4x4 projection matrix.
  149. Rect Projected(const Matrix4& projection) const;
  150. /// Update the planes. Called internally.
  151. void UpdatePlanes();
  152. /// Frustum planes.
  153. Plane planes_[NUM_FRUSTUM_PLANES];
  154. /// Frustum vertices.
  155. Vector3 vertices_[NUM_FRUSTUM_VERTICES];
  156. };
  157. }