SpatialComponent.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include <AnKi/Scene/Components/SpatialComponent.h>
  6. #include <AnKi/Scene/SceneNode.h>
  7. #include <AnKi/Scene/SceneGraph.h>
  8. namespace anki
  9. {
  10. ANKI_SCENE_COMPONENT_STATICS(SpatialComponent)
  11. SpatialComponent::SpatialComponent(SceneNode* node)
  12. : SceneComponent(node, getStaticClassId())
  13. , m_node(node)
  14. , m_markedForUpdate(true)
  15. , m_placed(false)
  16. , m_updateOctreeBounds(true)
  17. , m_alwaysVisible(false)
  18. {
  19. ANKI_ASSERT(node);
  20. m_octreeInfo.m_userData = this;
  21. setAabbWorldSpace(Aabb(Vec3(-1.0f), Vec3(1.0f)));
  22. }
  23. SpatialComponent::~SpatialComponent()
  24. {
  25. if(m_placed)
  26. {
  27. m_node->getSceneGraph().getOctree().remove(m_octreeInfo);
  28. }
  29. m_convexHullPoints.destroy(m_node->getAllocator());
  30. }
  31. void SpatialComponent::setConvexHullWorldSpace(const ConvexHullShape& hull)
  32. {
  33. ANKI_ASSERT(hull.getPoints().getSize() > 0);
  34. if(m_convexHullPoints.getSize() != hull.getPoints().getSize())
  35. {
  36. m_convexHullPoints.resize(m_node->getAllocator(), hull.getPoints().getSize());
  37. }
  38. memcpy(&m_convexHullPoints[0], &hull.getPoints()[0], hull.getPoints().getSizeInBytes());
  39. m_hull = ConvexHullShape(&m_convexHullPoints[0], m_convexHullPoints.getSize());
  40. if(!hull.isTransformIdentity())
  41. {
  42. m_hull.setTransform(hull.getTransform());
  43. }
  44. m_collisionObjectType = hull.CLASS_TYPE;
  45. m_markedForUpdate = true;
  46. }
  47. Error SpatialComponent::update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated)
  48. {
  49. ANKI_ASSERT(&node == m_node);
  50. updated = m_markedForUpdate;
  51. if(updated)
  52. {
  53. if(!m_alwaysVisible)
  54. {
  55. // Compute the AABB
  56. switch(m_collisionObjectType)
  57. {
  58. case CollisionShapeType::AABB:
  59. m_derivedAabb = m_aabb;
  60. break;
  61. case CollisionShapeType::OBB:
  62. m_derivedAabb = computeAabb(m_obb);
  63. break;
  64. case CollisionShapeType::SPHERE:
  65. m_derivedAabb = computeAabb(m_sphere);
  66. break;
  67. case CollisionShapeType::CONVEX_HULL:
  68. m_derivedAabb = computeAabb(m_hull);
  69. break;
  70. default:
  71. ANKI_ASSERT(0);
  72. }
  73. m_node->getSceneGraph().getOctree().place(m_derivedAabb, &m_octreeInfo, m_updateOctreeBounds);
  74. }
  75. else
  76. {
  77. m_node->getSceneGraph().getOctree().placeAlwaysVisible(&m_octreeInfo);
  78. }
  79. m_markedForUpdate = false;
  80. m_placed = true;
  81. }
  82. m_octreeInfo.reset();
  83. return Error::NONE;
  84. }
  85. } // end namespace anki