Radium Engine  1.5.20
Loading...
Searching...
No Matches
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>
13
14namespace Ra {
15
16namespace Engine {
17namespace Scene {
18
19using namespace Core::Utils; // log
20using namespace Core::Asset;
21
23
24//
25// Renderer pre/post calls
26//
27
28size_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
41void 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
116void LightManager::registerComponent( const Entity* entity, Component* component ) {
117 System::registerComponent( entity, component );
118 m_data->add( reinterpret_cast<Scene::Light*>( component ) );
119}
120
121void 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
This class allows tasks to be registered and then executed in parallel on separate threads.
Definition TaskQueue.hpp:48
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
std::unique_ptr< LightStorage > m_data
Stores the object that stores the lights...
void registerComponent(const Entity *entity, Component *component) final
void unregisterComponent(const Entity *entity, Component *component) final
std::vector< std::pair< const Entity *, Component * > > m_components
List of active components.
Definition System.hpp:103
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
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
Structure passed to each system before they fill the task queue.
Definition FrameInfo.hpp:8
T to_string(T... args)