SpatialComponent.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #ifndef ANKI_SCENE_SPATIAL_COMPONENT_H
  2. #define ANKI_SCENE_SPATIAL_COMPONENT_H
  3. #include "anki/scene/Common.h"
  4. #include "anki/Collision.h"
  5. #include "anki/util/Bitset.h"
  6. #include "anki/core/Timestamp.h"
  7. namespace anki {
  8. /// @addtogroup Scene
  9. /// @{
  10. /// Spatial component for scene nodes. It indicates scene nodes that need to
  11. /// be placed in the a sector and they participate in the visibility tests
  12. class SpatialComponent:
  13. public SceneHierarchicalObject<SpatialComponent>,
  14. public Bitset<U8>
  15. {
  16. public:
  17. typedef SceneHierarchicalObject<SpatialComponent> Base;
  18. /// Spatial flags
  19. enum SpatialFlag
  20. {
  21. SF_NONE = 0,
  22. SF_VISIBLE_CAMERA = 1 << 1,
  23. SF_VISIBLE_LIGHT = 1 << 2,
  24. /// Visible or not. The visibility tester sets it
  25. SF_VISIBLE_ANY = SF_VISIBLE_CAMERA | SF_VISIBLE_LIGHT,
  26. /// This is used for example in lights. If the light does not collide
  27. /// with any surface then it shouldn't be visible and be processed
  28. /// further. This flag is being used to check if we should test agains
  29. /// near plane when using the tiler for visibility tests.
  30. SF_FULLY_TRANSPARENT = 1 << 3
  31. };
  32. /// Pass the collision shape here so we can avoid the virtuals
  33. /// @param node The scene node. Used only to steal it's allocators
  34. /// @param cs The collision shape
  35. /// @param flags A mask of SpatialFlag
  36. SpatialComponent(SceneNode* node, const CollisionShape* cs,
  37. U32 flags = SF_NONE);
  38. // Remove from current OctreeNode
  39. virtual ~SpatialComponent();
  40. /// @name Accessors
  41. /// @{
  42. const CollisionShape& getSpatialCollisionShape() const
  43. {
  44. return *spatialCs;
  45. }
  46. const Aabb& getAabb() const
  47. {
  48. return aabb;
  49. }
  50. /// Get optimal collision shape for visibility tests
  51. const CollisionShape& getVisibilityCollisionShape() const
  52. {
  53. if(spatialCs->getCollisionShapeType() == CollisionShape::CST_SPHERE)
  54. {
  55. return *spatialCs;
  56. }
  57. else
  58. {
  59. return aabb;
  60. }
  61. }
  62. Timestamp getTimestamp() const
  63. {
  64. return timestamp;
  65. }
  66. /// Used for sorting spatials. In most object the origin is the center of
  67. /// the bounding volume but for cameras the origin is the eye point
  68. virtual const Vec3& getSpatialOrigin() const
  69. {
  70. return origin;
  71. }
  72. PtrSize getSubSpatialsCount() const
  73. {
  74. return getChildrenSize();
  75. }
  76. SpatialComponent& getSubSpatial(PtrSize i)
  77. {
  78. return getChild(i);
  79. }
  80. const SpatialComponent& getSubSpatial(PtrSize i) const
  81. {
  82. return getChild(i);
  83. }
  84. /// @}
  85. /// Visit this an the rest of the tree spatials
  86. template<typename VisitorFunc>
  87. void visitSubSpatials(VisitorFunc vis)
  88. {
  89. Base::visitChildren(vis);
  90. }
  91. /// The derived class has to manually call this method when the collision
  92. /// shape got updated
  93. void markForUpdate();
  94. void resetFrame();
  95. protected:
  96. const CollisionShape* spatialCs = nullptr;
  97. private:
  98. Aabb aabb; ///< A faster shape
  99. Vec3 origin; ///< Cached value
  100. Timestamp timestamp = getGlobTimestamp();
  101. void updateInternal();
  102. };
  103. /// @}
  104. } // end namespace anki
  105. #endif