Frustum.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #include "../Precompiled.h"
  23. #include "../Math/Frustum.h"
  24. #include "../DebugNew.h"
  25. namespace Urho3D
  26. {
  27. inline Vector3 ClipEdgeZ(const Vector3& v0, const Vector3& v1, float clipZ)
  28. {
  29. return Vector3(
  30. v1.x_ + (v0.x_ - v1.x_) * ((clipZ - v1.z_) / (v0.z_ - v1.z_)),
  31. v1.y_ + (v0.y_ - v1.y_) * ((clipZ - v1.z_) / (v0.z_ - v1.z_)),
  32. clipZ
  33. );
  34. }
  35. void ProjectAndMergeEdge(Vector3 v0, Vector3 v1, Rect& rect, const Matrix4& projection)
  36. {
  37. // Check if both vertices behind near plane
  38. if (v0.z_ < M_MIN_NEARCLIP && v1.z_ < M_MIN_NEARCLIP)
  39. return;
  40. // Check if need to clip one of the vertices
  41. if (v1.z_ < M_MIN_NEARCLIP)
  42. v1 = ClipEdgeZ(v1, v0, M_MIN_NEARCLIP);
  43. else if (v0.z_ < M_MIN_NEARCLIP)
  44. v0 = ClipEdgeZ(v0, v1, M_MIN_NEARCLIP);
  45. // Project, perspective divide and merge
  46. Vector3 tV0(projection * v0);
  47. Vector3 tV1(projection * v1);
  48. rect.Merge(Vector2(tV0.x_, tV0.y_));
  49. rect.Merge(Vector2(tV1.x_, tV1.y_));
  50. }
  51. Frustum::Frustum(const Frustum& frustum) noexcept
  52. {
  53. *this = frustum;
  54. }
  55. Frustum& Frustum::operator =(const Frustum& rhs) noexcept
  56. {
  57. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  58. planes_[i] = rhs.planes_[i];
  59. for (unsigned i = 0; i < NUM_FRUSTUM_VERTICES; ++i)
  60. vertices_[i] = rhs.vertices_[i];
  61. return *this;
  62. }
  63. void Frustum::Define(float fov, float aspectRatio, float zoom, float nearZ, float farZ, const Matrix3x4& transform)
  64. {
  65. nearZ = Max(nearZ, 0.0f);
  66. farZ = Max(farZ, nearZ);
  67. float halfViewSize = tanf(fov * M_DEGTORAD_2) / zoom;
  68. Vector3 near, far;
  69. near.z_ = nearZ;
  70. near.y_ = near.z_ * halfViewSize;
  71. near.x_ = near.y_ * aspectRatio;
  72. far.z_ = farZ;
  73. far.y_ = far.z_ * halfViewSize;
  74. far.x_ = far.y_ * aspectRatio;
  75. Define(near, far, transform);
  76. }
  77. void Frustum::Define(const Vector3& near, const Vector3& far, const Matrix3x4& transform)
  78. {
  79. vertices_[0] = transform * near;
  80. vertices_[1] = transform * Vector3(near.x_, -near.y_, near.z_);
  81. vertices_[2] = transform * Vector3(-near.x_, -near.y_, near.z_);
  82. vertices_[3] = transform * Vector3(-near.x_, near.y_, near.z_);
  83. vertices_[4] = transform * far;
  84. vertices_[5] = transform * Vector3(far.x_, -far.y_, far.z_);
  85. vertices_[6] = transform * Vector3(-far.x_, -far.y_, far.z_);
  86. vertices_[7] = transform * Vector3(-far.x_, far.y_, far.z_);
  87. UpdatePlanes();
  88. }
  89. void Frustum::Define(const BoundingBox& box, const Matrix3x4& transform)
  90. {
  91. vertices_[0] = transform * Vector3(box.max_.x_, box.max_.y_, box.min_.z_);
  92. vertices_[1] = transform * Vector3(box.max_.x_, box.min_.y_, box.min_.z_);
  93. vertices_[2] = transform * Vector3(box.min_.x_, box.min_.y_, box.min_.z_);
  94. vertices_[3] = transform * Vector3(box.min_.x_, box.max_.y_, box.min_.z_);
  95. vertices_[4] = transform * Vector3(box.max_.x_, box.max_.y_, box.max_.z_);
  96. vertices_[5] = transform * Vector3(box.max_.x_, box.min_.y_, box.max_.z_);
  97. vertices_[6] = transform * Vector3(box.min_.x_, box.min_.y_, box.max_.z_);
  98. vertices_[7] = transform * Vector3(box.min_.x_, box.max_.y_, box.max_.z_);
  99. UpdatePlanes();
  100. }
  101. void Frustum::Define(const Matrix4& projection)
  102. {
  103. Matrix4 projInverse = projection.Inverse();
  104. vertices_[0] = projInverse * Vector3(1.0f, 1.0f, 0.0f);
  105. vertices_[1] = projInverse * Vector3(1.0f, -1.0f, 0.0f);
  106. vertices_[2] = projInverse * Vector3(-1.0f, -1.0f, 0.0f);
  107. vertices_[3] = projInverse * Vector3(-1.0f, 1.0f, 0.0f);
  108. vertices_[4] = projInverse * Vector3(1.0f, 1.0f, 1.0f);
  109. vertices_[5] = projInverse * Vector3(1.0f, -1.0f, 1.0f);
  110. vertices_[6] = projInverse * Vector3(-1.0f, -1.0f, 1.0f);
  111. vertices_[7] = projInverse * Vector3(-1.0f, 1.0f, 1.0f);
  112. UpdatePlanes();
  113. }
  114. void Frustum::DefineOrtho(float orthoSize, float aspectRatio, float zoom, float nearZ, float farZ, const Matrix3x4& transform)
  115. {
  116. nearZ = Max(nearZ, 0.0f);
  117. farZ = Max(farZ, nearZ);
  118. float halfViewSize = orthoSize * 0.5f / zoom;
  119. Vector3 near, far;
  120. near.z_ = nearZ;
  121. far.z_ = farZ;
  122. far.y_ = near.y_ = halfViewSize;
  123. far.x_ = near.x_ = near.y_ * aspectRatio;
  124. Define(near, far, transform);
  125. }
  126. void Frustum::DefineSplit(const Matrix4& projection, float near, float far)
  127. {
  128. Matrix4 projInverse = projection.Inverse();
  129. // Figure out depth values for near & far
  130. Vector4 nearTemp = projection * Vector4(0.0f, 0.0f, near, 1.0f);
  131. Vector4 farTemp = projection * Vector4(0.0f, 0.0f, far, 1.0f);
  132. float nearZ = nearTemp.z_ / nearTemp.w_;
  133. float farZ = farTemp.z_ / farTemp.w_;
  134. vertices_[0] = projInverse * Vector3(1.0f, 1.0f, nearZ);
  135. vertices_[1] = projInverse * Vector3(1.0f, -1.0f, nearZ);
  136. vertices_[2] = projInverse * Vector3(-1.0f, -1.0f, nearZ);
  137. vertices_[3] = projInverse * Vector3(-1.0f, 1.0f, nearZ);
  138. vertices_[4] = projInverse * Vector3(1.0f, 1.0f, farZ);
  139. vertices_[5] = projInverse * Vector3(1.0f, -1.0f, farZ);
  140. vertices_[6] = projInverse * Vector3(-1.0f, -1.0f, farZ);
  141. vertices_[7] = projInverse * Vector3(-1.0f, 1.0f, farZ);
  142. UpdatePlanes();
  143. }
  144. void Frustum::Transform(const Matrix3& transform)
  145. {
  146. for (auto& vertice : vertices_)
  147. vertice = transform * vertice;
  148. UpdatePlanes();
  149. }
  150. void Frustum::Transform(const Matrix3x4& transform)
  151. {
  152. for (auto& vertice : vertices_)
  153. vertice = transform * vertice;
  154. UpdatePlanes();
  155. }
  156. Frustum Frustum::Transformed(const Matrix3& transform) const
  157. {
  158. Frustum transformed;
  159. for (unsigned i = 0; i < NUM_FRUSTUM_VERTICES; ++i)
  160. transformed.vertices_[i] = transform * vertices_[i];
  161. transformed.UpdatePlanes();
  162. return transformed;
  163. }
  164. Frustum Frustum::Transformed(const Matrix3x4& transform) const
  165. {
  166. Frustum transformed;
  167. for (unsigned i = 0; i < NUM_FRUSTUM_VERTICES; ++i)
  168. transformed.vertices_[i] = transform * vertices_[i];
  169. transformed.UpdatePlanes();
  170. return transformed;
  171. }
  172. Rect Frustum::Projected(const Matrix4& projection) const
  173. {
  174. Rect rect;
  175. ProjectAndMergeEdge(vertices_[0], vertices_[4], rect, projection);
  176. ProjectAndMergeEdge(vertices_[1], vertices_[5], rect, projection);
  177. ProjectAndMergeEdge(vertices_[2], vertices_[6], rect, projection);
  178. ProjectAndMergeEdge(vertices_[3], vertices_[7], rect, projection);
  179. ProjectAndMergeEdge(vertices_[4], vertices_[5], rect, projection);
  180. ProjectAndMergeEdge(vertices_[5], vertices_[6], rect, projection);
  181. ProjectAndMergeEdge(vertices_[6], vertices_[7], rect, projection);
  182. ProjectAndMergeEdge(vertices_[7], vertices_[4], rect, projection);
  183. return rect;
  184. }
  185. void Frustum::UpdatePlanes()
  186. {
  187. planes_[PLANE_NEAR].Define(vertices_[2], vertices_[1], vertices_[0]);
  188. planes_[PLANE_LEFT].Define(vertices_[3], vertices_[7], vertices_[6]);
  189. planes_[PLANE_RIGHT].Define(vertices_[1], vertices_[5], vertices_[4]);
  190. planes_[PLANE_UP].Define(vertices_[0], vertices_[4], vertices_[7]);
  191. planes_[PLANE_DOWN].Define(vertices_[6], vertices_[5], vertices_[1]);
  192. planes_[PLANE_FAR].Define(vertices_[5], vertices_[6], vertices_[7]);
  193. // Check if we ended up with inverted planes (reflected transform) and flip in that case
  194. if (planes_[PLANE_NEAR].Distance(vertices_[5]) < 0.0f)
  195. {
  196. for (auto& plane : planes_)
  197. {
  198. plane.normal_ = -plane.normal_;
  199. plane.d_ = -plane.d_;
  200. }
  201. }
  202. }
  203. }