Frustum.cpp 7.2 KB

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