FrustumComponent.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef ANKI_SCENE_FRUSTUM_COMPONENT_H
  2. #define ANKI_SCENE_FRUSTUM_COMPONENT_H
  3. #include "anki/collision/Frustum.h"
  4. #include "anki/scene/SpatialComponent.h"
  5. #include "anki/scene/Common.h"
  6. namespace anki {
  7. // Forward
  8. struct VisibilityTestResults;
  9. /// @addtogroup Scene
  10. /// @{
  11. /// Frustum component interface for scene nodes. Useful for nodes that are
  12. /// frustums like cameras and lights
  13. class FrustumComponent
  14. {
  15. public:
  16. /// @name Constructors
  17. /// @{
  18. /// Pass the frustum here so we can avoid the virtuals
  19. FrustumComponent(Frustum* fr)
  20. : frustum(fr)
  21. {
  22. ANKI_ASSERT(frustum);
  23. }
  24. /// @}
  25. /// @name Accessors
  26. /// @{
  27. const Frustum& getFrustum() const
  28. {
  29. return *frustum;
  30. }
  31. Timestamp getTimestamp() const
  32. {
  33. return timestamp;
  34. }
  35. const Mat4& getProjectionMatrix() const
  36. {
  37. return projectionMat;
  38. }
  39. const Mat4& getViewMatrix() const
  40. {
  41. return viewMat;
  42. }
  43. const Mat4& getViewProjectionMatrix() const
  44. {
  45. return viewProjectionMat;
  46. }
  47. /// Get the origin for sorting and visibility tests
  48. virtual const Vec3& getFrustumOrigin() const = 0;
  49. void setVisibilityTestResults(VisibilityTestResults* visible_)
  50. {
  51. ANKI_ASSERT(visible == nullptr);
  52. visible = visible_;
  53. }
  54. /// Call this after the tests. Before it will point to junk
  55. VisibilityTestResults& getVisibilityTestResults()
  56. {
  57. ANKI_ASSERT(visible != nullptr);
  58. return *visible;
  59. }
  60. /// @}
  61. void markForUpdate()
  62. {
  63. timestamp = getGlobTimestamp();
  64. }
  65. /// Is a spatial inside the frustum?
  66. Bool insideFrustum(const SpatialComponent& sp) const
  67. {
  68. return frustum->insideFrustum(sp.getSpatialCollisionShape());
  69. }
  70. /// Is a collision shape inside the frustum?
  71. Bool insideFrustum(const CollisionShape& cs) const
  72. {
  73. return frustum->insideFrustum(cs);
  74. }
  75. void resetFrame()
  76. {
  77. visible = nullptr;
  78. }
  79. protected:
  80. Frustum* frustum = nullptr;
  81. Mat4 projectionMat = Mat4::getIdentity();
  82. Mat4 viewMat = Mat4::getIdentity();
  83. Mat4 viewProjectionMat = Mat4::getIdentity();
  84. private:
  85. Timestamp timestamp = getGlobTimestamp();
  86. /// Visibility stuff. It's per frame so the pointer is invalid on the next
  87. /// frame and before any visibility tests are run
  88. VisibilityTestResults* visible = nullptr;
  89. };
  90. /// @}
  91. } // end namespace anki
  92. #endif