Skip to content

Commit

Permalink
-Link camera proxy in app
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Dacquay committed Mar 13, 2024
1 parent 2a41932 commit 15376dc
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 64 deletions.
9 changes: 4 additions & 5 deletions lib/app/include/app/component/render/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <util/callback.hpp>
#include <util/types.hpp>
Expand Down Expand Up @@ -54,10 +53,10 @@ namespace VTX::App::Component::Render

void print() const;

App::Core::CallbackEmitter<Mat4f> onMatrixViewChange;
App::Core::CallbackEmitter<Mat4f> onMatrixProjectionChange;
App::Core::CallbackEmitter<float, float> onClipInfosChange;
App::Core::CallbackEmitter<CAMERA_PROJECTION> onProjectionChange;
Util::Callback<Mat4f> onMatrixViewChange;
Util::Callback<Mat4f> onMatrixProjectionChange;
Util::Callback<float, float> onClipInfosChange;
Util::Callback<CAMERA_PROJECTION> onProjectionChange;

protected:
uint _screenWidth = 1u;
Expand Down
27 changes: 27 additions & 0 deletions lib/app/include/app/component/render/proxy_camera.hpp
Original file line number Diff line number Diff line change
@@ -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 <memory>
#include <renderer/proxy/camera.hpp>

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<VTX::Renderer::Proxy::Camera> _proxyPtr = nullptr;
};

} // namespace VTX::App::Component::Render
#endif
10 changes: 5 additions & 5 deletions lib/app/src/app/component/render/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ 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 )
{
// Avoid too little value.
_far = Util::Math::max( 1e-1f, p_far );

_updateProjectionMatrix();
onClipInfosChange.call( _near, _far );
onClipInfosChange( _near, _far );
}

void Camera::setFov( const float p_fov )
Expand Down Expand Up @@ -118,7 +118,7 @@ namespace VTX::App::Component::Render
{
_projection = p_projection;

onProjectionChange.call( _projection );
onProjectionChange( _projection );

_updateViewMatrix();
_updateProjectionMatrix();
Expand All @@ -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();
Expand All @@ -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;
}

Expand Down
54 changes: 54 additions & 0 deletions lib/app/src/app/component/render/proxy_camera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "app/component/render/proxy_camera.hpp"
#include "app/component/scene/transform_component.hpp"
#include "app/vtx_app.hpp"
#include <core/chemdb/atom.hpp>
#include <util/logger.hpp>
#include <util/math.hpp>

namespace VTX::App::Component::Render
{
ProxyCamera::ProxyCamera() {}

void ProxyCamera::init()
{
_generateProxy();
_initCallbacks();
}

void ProxyCamera::_generateProxy()
{
Component::Render::Camera & cameraComp = MAIN_REGISTRY().getComponent<Component::Render::Camera>( *this );
Component::Scene::Transform & transformComp
= MAIN_REGISTRY().getComponent<Component::Scene::Transform>( *this );

_proxyPtr = std::make_unique<VTX::Renderer::Proxy::Camera>( 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<Component::Render::Camera>( *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<Component::Scene::Transform>( *this );
// transformComp.onTransformChanged += [ this ]( const Util::Math::Transform & p_transform )
//{ _proxyPtr->onCameraPosition( p_transform.getTranslationVector() ); };
}

} // namespace VTX::App::Component::Render
2 changes: 1 addition & 1 deletion lib/app/src/app/component/render/proxy_molecule.cpp
Original file line number Diff line number Diff line change
@@ -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 <core/chemdb/atom.hpp>
#include <util/logger.hpp>
#include <util/math.hpp>
Expand Down
10 changes: 7 additions & 3 deletions lib/app/src/app/entity/scene/camera_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -14,13 +15,16 @@ namespace VTX::App::Entity::Scene
{
MAIN_REGISTRY().addComponent<Component::Scene::Transform>( p_entity );
MAIN_REGISTRY().addComponent<Component::Render::Camera>( p_entity );
MAIN_REGISTRY().addComponent<Component::Render::ProxyCamera>( p_entity );
}

void CameraEntityBuilder::setup( const Core::ECS::BaseEntity & p_entity, const Util::VariantMap & p_extraData )
{
Component::Render::Camera & cameraBehaviour
= MAIN_REGISTRY().getComponent<Component::Render::Camera>( p_entity );
Component::Render::Camera & camera = MAIN_REGISTRY().getComponent<Component::Render::Camera>( p_entity );
camera.init();

cameraBehaviour.init();
Component::Render::ProxyCamera & cameraProxy
= MAIN_REGISTRY().getComponent<Component::Render::ProxyCamera>( p_entity );
cameraProxy.init();
}
} // namespace VTX::App::Entity::Scene
43 changes: 0 additions & 43 deletions lib/ui/src/ui/qt/application_qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
#include <QLoggingCategory>
#include <QPalette>
#include <QStyleFactory>
#include <app/application/scene.hpp>
#include <app/component/render/camera.hpp>
#include <app/component/scene/transform_component.hpp>
#include <app/vtx_app.hpp>
#include <util/logger.hpp>
#include <util/math/transform.hpp>

Expand Down Expand Up @@ -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();
}
Expand Down
7 changes: 0 additions & 7 deletions lib/ui/src/ui/qt/tool/render/widget/opengl_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ); }
Expand Down

0 comments on commit 15376dc

Please sign in to comment.