From 5ab2ee4df1427daea7c55357331e9d8a0078ff08 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Mon, 1 Jun 2015 23:40:44 +0200 Subject: [PATCH] Continue to work on GUI for the testbed application --- testbed/common/Box.cpp | 4 +- testbed/common/Capsule.cpp | 2 +- .../src/VertexArrayObject.cpp | 70 +++++++ .../opengl-framework/src/VertexArrayObject.h | 99 +++++++++ testbed/shaders/gui.frag | 10 + testbed/shaders/gui.vert | 15 ++ testbed/src/Gui.cpp | 190 +++++++++++++----- testbed/src/Gui.h | 27 ++- testbed/src/Scene.h | 8 + testbed/src/TestbedApplication.cpp | 35 +++- testbed/src/TestbedApplication.h | 16 +- 11 files changed, 411 insertions(+), 65 deletions(-) create mode 100644 testbed/opengl-framework/src/VertexArrayObject.cpp create mode 100644 testbed/opengl-framework/src/VertexArrayObject.h create mode 100644 testbed/shaders/gui.frag create mode 100644 testbed/shaders/gui.vert diff --git a/testbed/common/Box.cpp b/testbed/common/Box.cpp index 29149488..4eefdf9e 100644 --- a/testbed/common/Box.cpp +++ b/testbed/common/Box.cpp @@ -60,7 +60,7 @@ GLuint Box::mCubeIndices[36] = { 0, 1, 2, // Constructor Box::Box(const openglframework::Vector3& size, const openglframework::Vector3 &position, reactphysics3d::CollisionWorld* world, openglframework::Shader& shader) - : openglframework::Object3D(), mColor(0.5f, 0.5f, 0.5f, 1.0f) { + : openglframework::Object3D(), mColor(0.01f, 0.62f, 0.39f, 1.0f) { // Initialize the size of the box mSize[0] = size.x * 0.5f; @@ -107,7 +107,7 @@ Box::Box(const openglframework::Vector3& size, const openglframework::Vector3 &p // Constructor Box::Box(const openglframework::Vector3& size, const openglframework::Vector3 &position, float mass, reactphysics3d::DynamicsWorld* world, openglframework::Shader& shader) - : openglframework::Object3D(), mColor(0.5f, 0.5f, 0.5f, 1.0f) { + : openglframework::Object3D(), mColor(0.01f, 0.62f, 0.39f, 1.0f) { // Initialize the size of the box mSize[0] = size.x * 0.5f; diff --git a/testbed/common/Capsule.cpp b/testbed/common/Capsule.cpp index 2a925a99..186970ff 100644 --- a/testbed/common/Capsule.cpp +++ b/testbed/common/Capsule.cpp @@ -32,7 +32,7 @@ Capsule::Capsule(float radius, float height, const openglframework::Vector3& pos const std::string& meshFolderPath, openglframework::Shader& shader) : openglframework::Mesh(), mRadius(radius), mHeight(height), mVBOVertices(GL_ARRAY_BUFFER), mVBONormals(GL_ARRAY_BUFFER), mVBOTextureCoords(GL_ARRAY_BUFFER), - mVBOIndices(GL_ELEMENT_ARRAY_BUFFER), mColor(1.0f, 0.5f, 0.5f, 1.0f) { + mVBOIndices(GL_ELEMENT_ARRAY_BUFFER), mColor(0.5f, 0.5f, 0.5f, 1.0f) { // Load the mesh from a file openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "capsule.obj", *this); diff --git a/testbed/opengl-framework/src/VertexArrayObject.cpp b/testbed/opengl-framework/src/VertexArrayObject.cpp new file mode 100644 index 00000000..2fb0d787 --- /dev/null +++ b/testbed/opengl-framework/src/VertexArrayObject.cpp @@ -0,0 +1,70 @@ +/******************************************************************************** +* OpenGL-Framework * +* Copyright (c) 2015 Daniel Chappuis * +********************************************************************************* +* * +* This software is provided 'as-is', without any express or implied warranty. * +* In no event will the authors be held liable for any damages arising from the * +* use of this software. * +* * +* Permission is granted to anyone to use this software for any purpose, * +* including commercial applications, and to alter it and redistribute it * +* freely, subject to the following restrictions: * +* * +* 1. The origin of this software must not be misrepresented; you must not claim * +* that you wrote the original software. If you use this software in a * +* product, an acknowledgment in the product documentation would be * +* appreciated but is not required. * +* * +* 2. Altered source versions must be plainly marked as such, and must not be * +* misrepresented as being the original software. * +* * +* 3. This notice may not be removed or altered from any source distribution. * +* * +********************************************************************************/ + +// Libraries +#include "VertexArrayObject.h" + +using namespace openglframework; + +// Constructor +VertexArrayObject::VertexArrayObject() : mVertexArrayID(0) { + +} + +// Destructor +VertexArrayObject::~VertexArrayObject() { + +} + +// Create the vertex buffer object +bool VertexArrayObject::create() { + + // Destroy the current VAO + destroy(); + + // Check that the needed OpenGL extensions are available + bool isExtensionOK = checkOpenGLExtensions(); + if (!isExtensionOK) { + std::cerr << "Error : Impossible to use Vertex Array Object on this platform" << std::endl; + assert(false); + return false; + } + + // Generate a new VAO + glGenVertexArrays(1, &mVertexArrayID); + assert(mVertexArrayID != 0); + + return true; +} + +// Destroy the VAO +void VertexArrayObject::destroy() { + + // Delete the vertex buffer object + if (mVertexArrayID) { + glDeleteVertexArrays(1, &mVertexArrayID); + mVertexArrayID = 0; + } +} diff --git a/testbed/opengl-framework/src/VertexArrayObject.h b/testbed/opengl-framework/src/VertexArrayObject.h new file mode 100644 index 00000000..2f7ed1b0 --- /dev/null +++ b/testbed/opengl-framework/src/VertexArrayObject.h @@ -0,0 +1,99 @@ +/******************************************************************************** +* OpenGL-Framework * +* Copyright (c) 2013 Daniel Chappuis * +********************************************************************************* +* * +* This software is provided 'as-is', without any express or implied warranty. * +* In no event will the authors be held liable for any damages arising from the * +* use of this software. * +* * +* Permission is granted to anyone to use this software for any purpose, * +* including commercial applications, and to alter it and redistribute it * +* freely, subject to the following restrictions: * +* * +* 1. The origin of this software must not be misrepresented; you must not claim * +* that you wrote the original software. If you use this software in a * +* product, an acknowledgment in the product documentation would be * +* appreciated but is not required. * +* * +* 2. Altered source versions must be plainly marked as such, and must not be * +* misrepresented as being the original software. * +* * +* 3. This notice may not be removed or altered from any source distribution. * +* * +********************************************************************************/ + +#ifndef VERTEX_ARRAY_OBJECT_H +#define VERTEX_ARRAY_OBJECT_H + +// Libraries +#include +#include +#include + +namespace openglframework { + + +// Class VertexArrayObject +class VertexArrayObject { + + private : + + // -------------------- Attributes -------------------- // + + /// ID of the Vertex Array Object + GLuint mVertexArrayID; + + public : + + // -------------------- Methods -------------------- // + + /// Constructor + VertexArrayObject(); + + /// Destructor + ~VertexArrayObject(); + + /// Create the vertex buffer object + bool create(); + + /// Bind the VAO + void bind() const; + + /// Unbind the VAO + void unbind() const; + + /// Return true if the needed OpenGL extensions are available for VAO + static bool checkOpenGLExtensions(); + + /// Destroy the VAO + void destroy(); +}; + +// Bind the VAO +inline void VertexArrayObject::bind() const { + assert(mVertexArrayID != 0); + + // Bind the VAO + glBindVertexArray(mVertexArrayID); +} + +// Unbind the VAO +inline void VertexArrayObject::unbind() const { + assert(mVertexArrayID != 0); + + // Unbind the VAO + glBindVertexArray(0); +} + +// Return true if the needed OpenGL extensions are available for VAO +inline bool VertexArrayObject::checkOpenGLExtensions() { + + // Check that OpenGL version is at least 3.0 or there the vertex array object extension exists + return (GLEW_VERSION_3_0 || GL_ARB_vertex_array_object); +} + +} + +#endif + diff --git a/testbed/shaders/gui.frag b/testbed/shaders/gui.frag new file mode 100644 index 00000000..a00d52bb --- /dev/null +++ b/testbed/shaders/gui.frag @@ -0,0 +1,10 @@ +#version 330 +uniform sampler2D Texture; +in vec2 Frag_UV; +in vec4 Frag_Color; +out vec4 Out_Color; + +void main() +{ + Out_Color = Frag_Color * texture( Texture, Frag_UV.st); +} diff --git a/testbed/shaders/gui.vert b/testbed/shaders/gui.vert new file mode 100644 index 00000000..18870a74 --- /dev/null +++ b/testbed/shaders/gui.vert @@ -0,0 +1,15 @@ +#version 330 + +uniform mat4 ProjMtx; +in vec2 Position; +in vec2 UV; +in vec4 Color; +out vec2 Frag_UV; +out vec4 Frag_Color; + +void main() +{ + Frag_UV = UV; + Frag_Color = Color; + gl_Position = ProjMtx * vec4(Position.xy,0,1); +} diff --git a/testbed/src/Gui.cpp b/testbed/src/Gui.cpp index 6c78006c..6d5ad0eb 100644 --- a/testbed/src/Gui.cpp +++ b/testbed/src/Gui.cpp @@ -26,6 +26,7 @@ // Libraries #include "Gui.h" #include +#include "TestbedApplication.h" GLFWwindow* Gui::mWindow = NULL; double Gui::g_Time = 0.0f; @@ -33,14 +34,16 @@ bool Gui::g_MousePressed[3] = {false, false, false}; float Gui::g_MouseWheel = 0.0f; GLuint Gui::g_FontTexture = 0; size_t Gui::g_VboSize = 0; -unsigned int Gui::g_VboHandle = 0; -unsigned int Gui::g_VaoHandle = 0; int Gui::g_AttribLocationTex = 0, Gui::g_AttribLocationProjMtx = 0; int Gui::g_AttribLocationPosition = 0, Gui::g_AttribLocationUV = 0, Gui::g_AttribLocationColor = 0; Shader Gui::mShader; +openglframework::VertexBufferObject Gui::mVBO(GL_ARRAY_BUFFER); +openglframework::VertexArrayObject Gui::mVAO; +Gui::LeftPane Gui::mLeftPane = SCENES; // Constructor Gui::Gui() { + g_Time = 0.0f; g_MousePressed[0] = false; g_MousePressed[1] = false; @@ -48,16 +51,13 @@ Gui::Gui() { g_MouseWheel = 0.0f; g_FontTexture = 0; g_VboSize = 0; - g_VboHandle = 0, g_VaoHandle = 0; } // Destructor Gui::~Gui() { - if (g_VaoHandle) glDeleteVertexArrays(1, &g_VaoHandle); - if (g_VboHandle) glDeleteBuffers(1, &g_VboHandle); - g_VaoHandle = 0; - g_VboHandle = 0; + mVAO.destroy(); + mVBO.destroy(); mShader.destroy(); @@ -101,72 +101,164 @@ void Gui::init() { io.RenderDrawListsFn = renderDrawLists; io.SetClipboardTextFn = setClipboardText; io.GetClipboardTextFn = getClipboardText; + + io.FontGlobalScale = GUI_SCALING; +} + +void Gui::displayHeader() { + + ImVec2 buttonSize(120, 40); + + int display_w, display_h; + glfwGetFramebufferSize(mWindow, &display_w, &display_h); + + ImGuiWindowFlags window_flags = 0; + window_flags |= ImGuiWindowFlags_NoTitleBar; + window_flags |= ImGuiWindowFlags_NoResize; + window_flags |= ImGuiWindowFlags_NoMove; + + ImGui::PushID("Header"); + ImGui::PushStyleColor(ImGuiCol_WindowBg, BACKGROUND_COLOR); + + ImGui::Begin("Header", NULL, ImVec2(display_w, HEADER_HEIGHT), 1.0f, window_flags); + ImGui::SetWindowPos(ImVec2(0, 0)); + ImGui::Button("Play", buttonSize); ImGui::SameLine(); + ImGui::Button("Pause", buttonSize); ImGui::SameLine(); + ImGui::Button("Step", buttonSize); ImGui::SameLine(); + ImGui::Button("Restart", buttonSize); ImGui::SameLine(); + ImGui::End(); + + ImGui::PopStyleColor(1); + ImGui::PopID(); +} + +void Gui::displayLeftPane() { + + const int nbButtonsHeader = 4; + ImVec2 buttonSize(LEFT_PANE_WIDTH / nbButtonsHeader - 9, LEFT_PANE_HEADER_HEIGHT); + + int display_w, display_h; + glfwGetFramebufferSize(mWindow, &display_w, &display_h); + + ImGuiWindowFlags window_flags = 0; + window_flags |= ImGuiWindowFlags_NoTitleBar; + window_flags |= ImGuiWindowFlags_NoResize; + window_flags |= ImGuiWindowFlags_NoMove; + + ImGui::PushID("LeftPane"); + ImGui::PushStyleColor(ImGuiCol_WindowBg, BACKGROUND_COLOR); + + ImGui::Begin("LeftPane", NULL, ImVec2(LEFT_PANE_WIDTH, display_h - HEADER_HEIGHT), 1.0f, window_flags); + ImGui::SetWindowPos(ImVec2(0, HEADER_HEIGHT)); + + // ----- Left Pane Header ----- // + ImGui::Button("Scenes", buttonSize); ImGui::SameLine(); + ImGui::Button("Physics", buttonSize); ImGui::SameLine(); + ImGui::Button("Rendering", buttonSize); ImGui::SameLine(); + ImGui::Button("Profiling", buttonSize); + + // Display the left pane content + switch(mLeftPane) { + case SCENES: displayScenesPane(); break; + case PHYSICS: displayPhysicsPane(); break; + case RENDERING: displayRenderingPane(); break; + case PROFILING: displayProfilingPane(); break; + } + + ImGui::End(); + + ImGui::PopStyleColor(1); + ImGui::PopID(); +} + +// Display the list of scenes +void Gui::displayScenesPane() { + + static int choice = 0; + int startChoice = choice; + TestbedApplication& app = TestbedApplication::getInstance(); + std::vector scenes = app.getScenes(); + + // For each scene + for (int i=0; igetName().c_str(), &choice, i); + } + + // If the user changed scene + if (choice != startChoice) { + app.switchScene(scenes[choice]); + } +} + +void Gui::displayPhysicsPane() { + +} + +void Gui::displayRenderingPane() { + +} + +void Gui::displayProfilingPane() { + } void Gui::createDeviceObjects() { - /* mShader.create("shaders/gui.vert", "shaders/gui.frag"); - GLuint shaderID = mShader.getProgramObjectId(); - g_AttribLocationTex = glGetUniformLocation(shaderID, "Texture"); - g_AttribLocationProjMtx = glGetUniformLocation(shaderID, "ProjMtx"); - g_AttribLocationPosition = glGetAttribLocation(shaderID, "Position"); - g_AttribLocationUV = glGetAttribLocation(shaderID, "UV"); - g_AttribLocationColor = glGetAttribLocation(shaderID, "Color"); + GLuint shaderID = mShader.getProgramObjectId(); + g_AttribLocationTex = glGetUniformLocation(shaderID, "Texture"); + g_AttribLocationProjMtx = glGetUniformLocation(shaderID, "ProjMtx"); + g_AttribLocationPosition = glGetAttribLocation(shaderID, "Position"); + g_AttribLocationUV = glGetAttribLocation(shaderID, "UV"); + g_AttribLocationColor = glGetAttribLocation(shaderID, "Color"); - glGenBuffers(1, &g_VboHandle); + mVBO.create(); - glGenVertexArrays(1, &g_VaoHandle); - glBindVertexArray(g_VaoHandle); - glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle); - glEnableVertexAttribArray(g_AttribLocationPosition); - glEnableVertexAttribArray(g_AttribLocationUV); - glEnableVertexAttribArray(g_AttribLocationColor); - #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT)) - glVertexAttribPointer(g_AttribLocationPosition, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, pos)); - glVertexAttribPointer(g_AttribLocationUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, uv)); - glVertexAttribPointer(g_AttribLocationColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, col)); - #undef OFFSETOF - glBindVertexArray(0); - glBindBuffer(GL_ARRAY_BUFFER, 0); + mVAO.create(); + mVAO.bind(); + mVBO.bind(); + glEnableVertexAttribArray(g_AttribLocationPosition); + glEnableVertexAttribArray(g_AttribLocationUV); + glEnableVertexAttribArray(g_AttribLocationColor); +#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT)) + glVertexAttribPointer(g_AttribLocationPosition, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, pos)); + glVertexAttribPointer(g_AttribLocationUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, uv)); + glVertexAttribPointer(g_AttribLocationColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, col)); +#undef OFFSETOF + mVAO.unbind(); + mVBO.unbind(); - createFontTextures(); - */ + createFontTextures(); } // Display the GUI void Gui::render() { -/* ImGuiIO& io = ImGui::GetIO(); //glfwPollEvents(); beginNewFrame(); - bool show_test_window = true; bool show_another_window = false; ImVec4 clear_color = ImColor(255, 255, 255); - // 1. Show a simple window - // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug" - { - ImGui::Text("Hello, world!"); - } + displayHeader(); + displayLeftPane(); + ImGui::ShowTestWindow(); // Render the GUI ImGui::Render(); - */ } void Gui::beginNewFrame() { - /* if (!g_FontTexture) createDeviceObjects(); - ImGuiIO& io = ImGui::GetIO(); // Setup display size (every frame to accommodate for window resizing) @@ -210,7 +302,6 @@ void Gui::beginNewFrame() { // Start the frame ImGui::NewFrame(); - */ } void Gui::createFontTextures() @@ -236,7 +327,6 @@ void Gui::createFontTextures() // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f) void Gui::renderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) { - std::cout << "OpenGLVersion : " << glGetString(GL_VERSION) << std::endl; if (cmd_lists_count == 0) return; @@ -252,7 +342,7 @@ void Gui::renderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) glEnable(GL_SCISSOR_TEST); glActiveTexture(GL_TEXTURE0); - /* + // Setup orthographic projection matrix const float width = ImGui::GetIO().DisplaySize.x; const float height = ImGui::GetIO().DisplaySize.y; @@ -279,12 +369,13 @@ void Gui::renderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) size_t total_vtx_count = 0; for (int n = 0; n < cmd_lists_count; n++) total_vtx_count += cmd_lists[n]->vtx_buffer.size(); - glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle); + mVBO.bind(); size_t needed_vtx_size = total_vtx_count * sizeof(ImDrawVert); if (g_VboSize < needed_vtx_size) { g_VboSize = needed_vtx_size + 5000 * sizeof(ImDrawVert); // Grow buffer - glBufferData(GL_ARRAY_BUFFER, g_VboSize, NULL, GL_STREAM_DRAW); + mVBO.copyDataIntoVBO(g_VboSize, NULL, GL_STREAM_DRAW); + //glBufferData(GL_ARRAY_BUFFER, g_VboSize, NULL, GL_STREAM_DRAW); } // Copy and convert all vertices into a single contiguous buffer @@ -298,8 +389,8 @@ void Gui::renderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) buffer_data += cmd_list->vtx_buffer.size() * sizeof(ImDrawVert); } glUnmapBuffer(GL_ARRAY_BUFFER); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(g_VaoHandle); + mVBO.unbind(); + mVAO.bind(); int cmd_offset = 0; for (int n = 0; n < cmd_lists_count; n++) @@ -324,14 +415,11 @@ void Gui::renderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) cmd_offset = vtx_offset; } - - // Restore modified state - glBindVertexArray(0); + mVAO.unbind(); glUseProgram(last_program); glDisable(GL_SCISSOR_TEST); glBindTexture(GL_TEXTURE_2D, last_texture); - */ } const char* Gui::getClipboardText() { diff --git a/testbed/src/Gui.h b/testbed/src/Gui.h index d61a6cdf..39b2aeb4 100644 --- a/testbed/src/Gui.h +++ b/testbed/src/Gui.h @@ -32,13 +32,25 @@ #include #include "openglframework.h" +// Constants +const float GUI_SCALING = 2.0f; +const int HEADER_HEIGHT = 80; +const int LEFT_PANE_WIDTH = 700; +const int LEFT_PANE_HEADER_HEIGHT = 60; +const ImColor BACKGROUND_COLOR = ImColor(41, 41, 41, 255); + using namespace openglframework; +// Declarations +class TestbedApplication; + // Class Gui class Gui { protected : + enum LeftPane {SCENES, PHYSICS, RENDERING, PROFILING}; + // -------------------- Constants -------------------- // @@ -48,7 +60,6 @@ class Gui { static GLFWwindow* mWindow; static Shader mShader; - static double g_Time; static bool g_MousePressed[3]; static float g_MouseWheel; @@ -57,7 +68,9 @@ class Gui { static int g_AttribLocationTex, g_AttribLocationProjMtx; static int g_AttribLocationPosition, g_AttribLocationUV, g_AttribLocationColor; static size_t g_VboSize; - static unsigned int g_VboHandle, g_VaoHandle; + static openglframework::VertexBufferObject mVBO; + static openglframework::VertexArrayObject mVAO; + static LeftPane mLeftPane; // -------------------- Methods -------------------- // @@ -74,6 +87,16 @@ class Gui { static void setClipboardText(const char* text); + static void displayHeader(); + static void displayLeftPane(); + + /// Display the list of scenes + static void displayScenesPane(); + + static void displayPhysicsPane(); + static void displayRenderingPane(); + static void displayProfilingPane(); + public : // -------------------- Methods -------------------- // diff --git a/testbed/src/Scene.h b/testbed/src/Scene.h index e37a9e20..2f269a36 100644 --- a/testbed/src/Scene.h +++ b/testbed/src/Scene.h @@ -152,6 +152,9 @@ class Scene { /// Set the interpolation factor void setInterpolationFactor(float interpolationFactor); + + /// Return the name of the scene + std::string getName() const; }; // Called when a keyboard event occurs @@ -185,4 +188,9 @@ inline void Scene::setInterpolationFactor(float interpolationFactor) { mInterpolationFactor = interpolationFactor; } +// Return the name of the scene +inline std::string Scene::getName() const { + return mName; +} + #endif diff --git a/testbed/src/TestbedApplication.cpp b/testbed/src/TestbedApplication.cpp index c89da4f8..d58150d0 100644 --- a/testbed/src/TestbedApplication.cpp +++ b/testbed/src/TestbedApplication.cpp @@ -137,7 +137,7 @@ void TestbedApplication::init() { Gui::getInstance().setWindow(mWindow); // Init the GUI - //Gui::getInstance().init(); + Gui::getInstance().init(); mTimer.start(); } @@ -149,21 +149,17 @@ void TestbedApplication::createScenes() { CubesScene* cubeScene = new CubesScene("Cubes"); mScenes.push_back(cubeScene); - /* // Joints scene JointsScene* jointsScene = new JointsScene("Joints"); mScenes.push_back(jointsScene); - */ // Collision shapes scene CollisionShapesScene* collisionShapesScene = new CollisionShapesScene("Collision Shapes"); mScenes.push_back(collisionShapesScene); - /* // Raycast scene RaycastScene* raycastScene = new RaycastScene("Raycast"); mScenes.push_back(raycastScene); - */ assert(mScenes.size() > 0); mCurrentScene = mScenes[1]; @@ -225,11 +221,22 @@ void TestbedApplication::update() { // Render void TestbedApplication::render() { + // Get the framebuffer dimension + int width, height; + glfwGetFramebufferSize(mWindow, &width, &height); + + // Resize the OpenGL viewport + glViewport(LEFT_PANE_WIDTH, HEADER_HEIGHT, + width - LEFT_PANE_WIDTH, height - HEADER_HEIGHT); + // Render the scene mCurrentScene->render(); + // Resize the OpenGL viewport + glViewport(0, 0, width, height); + // Display the GUI - //Gui::getInstance().render(); + Gui::getInstance().render(); // Check the OpenGL errors checkOpenGLErrors(); @@ -243,10 +250,11 @@ void TestbedApplication::reshape() { glfwGetFramebufferSize(mWindow, &width, &height); // Resize the camera viewport - mCurrentScene->reshape(width, height); + mCurrentScene->reshape(width - LEFT_PANE_WIDTH, height - HEADER_HEIGHT); // Resize the OpenGL viewport - glViewport(0, 0, width, height); + //glViewport(LEFT_PANE_WIDTH, HEADER_HEIGHT, + // width - LEFT_PANE_WIDTH, height - HEADER_HEIGHT); // Update the window size of the scene int windowWidth, windowHeight; @@ -281,6 +289,17 @@ void TestbedApplication::startMainLoop() { } } +// Change the current scene +void TestbedApplication::switchScene(Scene* newScene) { + + if (newScene == mCurrentScene) return; + + mCurrentScene = newScene; + + // Reset the scene + mCurrentScene->reset(); +} + // Check the OpenGL errors void TestbedApplication::checkOpenGLErrors() { GLenum glError; diff --git a/testbed/src/TestbedApplication.h b/testbed/src/TestbedApplication.h index f3b04a51..9843653c 100644 --- a/testbed/src/TestbedApplication.h +++ b/testbed/src/TestbedApplication.h @@ -135,7 +135,10 @@ class TestbedApplication { void createScenes(); /// Remove all the scenes - void destroyScenes() ; + void destroyScenes(); + + /// Return the list of the scenes + std::vector getScenes(); public : @@ -152,8 +155,19 @@ class TestbedApplication { /// Start the main loop where rendering occur void startMainLoop(); + + /// Change the current scene + void switchScene(Scene* newScene); + + // -------------------- Friendship -------------------- // + + friend class Gui; }; +// Return the list of the scenes +inline std::vector TestbedApplication::getScenes() { + return mScenes; +} #endif