Radium Engine  1.5.0
CameraManager.cpp
1 #include <Engine/Scene/CameraManager.hpp>
2 
3 #include <Core/Asset/Camera.hpp>
4 #include <Core/Asset/FileData.hpp>
5 #include <Core/Tasks/Task.hpp>
6 #include <Core/Tasks/TaskQueue.hpp>
7 #include <Engine/FrameInfo.hpp>
8 #include <Engine/RadiumEngine.hpp>
9 #include <Engine/Rendering/RenderObject.hpp>
10 #include <Engine/Scene/CameraComponent.hpp>
11 #include <Engine/Scene/ComponentMessenger.hpp>
12 #include <Engine/Scene/Entity.hpp>
13 #include <Engine/Scene/SystemDisplay.hpp>
14 
15 namespace Ra {
16 namespace Engine {
17 namespace Scene {
18 
19 using namespace Core::Utils; // log
20 using namespace Core::Asset;
21 
23 
25  defaultCamera.setFOV( 60.0_ra * Core::Math::toRad );
26  defaultCamera.setZNear( 0.1_ra );
27  defaultCamera.setZFar( 1000.0_ra );
28  m_activeCamera = defaultCamera;
29 }
30 
32 
33  if ( count() > 0 ) {
34  LOG( logDEBUG ) << "CameraManager seems to be already initialized, do nothing.";
35  return;
36  }
37 }
38 
39 void CameraManager::activate( Core::Utils::Index index ) {
40 
41  if ( index.isInvalid() || index > count() ) {
42  LOG( logDEBUG ) << "Try to activate camera with an invalid/out of bound index. Ignored.";
43  return;
44  }
45  m_activeIndex = index;
46  updateActiveCameraData();
47 }
48 
50  // save current size
51  auto width = m_activeCamera.getWidth();
52  auto height = m_activeCamera.getHeight();
53  auto camComp = getCamera( m_activeIndex );
54  m_activeCamera = *camComp->getCamera();
55  Core::Transform localFrame = m_activeCamera.getFrame();
56  Core::Transform globalFrame = camComp->getEntity()->getTransform() * localFrame;
57  m_activeCamera.setFrame( globalFrame );
58  m_activeCamera.setViewport( width, height );
59  m_activeCamera.updateProjMatrix();
60  // notify observers on the change of the active camera data
61  m_activeCameraObservers.notify( m_activeIndex );
62 }
63 
64 Ra::Core::Utils::Index CameraManager::getCameraIndex( const CameraComponent* cam ) {
65  for ( size_t i = 0; i < m_data->size(); ++i ) {
66  if ( cam == ( *m_data )[i] ) return i;
67  }
68  return {};
69 }
70 
71 size_t CameraManager::count() const {
72  return m_data->size();
73 }
74 
76  const Engine::FrameInfo& /*frameInfo*/ ) {
77 
78  class RoUpdater : public Ra::Core::Task
79  {
80  public:
81  void process() override { m_camera->updateTransform(); }
82  std::string getName() const override { return "camera updater"; }
83  CameraComponent* m_camera;
84  };
85 
86  // only update visible components.
87  for ( size_t i = 0; i < m_data->size(); ++i ) {
88  auto comp = ( *m_data )[i];
89  auto ro = comp->getRenderObject();
90  if ( ro->isVisible() ) {
91  auto updater = std::make_unique<RoUpdater>();
92  updater->m_camera = comp;
93  taskQueue->registerTask( std::move( updater ) );
94  }
95  }
96 }
97 
98 void CameraManager::handleAssetLoading( Entity* entity, const FileData* filedata ) {
99  std::vector<Camera*> cameraData = filedata->getCameraData();
100  uint id = 0;
101  uint cpt = 0;
102  for ( const auto& data : cameraData ) {
103  std::string componentName = "CAMERA_" + entity->getName() + std::to_string( id++ );
104  auto comp = new CameraComponent( entity, componentName, 100, 100 );
105  *( comp->getCamera() ) = *data;
106  cpt++;
107  registerComponent( entity, comp );
108  }
109  LOG( logINFO ) << "CameraManager : loaded " << cpt << " Cameras. (now manager has " << count()
110  << " cameras).";
111 }
112 
113 void CameraManager::registerComponent( const Entity* entity, Component* component ) {
114  System::registerComponent( entity, component );
115  m_data->add( reinterpret_cast<CameraComponent*>( component ) );
118  auto idx = getCameraIndex( reinterpret_cast<CameraComponent*>( component ) );
119  entity->transformationObservers().attach( [this, idx]( const Entity* ) {
120  if ( idx == m_activeIndex ) { updateActiveCameraData(); }
121  } );
122 }
123 
124 void CameraManager::unregisterComponent( const Entity* entity, Component* component ) {
125  System::unregisterComponent( entity, component );
126  m_data->remove( reinterpret_cast<CameraComponent*>( component ) );
127 }
128 
130  for ( const auto& comp : this->m_components ) {
131  if ( comp.first == entity ) {
132  m_data->remove( reinterpret_cast<CameraComponent*>( comp.second ) );
133  }
134  }
136 }
137 
138 } // namespace Scene
139 } // namespace Engine
140 } // namespace Ra
Camera class storing the Camera frame and the projection properties The view direction is -z in camer...
Definition: Camera.hpp:16
TaskId registerTask(std::unique_ptr< Task > task)
Definition: TaskQueue.cpp:30
void unregisterComponent(const Entity *entity, Component *component) final
void activate(Core::Utils::Index index)
void generateTasks(Core::TaskQueue *taskQueue, const Engine::FrameInfo &frameInfo) override
Pure virtual method to be overridden by any system. Must register in taskQueue the operations that mu...
virtual void initialize()
Add a default camera.
void handleAssetLoading(Entity *entity, const Core::Asset::FileData *data) override
virtual Core::Utils::Index getCameraIndex(const CameraComponent *cam)
virtual size_t count() const
Number of managed Cameras.
void registerComponent(const Entity *entity, Component *component) final
void unregisterAllComponents(const Entity *entity) final
void updateActiveCameraData()
update the active camera data
static Ra::Core::Asset::Camera defaultCamera
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
Core::Utils::Observable< const Entity * > & transformationObservers() const
get a ref to transformation observers to add/remove an observer
Definition: Entity.hpp:123
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