From e77f3c331ef1de095b962dc5149b3658bcd80c9f Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Wed, 26 Jun 2024 22:27:12 +0300 Subject: [PATCH] PlaneCameraController class & using it in MovingCubeApp example if USE_PLANE_CAM_CONTROL is nonzero --- examples/ExampleApps/MovingCubeApp.cpp | 16 +++++ .../Scene/PlaneCameraController.cpp | 67 +++++++++++++++++++ .../Scene/PlaneCameraController.hpp | 32 +++++++++ 3 files changed, 115 insertions(+) create mode 100644 openVulkanoCpp/Scene/PlaneCameraController.cpp create mode 100644 openVulkanoCpp/Scene/PlaneCameraController.hpp diff --git a/examples/ExampleApps/MovingCubeApp.cpp b/examples/ExampleApps/MovingCubeApp.cpp index 9297ee5..8dc9a12 100644 --- a/examples/ExampleApps/MovingCubeApp.cpp +++ b/examples/ExampleApps/MovingCubeApp.cpp @@ -17,6 +17,7 @@ #include "Scene/SimpleAnimationController.hpp" #include "Scene/SequenceAnimationController.hpp" #include "Scene/MorphableCameraController.hpp" +#include "Scene/PlaneCameraController.hpp" #include "Scene/UI/PerformanceInfo.hpp" #include "Input/InputManager.hpp" #include "Host/GraphicsAppManager.hpp" @@ -24,6 +25,8 @@ #include "Base/Logger.hpp" #include "Controller/FreeCamCameraController.hpp" +#define USE_PLANE_CAM_CONTROL 0 + namespace OpenVulkano { namespace @@ -34,13 +37,18 @@ namespace OpenVulkano Scene::SimpleDrawable m_drawable; Scene::Node m_node; }; + const Math::Vector3f PLANE_NORMAL(1, 0, 0); } class MovingCubeAppImpl final : public MovingCubeApp { Scene::Scene m_scene; Scene::MorphableCamera m_camera; Scene::MorphableCameraController m_morphableCameraControl; +#if USE_PLANE_CAM_CONTROL + Scene::PlaneCameraController m_cameraControl; +#else FreeCamCameraController m_cameraControl; +#endif Scene::Material m_material; Scene::Shader m_shader; @@ -117,7 +125,11 @@ namespace OpenVulkano MovingCubeAppImpl() : m_camera(90, 16, 9, 0.1, 1000) { m_morphableCameraControl.Init(&m_camera); +#if USE_PLANE_CAM_CONTROL + m_cameraControl.Init(&m_camera, PLANE_NORMAL); +#else m_cameraControl.Init(&m_camera); +#endif } void Init() override @@ -133,7 +145,11 @@ namespace OpenVulkano m_shader.AddVertexInputDescription(Vertex::GetVertexInputDescription()); GetGraphicsAppManager()->GetRenderer()->SetScene(&m_scene); +#if USE_PLANE_CAM_CONTROL + m_cameraControl.Init(&m_camera, PLANE_NORMAL); +#else m_cameraControl.Init(&m_camera); +#endif m_cameraControl.SetDefaultKeybindings(); CreateSceneElement(&m_whiteBox, Math::Vector4f(1, 1, 1, 1), 1); diff --git a/openVulkanoCpp/Scene/PlaneCameraController.cpp b/openVulkanoCpp/Scene/PlaneCameraController.cpp new file mode 100644 index 0000000..f72a0fa --- /dev/null +++ b/openVulkanoCpp/Scene/PlaneCameraController.cpp @@ -0,0 +1,67 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include "PlaneCameraController.hpp" +#include "Base/FrameMetadata.hpp" +#include "Input/InputManager.hpp" +#include "Input/InputKey.hpp" +#include "Scene/Camera.hpp" + +namespace OpenVulkano::Scene +{ + PlaneCameraController::PlaneCameraController(Camera* camera) + : CameraController(camera), m_planeNormal(0.0f, 1.0f, 0.0f), m_position(0.0f, 0.0f, 0.0f) + { + auto input = OpenVulkano::Input::InputManager::GetInstance(); + m_actionForward = input->GetAction("forward"); + m_actionSide = input->GetAction("side"); + m_actionUp = input->GetAction("up"); + m_actionBoost = input->GetAction("boost"); + } + + void PlaneCameraController::Init(Camera* camera, const Math::Vector3f& planeNormal) + { + CameraController::Init(camera); + m_planeNormal = Math::Utils::normalize(planeNormal); + SetDefaultKeybindings(); + } + + void PlaneCameraController::Tick() + { + auto input = OpenVulkano::Input::InputManager::GetInstance(); + + Math::Vector3f direction(input->GetAxis(m_actionSide), input->GetAxis(m_actionUp), + -input->GetAxis(m_actionForward)); + if (Math::Utils::length2(direction) > 1.0f) { direction = Math::Utils::normalize(direction); } + + const float SPEED = 3.0f; + const float BOOST_FACTOR = 3.0; + + if (input->GetButton(m_actionBoost)) { direction *= BOOST_FACTOR; } + + float dt = CURRENT_FRAME.frameTime; + + direction *= dt * SPEED; + direction = direction - (Math::Utils::dot(direction, m_planeNormal) * m_planeNormal); + m_position += direction; + + Math::Matrix4f transformation = Math::Utils::translate(m_position); + GetCamera()->SetMatrix(transformation); + } + + void PlaneCameraController::SetDefaultKeybindings() + { + m_actionForward->BindKey(Input::InputKey::Controller::AXIS_LEFT_Y); + m_actionForward->BindAxisButtons(Input::InputKey::Keyboard::KEY_W, Input::InputKey::Keyboard::KEY_S); + m_actionForward->BindKey(Input::InputKey::Touch::AXIS_PAN_TWO_FINGERS_Y, -0.0025f); + m_actionSide->BindKey(Input::InputKey::Controller::AXIS_LEFT_X); + m_actionSide->BindAxisButtons(Input::InputKey::Keyboard::KEY_D, Input::InputKey::Keyboard::KEY_A); + m_actionSide->BindKey(Input::InputKey::Touch::AXIS_PAN_TWO_FINGERS_X, 0.0025f); + m_actionUp->BindAxisButtons(Input::InputKey::Keyboard::KEY_SPACE, Input::InputKey::Keyboard::KEY_LEFT_CONTROL); + } + + void PlaneCameraController::SetPlaneNormal(const Math::Vector3f& planeNormal) { m_planeNormal = planeNormal; } +} \ No newline at end of file diff --git a/openVulkanoCpp/Scene/PlaneCameraController.hpp b/openVulkanoCpp/Scene/PlaneCameraController.hpp new file mode 100644 index 0000000..02d33af --- /dev/null +++ b/openVulkanoCpp/Scene/PlaneCameraController.hpp @@ -0,0 +1,32 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include "Controller/CameraController.hpp" +#include "Input/InputAction.hpp" +#include "Math/Math.hpp" + +namespace OpenVulkano::Scene +{ + class PlaneCameraController final : public CameraController + { + Math::Vector3f m_planeNormal; + Math::Vector3f m_position; + Input::InputAction* m_actionForward; + Input::InputAction* m_actionSide; + Input::InputAction* m_actionUp; + Input::InputAction* m_actionBoost; + + public: + PlaneCameraController(Camera* camera = nullptr); + + void Init(Camera* camera, const Math::Vector3f& planeNormal); + void Tick() override; + void SetDefaultKeybindings(); + void SetPlaneNormal(const Math::Vector3f& planeNormal); + }; +} \ No newline at end of file