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/EntityManager.hpp>
14#include <Engine/Scene/GeometrySystem.hpp>
15#include <Engine/Scene/SkeletonBasedAnimationSystem.hpp>
20using namespace Ra::Core::Utils;
22constexpr int defaultSystemPriority = 1000;
31 Ra::Engine::RadiumEngine::destroyInstance();
38 if ( parseResult != 0 ) {
39 if ( parseResult != 1 ) {
40 LOG( logERROR ) <<
"Invalid command line argument, the application can't run";
47 LOG( logERROR ) <<
"Invalid openglContext, the application can't run";
52 m_engine = Ra::Engine::RadiumEngine::createInstance();
79 addOption(
"-f,--file", m_parameters.m_dataFile,
"Data file to process." )
80 ->check( CLI::ExistingFile );
81 addOption(
"-s,--size", m_parameters.m_size,
"Size of the computed image." )->delimiter(
'x' );
82 addFlag(
"-a,--animation", m_parameters.m_animationEnable,
"Enable Radium Animation system." );
85CLIViewer::~CLIViewer() {
93const CLIViewer::ViewerParameters& CLIViewer::getCommandLineParameters()
const {
97int CLIViewer::init(
int argc,
const char* argv[] ) {
100 if ( parseResult != 0 ) {
return parseResult; };
103 LOG( logINFO ) <<
"CLIViewer :\n" <<
m_glContext->getInfo();
109 "GeometrySystem",
new Ra::Engine::Scene::GeometrySystem, defaultSystemPriority );
111 if ( m_parameters.m_animationEnable ) {
116 defaultSystemPriority );
121 [
this](
int width,
int height ) { resize( width, height ); } );
126int CLIViewer::oneFrame(
float timeStep ) {
127 if ( m_parameters.m_animationEnable ) {
130 if ( animationSystem ) { animationSystem->toggleSkeleton(
false ); }
139 tasks.waitForTasks();
140 tasks.flushTaskQueue();
145 m_camera->getViewMatrix(), m_camera->
getProjMatrix(), timeStep };
146 if ( m_renderer ) m_renderer->
render( data );
157 m_renderer.
reset( renderer );
158 m_renderer->
initialize( m_parameters.m_size[0], m_parameters.m_size[1] );
163void CLIViewer::addDataFileLoader( Ra::Core::Asset::FileLoaderInterface* loader ) {
167void CLIViewer::loadScene() {
171void CLIViewer::compileScene() {
176 Ra::Engine::Scene::SystemEntity::getInstance(),
"headlight" );
177 headlight->setColor( Ra::Core::Utils::Color::Grey( 2.0_ra ) );
183Ra::Core::Utils::Index CLIViewer::setCamera( Ra::Core::Utils::Index camIdx ) {
185 Ra::Engine::RadiumEngine::getInstance()->getSystem(
"DefaultCameraManager" ) );
187 if ( camIdx.isInvalid() || cameraManager->count() <=
size_t( camIdx ) ) {
188 LOG( logDEBUG ) <<
"CLIViewer::setCamera : invalid camera index, using camera at index 0.";
192 if ( cameraManager->count() == 0 ) {
194 <<
"CLIViewer::setCamera : no camera in the manager, creating default camera.";
198 camera->initialize();
199 cameraManager->addCamera( camera );
202 auto cam = camera->getCamera();
203 cam->setFOV( 60.0_ra * Ra::Core::Math::toRad );
204 cam->setZNear( 0.1_ra );
205 cam->setZFar( 100_ra );
206 auto aabb = Ra::Engine::RadiumEngine::getInstance()->computeSceneAabb();
207 Scalar f = cam->getFOV();
208 Scalar a = cam->getAspect();
209 const Scalar r = ( aabb.max() - aabb.min() ).norm() / 2_ra;
210 const Scalar x = r /
std::sin( f / 2_ra );
211 const Scalar y = r /
std::sin( f * a / 2_ra );
214 Ra::Core::Vector3( aabb.center().x(), aabb.center().y(), aabb.center().z() + d ) );
215 cam->setDirection( Ra::Core::Vector3( 0_ra, 0_ra, -1_ra ) );
216 Scalar zfar =
std::max( d + ( aabb.max().z() - aabb.min().z() ) * 2_ra, cam->getZFar() );
217 cam->setZFar( zfar );
221 m_camera = cameraManager->getActiveCamera();
226void CLIViewer::setImageNamePrefix(
std::string s ) {
227 m_parameters.m_imgPrefix =
std::move( s );
231 m_exposedWindow = on;
232 if ( m_exposedWindow ) {
239void CLIViewer::renderLoop(
std::function<
void(
float )> render ) {
243void CLIViewer::resize(
int width,
int height ) {
245 m_renderer->
resize( width, height );
void setViewport(Scalar width, Scalar height)
Change the viewport size. Also compute aspectRatio.
Core::Matrix4 getProjMatrix() const
Return the projection matrix.
Core::Vector3 getDirection() const
Return the direction the camera is looking at.
This class allows tasks to be registered and then executed in parallel on separate threads.
Scene::System * getSystem(const std::string &system) const
bool loadFile(const std::string &file)
Scene::EntityManager * getEntityManager() const
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 render(const Data::ViewingParameters &renderData)
Tell the renderer it needs to render. This method does the following steps :
void initialize(uint width, uint height)
Initialize renderer.
void resize(uint width, uint height)
Resize the viewport and all the screen textures, fbos. This function must be overrided as soon as som...
bool hasLight() const
Tell if the renderer has an usable light.
virtual void addLight(const Scene::Light *light)
int buildAllRenderTechniques() const
virtual std::unique_ptr< uchar[]> grabFrame(size_t &w, size_t &h) const
void activate(Core::Utils::Index index)
virtual int init(int argc, const char *argv[])
CLI::Option * addFlag(Args &&... args)
CLI::Option * addOption(Args &&... args)
Ra::Engine::RadiumEngine * m_engine
Instance of the radium engine.
virtual ~CLIRadiumEngineApp()
Base destructor.
bool m_engineInitialized
is the engine initialized ?
std::unique_ptr< OpenGLContext > m_glContext
Headless OpenGLContext.
int init(int argc, const char *argv[]) override
Application initialization method.
void openGlAddOns(std::function< void()> f)
void bindOpenGLContext(bool on=true)
CLIRadiumEngineApp(std::unique_ptr< OpenGLContext > context)
Construct the viewer using an OpenGL context of the given version.
EventMode
Identify the event processing method when the window is exposed.
T hardware_concurrency(T... args)
hepler function to manage enum as underlying types in VariableSet
the set of viewing parameters extracted from the camera and given to the renderer