Frustum.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 orthographic projection parameters and a transform matrix.
  56. void DefineOrtho(float orthoSize, float aspectRatio, float zoom, float nearZ, float farZ, const Matrix3x4& transform = Matrix3x4::IDENTITY);
  57. /// Transform by a 3x3 matrix.
  58. void Transform(const Matrix3& transform);
  59. /// Transform by a 3x4 matrix.
  60. void Transform(const Matrix3x4& transform);
  61. /// Test if a point is inside or outside.
  62. Intersection IsInside(const Vector3& point) const
  63. {
  64. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  65. {
  66. if (planes_[i].Distance(point) < 0.0f)
  67. return OUTSIDE;
  68. }
  69. return INSIDE;
  70. }
  71. /// Test if a sphere is inside, outside or intersects.
  72. Intersection IsInside(const Sphere& sphere) const
  73. {
  74. bool allInside = true;
  75. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  76. {
  77. float dist = planes_[i].Distance(sphere.center_);
  78. if (dist < -sphere.radius_)
  79. return OUTSIDE;
  80. else if (dist < sphere.radius_)
  81. allInside = false;
  82. }
  83. return allInside ? INSIDE : INTERSECTS;
  84. }
  85. /// Test if a sphere if (partially) inside or outside.
  86. Intersection IsInsideFast(const Sphere& sphere) const
  87. {
  88. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  89. {
  90. if (planes_[i].Distance(sphere.center_) < -sphere.radius_)
  91. return OUTSIDE;
  92. }
  93. return INSIDE;
  94. }
  95. /// Test if a bounding box is inside, outside or intersects.
  96. Intersection IsInside(const BoundingBox& box) const
  97. {
  98. Vector3 center = box.Center();
  99. Vector3 edge = center - box.min_;
  100. bool allInside = true;
  101. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  102. {
  103. const Plane& plane = planes_[i];
  104. float dist = plane.normal_.DotProduct(center) - plane.intercept_;
  105. float absDist = plane.absNormal_.DotProduct(edge);
  106. if (dist < -absDist)
  107. return OUTSIDE;
  108. else if (dist < absDist)
  109. allInside = false;
  110. }
  111. return allInside ? INSIDE : INTERSECTS;
  112. }
  113. /// Test if a bounding box is (partially) inside or outside.
  114. Intersection IsInsideFast(const BoundingBox& box) const
  115. {
  116. Vector3 center = box.Center();
  117. Vector3 edge = center - box.min_;
  118. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  119. {
  120. const Plane& plane = planes_[i];
  121. float dist = plane.normal_.DotProduct(center) - plane.intercept_;
  122. float absDist = plane.absNormal_.DotProduct(edge);
  123. if (dist < -absDist)
  124. return OUTSIDE;
  125. }
  126. return INSIDE;
  127. }
  128. /// Return distance of a point to the frustum, or 0 if inside.
  129. float Distance(const Vector3& point) const
  130. {
  131. float distance = 0.0f;
  132. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  133. distance = Max(-planes_[i].Distance(point), distance);
  134. return distance;
  135. }
  136. /// Return transformed by a 3x3 matrix.
  137. Frustum Transformed(const Matrix3& transform) const;
  138. /// Return transformed by a 3x4 matrix.
  139. Frustum Transformed(const Matrix3x4& transform) const;
  140. /// Return projected by a 4x4 projection matrix.
  141. Rect Projected(const Matrix4& transform) const;
  142. /// Update the planes. Called internally.
  143. void UpdatePlanes();
  144. /// Frustum planes.
  145. Plane planes_[NUM_FRUSTUM_PLANES];
  146. /// Frustum vertices.
  147. Vector3 vertices_[NUM_FRUSTUM_VERTICES];
  148. /// Defined flag.
  149. bool defined_;
  150. };