From 2b0b4ea5fde23417fee5cce5b952911606317d62 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Tue, 23 Nov 2021 17:41:35 +0100 Subject: [PATCH] Add camera rotation animation and possibility to show/hide GUI in testbed application --- testbed/src/Gui.cpp | 14 +++++++++----- testbed/src/Gui.h | 15 +++++++++++++++ testbed/src/Scene.cpp | 2 +- testbed/src/Scene.h | 20 ++++++++++++++++++++ testbed/src/SceneDemo.cpp | 12 +++++++++++- testbed/src/SceneDemo.h | 4 ++++ testbed/src/TestbedApplication.cpp | 14 +++++++++++++- 7 files changed, 73 insertions(+), 8 deletions(-) diff --git a/testbed/src/Gui.cpp b/testbed/src/Gui.cpp index e429b946..252e5fd7 100644 --- a/testbed/src/Gui.cpp +++ b/testbed/src/Gui.cpp @@ -43,7 +43,7 @@ double Gui::mCachedPhysicsStepTime = 0; Gui::Gui(TestbedApplication* app) : mApp(app), mSimulationPanel(nullptr), mSettingsPanel(nullptr), mPhysicsPanel(nullptr), mRenderingPanel(nullptr), mFPSLabel(nullptr), mFrameTimeLabel(nullptr), mTotalPhysicsTimeLabel(nullptr), - mPhysicsStepTimeLabel(nullptr) + mPhysicsStepTimeLabel(nullptr), mIsDisplayed(true) { } @@ -84,14 +84,18 @@ void Gui::drawAll() { void Gui::draw() { - mScreen->draw_setup(); - mScreen->clear(); - mScreen->draw_contents(); + if (mIsDisplayed) { + mScreen->draw_setup(); + mScreen->draw_contents(); + } } void Gui::drawTearDown() { - mScreen->draw_widgets(); + if (mIsDisplayed) { + mScreen->draw_widgets(); + } + mScreen->draw_teardown(); } diff --git a/testbed/src/Gui.h b/testbed/src/Gui.h index 10063637..ac1b9a9a 100644 --- a/testbed/src/Gui.h +++ b/testbed/src/Gui.h @@ -92,6 +92,9 @@ class Gui { std::vector mCheckboxesScenes; ComboBox* mComboBoxScenes; + /// True if the GUI is displayed + bool mIsDisplayed; + // -------------------- Methods -------------------- // static void resetScroll(); @@ -158,6 +161,10 @@ class Gui { void onMouseButtonEvent(int button, int action, int modifiers); void onKeyboardEvent(int key, int scancode, int action, int modifiers); + + bool getIsDisplayed() const; + + void setIsDisplayed(bool isDisplayed); }; inline void Gui::resetScroll() { @@ -176,4 +183,12 @@ inline std::string Gui::floatToString(float value, int precision) { return ss.str(); } +inline bool Gui::getIsDisplayed() const { + return mIsDisplayed; +} + +inline void Gui::setIsDisplayed(bool isDisplayed) { + mIsDisplayed = isDisplayed; +} + #endif diff --git a/testbed/src/Scene.cpp b/testbed/src/Scene.cpp index 29d29251..dc0260c7 100644 --- a/testbed/src/Scene.cpp +++ b/testbed/src/Scene.cpp @@ -38,7 +38,7 @@ Scene::Scene(const std::string& name, EngineSettings& engineSettings, bool isSha mViewportWidth(0), mViewportHeight(0), mIsShadowMappingEnabled(isShadowMappingEnabled), mAreContactPointsDisplayed(true), mAreContactNormalsDisplayed(false), mAreBroadPhaseAABBsDisplayed(false), mAreCollidersAABBsDisplayed(false), mAreCollisionShapesDisplayed(false), mIsWireframeEnabled(false), - mInitZoom(2.0f) { + mInitZoom(2.0f), mIsCameraRotationAnimationEnabled(false) { } diff --git a/testbed/src/Scene.h b/testbed/src/Scene.h index 62acf390..93ec05bb 100644 --- a/testbed/src/Scene.h +++ b/testbed/src/Scene.h @@ -158,6 +158,9 @@ class Scene : public rp3d::EventListener { /// Initial zoom factor float mInitZoom; + /// True if the automatic camera rotation animation is enabled + bool mIsCameraRotationAnimationEnabled; + // -------------------- Methods -------------------- // /// Set the scene position (where the camera needs to look at) @@ -274,6 +277,13 @@ class Scene : public rp3d::EventListener { /// Return a reference to the engine settings of the scene EngineSettings& getEngineSettings(); + + /// Return true if the camera rotation animation is enabled + bool getIsCameraRotationAnimationEnabled() const; + + /// Set whether the camera rotation animation is enabled or not + void setIsCameraRotationAnimationEnabled(bool isRotationEnabled); + }; // Called when a keyboard event occurs @@ -381,4 +391,14 @@ inline EngineSettings& Scene::getEngineSettings() { return mEngineSettings; } +// Return true if the scene rotation is enabled +inline bool Scene::getIsCameraRotationAnimationEnabled() const { + return mIsCameraRotationAnimationEnabled; +} + +// Set whether the scene rotation is enabled or no +inline void Scene::setIsCameraRotationAnimationEnabled(bool isRotationEnabled) { + mIsCameraRotationAnimationEnabled = isRotationEnabled; +} + #endif diff --git a/testbed/src/SceneDemo.cpp b/testbed/src/SceneDemo.cpp index c1ba527c..ef2af69b 100644 --- a/testbed/src/SceneDemo.cpp +++ b/testbed/src/SceneDemo.cpp @@ -49,7 +49,7 @@ SceneDemo::SceneDemo(const std::string& name, EngineSettings& settings, reactphy mQuadShader("shaders/quad.vert", "shaders/quad.frag"), mVBOQuad(GL_ARRAY_BUFFER), mDebugVBOLinesVertices(GL_ARRAY_BUFFER), mDebugVBOTrianglesVertices(GL_ARRAY_BUFFER), mMeshFolderPath("meshes/"), mPhysicsCommon(physicsCommon), mPhysicsWorld(nullptr), mIsPhysicsWorldSimulated(isPhysicsWorldSimulated), - mIsMovingBody(false), mMovingBody(nullptr) { + mIsMovingBody(false), mMovingBody(nullptr), mCameraRotationAngle(0) { shadowMapTextureLevel++; @@ -143,6 +143,16 @@ void SceneDemo::update() { // Update the transform used for the rendering (*it)->updateTransform(mInterpolationFactor); } + + if (mIsCameraRotationAnimationEnabled) { + rotateCameraAnimation(); + } +} + +void SceneDemo::rotateCameraAnimation() { + + const float angle = 0.1f * (PI / 180.0); + mCamera.rotateAroundWorldPoint(Vector3(0, 1, 0), angle, mCenterScene); } // Update the physics world (take a simulation step) diff --git a/testbed/src/SceneDemo.h b/testbed/src/SceneDemo.h index 4a501f2a..a3a204fa 100644 --- a/testbed/src/SceneDemo.h +++ b/testbed/src/SceneDemo.h @@ -135,6 +135,8 @@ class SceneDemo : public Scene, rp3d::RaycastCallback { /// Pointer to the body that is currently moved with the mouse by the user rp3d::RigidBody* mMovingBody; + float mCameraRotationAngle; + // -------------------- Methods -------------------- // /// Create the Shadow map FBO and texture @@ -208,6 +210,8 @@ class SceneDemo : public Scene, rp3d::RaycastCallback { /// Called when a raycast hit occurs (used to move a body with the mouse) virtual rp3d::decimal notifyRaycastHit(const rp3d::RaycastInfo& raycastInfo) override; + + void rotateCameraAnimation(); }; // Enabled/Disable the shadow mapping diff --git a/testbed/src/TestbedApplication.cpp b/testbed/src/TestbedApplication.cpp index 849e9c19..e098c980 100755 --- a/testbed/src/TestbedApplication.cpp +++ b/testbed/src/TestbedApplication.cpp @@ -604,7 +604,19 @@ void TestbedApplication::keyboard_event(int key, int scancode, int action, int m return; } - // Close application on escape key + // Show/hide the GUI with "i" key + if (key == GLFW_KEY_I && action == GLFW_PRESS) { + mGui.setIsDisplayed(!mGui.getIsDisplayed()); + return; + } + + // Start/Stop camera rotation animation with "r" key + if (key == GLFW_KEY_R && action == GLFW_PRESS) { + mCurrentScene->setIsCameraRotationAnimationEnabled(!mCurrentScene->getIsCameraRotationAnimationEnabled()); + return; + } + + // Pause the application on "p" key if (key == GLFW_KEY_P && action == GLFW_PRESS) { if (mTimer.isRunning()) {