SpatialComponent.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Scene/Components/SceneComponent.h>
  7. #include <AnKi/Scene/Octree.h>
  8. #include <AnKi/Collision.h>
  9. #include <AnKi/Util/BitMask.h>
  10. #include <AnKi/Util/Enum.h>
  11. #include <AnKi/Util/List.h>
  12. namespace anki {
  13. /// @addtogroup scene
  14. /// @{
  15. /// Spatial component. It is used by scene nodes that need to be placed inside the visibility structures.
  16. class SpatialComponent : public SceneComponent
  17. {
  18. ANKI_SCENE_COMPONENT(SpatialComponent)
  19. public:
  20. SpatialComponent(SceneNode* node);
  21. ~SpatialComponent();
  22. void setObbWorldSpace(const Obb& obb)
  23. {
  24. m_obb = obb;
  25. m_collisionObjectType = obb.CLASS_TYPE;
  26. m_markedForUpdate = true;
  27. }
  28. void setAabbWorldSpace(const Aabb& aabb)
  29. {
  30. m_aabb = aabb;
  31. m_collisionObjectType = aabb.CLASS_TYPE;
  32. m_markedForUpdate = true;
  33. }
  34. void setSphereWorldSpace(const Sphere& sphere)
  35. {
  36. m_sphere = sphere;
  37. m_collisionObjectType = sphere.CLASS_TYPE;
  38. m_markedForUpdate = true;
  39. }
  40. void setConvexHullWorldSpace(const ConvexHullShape& hull);
  41. template<typename T>
  42. const T& getCollisionShape() const
  43. {
  44. ANKI_ASSERT(T::CLASS_TYPE == m_collisionObjectType);
  45. return *reinterpret_cast<const T*>(&m_anyShape);
  46. }
  47. CollisionShapeType getCollisionShapeType() const
  48. {
  49. return m_collisionObjectType;
  50. }
  51. const Aabb& getAabbWorldSpace() const
  52. {
  53. ANKI_ASSERT(!m_alwaysVisible);
  54. return m_derivedAabb;
  55. }
  56. const SceneNode& getSceneNode() const
  57. {
  58. return *m_node;
  59. }
  60. SceneNode& getSceneNode()
  61. {
  62. return *m_node;
  63. }
  64. /// Used for sorting spatials. In most object the origin is the center of mass but for cameras the origin is the
  65. /// eye point.
  66. const Vec3& getSpatialOrigin() const
  67. {
  68. ANKI_ASSERT(m_origin.x() != MAX_F32);
  69. return m_origin;
  70. }
  71. /// See getSpatialOrigin()
  72. void setSpatialOrigin(const Vec3& origin)
  73. {
  74. m_origin = origin;
  75. }
  76. /// Update the "actual scene bounds" of the octree or not.
  77. void setUpdateOctreeBounds(Bool update)
  78. {
  79. m_updateOctreeBounds = update;
  80. }
  81. /// Make it or not always visible.
  82. void setAlwaysVisible(Bool alwaysVisible)
  83. {
  84. m_alwaysVisible = alwaysVisible;
  85. }
  86. /// See if it's always visible or not.
  87. Bool getAlwaysVisible() const
  88. {
  89. return m_alwaysVisible;
  90. }
  91. ANKI_USE_RESULT Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override;
  92. private:
  93. SceneNode* m_node;
  94. union
  95. {
  96. Obb m_obb;
  97. Aabb m_aabb;
  98. Sphere m_sphere;
  99. ConvexHullShape m_hull;
  100. U8 m_anyShape;
  101. };
  102. DynamicArray<Vec4> m_convexHullPoints;
  103. CollisionShapeType m_collisionObjectType = CollisionShapeType::COUNT;
  104. Aabb m_derivedAabb; ///< A faster shape
  105. Vec3 m_origin = Vec3(MAX_F32);
  106. OctreePlaceable m_octreeInfo;
  107. Bool m_markedForUpdate : 1;
  108. Bool m_placed : 1;
  109. Bool m_updateOctreeBounds : 1;
  110. Bool m_alwaysVisible : 1;
  111. };
  112. /// @}
  113. } // end namespace anki