SpatialComponent.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_SCENE_SPATIAL_COMPONENT_H
  6. #define ANKI_SCENE_SPATIAL_COMPONENT_H
  7. #include "anki/scene/Common.h"
  8. #include "anki/scene/SceneComponent.h"
  9. #include "anki/Collision.h"
  10. #include "anki/util/Bitset.h"
  11. #include "anki/util/Enum.h"
  12. namespace anki {
  13. /// @addtogroup scene
  14. /// @{
  15. /// Spatial flags
  16. enum class SpatialComponentFlag: U8
  17. {
  18. NONE = 0,
  19. VISIBLE_CAMERA = 1 << 1,
  20. VISIBLE_LIGHT = 1 << 2,
  21. /// Visible or not. The visibility tester sets it
  22. VISIBLE_ANY = VISIBLE_CAMERA | VISIBLE_LIGHT,
  23. /// This is used for example in lights. If the light does not collide
  24. /// with any surface then it shouldn't be visible and be processed
  25. /// further. This flag is being used to check if we should test agains
  26. /// near plane when using the tiler for visibility tests.
  27. FULLY_TRANSPARENT = 1 << 3,
  28. MARKED_FOR_UPDATE = 1 << 4
  29. };
  30. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(SpatialComponentFlag, inline)
  31. /// Spatial component for scene nodes. It indicates scene nodes that need to
  32. /// be placed in the a sector and they participate in the visibility tests
  33. class SpatialComponent: public SceneComponent,
  34. public Bitset<SpatialComponentFlag>
  35. {
  36. public:
  37. using Flag = SpatialComponentFlag;
  38. SpatialComponent(
  39. SceneNode* node,
  40. const CollisionShape* shape,
  41. Flag flags = Flag::NONE);
  42. ~SpatialComponent();
  43. const CollisionShape& getSpatialCollisionShape() const
  44. {
  45. return *m_shape;
  46. }
  47. const Aabb& getAabb() const
  48. {
  49. return m_aabb;
  50. }
  51. /// Get optimal collision shape for visibility tests
  52. const CollisionShape& getVisibilityCollisionShape()
  53. {
  54. const CollisionShape& cs = getSpatialCollisionShape();
  55. if(cs.getType() == CollisionShape::Type::SPHERE)
  56. {
  57. return cs;
  58. }
  59. else
  60. {
  61. return m_aabb;
  62. }
  63. }
  64. /// Used for sorting spatials. In most object the origin is the center of
  65. /// mess but for cameras the origin is the eye point
  66. const Vec4& getSpatialOrigin() const
  67. {
  68. ANKI_ASSERT(m_origin.x() != MAX_F32);
  69. return m_origin;
  70. }
  71. void setSpatialOrigin(const Vec4& origin)
  72. {
  73. m_origin = origin;
  74. }
  75. /// The derived class has to manually call this method when the collision
  76. /// shape got updated
  77. void markForUpdate()
  78. {
  79. enableBits(Flag::MARKED_FOR_UPDATE);
  80. }
  81. /// @name SceneComponent overrides
  82. /// @{
  83. ANKI_USE_RESULT Error update(SceneNode&, F32, F32, Bool& updated) override;
  84. /// Disable some flags
  85. void reset() override;
  86. /// @}
  87. static constexpr Type getClassType()
  88. {
  89. return Type::SPATIAL;
  90. }
  91. private:
  92. const CollisionShape* m_shape;
  93. Aabb m_aabb; ///< A faster shape
  94. Vec4 m_origin = Vec4(MAX_F32, MAX_F32, MAX_F32, 0.0);
  95. };
  96. /// @}
  97. } // end namespace anki
  98. #endif