SpatialComponent.h 2.9 KB

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