From 97436877fb5089c63593115cc8f1dfb3bcace85e Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Tue, 1 Mar 2016 23:19:45 +0100 Subject: [PATCH] Fix issue with fixed framerate in nanogui --- testbed/nanogui/src/common.cpp | 9 +++++---- testbed/src/Gui.cpp | 17 ++++------------- testbed/src/TestbedApplication.cpp | 21 +++++++++++++++++++-- testbed/src/TestbedApplication.h | 5 +++-- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/testbed/nanogui/src/common.cpp b/testbed/nanogui/src/common.cpp index 2de9c244..865e61ee 100644 --- a/testbed/nanogui/src/common.cpp +++ b/testbed/nanogui/src/common.cpp @@ -55,7 +55,7 @@ void mainloop() { view roughly every 50 ms; this is to support animations such as progress bars while keeping the system load reasonably low */ - std::thread refresh_thread = std::thread( + /*std::thread refresh_thread = std::thread( [&]() { std::chrono::milliseconds time(50); while (__mainloop_active) { @@ -63,7 +63,7 @@ void mainloop() { glfwPostEmptyEvent(); } } - ); + );*/ try { while (__mainloop_active) { @@ -87,14 +87,15 @@ void mainloop() { } /* Wait for mouse/keyboard or empty refresh events */ - glfwWaitEvents(); + //glfwWaitEvents(); + glfwPollEvents(); } } catch (const std::exception &e) { std::cerr << "Caught exception in main loop: " << e.what() << std::endl; abort(); } - refresh_thread.join(); + //refresh_thread.join(); } void leave() { diff --git a/testbed/src/Gui.cpp b/testbed/src/Gui.cpp index 4e50a7ce..56d99d4a 100644 --- a/testbed/src/Gui.cpp +++ b/testbed/src/Gui.cpp @@ -64,14 +64,6 @@ void Gui::init() { mApp->setVisible(true); mApp->performLayout(); - //window->center(); - - - // Init UI - /*if (!imguiRenderGLInit("DroidSans.ttf")) { - fprintf(stderr, "Could not init GUI renderer.\n"); - exit(EXIT_FAILURE); - }*/ mTimeSinceLastProfilingDisplay = glfwGetTime(); } @@ -91,11 +83,10 @@ void Gui::update() { mFPSLabel->setCaption(std::string("FPS : ") + floatToString(mCachedFPS, 0)); // Frame time - mFrameTimeLabel->setCaption(std::string("Frame time (ms) : ") + floatToString(mCachedUpdateTime * 1000.0, 1)); + mFrameTimeLabel->setCaption(std::string("Frame time : ") + floatToString(mCachedUpdateTime * 1000.0, 1) + std::string(" ms")); // Physics time - mPhysicsTimeLabel->setCaption("Physics time (ms) : " + floatToString(mCachedPhysicsUpdateTime * 1000.0, 1)); - + mPhysicsTimeLabel->setCaption(std::string("Physics time : ") + floatToString(mCachedPhysicsUpdateTime * 1000.0, 1) + std::string(" ms")); } void Gui::createSimulationPanel() { @@ -426,10 +417,10 @@ void Gui::createProfilingPanel() { mFPSLabel = new Label(profilingPanel, std::string("FPS : ") + floatToString(mCachedFPS, 0),"sans-bold"); // Update time - mFrameTimeLabel = new Label(profilingPanel, std::string("Frame time (ms) : ") + floatToString(mCachedUpdateTime * 1000.0, 1),"sans-bold"); + mFrameTimeLabel = new Label(profilingPanel, std::string("Frame time : ") + floatToString(mCachedUpdateTime * 1000.0, 1) + std::string(" ms"),"sans-bold"); // Update time - mPhysicsTimeLabel = new Label(profilingPanel, "Physics time (ms) : " + floatToString(mCachedPhysicsUpdateTime * 1000.0, 1),"sans-bold"); + mPhysicsTimeLabel = new Label(profilingPanel, std::string("Physics time : ") + floatToString(mCachedPhysicsUpdateTime * 1000.0, 1) + std::string(" ms"),"sans-bold"); profilingPanel->setVisible(true); } diff --git a/testbed/src/TestbedApplication.cpp b/testbed/src/TestbedApplication.cpp index cb970844..dad2cdf3 100644 --- a/testbed/src/TestbedApplication.cpp +++ b/testbed/src/TestbedApplication.cpp @@ -213,11 +213,12 @@ void TestbedApplication::drawContents() { // Render the scene mCurrentScene->render(); - mGui.update(); - // Check the OpenGL errors checkOpenGLErrors(); + + mGui.update(); + // Compute the current framerate computeFPS(); } @@ -291,6 +292,12 @@ void TestbedApplication::checkOpenGLErrorsInternal(const char* file, int line) { // Compute the FPS void TestbedApplication::computeFPS() { + // Note : By default the nanogui library is using glfwWaitEvents() to process + // events and sleep to target a framerate of 50 ms (using a thread + // sleeping). However, for games we prefer to use glfwPollEvents() + // instead and remove the update. Therefore the file common.cpp of the + // nanogui library has been modified to have a faster framerate + mNbFrames++; // Get the number of seconds since start @@ -386,6 +393,16 @@ bool TestbedApplication::scrollEvent(const Vector2i &p, const Vector2f &rel) { return mCurrentScene->scrollingEvent(rel[0], rel[1], SCROLL_SENSITIVITY); } +void TestbedApplication::drawAll() { + glClearColor(mBackground[0], mBackground[1], mBackground[2], 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + + drawContents(); + drawWidgets(); + + glfwSwapBuffers(mGLFWWindow); +} + // Callback method to receive scrolling events void TestbedApplication::scroll(GLFWwindow* window, double xAxis, double yAxis) { diff --git a/testbed/src/TestbedApplication.h b/testbed/src/TestbedApplication.h index 29bdc545..6aebe982 100644 --- a/testbed/src/TestbedApplication.h +++ b/testbed/src/TestbedApplication.h @@ -190,6 +190,7 @@ class TestbedApplication : public Screen { /// Destructor virtual ~TestbedApplication(); + virtual void drawAll(); virtual void drawContents(); /// Window resize event handler @@ -275,13 +276,13 @@ inline void TestbedApplication::displayContactPoints(bool display) { // Enable/Disable Vertical synchronization inline void TestbedApplication::enableVSync(bool enable) { - /*mIsVSyncEnabled = enable; + mIsVSyncEnabled = enable; if (mIsVSyncEnabled) { glfwSwapInterval(1); } else { glfwSwapInterval(0); - }*/ + } } #endif