Skip to content

Commit

Permalink
WIP: Still figuring out how to rendering multiple windows.
Browse files Browse the repository at this point in the history
It appears to be a limition of a Platform::Application.

You can apparently use a single context to render multiple windows: mosra/magnum#321 (comment)

Will try a much more basic native SDL apprach next.
  • Loading branch information
thomas-gale committed Nov 16, 2020
1 parent 7482992 commit 0d92e38
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions include/learn/environment/bullet/BasicBulletEnvironment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "learn/environment/gym/Environment.hpp"
#include "learn/environment/gym/Space.hpp"
#include "learn/environment/gym/State.hpp"
#include <Magnum/Platform/GLContext.h>
#include <Magnum/Platform/Sdl2Application.h>

namespace learn {
Expand All @@ -13,6 +14,7 @@ namespace bullet {
class BasicBulletEnvironment : public gym::Environment {
public:
BasicBulletEnvironment();
~BasicBulletEnvironment();

std::shared_ptr<gym::Space> actionSpace() const override;
std::shared_ptr<gym::Space> observationSpace() const override;
Expand All @@ -35,6 +37,7 @@ class BasicBulletEnvironment : public gym::Environment {
std::shared_ptr<gym::State> state_;

// Just a hack for now.
//Magnum::Platform::GLContext context_;
Magnum::Platform::Application* app_;
};

Expand Down
65 changes: 57 additions & 8 deletions src/environment/bullet/BasicBulletEnvironment.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
#include <chrono>
#include <thread>

#include "SDL.h"
#include <Corrade/Containers/GrowableArray.h>
#include <Corrade/Containers/Optional.h>
#include <Corrade/Containers/Pointer.h>
#include <Magnum/BulletIntegration/DebugDraw.h>
#include <Magnum/BulletIntegration/Integration.h>
#include <Magnum/BulletIntegration/MotionState.h>
#include <Magnum/GL/Context.h>
#include <Magnum/GL/DefaultFramebuffer.h>
#include <Magnum/GL/Mesh.h>
#include <Magnum/GL/Renderer.h>
#include <Magnum/Math/Color.h>
#include <Magnum/Math/Constants.h>
#include <Magnum/MeshTools/Compile.h>
#include <Magnum/MeshTools/Transform.h>
#include <Magnum/Platform/GLContext.h>
#include <Magnum/Platform/Sdl2Application.h>
#include <Magnum/Primitives/Cube.h>
#include <Magnum/Primitives/UVSphere.h>
Expand Down Expand Up @@ -140,7 +146,7 @@ class RigidBody : public Object3D {
Containers::Pointer<btRigidBody> _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
Expand All @@ -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));
}
Expand Down Expand Up @@ -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>(
gym::Space::SpaceType::BOX, std::vector<int>{2},
std::vector<float>{1.0, 1.0}, std::vector<float>{-1.0, -1.0}, -1);
Expand All @@ -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<gym::Space> BasicBulletEnvironment::actionSpace() const {
return actionSpace_;
}
Expand All @@ -317,15 +365,15 @@ std::shared_ptr<gym::State> BasicBulletEnvironment::reset() {
// Running reset...

// This is wrenched out of the run wrapper macro for magnum
int argc = 1;
std::vector<char*> argv{
"/workspaces/learn/build/Debug/bin/learn_tests"};
Magnum::Platform::Sdl2Application::Arguments dummyArgs(argc, &argv[0]);
// int argc = 0;
// std::vector<char*> 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;
Expand All @@ -344,6 +392,7 @@ std::shared_ptr<gym::State> BasicBulletEnvironment::reset() {
std::shared_ptr<gym::State>
BasicBulletEnvironment::step(const std::vector<float>& action, bool render) {
// Running step...
app_->mainLoopIteration();

// Mutate state_

Expand Down

0 comments on commit 0d92e38

Please sign in to comment.