diff --git a/src/experimental/filament/filament/filament_context.cc b/src/experimental/filament/filament/filament_context.cc index 884c16f2..1c76b652 100644 --- a/src/experimental/filament/filament/filament_context.cc +++ b/src/experimental/filament/filament/filament_context.cc @@ -49,9 +49,8 @@ namespace mujoco { -FilamentContext::FilamentContext(const mjrFilamentConfig* config, - const mjModel* model) - : config_(*config), model_(model) { +FilamentContext::FilamentContext(const mjrFilamentConfig* config) + : config_(*config) { FilamentPlatformSetup setup = CreateFilamentPlatform(config_); platform_ = std::move(setup.platform); @@ -63,32 +62,16 @@ FilamentContext::FilamentContext(const mjrFilamentConfig* config, engine_ = engine_builder.build(); renderer_ = engine_->createRenderer(); - - const int width = model_->vis.global.offwidth; - const int height = model_->vis.global.offheight; #ifdef __EMSCRIPTEN__ window_swap_chain_ = engine_->createSwapChain(nullptr); #else if (config_.native_window) { window_swap_chain_ = engine_->createSwapChain(config_.native_window); } else { - window_swap_chain_ = engine_->createSwapChain(width, height); + window_swap_chain_ = engine_->createSwapChain(config_.width, config_.height); } #endif - - offscreen_swap_chain_ = engine_->createSwapChain(width, height); - object_manager_ = std::make_unique(model, engine_); - - // Set clear options. - filament::Renderer::ClearOptions opts; - opts.clear = true; - opts.discard = true; - opts.clearColor = ReadElement(model_, "filament.clearColor", - filament::math::float4(0, 0, 0, 1)); - renderer_->setClearOptions(opts); - - scene_view_ = std::make_unique(engine_, object_manager_.get()); - gui_view_ = std::make_unique(engine_, object_manager_.get()); + offscreen_swap_chain_ = engine_->createSwapChain(config_.width, config_.height); } FilamentContext::~FilamentContext() { @@ -102,6 +85,20 @@ FilamentContext::~FilamentContext() { filament::Engine::destroy(engine_); } +void FilamentContext::Init(const mjModel* model) { + object_manager_ = std::make_unique(model, engine_); + scene_view_ = std::make_unique(engine_, object_manager_.get()); + gui_view_ = std::make_unique(engine_, object_manager_.get()); + + // Set clear options. + filament::Renderer::ClearOptions opts; + opts.clear = true; + opts.discard = true; + opts.clearColor = ReadElement(model, "filament.clearColor", + filament::math::float4(0, 0, 0, 1)); + renderer_->setClearOptions(opts); +} + void FilamentContext::Render(const mjrRect& viewport, const mjvScene* scene) { // If we're rendering to the window, and the window size has changed, we need // to reacquire the swap chain. diff --git a/src/experimental/filament/filament/filament_context.h b/src/experimental/filament/filament/filament_context.h index 51208c60..ec0a7243 100644 --- a/src/experimental/filament/filament/filament_context.h +++ b/src/experimental/filament/filament/filament_context.h @@ -35,9 +35,11 @@ namespace mujoco { // Manages the filament renderer that is exposed via the mjr functions. class FilamentContext { public: - FilamentContext(const mjrFilamentConfig* config, const mjModel* model); + explicit FilamentContext(const mjrFilamentConfig* config); ~FilamentContext(); + void Init(const mjModel* model); + void Render(const mjrRect& viewport, const mjvScene* scene); void SetFrameBuffer(int framebuffer); @@ -70,7 +72,6 @@ class FilamentContext { void DestroyRenderTargets(); mjrFilamentConfig config_; - const mjModel* model_ = nullptr; filament::Engine* engine_ = nullptr; filament::Renderer* renderer_ = nullptr; diff --git a/src/experimental/filament/render_context_filament.cc b/src/experimental/filament/render_context_filament.cc index 51e91063..2e00bc79 100644 --- a/src/experimental/filament/render_context_filament.cc +++ b/src/experimental/filament/render_context_filament.cc @@ -49,7 +49,8 @@ void mjrf_makeFilamentContext(const mjModel* m, mjrContext* con, if (g_filament_context != nullptr) { mju_error("Context already exists!"); } - g_filament_context = new mujoco::FilamentContext(config, m); + g_filament_context = new mujoco::FilamentContext(config); + g_filament_context->Init(m); } void mjrf_defaultContext(mjrContext* con) { @@ -60,6 +61,8 @@ void mjrf_makeContext(const mjModel* m, mjrContext* con, int fontscale) { mjr_freeContext(con); mjrFilamentConfig cfg; mjrf_defaultFilamentConfig(&cfg); + cfg.width = m->vis.global.offwidth; + cfg.height = m->vis.global.offheight; mjrf_makeFilamentContext(m, con, &cfg); } diff --git a/src/experimental/filament/render_context_filament.h b/src/experimental/filament/render_context_filament.h index 8596bf82..9db6554f 100644 --- a/src/experimental/filament/render_context_filament.h +++ b/src/experimental/filament/render_context_filament.h @@ -39,6 +39,10 @@ struct mjrFilamentConfig { // The native window handle into which we can render directly. void* native_window; + // The initial width and height of the offscreen framebuffer. + int width; + int height; + // The backend graphics API to use. int graphics_api; diff --git a/src/experimental/platform/renderer.cc b/src/experimental/platform/renderer.cc index 7583a46e..42c77d5f 100644 --- a/src/experimental/platform/renderer.cc +++ b/src/experimental/platform/renderer.cc @@ -88,6 +88,8 @@ void Renderer::Init(const mjModel* model) { mjrFilamentConfig render_config; mjrf_defaultFilamentConfig(&render_config); render_config.native_window = native_window_; + render_config.width = model->vis.global.offwidth; + render_config.height = model->vis.global.offheight; render_config.force_software_rendering = IsSoftware(gfx_); render_config.graphics_api = IsOpenGl(gfx_) || IsWebGl(gfx_) ? mjGFX_OPENGL : mjGFX_VULKAN;