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