Radium Engine  1.5.20
Loading...
Searching...
No Matches
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>
14
15namespace Ra {
16namespace Engine {
17namespace Scene {
18
19using namespace Core::Utils; // log
20using 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 );
29}
30
32
33 if ( count() > 0 ) {
34 LOG( logDEBUG ) << "CameraManager seems to be already initialized, do nothing.";
35 return;
36 }
37}
38
39void 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;
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 );
60 // notify observers on the change of the active camera data
62}
63
64Ra::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
71size_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
98void 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
113void 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
124void 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
void setViewport(Scalar width, Scalar height)
Change the viewport size. Also compute aspectRatio.
Definition Camera.cpp:50
void updateProjMatrix()
Update the projection matrix according to the current parameters.
Definition Camera.cpp:66
Scalar getWidth() const
Return the width of the viewport.
Definition Camera.hpp:311
Scalar getHeight() const
Return the height of the viewport.
Definition Camera.hpp:315
void setZNear(Scalar zNear)
Set the Z Near plane distance to zNear.
Definition Camera.hpp:288
void setZFar(Scalar zFar)
Set the Z Far plane distance to zFar.
Definition Camera.hpp:297
void setFrame(const Core::Transform &frame)
Set the frame of the camera to frame.
Definition Camera.hpp:240
void setFOV(Scalar fov)
Definition Camera.hpp:276
Core::Transform getFrame() const
Definition Camera.hpp:236
This class allows tasks to be registered and then executed in parallel on separate threads.
Definition TaskQueue.hpp:48
TaskId registerTask(std::unique_ptr< Task > task)
Definition TaskQueue.cpp:33
void notify(Args... p) const
Notify (i.e. call) each attached observer with argument p.
Core::Utils::Index m_activeIndex
active camera index
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...
Core::Utils::Observable< Core::Utils::Index > m_activeCameraObservers
Observers on active camera changes.
Ra::Core::Asset::Camera m_activeCamera
active camera data, active camera hasn't any component just pure data.
virtual void initialize()
Add a default camera.
std::unique_ptr< CameraStorage > m_data
Stores the object that stores the Cameras...
virtual const CameraComponent * getCamera(size_t i) const =0
Get a pointer to the i-th 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:122
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
T move(T... args)
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)