Loading [MathJax]/extensions/TeX/AMSmath.js
Radium Engine  1.5.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
CLIViewer.cpp
1 #include <Headless/CLIViewer.hpp>
2 
3 #include <Core/Asset/Camera.hpp>
4 #include <Core/Asset/FileLoaderInterface.hpp>
5 #include <Core/Tasks/TaskQueue.hpp>
6 #include <Core/Utils/Log.hpp>
7 
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>
16 
17 namespace Ra {
18 namespace Headless {
19 using namespace Ra::Core::Utils;
20 
21 constexpr int defaultSystemPriority = 1000;
22 
23 CLIViewer::CLIViewer( std::unique_ptr<OpenGLContext> context ) :
24  CLIBaseApplication(), m_glContext { std::move( context ) } {
25  // add ->required() to force user to give a filename;
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." );
30 }
31 
33  if ( m_engineInitialized ) {
34  m_glContext->makeCurrent();
35  m_renderer.reset();
36  m_engine->cleanup();
37  Ra::Engine::RadiumEngine::destroyInstance();
38  m_glContext->doneCurrent();
39  }
40 }
41 
42 const CLIViewer::ViewerParameters& CLIViewer::getCommandLineParameters() const {
43  return m_parameters;
44 }
45 
46 int CLIViewer::init( int argc, const char* argv[] ) {
47  int parseResult = CLIBaseApplication::init( argc, argv );
48  if ( parseResult != 0 ) {
49  if ( parseResult != 1 ) {
50  LOG( logERROR ) << "Invalid command line argument, the application can't run";
51  }
52  return 1;
53  };
54  // Do the Viewer init
55  if ( !m_glContext->isValid() ) {
56  LOG( logERROR ) << "Invalid openglContext, the application can't run";
57  return 1;
58  }
59 
60  m_glContext->resize( m_parameters.m_size );
61  LOG( logINFO ) << "CLIViewer :\n" << m_glContext->getInfo();
62 
63  // Initialize the Radium engine environment
64 
65  // Create engine
66  m_engine = Ra::Engine::RadiumEngine::createInstance();
68  m_engineInitialized = true;
69 
70  // Register the GeometrySystem converting loaded assets to meshes
72  "GeometrySystem", new Ra::Engine::Scene::GeometrySystem, defaultSystemPriority );
73 
74  if ( m_parameters.m_animationEnable ) {
75  // Register the SkeletonBasedAnimationSystem converting loaded assets to
76  // skeletons and skinning data
77  m_engine->registerSystem( "SkeletonBasedAnimationSystem",
79  defaultSystemPriority );
80  }
81 
82  // initialize OpenGL part of the Engine
83  m_glContext->makeCurrent();
85  m_glContext->doneCurrent();
86 
87  // register listeners on the OpenGL Context
88  m_glContext->resizeListener().attach(
89  [this]( int width, int height ) { resize( width, height ); } );
90  // Init is OK
91  return 0;
92 }
93 
94 int CLIViewer::oneFrame( float timeStep ) {
95  if ( m_parameters.m_animationEnable ) {
96  auto animationSystem = dynamic_cast<Ra::Engine::Scene::SkeletonBasedAnimationSystem*>(
97  m_engine->getSystem( "SkeletonBasedAnimationSystem" ) );
98  if ( animationSystem ) { animationSystem->toggleSkeleton( false ); }
99  m_engine->setConstantTimeStep( timeStep );
100  m_engine->step();
101  }
102 
103  Ra::Core::TaskQueue tasks( std::thread::hardware_concurrency() - 1 );
104  m_engine->getTasks( &tasks, Scalar( timeStep ) );
105  tasks.startTasks();
106  tasks.waitForTasks();
107  tasks.flushTaskQueue();
108 
110  m_camera->getViewMatrix(), m_camera->getProjMatrix(), timeStep };
111  m_renderer->render( data );
112 
113  return 0;
114 }
115 
116 std::unique_ptr<unsigned char[]> CLIViewer::grabFrame( size_t& w, size_t& h ) const {
117  return m_renderer->grabFrame( w, h );
118 }
119 
121  m_glContext->makeCurrent();
122 
123  m_renderer.reset( renderer );
124  m_renderer->initialize( m_parameters.m_size[0], m_parameters.m_size[1] );
125 
126  m_glContext->doneCurrent();
127 }
128 
129 void CLIViewer::addDataFileLoader( Ra::Core::Asset::FileLoaderInterface* loader ) {
130  m_engine->registerFileLoader( std::shared_ptr<Ra::Core::Asset::FileLoaderInterface>( loader ) );
131 }
132 
134  m_engine->loadFile( m_parameters.m_dataFile );
135 }
136 
138  if ( m_renderer ) {
139  m_renderer->buildAllRenderTechniques();
140  if ( !m_renderer->hasLight() ) {
141  auto headlight = new Ra::Engine::Scene::DirectionalLight(
142  Ra::Engine::Scene::SystemEntity::getInstance(), "headlight" );
143  headlight->setColor( Ra::Core::Utils::Color::Grey( 2.0_ra ) );
144  headlight->setDirection( m_camera->getDirection() );
145  m_renderer->addLight( headlight );
146  }
147  }
148 }
149 
150 void CLIViewer::openGlAddOns( std::function<void()> f ) {
151  m_glContext->makeCurrent();
152  f();
153  m_glContext->doneCurrent();
154 }
155 
157  if ( on ) { m_glContext->makeCurrent(); }
158  else { m_glContext->doneCurrent(); }
159 }
160 
161 void CLIViewer::setCamera( Ra::Core::Utils::Index camIdx ) {
162  auto cameraManager = static_cast<Ra::Engine::Scene::CameraManager*>(
163  Ra::Engine::RadiumEngine::getInstance()->getSystem( "DefaultCameraManager" ) );
164 
165  if ( camIdx.isInvalid() || cameraManager->count() <= size_t( camIdx ) ) {
166  m_camera = new Ra::Core::Asset::Camera( m_parameters.m_size[0], m_parameters.m_size[1] );
167  m_camera->setFOV( 60.0_ra * Ra::Core::Math::toRad );
168  m_camera->setZNear( 0.1_ra );
169  m_camera->setZFar( 100_ra );
170 
171  auto aabb = Ra::Engine::RadiumEngine::getInstance()->computeSceneAabb();
172  Scalar f = m_camera->getFOV();
173  Scalar a = m_camera->getAspect();
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 );
178 
179  m_camera->setPosition(
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 ) );
182  Scalar zfar =
183  std::max( d + ( aabb.max().z() - aabb.min().z() ) * 2_ra, m_camera->getZFar() );
184  m_camera->setZFar( zfar );
185  }
186  else {
187  cameraManager->activate( camIdx );
188  m_camera = cameraManager->getActiveCamera();
189  }
190 }
191 
192 void CLIViewer::setImageNamePrefix( std::string s ) {
193  m_parameters.m_imgPrefix = std::move( s );
194 }
195 
196 void CLIViewer::showWindow( bool on, OpenGLContext::EventMode mode, float delay ) {
197  m_exposedWindow = on;
198  if ( m_exposedWindow ) {
199  m_glContext->resize( m_parameters.m_size );
200  m_glContext->show( mode, delay );
201  }
202  else { m_glContext->hide(); }
203 }
204 
205 void CLIViewer::renderLoop( std::function<void( float )> render ) {
206  m_glContext->renderLoop( render );
207 }
208 
209 void CLIViewer::resize( int width, int height ) {
210  if ( m_renderer ) {
211  m_renderer->resize( width, height );
212  m_camera->setViewport( width, height );
213  }
214 }
215 
216 } // namespace Headless
217 } // 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
Scalar getZFar() const
Return the Z Far plane distance from the camera.
Definition: Camera.hpp:293
void setPosition(const Core::Vector3 &position)
Set the position of the camera to position.
Definition: Camera.hpp:248
void setDirection(const Core::Vector3 &direction)
Definition: Camera.cpp:32
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
Core::Matrix4 getProjMatrix() const
Return the projection matrix.
Definition: Camera.hpp:335
Scalar getFOV() const
Definition: Camera.hpp:272
Scalar getAspect() const
Return the aspect ratio of the viewport.
Definition: Camera.hpp:319
void setFOV(Scalar fov)
Definition: Camera.hpp:276
Core::Vector3 getDirection() const
Return the direction the camera is looking at.
Definition: Camera.hpp:254
void flushTaskQueue()
Erases all tasks. Will assert if tasks are unprocessed.
Definition: TaskQueue.cpp:255
void waitForTasks()
Blocks until all tasks and dependencies are finished.
Definition: TaskQueue.cpp:239
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.
Definition: CLIViewer.cpp:46
void showWindow(bool on=true, OpenGLContext::EventMode mode=OpenGLContext::EventMode::POLL, float delay=1.f/60.f)
Definition: CLIViewer.cpp:196
void addDataFileLoader(Ra::Core::Asset::FileLoaderInterface *loader)
Definition: CLIViewer.cpp:129
virtual ~CLIViewer()
Base destructor.
Definition: CLIViewer.cpp:32
Ra::Engine::RadiumEngine * m_engine
Instance of the radium engine.
Definition: CLIViewer.hpp:50
void setImageNamePrefix(std::string s)
Definition: CLIViewer.cpp:192
const ViewerParameters & getCommandLineParameters() const
Definition: CLIViewer.cpp:42
std::unique_ptr< unsigned char[]> grabFrame(size_t &w, size_t &h) const
Definition: CLIViewer.cpp:116
void bindOpenGLContext(bool on=true)
Definition: CLIViewer.cpp:156
void setCamera(Ra::Core::Utils::Index camIdx=0)
Definition: CLIViewer.cpp:161
CLIViewer(std::unique_ptr< OpenGLContext > context)
Construct the viewer using an OpenGL context of the given version.
Definition: CLIViewer.cpp:23
void setRenderer(Ra::Engine::Rendering::Renderer *renderer)
Definition: CLIViewer.cpp:120
int oneFrame(float timeStep=1.f/60.f)
Definition: CLIViewer.cpp:94
void resize(int width, int height)
Observer of the resize event on the OpenGLContext.
Definition: CLIViewer.cpp:209
void renderLoop(std::function< void(float)> render)
Definition: CLIViewer.cpp:205
void openGlAddOns(std::function< void()> f)
Definition: CLIViewer.cpp:150
Definition: Cage.cpp:3
the set of viewing parameters extracted from the camera and given to the renderer