From 0d92e38382f5fbcceb3254eb33c503289e8caa28 Mon Sep 17 00:00:00 2001 From: Thomas Gale Date: Mon, 16 Nov 2020 00:20:38 +0000 Subject: [PATCH] WIP: Still figuring out how to rendering multiple windows. It appears to be a limition of a Platform::Application. You can apparently use a single context to render multiple windows: https://github.com/mosra/magnum/issues/321#issuecomment-466667403 Will try a much more basic native SDL apprach next. --- CMakeLists.txt | 3 + .../bullet/BasicBulletEnvironment.hpp | 3 + .../bullet/BasicBulletEnvironment.cpp | 65 ++++++++++++++++--- 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 94a8c1d..22861b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,6 +32,9 @@ set(BUILD_PLUGINS_STATIC ON CACHE BOOL "" FORCE) # -- File Loading -- +# Corrade build multithreaded +#set(CORRADE_BUILD_MULTITHREADED ON CACHE BOOL "" FORCE) + # Loading OBJ files set(WITH_ANYIMAGEIMPORTER ON CACHE BOOL "" FORCE) set(WITH_ANYSCENEIMPORTER ON CACHE BOOL "" FORCE) diff --git a/include/learn/environment/bullet/BasicBulletEnvironment.hpp b/include/learn/environment/bullet/BasicBulletEnvironment.hpp index 0074bc5..885bcf1 100644 --- a/include/learn/environment/bullet/BasicBulletEnvironment.hpp +++ b/include/learn/environment/bullet/BasicBulletEnvironment.hpp @@ -4,6 +4,7 @@ #include "learn/environment/gym/Environment.hpp" #include "learn/environment/gym/Space.hpp" #include "learn/environment/gym/State.hpp" +#include #include namespace learn { @@ -13,6 +14,7 @@ namespace bullet { class BasicBulletEnvironment : public gym::Environment { public: BasicBulletEnvironment(); + ~BasicBulletEnvironment(); std::shared_ptr actionSpace() const override; std::shared_ptr observationSpace() const override; @@ -35,6 +37,7 @@ class BasicBulletEnvironment : public gym::Environment { std::shared_ptr state_; // Just a hack for now. + //Magnum::Platform::GLContext context_; Magnum::Platform::Application* app_; }; diff --git a/src/environment/bullet/BasicBulletEnvironment.cpp b/src/environment/bullet/BasicBulletEnvironment.cpp index 5246564..676bc9f 100644 --- a/src/environment/bullet/BasicBulletEnvironment.cpp +++ b/src/environment/bullet/BasicBulletEnvironment.cpp @@ -1,9 +1,14 @@ +#include +#include + +#include "SDL.h" #include #include #include #include #include #include +#include #include #include #include @@ -11,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -140,7 +146,7 @@ class RigidBody : public Object3D { Containers::Pointer _bRigidBody; }; -// C'tor for the BulletExample +// C'tor for the basic bullet magnum application. BasicBulletApplication::BasicBulletApplication(const Arguments& arguments) : Platform::Application(arguments, NoCreate) { /* Try 8x MSAA, fall back to zero samples if not possible. Enable only 2x @@ -152,6 +158,7 @@ BasicBulletApplication::BasicBulletApplication(const Arguments& arguments) .setSize(conf.size(), dpiScaling); GLConfiguration glConf; glConf.setSampleCount(dpiScaling.max() < 2.0f ? 8 : 2); + if (!tryCreate(conf, glConf)) create(conf, glConf.setSampleCount(0)); } @@ -291,7 +298,18 @@ void BasicBulletApplication::drawEvent() { // ---------------------- ENV +// C'tor BasicBulletEnvironment::BasicBulletEnvironment() { + // Context faff. + //Platform::GLContext context; + //Platform::GLContext::makeCurrent(&context); + + // Internal bullet magnum application setup. + // Assigning a raw pointer on free store to dangle is *NOT* what we should + // do :) + int argc = 0; + app_ = new BasicBulletApplication({argc, nullptr}); + actionSpace_ = std::make_shared( gym::Space::SpaceType::BOX, std::vector{2}, std::vector{1.0, 1.0}, std::vector{-1.0, -1.0}, -1); @@ -305,6 +323,36 @@ BasicBulletEnvironment::BasicBulletEnvironment() { state_ = reset(); } +// D'tor +BasicBulletEnvironment::~BasicBulletEnvironment() { + + // SDL_GLContext context = app_->glContext(); + // SDL_Window* win = app_->window(); + + // SDL_GL_DeleteContext(context); + // SDL_DestroyWindow(win); + // SDL_Quit(); + + // delete app_; + // Magnum::GL::Context + + // Platform::GLContext::makeCurrent(nullptr); + + // app_->con + SDL_GLContext context = app_->glContext(); + SDL_Window* win = app_->window(); + + SDL_GL_DeleteContext(context); + SDL_DestroyWindow(win); + SDL_Quit(); + + // Trying to reset the GL internal state in magnum + //GL::Context::resetState(); + + // LEAK!!!!! + app_ = nullptr; +} + std::shared_ptr BasicBulletEnvironment::actionSpace() const { return actionSpace_; } @@ -317,15 +365,15 @@ std::shared_ptr BasicBulletEnvironment::reset() { // Running reset... // This is wrenched out of the run wrapper macro for magnum - int argc = 1; - std::vector argv{ - "/workspaces/learn/build/Debug/bin/learn_tests"}; - Magnum::Platform::Sdl2Application::Arguments dummyArgs(argc, &argv[0]); + // int argc = 0; + // std::vector argv{"/workspaces/learn/build/Debug/bin/learn_tests"}; + // Magnum::Platform::Sdl2Application::Arguments dummyArgs(argc, &argv[0]); - app_ = new BasicBulletApplication(dummyArgs); - app_->exec(); + // Faffing with GL contexts, trying to make magnum happy to switch to new + // Run one step of application main + // app_->mainLoopIteration(); - // Get the state from bullet + // TODO - Set intial state of bullet simulation. // Bullet reset things float cubeX = 0; float cubeY = 0; @@ -344,6 +392,7 @@ std::shared_ptr BasicBulletEnvironment::reset() { std::shared_ptr BasicBulletEnvironment::step(const std::vector& action, bool render) { // Running step... + app_->mainLoopIteration(); // Mutate state_