Frustum.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // Copyright (c) 2008-2017 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 from a projection or view-projection matrix.
  60. void Define(const Matrix4& projection);
  61. /// Define with orthographic projection parameters and a transform matrix.
  62. void DefineOrtho
  63. (float orthoSize, float aspectRatio, float zoom, float nearZ, float farZ, const Matrix3x4& transform = Matrix3x4::IDENTITY);
  64. /// Define a split (limited) frustum from a projection matrix, with near & far distances specified.
  65. void DefineSplit(const Matrix4& projection, float near, float far);
  66. /// Transform by a 3x3 matrix.
  67. void Transform(const Matrix3& transform);
  68. /// Transform by a 3x4 matrix.
  69. void Transform(const Matrix3x4& transform);
  70. /// Test if a point is inside or outside.
  71. Intersection IsInside(const Vector3& point) const
  72. {
  73. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  74. {
  75. if (planes_[i].Distance(point) < 0.0f)
  76. return OUTSIDE;
  77. }
  78. return INSIDE;
  79. }
  80. /// Test if a sphere is inside, outside or intersects.
  81. Intersection IsInside(const Sphere& sphere) const
  82. {
  83. bool allInside = true;
  84. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  85. {
  86. float dist = planes_[i].Distance(sphere.center_);
  87. if (dist < -sphere.radius_)
  88. return OUTSIDE;
  89. else if (dist < sphere.radius_)
  90. allInside = false;
  91. }
  92. return allInside ? INSIDE : INTERSECTS;
  93. }
  94. /// Test if a sphere if (partially) inside or outside.
  95. Intersection IsInsideFast(const Sphere& sphere) const
  96. {
  97. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  98. {
  99. if (planes_[i].Distance(sphere.center_) < -sphere.radius_)
  100. return OUTSIDE;
  101. }
  102. return INSIDE;
  103. }
  104. /// Test if a bounding box is inside, outside or intersects.
  105. Intersection IsInside(const BoundingBox& box) const
  106. {
  107. Vector3 center = box.Center();
  108. Vector3 edge = center - box.min_;
  109. bool allInside = true;
  110. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  111. {
  112. const Plane& plane = planes_[i];
  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 (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  128. {
  129. const Plane& plane = planes_[i];
  130. float dist = plane.normal_.DotProduct(center) + plane.d_;
  131. float absDist = plane.absNormal_.DotProduct(edge);
  132. if (dist < -absDist)
  133. return OUTSIDE;
  134. }
  135. return INSIDE;
  136. }
  137. /// Return distance of a point to the frustum, or 0 if inside.
  138. float Distance(const Vector3& point) const
  139. {
  140. float distance = 0.0f;
  141. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  142. distance = Max(-planes_[i].Distance(point), distance);
  143. return distance;
  144. }
  145. /// Return transformed by a 3x3 matrix.
  146. Frustum Transformed(const Matrix3& transform) const;
  147. /// Return transformed by a 3x4 matrix.
  148. Frustum Transformed(const Matrix3x4& transform) const;
  149. /// Return projected by a 4x4 projection matrix.
  150. Rect Projected(const Matrix4& transform) const;
  151. /// Update the planes. Called internally.
  152. void UpdatePlanes();
  153. /// Frustum planes.
  154. Plane planes_[NUM_FRUSTUM_PLANES];
  155. /// Frustum vertices.
  156. Vector3 vertices_[NUM_FRUSTUM_VERTICES];
  157. // ATOMIC BEGIN
  158. /// Construct from a float array for bindings
  159. explicit Frustum(const float* data)
  160. {
  161. // unpack planes
  162. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  163. {
  164. planes_[i].normal_ = Vector3(data);
  165. data += 3;
  166. planes_[i].absNormal_ = Vector3(data);
  167. data += 3;
  168. planes_[i].d_ = *data;
  169. data++;
  170. }
  171. // unpack vertices
  172. for (unsigned i = 0; i < NUM_FRUSTUM_VERTICES; ++i)
  173. {
  174. vertices_[i] = Vector3(data);
  175. data += 3;
  176. }
  177. }
  178. /// Return float data for bindings
  179. const float* Data() const { return (float*) &planes_; }
  180. // ATOMIC END
  181. };
  182. }