Frustum.h 6.1 KB

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