Radium Engine  1.5.0
LightManager.cpp
1 #include <Core/Asset/FileData.hpp>
2 #include <Core/Tasks/Task.hpp>
3 #include <Core/Tasks/TaskQueue.hpp>
4 #include <Engine/FrameInfo.hpp>
5 #include <Engine/RadiumEngine.hpp>
6 #include <Engine/Scene/ComponentMessenger.hpp>
7 #include <Engine/Scene/DirLight.hpp>
8 #include <Engine/Scene/Entity.hpp>
9 #include <Engine/Scene/LightManager.hpp>
10 #include <Engine/Scene/PointLight.hpp>
11 #include <Engine/Scene/SpotLight.hpp>
12 #include <Engine/Scene/SystemDisplay.hpp>
13 
14 namespace Ra {
15 
16 namespace Engine {
17 namespace Scene {
18 
19 using namespace Core::Utils; // log
20 using namespace Core::Asset;
21 
22 LightManager::~LightManager() = default;
23 
24 //
25 // Renderer pre/post calls
26 //
27 
28 size_t LightManager::count() const {
29  return m_data->size();
30 }
31 
32 //
33 // System
34 //
35 
37  const Engine::FrameInfo& /*frameInfo*/ ) {
38  // do nothing as this system only manage light related asset loading
39 }
40 
41 void LightManager::handleAssetLoading( Entity* entity, const FileData* filedata ) {
42  std::vector<LightData*> lightData = filedata->getLightData();
43  uint id = 0;
44 
45  // If thereis some lights already in the manager, just remove from the manager the lights that
46  // belong to the system entity (e.g. the headlight) from the list of managed lights. Beware to
47  // not destroy the headlight component, that do not belong to this system, so that it could be
48  // added again
49  for ( size_t i = 0; i < m_data->size(); ) {
50  auto l = ( *m_data )[i];
51  if ( l->getEntity() == SystemEntity::getInstance() ) { m_data->remove( l ); }
52  else { ++i; }
53  }
54 
55  for ( const auto& data : lightData ) {
56  std::string componentName =
57  "LIGHT_" + data->getName() + " (" + std::to_string( id++ ) + ")";
58  Scene::Light* comp = nullptr;
59 
60  switch ( data->getType() ) {
61  case LightData::DIRECTIONAL_LIGHT: {
62 
63  auto thelight = new DirectionalLight( entity, componentName );
64  thelight->setColor( data->m_color );
65  thelight->setDirection( data->m_dirlight.direction );
66  comp = thelight;
67  break;
68  }
69  case LightData::POINT_LIGHT: {
70  auto thelight = new PointLight( entity, componentName );
71  thelight->setColor( data->m_color );
72  thelight->setPosition( data->m_pointlight.position );
73  thelight->setAttenuation( data->m_pointlight.attenuation.constant,
74  data->m_pointlight.attenuation.linear,
75  data->m_pointlight.attenuation.quadratic );
76  comp = thelight;
77  break;
78  }
79  case LightData::SPOT_LIGHT: {
80  auto thelight = new SpotLight( entity, componentName );
81  thelight->setColor( data->m_color );
82  thelight->setPosition( data->m_spotlight.position );
83  thelight->setDirection( data->m_spotlight.direction );
84  thelight->setAttenuation( data->m_spotlight.attenuation.constant,
85  data->m_spotlight.attenuation.linear,
86  data->m_spotlight.attenuation.quadratic );
87  thelight->setInnerAngleInRadians( data->m_spotlight.innerAngle );
88  thelight->setOuterAngleInRadians( data->m_spotlight.outerAngle );
89  comp = thelight;
90  break;
91  }
92  case LightData::AREA_LIGHT: {
93  // Radium-V2 : manage real area light. For the moment, transform them in point light
94  // using given position
95  auto thelight = new PointLight( entity, componentName );
96  thelight->setColor( data->m_color );
97  thelight->setPosition( data->m_arealight.position );
98  thelight->setAttenuation( data->m_arealight.attenuation.constant,
99  data->m_arealight.attenuation.linear,
100  data->m_arealight.attenuation.quadratic );
101  comp = thelight;
102  break;
103  }
104  default:
105  comp = nullptr;
106  }
107 
108  // comp should be allocated in LightStorage (well, not sure ...)
109  if ( !comp ) continue;
110 
111  registerComponent( entity, comp );
112  }
113  LOG( logINFO ) << "LightManager : loaded " << count() << " lights.";
114 }
115 
116 void LightManager::registerComponent( const Entity* entity, Component* component ) {
117  System::registerComponent( entity, component );
118  m_data->add( reinterpret_cast<Scene::Light*>( component ) );
119 }
120 
121 void LightManager::unregisterComponent( const Entity* entity, Component* component ) {
122  System::unregisterComponent( entity, component );
123  m_data->remove( reinterpret_cast<Scene::Light*>( component ) );
124 }
125 
127  for ( const auto& comp : this->m_components ) {
128  if ( comp.first == entity ) {
129  m_data->remove( reinterpret_cast<Scene::Light*>( comp.second ) );
130  }
131  }
133 }
134 
135 } // namespace Scene
136 } // namespace Engine
137 } // namespace Ra
A component is an element that can be updated by a system. It is also linked to some other components...
Definition: Component.hpp:31
An entity is an scene element. It ties together components with a transform.
Definition: Entity.hpp:23
void generateTasks(Core::TaskQueue *taskQueue, const Engine::FrameInfo &frameInfo) override
Do nothing as this system only manage light related asset loading.
virtual size_t count() const
Number of lights. This is still a work in progress. The idea is to make it possible for a LightManage...
void handleAssetLoading(Entity *entity, const Core::Asset::FileData *data) override
Transform loaded file data to usable entities and component in the engine.
~LightManager() override
Virtual destructor.
void unregisterAllComponents(const Entity *entity) final
void registerComponent(const Entity *entity, Component *component) final
void unregisterComponent(const Entity *entity, Component *component) final
virtual void unregisterAllComponents(const Entity *entity)
Definition: System.cpp:40
virtual void registerComponent(const Entity *entity, Component *component)
Definition: System.cpp:10
virtual void unregisterComponent(const Entity *entity, Component *component)
Definition: System.cpp:26
Definition: Cage.cpp:3
Structure passed to each system before they fill the task queue.
Definition: FrameInfo.hpp:8