Spatial.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef ANKI_SCENE_SPATIAL_H
  2. #define ANKI_SCENE_SPATIAL_H
  3. #include "anki/collision/Collision.h"
  4. #include "anki/util/Flags.h"
  5. #include "anki/core/Timestamp.h"
  6. namespace anki {
  7. class OctreeNode;
  8. class Grid;
  9. class SceneNode;
  10. /// @addtogroup Scene
  11. /// @{
  12. /// Spatial "interface" for scene nodes. It indicates scene nodes that need to
  13. /// be placed in the scene's octree and they participate in the visibility
  14. /// tests
  15. class Spatial: public Flags<U32>
  16. {
  17. friend class OctreeNode;
  18. friend class Grid;
  19. public:
  20. /// Spatial flags
  21. enum SpatialFlag
  22. {
  23. SF_NONE = 0,
  24. SF_VISIBLE_CAMERA = 1 << 1,
  25. SF_VISIBLE_LIGHT = 1 << 2,
  26. /// Visible or not. The visibility tester sets it
  27. SF_VISIBLE_ANY = SF_VISIBLE_CAMERA | SF_VISIBLE_LIGHT
  28. };
  29. /// Pass the collision shape here so we can avoid the virtuals
  30. Spatial(SceneNode* sceneNode_, CollisionShape* cs)
  31. : spatialCs(cs), sceneNode(sceneNode_)
  32. {}
  33. // Remove from current OctreeNode
  34. ~Spatial();
  35. /// @name Accessors
  36. /// @{
  37. const CollisionShape& getSpatialCollisionShape() const
  38. {
  39. return *spatialCs;
  40. }
  41. const Aabb& getAabb() const
  42. {
  43. return aabb;
  44. }
  45. /// Get optimal collision shape for visibility tests
  46. const CollisionShape& getOptimalCollisionShape() const
  47. {
  48. if(spatialCs->getCollisionShapeType() == CollisionShape::CST_SPHERE)
  49. {
  50. return *spatialCs;
  51. }
  52. else
  53. {
  54. return aabb;
  55. }
  56. }
  57. U32 getSpatialTimestamp() const
  58. {
  59. return timestamp;
  60. }
  61. virtual const Vec3& getSpatialOrigin() const = 0;
  62. /// @}
  63. /// The derived class has to manually set when the collision shape got
  64. /// updated
  65. void spatialMarkUpdated()
  66. {
  67. timestamp = Timestamp::getTimestamp();
  68. spatialCs->toAabb(aabb);
  69. origin = (aabb.getMin() + aabb.getMax()) * 0.5;
  70. }
  71. protected:
  72. CollisionShape* spatialCs = nullptr;
  73. private:
  74. U32 timestamp = Timestamp::getTimestamp();
  75. OctreeNode* octreeNode = nullptr; ///< What octree node includes this
  76. Grid* grid = nullptr;
  77. Aabb aabb; ///< A faster shape
  78. SceneNode* sceneNode; ///< Know your father
  79. Vec3 origin;
  80. };
  81. /// @}
  82. } // end namespace anki
  83. #endif