diff --git a/examples/ExampleApps/MovingCubeApp.cpp b/examples/ExampleApps/MovingCubeApp.cpp index 5adcc8b..281f4e6 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, 1); } 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,12 @@ namespace OpenVulkano MovingCubeAppImpl() : m_camera(90) { m_morphableCameraControl.Init(&m_camera); +#if USE_PLANE_CAM_CONTROL + m_cameraControl.Init(&m_camera, PLANE_NORMAL); + // m_cameraControl.Init(&m_camera, Scene::PlaneCameraController::DefaultAxis::OYZ); +#else m_cameraControl.Init(&m_camera); +#endif m_camera.SetZoom(50); } @@ -133,7 +146,12 @@ namespace OpenVulkano m_shader.AddVertexInputDescription(Vertex::GetVertexInputDescription()); GetGraphicsAppManager()->GetRenderer()->SetScene(&m_scene); +#if USE_PLANE_CAM_CONTROL + m_cameraControl.Init(&m_camera, PLANE_NORMAL); + // m_cameraControl.Init(&m_camera, Scene::PlaneCameraController::DefaultAxis::OYZ); +#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..59a08d9 --- /dev/null +++ b/openVulkanoCpp/Scene/PlaneCameraController.cpp @@ -0,0 +1,86 @@ +/* + * 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_actionBackward = input->GetAction("backward"); + m_actionLeft = input->GetAction("left"); + m_actionRight = input->GetAction("right"); + m_actionUp = input->GetAction("up"); + } + + void PlaneCameraController::Init(Camera* camera, const Math::Vector3f& planeNormal) + { + CameraController::Init(camera); + SetPlaneNormal(planeNormal); + SetDefaultKeybindings(); + } + + void PlaneCameraController::Init(Camera* camera, DefaultAxis axis) + { + Math::Vector3f vector; + switch (axis) + { + case DefaultAxis::OXY: vector = Math::Vector3f(0, 0, 1); break; + case DefaultAxis::OXZ: vector = Math::Vector3f(0, 1, 0); break; + case DefaultAxis::OYZ: vector = Math::Vector3f(1, 0, 0); break; + } + Init(camera, vector); + } + + void PlaneCameraController::Tick() + { + auto input = OpenVulkano::Input::InputManager::GetInstance(); + + Math::Vector3f direction(0.0f, 0.0f, 0.0f); + + if (input->GetButton(m_actionForward)) { direction += m_planeForward; } + if (input->GetButton(m_actionBackward)) { direction -= m_planeForward; } + if (input->GetButton(m_actionLeft)) { direction -= m_planeRight; } + if (input->GetButton(m_actionRight)) { direction += m_planeRight; } + + if (Math::Utils::length2(direction) > 0.0f) { direction = Math::Utils::normalize(direction); } + + float timeScale = CURRENT_FRAME.frameTime; + float speed = 3.0f; + direction *= timeScale * 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::Keyboard::KEY_W); + m_actionBackward->BindKey(Input::InputKey::Keyboard::KEY_S); + m_actionLeft->BindKey(Input::InputKey::Keyboard::KEY_A); + m_actionRight->BindKey(Input::InputKey::Keyboard::KEY_D); + m_actionUp->BindAxisButtons(Input::InputKey::Keyboard::KEY_SPACE, Input::InputKey::Keyboard::KEY_LEFT_CONTROL); + } + + void PlaneCameraController::SetPlaneNormal(const Math::Vector3f& planeNormal) + { + m_planeNormal = Math::Utils::normalize(planeNormal); + + Math::Vector3f arbitraryVector = (fabs(m_planeNormal.x) > 0.9f) ? Math::Vector3f(0.0f, 1.0f, 0.0f) : + Math::Vector3f(1.0f, 0.0f, 0.0f); + m_planeRight = Math::Utils::normalize(Math::Utils::cross(m_planeNormal, arbitraryVector)); + m_planeForward = Math::Utils::normalize(Math::Utils::cross(m_planeNormal, m_planeRight)); + } +} \ No newline at end of file diff --git a/openVulkanoCpp/Scene/PlaneCameraController.hpp b/openVulkanoCpp/Scene/PlaneCameraController.hpp new file mode 100644 index 0000000..c1bdfac --- /dev/null +++ b/openVulkanoCpp/Scene/PlaneCameraController.hpp @@ -0,0 +1,43 @@ +/* + * 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; + Math::Vector3f m_planeRight; + Math::Vector3f m_planeForward; + Input::InputAction* m_actionForward; + Input::InputAction* m_actionBackward; + Input::InputAction* m_actionLeft; + Input::InputAction* m_actionRight; + Input::InputAction* m_actionUp; + + public: + enum class DefaultAxis + { + OXY, + OXZ, + OYZ, + }; + + PlaneCameraController(Camera* camera = nullptr); + + void Init(Camera* camera, const Math::Vector3f& planeNormal); + void Init(Camera* camera, DefaultAxis axis); + void Tick() override; + void SetDefaultKeybindings(); + void SetPlaneNormal(const Math::Vector3f& planeNormal); + }; +} \ No newline at end of file