diff --git a/lib/app/include/app/component/render/camera.hpp b/lib/app/include/app/component/render/camera.hpp index 1a26fc5bd..e1810f009 100644 --- a/lib/app/include/app/component/render/camera.hpp +++ b/lib/app/include/app/component/render/camera.hpp @@ -3,7 +3,6 @@ #include "app/component/render/enum_camera.hpp" #include "app/component/scene/transform_component.hpp" -#include "app/core/callback_event.hpp" #include "app/internal/constants.hpp" #include #include @@ -54,10 +53,10 @@ namespace VTX::App::Component::Render void print() const; - App::Core::CallbackEmitter onMatrixViewChange; - App::Core::CallbackEmitter onMatrixProjectionChange; - App::Core::CallbackEmitter onClipInfosChange; - App::Core::CallbackEmitter onProjectionChange; + Util::Callback onMatrixViewChange; + Util::Callback onMatrixProjectionChange; + Util::Callback onClipInfosChange; + Util::Callback onProjectionChange; protected: uint _screenWidth = 1u; diff --git a/lib/app/include/app/component/render/proxy_camera.hpp b/lib/app/include/app/component/render/proxy_camera.hpp new file mode 100644 index 000000000..29c369c76 --- /dev/null +++ b/lib/app/include/app/component/render/proxy_camera.hpp @@ -0,0 +1,27 @@ +#ifndef __VTX_APP_COMPONENT_RENDER_PROXY_CAMERA__ +#define __VTX_APP_COMPONENT_RENDER_PROXY_CAMERA__ + +#include "app/component/render/camera.hpp" +#include +#include + +namespace VTX::App::Component::Render +{ + class ProxyCamera + { + public: + ProxyCamera(); + void init(); + + VTX::Renderer::Proxy::Camera & getProxy() { return *_proxyPtr; } + const VTX::Renderer::Proxy::Camera & getProxy() const { return *_proxyPtr; } + + private: + void _generateProxy(); + void _initCallbacks(); + + std::unique_ptr _proxyPtr = nullptr; + }; + +} // namespace VTX::App::Component::Render +#endif diff --git a/lib/app/src/app/component/render/camera.cpp b/lib/app/src/app/component/render/camera.cpp index f971e0ac1..b513d9889 100644 --- a/lib/app/src/app/component/render/camera.cpp +++ b/lib/app/src/app/component/render/camera.cpp @@ -55,7 +55,7 @@ namespace VTX::App::Component::Render _near = Util::Math::max( 1e-1f, p_near ); _updateProjectionMatrix(); - onClipInfosChange.call( _near, _far ); + onClipInfosChange( _near, _far ); } void Camera::setFar( const float p_far ) { @@ -63,7 +63,7 @@ namespace VTX::App::Component::Render _far = Util::Math::max( 1e-1f, p_far ); _updateProjectionMatrix(); - onClipInfosChange.call( _near, _far ); + onClipInfosChange( _near, _far ); } void Camera::setFov( const float p_fov ) @@ -118,7 +118,7 @@ namespace VTX::App::Component::Render { _projection = p_projection; - onProjectionChange.call( _projection ); + onProjectionChange( _projection ); _updateViewMatrix(); _updateProjectionMatrix(); @@ -130,7 +130,7 @@ namespace VTX::App::Component::Render _transform->getPosition(), _transform->getPosition() + _transform->getFront(), _transform->getUp() ); - onMatrixViewChange.call( _viewMatrix ); + onMatrixViewChange( _viewMatrix ); if ( _projection == CAMERA_PROJECTION::ORTHOGRAPHIC ) _updateProjectionMatrix(); @@ -148,7 +148,7 @@ namespace VTX::App::Component::Render break; } - onMatrixProjectionChange.call( _projectionMatrix ); + onMatrixProjectionChange( _projectionMatrix ); // App::Old::VTXApp::get().MASK |= App::Old::Render::VTX_MASK_CAMERA_UPDATED; } diff --git a/lib/app/src/app/component/render/proxy_camera.cpp b/lib/app/src/app/component/render/proxy_camera.cpp new file mode 100644 index 000000000..d4a5642f8 --- /dev/null +++ b/lib/app/src/app/component/render/proxy_camera.cpp @@ -0,0 +1,54 @@ +#include "app/component/render/proxy_camera.hpp" +#include "app/component/scene/transform_component.hpp" +#include "app/vtx_app.hpp" +#include +#include +#include + +namespace VTX::App::Component::Render +{ + ProxyCamera::ProxyCamera() {} + + void ProxyCamera::init() + { + _generateProxy(); + _initCallbacks(); + } + + void ProxyCamera::_generateProxy() + { + Component::Render::Camera & cameraComp = MAIN_REGISTRY().getComponent( *this ); + Component::Scene::Transform & transformComp + = MAIN_REGISTRY().getComponent( *this ); + + _proxyPtr = std::make_unique( VTX::Renderer::Proxy::Camera { + &cameraComp.getViewMatrix(), + &cameraComp.getProjectionMatrix(), + + transformComp.getPosition(), + VEC2I_ZERO, + cameraComp.getNear(), + cameraComp.getFar(), + cameraComp.getProjection() == CAMERA_PROJECTION::PERSPECTIVE, + } ); + } + void ProxyCamera::_initCallbacks() + { + Component::Render::Camera & cameraComp = MAIN_REGISTRY().getComponent( *this ); + + cameraComp.onMatrixViewChange += [ this ]( const Mat4f & p_viewMatrix ) { _proxyPtr->onMatrixView(); }; + cameraComp.onMatrixProjectionChange += + [ this ]( const Mat4f & p_projMatrix ) { _proxyPtr->onMatrixProjection(); }; + + cameraComp.onClipInfosChange += + [ this ]( float p_near, float p_far ) { _proxyPtr->onCameraNearFar( p_near, p_far ); }; + cameraComp.onProjectionChange += [ this ]( CAMERA_PROJECTION p_projection ) + { _proxyPtr->onPerspective( p_projection == CAMERA_PROJECTION::PERSPECTIVE ); }; + + // Component::Scene::Transform & transformComp + // = MAIN_REGISTRY().getComponent( *this ); + // transformComp.onTransformChanged += [ this ]( const Util::Math::Transform & p_transform ) + //{ _proxyPtr->onCameraPosition( p_transform.getTranslationVector() ); }; + } + +} // namespace VTX::App::Component::Render diff --git a/lib/app/src/app/component/render/proxy_molecule.cpp b/lib/app/src/app/component/render/proxy_molecule.cpp index 9fc2ca506..3eb54ea8a 100644 --- a/lib/app/src/app/component/render/proxy_molecule.cpp +++ b/lib/app/src/app/component/render/proxy_molecule.cpp @@ -1,6 +1,6 @@ #include "app/component/render/proxy_molecule.hpp" +#include "app/application/system/ecs_system.hpp" #include "app/component/scene/transform_component.hpp" -#include "app/vtx_app.hpp" #include #include #include diff --git a/lib/app/src/app/entity/scene/camera_entity.cpp b/lib/app/src/app/entity/scene/camera_entity.cpp index a4b726a5a..9fc04d361 100644 --- a/lib/app/src/app/entity/scene/camera_entity.cpp +++ b/lib/app/src/app/entity/scene/camera_entity.cpp @@ -2,6 +2,7 @@ #include "app/application/ecs/registry_manager.hpp" #include "app/application/system/ecs_system.hpp" #include "app/component/render/camera.hpp" +#include "app/component/render/proxy_camera.hpp" #include "app/component/scene/transform_component.hpp" #include "app/vtx_app.hpp" @@ -14,13 +15,16 @@ namespace VTX::App::Entity::Scene { MAIN_REGISTRY().addComponent( p_entity ); MAIN_REGISTRY().addComponent( p_entity ); + MAIN_REGISTRY().addComponent( p_entity ); } void CameraEntityBuilder::setup( const Core::ECS::BaseEntity & p_entity, const Util::VariantMap & p_extraData ) { - Component::Render::Camera & cameraBehaviour - = MAIN_REGISTRY().getComponent( p_entity ); + Component::Render::Camera & camera = MAIN_REGISTRY().getComponent( p_entity ); + camera.init(); - cameraBehaviour.init(); + Component::Render::ProxyCamera & cameraProxy + = MAIN_REGISTRY().getComponent( p_entity ); + cameraProxy.init(); } } // namespace VTX::App::Entity::Scene diff --git a/lib/ui/src/ui/qt/application_qt.cpp b/lib/ui/src/ui/qt/application_qt.cpp index 78b0a78e9..db8560856 100644 --- a/lib/ui/src/ui/qt/application_qt.cpp +++ b/lib/ui/src/ui/qt/application_qt.cpp @@ -10,10 +10,6 @@ #include #include #include -#include -#include -#include -#include #include #include @@ -110,45 +106,6 @@ namespace VTX::UI::QT { Core::BaseUIApplication::_postInit( p_args ); - // App::VTXApp::get().onPreRender().addCallback( - // this, - // []( float p_deltaTime ) - // { - // // TODO: do not apply each frame, only when camera changes. - // RendererQt qtRenderer = QT_RENDERER(); - - // qtRenderer.get().setMatrixView( App::SCENE().getCamera().getViewMatrix() ); - // qtRenderer.get().setMatrixProjection( App::SCENE().getCamera().getProjectionMatrix() ); - // qtRenderer.get().setCameraClipInfos( - // App::SCENE().getCamera().getNear(), App::SCENE().getCamera().getFar() - // ); - // } - //); - - App::SCENE().getCamera().onMatrixViewChange.addCallback( - this, []( const Mat4f & p_matrix ) { QT_RENDERER().get().setMatrixView( p_matrix ); } - ); - App::SCENE().getCamera().onMatrixProjectionChange.addCallback( - this, []( const Mat4f & p_matrix ) { QT_RENDERER().get().setMatrixProjection( p_matrix ); } - ); - App::SCENE().getCamera().getTransform().onTransformChanged.addCallback( - this, - []( const VTX::Util::Math::Transform & p_transform ) - { QT_RENDERER().get().setCameraPosition( p_transform.getTranslationVector() ); } - ); - App::SCENE().getCamera().onClipInfosChange.addCallback( - this, - []( const float p_near, const float p_far ) { QT_RENDERER().get().setCameraClipInfos( p_near, p_far ); } - ); - App::SCENE().getCamera().onProjectionChange.addCallback( - this, - []( const App::Component::Render::CAMERA_PROJECTION p_projection ) { - QT_RENDERER().get().setPerspective( - p_projection == App::Component::Render::CAMERA_PROJECTION::PERSPECTIVE - ); - } - ); - _mainWindow->show(); _mainWindow->initWindowLayout(); } diff --git a/lib/ui/src/ui/qt/tool/render/widget/opengl_widget.cpp b/lib/ui/src/ui/qt/tool/render/widget/opengl_widget.cpp index 17386ff0a..c13ab36ae 100644 --- a/lib/ui/src/ui/qt/tool/render/widget/opengl_widget.cpp +++ b/lib/ui/src/ui/qt/tool/render/widget/opengl_widget.cpp @@ -29,13 +29,6 @@ namespace VTX::UI::QT::Tool::Render::Widget App::RENDERER().build( defaultFramebufferObject() ); App::VTXApp::get().onPostRender().addCallback( this, [ this ]( float p_deltaTime ) { update(); } ); App::RENDERER().setProxyColorLayout( VTX::Core::ChemDB::Color::COLOR_LAYOUT_JMOL ); - - RendererQt renderer = QT_RENDERER(); - renderer.get().setMatrixView( App::SCENE().getCamera().getViewMatrix() ); - renderer.get().setMatrixProjection( App::SCENE().getCamera().getProjectionMatrix() ); - renderer.get().setCameraPosition( App::SCENE().getCamera().getTransform().getPosition() ); - renderer.get().setCameraClipInfos( App::SCENE().getCamera().getNear(), App::SCENE().getCamera().getFar() ); - renderer.get().setPerspective( App::SCENE().getCamera().isPerspective() ); } void OpenGLWidget::paintGL() { VTX::App::VTXApp::get().getRenderer().render( 0.15f ); }