Radium Engine  1.5.0
Component.cpp
1 #include <Engine/Scene/Component.hpp>
2 
3 #include <Core/Math/LinearAlgebra.hpp>
4 #include <Core/Utils/Log.hpp>
5 #include <Engine/Data/Mesh.hpp>
6 #include <Engine/RadiumEngine.hpp>
7 #include <Engine/Rendering/RenderObject.hpp>
8 #include <Engine/Rendering/RenderObjectManager.hpp>
9 #include <Engine/Scene/ComponentMessenger.hpp>
10 #include <Engine/Scene/Entity.hpp>
11 #include <Engine/Scene/SignalManager.hpp>
12 #include <Engine/Scene/System.hpp>
13 
14 namespace Ra {
15 namespace Engine {
16 namespace Scene {
17 
18 using namespace Core::Utils; // log
19 
20 Component::Component( const std::string& name, Entity* entity ) :
21  m_name { name }, m_entity { entity } {
22  m_entity->addComponent( this );
23 }
24 
26  for ( const auto& ro : m_renderObjects ) {
27  getRoMgr()->removeRenderObject( ro );
28  }
29  if ( m_system ) { m_system->unregisterComponent( getEntity(), this ); }
30  auto cm = ComponentMessenger::getInstance();
31  cm->unregisterAll( getEntity(), this );
32 
33  RadiumEngine::getInstance()->getSignalManager()->fireComponentRemoved(
34  ItemEntry( getEntity(), this ) );
35 }
36 
37 Rendering::RenderObjectManager* Component::getRoMgr() {
38  return RadiumEngine::getInstance()->getRenderObjectManager();
39 }
40 
41 Core::Utils::Index Component::addRenderObject( Rendering::RenderObject* renderObject ) {
42  m_renderObjects.push_back( getRoMgr()->addRenderObject( renderObject ) );
43  invalidateAabb();
44  return m_renderObjects.back();
45 }
46 
47 void Component::removeRenderObject( const Core::Utils::Index& roIdx ) {
48  auto found = std::find( m_renderObjects.cbegin(), m_renderObjects.cend(), roIdx );
49  CORE_WARN_IF( found == m_renderObjects.cend(), " Render object not found in component" );
50  if ( ( found != m_renderObjects.cend() ) && getRoMgr() ) {
51  getRoMgr()->removeRenderObject( *found );
52  m_renderObjects.erase( found );
53  }
54  invalidateAabb();
55 }
56 
57 void Component::notifyRenderObjectExpired( const Core::Utils::Index& idx ) {
58  auto found = std::find( m_renderObjects.cbegin(), m_renderObjects.cend(), idx );
59  CORE_WARN_IF( found == m_renderObjects.cend(), " Render object not found in component" );
60  if ( found != m_renderObjects.cend() ) { m_renderObjects.erase( found ); }
61 }
62 
63 Core::Aabb Component::computeAabb() {
64  if ( !m_isAabbValid ) {
65  // component haven't any transformation, so ask render objects own aabb.
66  Core::Aabb aabb;
67  for ( const auto& roIndex : m_renderObjects ) {
68  auto ro =
69  RadiumEngine::getInstance()->getRenderObjectManager()->getRenderObject( roIndex );
70  if ( ro->isVisible() ) { aabb.extend( ro->computeAabb() ); }
71  }
72 
73  m_aabb = aabb;
74  m_isAabbValid = true;
75  }
76 
77  return m_aabb;
78 }
79 
80 void Component::invalidateAabb() {
81  m_isAabbValid = false;
82  m_entity->invalidateAabb();
83 }
84 
85 } // namespace Scene
86 } // namespace Engine
87 } // namespace Ra
Component(const std::string &name, Entity *entity)
CONSTRUCTOR.
Definition: Component.cpp:20
static Rendering::RenderObjectManager * getRoMgr()
Shortcut to access the render object manager.
Definition: Component.cpp:37
virtual ~Component()
DESTRUCTOR.
Definition: Component.cpp:25
void removeRenderObject(const Core::Utils::Index &roIdx)
Remove the render object from the component.
Definition: Component.cpp:47
virtual Entity * getEntity() const
Return the entity the component belongs to.
Definition: Component.hpp:53
Core::Utils::Index addRenderObject(Rendering::RenderObject *renderObject)
Add a new render object to the component. This adds the RO to the manager for drawing.
Definition: Component.cpp:41
An entity is an scene element. It ties together components with a transform.
Definition: Entity.hpp:23
void addComponent(Component *component)
Add a component to the given entity. Component ownership is transfered to the entity.
Definition: Entity.cpp:26
virtual void unregisterComponent(const Entity *entity, Component *component)
Definition: System.cpp:26
Definition: Cage.cpp:3