Frustum.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #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.mX + (v0.mX - v1.mX) * ((clipZ - v1.mZ) / (v0.mZ - v1.mZ)),
  29. v1.mY + (v0.mY - v1.mY) * ((clipZ - v1.mZ) / (v0.mZ - v1.mZ)),
  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.mZ < M_MIN_NEARCLIP) && (v1.mZ < M_MIN_NEARCLIP))
  37. return;
  38. // Check if need to clip one of the vertices
  39. if (v1.mZ < M_MIN_NEARCLIP)
  40. v1 = clipEdgeZ(v1, v0, M_MIN_NEARCLIP);
  41. else if (v0.mZ < 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.mX, tV0.mY));
  47. rect.merge(Vector2(tV1.mX, tV1.mY));
  48. }
  49. Frustum::Frustum() :
  50. mDefined(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. mPlanes[i] = rhs.mPlanes[i];
  61. for (unsigned i = 0; i < NUM_FRUSTUM_VERTICES; ++i)
  62. mVertices[i] = rhs.mVertices[i];
  63. mDefined = rhs.mDefined;
  64. return *this;
  65. }
  66. void Frustum::define(float fov, float aspectRatio, float zoom, float nearZ, float farZ, const Matrix4x3& transform)
  67. {
  68. float halfViewSize = tanf(fov * M_DEGTORAD * 0.5f) / zoom;
  69. Vector3 near;
  70. Vector3 far;
  71. nearZ = max(nearZ, 0.0f);
  72. farZ = max(farZ, nearZ);
  73. near.mZ = nearZ;
  74. near.mY = near.mZ * halfViewSize;
  75. near.mX = near.mY * aspectRatio;
  76. far.mZ = farZ;
  77. far.mY = far.mZ * halfViewSize;
  78. far.mX = far.mY * aspectRatio;
  79. define(near, far, transform);
  80. }
  81. void Frustum::define(const Vector3& near, const Vector3& far, const Matrix4x3& transform)
  82. {
  83. mVertices[0] = transform * near;
  84. mVertices[1] = transform * Vector3(near.mX, -near.mY, near.mZ);
  85. mVertices[2] = transform * Vector3(-near.mX, -near.mY, near.mZ);
  86. mVertices[3] = transform * Vector3(-near.mX, near.mY, near.mZ);
  87. mVertices[4] = transform * far;
  88. mVertices[5] = transform * Vector3(far.mX, -far.mY, far.mZ);
  89. mVertices[6] = transform * Vector3(-far.mX, -far.mY, far.mZ);
  90. mVertices[7] = transform * Vector3(-far.mX, far.mY, far.mZ);
  91. updatePlanes();
  92. }
  93. void Frustum::defineOrtho(float orthoSize, float aspectRatio, float zoom, float nearZ, float farZ, const Matrix4x3& transform)
  94. {
  95. float halfViewSize = orthoSize * 0.5f / zoom;
  96. Vector3 near;
  97. Vector3 far;
  98. nearZ = max(nearZ, 0.0f);
  99. farZ = max(farZ, nearZ);
  100. near.mZ = nearZ;
  101. far.mZ = farZ;
  102. far.mY = near.mY = halfViewSize;
  103. far.mX = near.mX = near.mY * aspectRatio;
  104. define(near, far, transform);
  105. }
  106. void Frustum::transform(const Matrix3& transform)
  107. {
  108. for (unsigned i = 0; i < NUM_FRUSTUM_VERTICES; ++i)
  109. mVertices[i] = transform * mVertices[i];
  110. updatePlanes();
  111. }
  112. void Frustum::transform(const Matrix4x3& transform)
  113. {
  114. for (unsigned i = 0; i < NUM_FRUSTUM_VERTICES; ++i)
  115. mVertices[i] = transform * mVertices[i];
  116. updatePlanes();
  117. }
  118. void Frustum::updatePlanes()
  119. {
  120. mPlanes[PLANE_NEAR].define(mVertices[2], mVertices[1], mVertices[0]);
  121. mPlanes[PLANE_LEFT].define(mVertices[3], mVertices[7], mVertices[6]);
  122. mPlanes[PLANE_RIGHT].define(mVertices[1], mVertices[5], mVertices[4]);
  123. mPlanes[PLANE_UP].define(mVertices[0], mVertices[4], mVertices[7]);
  124. mPlanes[PLANE_DOWN].define(mVertices[6], mVertices[5], mVertices[1]);
  125. mPlanes[PLANE_FAR].define(mVertices[5], mVertices[6], mVertices[7]);
  126. mDefined = true;
  127. }
  128. Frustum Frustum::getTransformed(const Matrix3& transform) const
  129. {
  130. Frustum transformed;
  131. for (unsigned i = 0; i < NUM_FRUSTUM_VERTICES; ++i)
  132. transformed.mVertices[i] = transform * mVertices[i];
  133. transformed.updatePlanes();
  134. return transformed;
  135. }
  136. Frustum Frustum::getTransformed(const Matrix4x3& transform) const
  137. {
  138. Frustum transformed;
  139. for (unsigned i = 0; i < NUM_FRUSTUM_VERTICES; ++i)
  140. transformed.mVertices[i] = transform * mVertices[i];
  141. transformed.updatePlanes();
  142. return transformed;
  143. }
  144. Rect Frustum::getProjected(const Matrix4& projection) const
  145. {
  146. Rect rect;
  147. projectAndMergeEdge(mVertices[0], mVertices[4], rect, projection);
  148. projectAndMergeEdge(mVertices[1], mVertices[5], rect, projection);
  149. projectAndMergeEdge(mVertices[2], mVertices[6], rect, projection);
  150. projectAndMergeEdge(mVertices[3], mVertices[7], rect, projection);
  151. projectAndMergeEdge(mVertices[4], mVertices[5], rect, projection);
  152. projectAndMergeEdge(mVertices[5], mVertices[6], rect, projection);
  153. projectAndMergeEdge(mVertices[6], mVertices[7], rect, projection);
  154. projectAndMergeEdge(mVertices[7], mVertices[4], rect, projection);
  155. return rect;
  156. }