1 #include <Headless/CLIViewer.hpp>
3 #include <Core/Asset/Camera.hpp>
4 #include <Core/Asset/FileLoaderInterface.hpp>
5 #include <Core/Tasks/TaskQueue.hpp>
6 #include <Core/Utils/Log.hpp>
8 #include <Engine/Data/ViewingParameters.hpp>
9 #include <Engine/RadiumEngine.hpp>
10 #include <Engine/Rendering/Renderer.hpp>
11 #include <Engine/Scene/DefaultCameraManager.hpp>
12 #include <Engine/Scene/DirLight.hpp>
13 #include <Engine/Scene/GeometrySystem.hpp>
14 #include <Engine/Scene/SkeletonBasedAnimationSystem.hpp>
15 #include <Engine/Scene/SystemDisplay.hpp>
19 using namespace Ra::Core::Utils;
21 constexpr
int defaultSystemPriority = 1000;
26 addOption(
"-f,--file", m_parameters.m_dataFile,
"Data file to process." )
27 ->check( CLI::ExistingFile );
28 addOption(
"-s,--size", m_parameters.m_size,
"Size of the computed image." )->delimiter(
'x' );
29 addFlag(
"-a,--animation", m_parameters.m_animationEnable,
"Enable Radium Animation system." );
33 if ( m_engineInitialized ) {
34 m_glContext->makeCurrent();
37 Ra::Engine::RadiumEngine::destroyInstance();
38 m_glContext->doneCurrent();
48 if ( parseResult != 0 ) {
49 if ( parseResult != 1 ) {
50 LOG( logERROR ) <<
"Invalid command line argument, the application can't run";
55 if ( !m_glContext->isValid() ) {
56 LOG( logERROR ) <<
"Invalid openglContext, the application can't run";
60 m_glContext->resize( m_parameters.m_size );
61 LOG( logINFO ) <<
"CLIViewer :\n" << m_glContext->getInfo();
66 m_engine = Ra::Engine::RadiumEngine::createInstance();
68 m_engineInitialized =
true;
72 "GeometrySystem",
new Ra::Engine::Scene::GeometrySystem, defaultSystemPriority );
74 if ( m_parameters.m_animationEnable ) {
79 defaultSystemPriority );
83 m_glContext->makeCurrent();
85 m_glContext->doneCurrent();
88 m_glContext->resizeListener().attach(
89 [
this](
int width,
int height ) {
resize( width, height ); } );
95 if ( m_parameters.m_animationEnable ) {
98 if ( animationSystem ) { animationSystem->
toggleSkeleton(
false ); }
110 m_camera->getViewMatrix(), m_camera->
getProjMatrix(), timeStep };
111 m_renderer->render( data );
117 return m_renderer->grabFrame( w, h );
121 m_glContext->makeCurrent();
123 m_renderer.reset( renderer );
124 m_renderer->initialize( m_parameters.m_size[0], m_parameters.m_size[1] );
126 m_glContext->doneCurrent();
139 m_renderer->buildAllRenderTechniques();
140 if ( !m_renderer->hasLight() ) {
142 Ra::Engine::Scene::SystemEntity::getInstance(),
"headlight" );
143 headlight->setColor( Ra::Core::Utils::Color::Grey( 2.0_ra ) );
145 m_renderer->addLight( headlight );
151 m_glContext->makeCurrent();
153 m_glContext->doneCurrent();
157 if ( on ) { m_glContext->makeCurrent(); }
158 else { m_glContext->doneCurrent(); }
163 Ra::Engine::RadiumEngine::getInstance()->getSystem(
"DefaultCameraManager" ) );
165 if ( camIdx.isInvalid() || cameraManager->count() <= size_t( camIdx ) ) {
167 m_camera->
setFOV( 60.0_ra * Ra::Core::Math::toRad );
171 auto aabb = Ra::Engine::RadiumEngine::getInstance()->computeSceneAabb();
172 Scalar f = m_camera->
getFOV();
174 const Scalar r = ( aabb.max() - aabb.min() ).norm() / 2_ra;
175 const Scalar x = r / std::sin( f / 2_ra );
176 const Scalar y = r / std::sin( f * a / 2_ra );
177 Scalar d = std::max( std::max( x, y ), 0.001_ra );
180 Ra::Core::Vector3( aabb.center().x(), aabb.center().y(), aabb.center().z() + d ) );
181 m_camera->
setDirection( Ra::Core::Vector3( 0_ra, 0_ra, -1_ra ) );
183 std::max( d + ( aabb.max().z() - aabb.min().z() ) * 2_ra, m_camera->
getZFar() );
187 cameraManager->activate( camIdx );
188 m_camera = cameraManager->getActiveCamera();
193 m_parameters.m_imgPrefix = std::move( s );
197 m_exposedWindow = on;
198 if ( m_exposedWindow ) {
199 m_glContext->resize( m_parameters.m_size );
200 m_glContext->show( mode, delay );
202 else { m_glContext->hide(); }
206 m_glContext->renderLoop( render );
211 m_renderer->resize( width, height );
Camera class storing the Camera frame and the projection properties The view direction is -z in camer...
void setViewport(Scalar width, Scalar height)
Change the viewport size. Also compute aspectRatio.
Scalar getZFar() const
Return the Z Far plane distance from the camera.
void setPosition(const Core::Vector3 &position)
Set the position of the camera to position.
void setDirection(const Core::Vector3 &direction)
void setZNear(Scalar zNear)
Set the Z Near plane distance to zNear.
void setZFar(Scalar zFar)
Set the Z Far plane distance to zFar.
Core::Matrix4 getProjMatrix() const
Return the projection matrix.
Scalar getAspect() const
Return the aspect ratio of the viewport.
Core::Vector3 getDirection() const
Return the direction the camera is looking at.
void flushTaskQueue()
Erases all tasks. Will assert if tasks are unprocessed.
void waitForTasks()
Blocks until all tasks and dependencies are finished.
Scene::System * getSystem(const std::string &system) const
bool loadFile(const std::string &file)
void getTasks(Core::TaskQueue *taskQueue, Scalar dt)
void registerFileLoader(std::shared_ptr< Core::Asset::FileLoaderInterface > fileLoader)
bool setConstantTimeStep(Scalar dt, bool forceConstantTime=false)
Sets the time delta between two frames for Constant-time time flow.
bool registerSystem(const std::string &name, Scene::System *system, int priority=1)
void toggleSkeleton(const bool status)
Toggles skeleton display for all AnimationComponents.
virtual int init(int argc, const char *argv[])
CLI::Option * addFlag(Args &&... args)
CLI::Option * addOption(Args &&... args)
int init(int argc, const char *argv[]) override
Application initialization method.
void showWindow(bool on=true, OpenGLContext::EventMode mode=OpenGLContext::EventMode::POLL, float delay=1.f/60.f)
void addDataFileLoader(Ra::Core::Asset::FileLoaderInterface *loader)
virtual ~CLIViewer()
Base destructor.
Ra::Engine::RadiumEngine * m_engine
Instance of the radium engine.
void setImageNamePrefix(std::string s)
const ViewerParameters & getCommandLineParameters() const
std::unique_ptr< unsigned char[]> grabFrame(size_t &w, size_t &h) const
void bindOpenGLContext(bool on=true)
void setCamera(Ra::Core::Utils::Index camIdx=0)
CLIViewer(std::unique_ptr< OpenGLContext > context)
Construct the viewer using an OpenGL context of the given version.
void setRenderer(Ra::Engine::Rendering::Renderer *renderer)
int oneFrame(float timeStep=1.f/60.f)
void resize(int width, int height)
Observer of the resize event on the OpenGLContext.
void renderLoop(std::function< void(float)> render)
void openGlAddOns(std::function< void()> f)
the set of viewing parameters extracted from the camera and given to the renderer