Frustum.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Scene/Common.h>
  7. #include <AnKi/Collision/Obb.h>
  8. #include <AnKi/Collision/ConvexHullShape.h>
  9. #include <AnKi/Collision/Plane.h>
  10. namespace anki {
  11. // A helper class that represents a frustum.
  12. class Frustum
  13. {
  14. public:
  15. Frustum();
  16. ~Frustum();
  17. void init(FrustumType type);
  18. void setPerspective(F32 near, F32 far, F32 fovX, F32 fovY)
  19. {
  20. ANKI_ASSERT(m_frustumType == FrustumType::kPerspective);
  21. setNear(near);
  22. setFar(far);
  23. setFovX(fovX);
  24. setFovY(fovY);
  25. }
  26. void setOrthographic(F32 near, F32 far, F32 right, F32 left, F32 top, F32 bottom)
  27. {
  28. ANKI_ASSERT(m_frustumType == FrustumType::kOrthographic);
  29. setNear(near);
  30. setFar(far);
  31. setLeft(left);
  32. setRight(right);
  33. setTop(top);
  34. setBottom(bottom);
  35. }
  36. FrustumType getFrustumType() const
  37. {
  38. ANKI_ASSERT(m_frustumType != FrustumType::kCount);
  39. return m_frustumType;
  40. }
  41. void setNear(F32 near)
  42. {
  43. ANKI_ASSERT(near > kEpsilonf);
  44. m_common.m_near = near;
  45. m_shapeDirty = true;
  46. }
  47. F32 getNear() const
  48. {
  49. return m_common.m_near;
  50. }
  51. void setFar(F32 far)
  52. {
  53. ANKI_ASSERT(far > kEpsilonf);
  54. m_common.m_far = far;
  55. m_shapeDirty = true;
  56. }
  57. F32 getFar() const
  58. {
  59. return m_common.m_far;
  60. }
  61. void setFovX(F32 fovx)
  62. {
  63. ANKI_ASSERT(m_frustumType == FrustumType::kPerspective && fovx > 0.0f && fovx < kPi);
  64. m_shapeDirty = true;
  65. m_perspective.m_fovX = fovx;
  66. }
  67. F32 getFovX() const
  68. {
  69. ANKI_ASSERT(m_frustumType == FrustumType::kPerspective);
  70. return m_perspective.m_fovX;
  71. }
  72. void setFovY(F32 fovy)
  73. {
  74. ANKI_ASSERT(m_frustumType == FrustumType::kPerspective && fovy > 0.0f && fovy < kPi);
  75. m_shapeDirty = true;
  76. m_perspective.m_fovY = fovy;
  77. }
  78. F32 getFovY() const
  79. {
  80. ANKI_ASSERT(m_frustumType == FrustumType::kPerspective);
  81. return m_perspective.m_fovY;
  82. }
  83. F32 getLeft() const
  84. {
  85. ANKI_ASSERT(m_frustumType == FrustumType::kOrthographic);
  86. return m_ortho.m_left;
  87. }
  88. void setLeft(F32 value)
  89. {
  90. ANKI_ASSERT(m_frustumType == FrustumType::kOrthographic);
  91. m_shapeDirty = true;
  92. m_ortho.m_left = value;
  93. }
  94. F32 getRight() const
  95. {
  96. ANKI_ASSERT(m_frustumType == FrustumType::kOrthographic);
  97. return m_ortho.m_right;
  98. }
  99. void setRight(F32 value)
  100. {
  101. ANKI_ASSERT(m_frustumType == FrustumType::kOrthographic);
  102. m_shapeDirty = true;
  103. m_ortho.m_right = value;
  104. }
  105. F32 getTop() const
  106. {
  107. ANKI_ASSERT(m_frustumType == FrustumType::kOrthographic);
  108. return m_ortho.m_top;
  109. }
  110. void setTop(F32 value)
  111. {
  112. ANKI_ASSERT(m_frustumType == FrustumType::kOrthographic);
  113. m_shapeDirty = true;
  114. m_ortho.m_top = value;
  115. }
  116. F32 getBottom() const
  117. {
  118. ANKI_ASSERT(m_frustumType == FrustumType::kOrthographic);
  119. return m_ortho.m_bottom;
  120. }
  121. void setBottom(F32 value)
  122. {
  123. ANKI_ASSERT(m_frustumType == FrustumType::kOrthographic);
  124. m_shapeDirty = true;
  125. m_ortho.m_bottom = value;
  126. }
  127. const Mat4& getProjectionMatrix() const
  128. {
  129. ANKI_ASSERT(!isDirty());
  130. return m_projMat;
  131. }
  132. const Mat3x4& getViewMatrix() const
  133. {
  134. ANKI_ASSERT(!isDirty());
  135. return m_viewMat;
  136. }
  137. const Mat4& getViewProjectionMatrix() const
  138. {
  139. ANKI_ASSERT(!isDirty());
  140. return m_viewProjMat;
  141. }
  142. void setWorldTransform(const Transform& worldTransform)
  143. {
  144. m_worldTransform = worldTransform;
  145. m_worldTransformDirty = true;
  146. }
  147. const Transform& getWorldTransform() const
  148. {
  149. return m_worldTransform;
  150. }
  151. Bool update();
  152. // Get the precalculated rotations of each of the 6 frustums of an omnidirectional source (eg a point light).
  153. static const Array<Mat3x4, 6>& getOmnidirectionalFrustumRotations()
  154. {
  155. return m_omnidirectionalRotations;
  156. }
  157. private:
  158. class Common
  159. {
  160. public:
  161. F32 m_near;
  162. F32 m_far;
  163. };
  164. class Perspective : public Common
  165. {
  166. public:
  167. F32 m_fovX;
  168. F32 m_fovY;
  169. };
  170. class Ortho : public Common
  171. {
  172. public:
  173. F32 m_left;
  174. F32 m_right;
  175. F32 m_top;
  176. F32 m_bottom;
  177. };
  178. static constexpr F32 kDefaultNear = 0.1f;
  179. static constexpr F32 kDefaultFar = 100.0f;
  180. static constexpr F32 kDefaultFovAngle = toRad(45.0f);
  181. union
  182. {
  183. Perspective m_perspective;
  184. Ortho m_ortho;
  185. Common m_common;
  186. };
  187. Transform m_worldTransform = Transform::getIdentity();
  188. Mat4 m_projMat = Mat4::getIdentity(); ///< Projection matrix
  189. Mat3x4 m_viewMat = Mat3x4::getIdentity(); ///< View matrix
  190. Mat4 m_viewProjMat = Mat4::getIdentity(); ///< View projection matrix
  191. FrustumType m_frustumType = FrustumType::kCount;
  192. Bool m_shapeDirty : 1 = true;
  193. Bool m_worldTransformDirty : 1 = true;
  194. static Array<Mat3x4, 6> m_omnidirectionalRotations;
  195. Bool isDirty() const
  196. {
  197. return m_shapeDirty || m_worldTransformDirty;
  198. }
  199. };
  200. } // end namespace anki