Frustum.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include "Precompiled.h"
  24. #include "Frustum.h"
  25. inline Vector3 ClipEdgeZ(const Vector3& v0, const Vector3& v1, float clipZ)
  26. {
  27. return Vector3(
  28. v1.x_ + (v0.x_ - v1.x_) * ((clipZ - v1.z_) / (v0.z_ - v1.z_)),
  29. v1.y_ + (v0.y_ - v1.y_) * ((clipZ - v1.z_) / (v0.z_ - v1.z_)),
  30. clipZ
  31. );
  32. }
  33. void ProjectAndMergeEdge(Vector3 v0, Vector3 v1, Rect& rect, const Matrix4& projection)
  34. {
  35. // Check if both vertices behind near plane
  36. if (v0.z_ < M_MIN_NEARCLIP && v1.z_ < M_MIN_NEARCLIP)
  37. return;
  38. // Check if need to clip one of the vertices
  39. if (v1.z_ < M_MIN_NEARCLIP)
  40. v1 = ClipEdgeZ(v1, v0, M_MIN_NEARCLIP);
  41. else if (v0.z_ < M_MIN_NEARCLIP)
  42. v0 = ClipEdgeZ(v0, v1, M_MIN_NEARCLIP);
  43. // Project, perspective divide and merge
  44. Vector3 tV0(projection * v0);
  45. Vector3 tV1(projection * v1);
  46. rect.Merge(Vector2(tV0.x_, tV0.y_));
  47. rect.Merge(Vector2(tV1.x_, tV1.y_));
  48. }
  49. Frustum::Frustum() :
  50. defined_(false)
  51. {
  52. }
  53. Frustum::Frustum(const Frustum& frustum)
  54. {
  55. *this = frustum;
  56. }
  57. Frustum& Frustum::operator = (const Frustum& rhs)
  58. {
  59. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  60. planes_[i] = rhs.planes_[i];
  61. for (unsigned i = 0; i < NUM_FRUSTUM_VERTICES; ++i)
  62. vertices_[i] = rhs.vertices_[i];
  63. defined_ = rhs.defined_;
  64. return *this;
  65. }
  66. void Frustum::Define(float fov, float aspectRatio, float zoom, float nearZ, float farZ, const Matrix3x4& transform)
  67. {
  68. nearZ = Max(nearZ, 0.0f);
  69. farZ = Max(farZ, nearZ);
  70. float halfViewSize = tanf(fov * M_DEGTORAD * 0.5f) / zoom;
  71. Vector3 near, far;
  72. near.z_ = nearZ;
  73. near.y_ = near.z_ * halfViewSize;
  74. near.x_ = near.y_ * aspectRatio;
  75. far.z_ = farZ;
  76. far.y_ = far.z_ * halfViewSize;
  77. far.x_ = far.y_ * aspectRatio;
  78. Define(near, far, transform);
  79. }
  80. void Frustum::Define(const Vector3& near, const Vector3& far, const Matrix3x4& transform)
  81. {
  82. vertices_[0] = transform * near;
  83. vertices_[1] = transform * Vector3(near.x_, -near.y_, near.z_);
  84. vertices_[2] = transform * Vector3(-near.x_, -near.y_, near.z_);
  85. vertices_[3] = transform * Vector3(-near.x_, near.y_, near.z_);
  86. vertices_[4] = transform * far;
  87. vertices_[5] = transform * Vector3(far.x_, -far.y_, far.z_);
  88. vertices_[6] = transform * Vector3(-far.x_, -far.y_, far.z_);
  89. vertices_[7] = transform * Vector3(-far.x_, far.y_, far.z_);
  90. UpdatePlanes();
  91. }
  92. void Frustum::DefineOrtho(float orthoSize, float aspectRatio, float zoom, float nearZ, float farZ, const Matrix3x4& transform)
  93. {
  94. nearZ = Max(nearZ, 0.0f);
  95. farZ = Max(farZ, nearZ);
  96. float halfViewSize = orthoSize * 0.5f / zoom;
  97. Vector3 near, far;
  98. near.z_ = nearZ;
  99. far.z_ = farZ;
  100. far.y_ = near.y_ = halfViewSize;
  101. far.x_ = near.x_ = near.y_ * aspectRatio;
  102. Define(near, far, transform);
  103. }
  104. void Frustum::Transform(const Matrix3& transform)
  105. {
  106. for (unsigned i = 0; i < NUM_FRUSTUM_VERTICES; ++i)
  107. vertices_[i] = transform * vertices_[i];
  108. UpdatePlanes();
  109. }
  110. void Frustum::Transform(const Matrix3x4& transform)
  111. {
  112. for (unsigned i = 0; i < NUM_FRUSTUM_VERTICES; ++i)
  113. vertices_[i] = transform * vertices_[i];
  114. UpdatePlanes();
  115. }
  116. Frustum Frustum::Transformed(const Matrix3& transform) const
  117. {
  118. Frustum transformed;
  119. for (unsigned i = 0; i < NUM_FRUSTUM_VERTICES; ++i)
  120. transformed.vertices_[i] = transform * vertices_[i];
  121. transformed.UpdatePlanes();
  122. return transformed;
  123. }
  124. Frustum Frustum::Transformed(const Matrix3x4& transform) const
  125. {
  126. Frustum transformed;
  127. for (unsigned i = 0; i < NUM_FRUSTUM_VERTICES; ++i)
  128. transformed.vertices_[i] = transform * vertices_[i];
  129. transformed.UpdatePlanes();
  130. return transformed;
  131. }
  132. Rect Frustum::Projected(const Matrix4& projection) const
  133. {
  134. Rect rect;
  135. ProjectAndMergeEdge(vertices_[0], vertices_[4], rect, projection);
  136. ProjectAndMergeEdge(vertices_[1], vertices_[5], rect, projection);
  137. ProjectAndMergeEdge(vertices_[2], vertices_[6], rect, projection);
  138. ProjectAndMergeEdge(vertices_[3], vertices_[7], rect, projection);
  139. ProjectAndMergeEdge(vertices_[4], vertices_[5], rect, projection);
  140. ProjectAndMergeEdge(vertices_[5], vertices_[6], rect, projection);
  141. ProjectAndMergeEdge(vertices_[6], vertices_[7], rect, projection);
  142. ProjectAndMergeEdge(vertices_[7], vertices_[4], rect, projection);
  143. return rect;
  144. }
  145. void Frustum::UpdatePlanes()
  146. {
  147. planes_[PLANE_NEAR].Define(vertices_[2], vertices_[1], vertices_[0]);
  148. planes_[PLANE_LEFT].Define(vertices_[3], vertices_[7], vertices_[6]);
  149. planes_[PLANE_RIGHT].Define(vertices_[1], vertices_[5], vertices_[4]);
  150. planes_[PLANE_UP].Define(vertices_[0], vertices_[4], vertices_[7]);
  151. planes_[PLANE_DOWN].Define(vertices_[6], vertices_[5], vertices_[1]);
  152. planes_[PLANE_FAR].Define(vertices_[5], vertices_[6], vertices_[7]);
  153. defined_ = true;
  154. }