From 669d52fe0cf99118ff87c1d6cd86398e28bdefa6 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Tue, 25 Jun 2024 22:00:20 +0300 Subject: [PATCH 1/7] Fixed bug with unitialized texture pointer --- openVulkanoCpp/Scene/Material.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openVulkanoCpp/Scene/Material.hpp b/openVulkanoCpp/Scene/Material.hpp index 59b199b..5c6e206 100644 --- a/openVulkanoCpp/Scene/Material.hpp +++ b/openVulkanoCpp/Scene/Material.hpp @@ -18,6 +18,6 @@ namespace OpenVulkano::Scene struct Material { MaterialProperties properties; - Texture* texture; + Texture* texture = nullptr; }; } From 624742eeb540d6727d389c2e85833f3b35099557 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Tue, 25 Jun 2024 22:01:36 +0300 Subject: [PATCH 2/7] Removed final attribute from UpdateProjectionMatrix() --- openVulkanoCpp/Scene/Camera.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openVulkanoCpp/Scene/Camera.hpp b/openVulkanoCpp/Scene/Camera.hpp index 0ae069c..46e4542 100644 --- a/openVulkanoCpp/Scene/Camera.hpp +++ b/openVulkanoCpp/Scene/Camera.hpp @@ -226,7 +226,7 @@ namespace OpenVulkano::Scene return 2.0f * atanf(tanf(m_fov * 0.5f) * m_aspect); } - void UpdateProjectionMatrix() final + void UpdateProjectionMatrix() { m_projection = Math::Utils::perspectiveRH_ZO(m_fov, m_aspect, m_nearPlane, m_farPlane); UpdateViewProjectionMatrix(); From 27c3e52f8caf531f334e83701aab5477e1db1878 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Tue, 25 Jun 2024 22:05:21 +0300 Subject: [PATCH 3/7] MorphableCamera class --- openVulkanoCpp/Scene/MorphableCamera.cpp | 32 +++++++++++++++++++++++ openVulkanoCpp/Scene/MorphableCamera.hpp | 33 ++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 openVulkanoCpp/Scene/MorphableCamera.cpp create mode 100644 openVulkanoCpp/Scene/MorphableCamera.hpp diff --git a/openVulkanoCpp/Scene/MorphableCamera.cpp b/openVulkanoCpp/Scene/MorphableCamera.cpp new file mode 100644 index 0000000..40807b6 --- /dev/null +++ b/openVulkanoCpp/Scene/MorphableCamera.cpp @@ -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/. + */ + +#include "MorphableCamera.hpp" + +namespace OpenVulkano::Scene +{ + namespace + { + Math::Matrix4f BlendMatrices(const Math::Matrix4f& mat1, const Math::Matrix4f& mat2, float t) + { + Math::Matrix4f result; + for (int i = 0; i < 4; ++i) + { + for (int j = 0; j < 4; ++j) { result[i][j] = glm::mix(mat1[i][j], mat2[i][j], t); } + } + return result; + } + } + void MorphableCamera::UpdateProjectionMatrix() + { + PerspectiveCamera::UpdateProjectionMatrix(); + float aspectH = m_aspect; + float aspectV = 1; + m_orthoMatrix = Math::Utils::orthoRH_ZO(-aspectH, aspectH, -aspectV, aspectV, m_nearPlane, m_farPlane); + m_projection = BlendMatrices(m_projection, m_orthoMatrix, m_morphState); + UpdateViewProjectionMatrix(); + } +} \ No newline at end of file diff --git a/openVulkanoCpp/Scene/MorphableCamera.hpp b/openVulkanoCpp/Scene/MorphableCamera.hpp new file mode 100644 index 0000000..47b8b10 --- /dev/null +++ b/openVulkanoCpp/Scene/MorphableCamera.hpp @@ -0,0 +1,33 @@ +/* + * 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 "Camera.hpp" +#include "Math/Math.hpp" + +namespace OpenVulkano::Scene +{ + class MorphableCamera : public PerspectiveCamera + { + float m_morphState; + Math::Matrix4f m_orthoMatrix; + + public: + MorphableCamera(float fovDegrees, float width, float height, float nearPlane, float farPlane) + : PerspectiveCamera(fovDegrees, nearPlane, farPlane, width, height), m_morphState(0.0f) + { + UpdateProjectionMatrix(); + } + + void SetMorphState(float state) + { + m_morphState = Math::Utils::clamp(state, 0.0f, 1.0f); + UpdateProjectionMatrix(); + } + + void UpdateProjectionMatrix() override; + }; +} \ No newline at end of file From 4dabb2b5191bb83c0efb379d76415f3c9a766c1f Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Tue, 25 Jun 2024 22:05:34 +0300 Subject: [PATCH 4/7] MorphableCameraController class --- .../Scene/MorphableCameraController.cpp | 109 ++++++++++++++++++ .../Scene/MorphableCameraController.hpp | 46 ++++++++ 2 files changed, 155 insertions(+) create mode 100644 openVulkanoCpp/Scene/MorphableCameraController.cpp create mode 100644 openVulkanoCpp/Scene/MorphableCameraController.hpp diff --git a/openVulkanoCpp/Scene/MorphableCameraController.cpp b/openVulkanoCpp/Scene/MorphableCameraController.cpp new file mode 100644 index 0000000..cfce8fa --- /dev/null +++ b/openVulkanoCpp/Scene/MorphableCameraController.cpp @@ -0,0 +1,109 @@ +/* + * 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 "MorphableCameraController.hpp" +#include "Input/InputManager.hpp" +#include "Input/InputKey.hpp" +#include "Base/FrameMetadata.hpp" +#include "Scene/Camera.hpp" + +namespace OpenVulkano::Scene +{ + MorphableCameraController::MorphableCameraController(MorphableCamera* camera) + : CameraController(camera) + , m_animationDuration(1.0f) + , m_currentTime(0.0f) + , m_isMorphing(false) + , m_targetMorphState(false) + { + auto input = OpenVulkano::Input::InputManager::GetInstance(); + m_actionMorph = input->GetAction("morph"); + m_actionForward = input->GetAction("forward"); + m_actionSide = input->GetAction("side"); + m_actionUp = input->GetAction("up"); + m_actionLookUp = input->GetAction("look up"); + m_actionLookSide = input->GetAction("look side"); + m_actionBoost = input->GetAction("boost"); + } + + void MorphableCameraController::Init(MorphableCamera* camera) + { + CameraController::Init(camera); + SetDefaultKeybindings(); + } + + void MorphableCameraController::Tick() + { + auto input = OpenVulkano::Input::InputManager::GetInstance(); + + bool isMorphingDown = input->GetButton(m_actionMorph); + if (!m_wasMorphingKeyDown && isMorphingDown) + { + m_isMorphing = true; + m_currentTime = 0.0f; + m_targetMorphState = !m_targetMorphState; + } + m_wasMorphingKeyDown = isMorphingDown; + + if (m_isMorphing) + { + m_currentTime += CURRENT_FRAME.frameTime; + if (m_currentTime > m_animationDuration) m_currentTime = m_animationDuration; + float t = m_currentTime / m_animationDuration; + if (t >= 1.0f) + { + t = 1.0f; + m_isMorphing = false; + } + float newState = m_targetMorphState ? t : (1.0f - t); + static_cast(GetCamera())->SetMorphState(newState); + } + + // Movement logic + const float MOVEMENT_SPEED = 3.0f; + const float BOOST_MULT = 2.0f; + + Math::Vector3f_SIMD vec(input->GetAxis(m_actionSide), input->GetAxis(m_actionUp), + -input->GetAxis(m_actionForward)); + if (Math::Utils::length2(vec) > 1.0f) { vec = Math::Utils::normalize(vec); } + + float dt = CURRENT_FRAME.frameTime; + vec = vec * dt * MOVEMENT_SPEED; + if (input->GetButton(m_actionBoost)) { vec *= BOOST_MULT; } + + m_yaw -= input->GetAxis(m_actionLookSide) * dt / 2.0f; + m_pitch -= input->GetAxis(m_actionLookUp) * dt / 2.0f; + m_pitch = std::min(1.4f, std::max(-1.4f, m_pitch)); + + const Math::QuaternionF rot(Math::Vector3f(m_pitch, m_yaw, 0)); + m_position += rot * vec; + Math::Matrix4f camTransformation = Math::Utils::toMat4(rot); + camTransformation[3] = Math::Vector4f(m_position, 1); + GetCamera()->SetMatrix(camTransformation); + } + + void MorphableCameraController::SetDefaultKeybindings() + { + m_actionMorph->BindKey(Input::InputKey::Keyboard::KEY_P); + + 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_actionLookUp->BindKey(Input::InputKey::Controller::AXIS_RIGHT_Y); + m_actionLookUp->BindAxisButtons(Input::InputKey::Keyboard::KEY_DOWN, Input::InputKey::Keyboard::KEY_UP); + m_actionLookUp->BindKey(Input::InputKey::Touch::AXIS_PAN_Y, 0.10f); + m_actionLookSide->BindKey(Input::InputKey::Controller::AXIS_RIGHT_X); + m_actionLookSide->BindAxisButtons(Input::InputKey::Keyboard::KEY_RIGHT, Input::InputKey::Keyboard::KEY_LEFT); + m_actionLookSide->BindKey(Input::InputKey::Touch::AXIS_PAN_X, 0.10f); + m_actionLookUp->BindKey(Input::InputKey::Mouse::AXIS_Y); + m_actionLookSide->BindKey(Input::InputKey::Mouse::AXIS_X); + m_actionUp->BindAxisButtons(Input::InputKey::Keyboard::KEY_SPACE, Input::InputKey::Keyboard::KEY_LEFT_CONTROL); + m_actionBoost->BindKey(Input::InputKey::Keyboard::KEY_LEFT_SHIFT); + } +} \ No newline at end of file diff --git a/openVulkanoCpp/Scene/MorphableCameraController.hpp b/openVulkanoCpp/Scene/MorphableCameraController.hpp new file mode 100644 index 0000000..77f403c --- /dev/null +++ b/openVulkanoCpp/Scene/MorphableCameraController.hpp @@ -0,0 +1,46 @@ +/* + * 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 "MorphableCamera.hpp" +#include "Controller/CameraController.hpp" +#include "Input/InputManager.hpp" +#include "Input/InputKey.hpp" +#include "Math/Math.hpp" + +namespace OpenVulkano::Scene +{ + class MorphableCameraController final : public CameraController + { + double m_animationDuration; + double m_currentTime; + bool m_isMorphing; + bool m_targetMorphState; // true for perspective, false for orthographic + + float m_yaw = 0, m_pitch = 0, m_boostFactor = 2; + Math::Vector3f_SIMD m_position = { 0, 0, 0 }; + + Input::InputAction* m_actionForward; + Input::InputAction* m_actionSide; + Input::InputAction* m_actionUp; + Input::InputAction* m_actionLookUp; + Input::InputAction* m_actionLookSide; + Input::InputAction* m_actionBoost; + Input::InputAction* m_actionMorph; + bool m_wasMorphingKeyDown = false; + + public: + MorphableCameraController(MorphableCamera* camera = nullptr); + + void Init(MorphableCamera* camera); + void Tick() override; + void SetDefaultKeybindings(); + + void SetDuration(double duration) { m_animationDuration = duration; } + double GetDuration() { return m_animationDuration; } + void Reset() { m_currentTime = 0; } + }; +} \ No newline at end of file From 1fbd072429939f76f48880ea7f97d86cfd484714 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Tue, 25 Jun 2024 22:05:55 +0300 Subject: [PATCH 5/7] Using MorphableCameraController class in MovingCubeApp example --- examples/ExampleApps/MovingCubeApp.cpp | 58 +++++++++++++------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/examples/ExampleApps/MovingCubeApp.cpp b/examples/ExampleApps/MovingCubeApp.cpp index 24874fd..ba970b6 100644 --- a/examples/ExampleApps/MovingCubeApp.cpp +++ b/examples/ExampleApps/MovingCubeApp.cpp @@ -16,6 +16,7 @@ #include "Scene/Camera.hpp" #include "Scene/SimpleAnimationController.hpp" #include "Scene/SequenceAnimationController.hpp" +#include "Scene/MorphableCameraController.hpp" #include "Scene/UI/PerformanceInfo.hpp" #include "Input/InputManager.hpp" #include "Host/GraphicsAppManager.hpp" @@ -37,8 +38,8 @@ namespace OpenVulkano class MovingCubeAppImpl final : public MovingCubeApp { Scene::Scene m_scene; - Scene::PerspectiveCamera m_camera; - FreeCamCameraController m_cameraControl; + Scene::MorphableCamera m_camera; + Scene::MorphableCameraController m_cameraControl; Scene::Material m_material; Scene::Shader m_shader; @@ -74,48 +75,50 @@ namespace OpenVulkano CompleteSceneElement(dest); } - void CreatePlane(SceneElement *dest, const Math::Vector4f& color) + void CreatePlane(SceneElement *dest, const Math::Vector4f &color) { dest->m_geometry = Scene::GeometryFactory::MakePlane(1, 1, color); CompleteSceneElement(dest); } - void CreateSphere(SceneElement *dest, const Math::Vector4f& color) + void CreateSphere(SceneElement *dest, const Math::Vector4f &color) { dest->m_geometry = Scene::GeometryFactory::MakeSphere(1, 32, 16, color); CompleteSceneElement(dest); } - void CreateHemisphere(SceneElement *dest, const Math::Vector4f& color) + void CreateHemisphere(SceneElement *dest, const Math::Vector4f &color) { dest->m_geometry = Scene::GeometryFactory::MakeHemisphere(1, 16, 16, color); CompleteSceneElement(dest); } - void CreateTriangle(SceneElement *dest, const Math::Vector4f& color) + void CreateTriangle(SceneElement *dest, const Math::Vector4f &color) { - dest->m_geometry = Scene::GeometryFactory::MakeTriangle(Math::Vector3f(0.5, 0., 0.), Math::Vector3f(0., 0.5, 0.), Math::Vector3f(-0.5, 0., 0.), color); + dest->m_geometry = Scene::GeometryFactory::MakeTriangle( + Math::Vector3f(0.5, 0., 0.), Math::Vector3f(0., 0.5, 0.), Math::Vector3f(-0.5, 0., 0.), color); CompleteSceneElement(dest); } - void CreateCylinder(SceneElement *dest, const Math::Vector4f& color) + void CreateCylinder(SceneElement *dest, const Math::Vector4f &color) { dest->m_geometry = Scene::GeometryFactory::MakeCylinder(1, 3, 64, color); CompleteSceneElement(dest); } - void CreatePyramid(SceneElement *dest, const Math::Vector4f& color) + void CreatePyramid(SceneElement *dest, const Math::Vector4f &color) { dest->m_geometry = Scene::GeometryFactory::MakePyramid(0.5, 2, color); CompleteSceneElement(dest); } public: + MovingCubeAppImpl() : m_camera(90, 16, 9, 0.1, 1000) { m_cameraControl.Init(&m_camera); } + void Init() override { auto engineConfig = EngineConfiguration::GetEngineConfiguration(); m_camera.Init(70, 16, 9, 0.1, 100); - // m_camera.SetMatrix(OpenVulkano::Math::Utils::translate(OpenVulkano::Math::Matrix4f(1), OpenVulkano::Math::Vector3f_SIMD(0, 0, -50))); m_scene.Init(); m_scene.SetCamera(&m_camera); @@ -136,16 +139,23 @@ namespace OpenVulkano Math::Pose srcPose(Math::Quaternion(), Math::Vector3f_SIMD(-3, 0, 0)); Math::Pose destPose(Math::Quaternion(), Math::Vector3f_SIMD(3, 0, 0)); m_simpleAnimationController.SetPoses(srcPose, destPose); - m_simpleAnimationController.m_completionEvent += EventHandler(this, &MovingCubeAppImpl::OnSimpleAnimationCompleted); + m_simpleAnimationController.m_completionEvent += + EventHandler(this, &MovingCubeAppImpl::OnSimpleAnimationCompleted); m_sequenceAnimationController.EnableLoop(true); m_sequenceAnimationController.SetNode(&m_redBox.m_node); - m_sequenceAnimationController.AddAnimationStep(Math::PoseF(Math::Utils::normalize(Math::QuaternionF(1, 0, 0, 1)), Math::Vector3f_SIMD(0, 0, 1)), 5); - m_sequenceAnimationController.AddAnimationStep(Math::PoseF(Math::Utils::normalize(Math::QuaternionF(2, 1, 0, 1)), Math::Vector3f_SIMD(1, 1, -1)), 3); - m_sequenceAnimationController.AddAnimationStep(Math::PoseF(Math::Utils::normalize(Math::QuaternionF(1, 1, 1, 1)), Math::Vector3f_SIMD(2, 1, -2)), 3); - m_sequenceAnimationController.AddAnimationStep(Math::PoseF(Math::Utils::normalize(Math::QuaternionF(0, 1, 1, 0)), Math::Vector3f_SIMD(2, 1, -1)), 3); - m_sequenceAnimationController.AddAnimationStep(Math::PoseF(Math::Utils::normalize(Math::QuaternionF(3, 2, 1, 1)), Math::Vector3f_SIMD(1, 1, -1)), 3); - m_sequenceAnimationController.AddAnimationStep(Math::PoseF(Math::Utils::normalize(Math::QuaternionF(0, 0, 1, 1)), Math::Vector3f_SIMD(0, 1, 0)), 1); + m_sequenceAnimationController.AddAnimationStep( + Math::PoseF(Math::Utils::normalize(Math::QuaternionF(1, 0, 0, 1)), Math::Vector3f_SIMD(0, 0, 1)), 5); + m_sequenceAnimationController.AddAnimationStep( + Math::PoseF(Math::Utils::normalize(Math::QuaternionF(2, 1, 0, 1)), Math::Vector3f_SIMD(1, 1, -1)), 3); + m_sequenceAnimationController.AddAnimationStep( + Math::PoseF(Math::Utils::normalize(Math::QuaternionF(1, 1, 1, 1)), Math::Vector3f_SIMD(2, 1, -2)), 3); + m_sequenceAnimationController.AddAnimationStep( + Math::PoseF(Math::Utils::normalize(Math::QuaternionF(0, 1, 1, 0)), Math::Vector3f_SIMD(2, 1, -1)), 3); + m_sequenceAnimationController.AddAnimationStep( + Math::PoseF(Math::Utils::normalize(Math::QuaternionF(3, 2, 1, 1)), Math::Vector3f_SIMD(1, 1, -1)), 3); + m_sequenceAnimationController.AddAnimationStep( + Math::PoseF(Math::Utils::normalize(Math::QuaternionF(0, 0, 1, 1)), Math::Vector3f_SIMD(0, 1, 0)), 1); m_sequenceAnimationController.SetAnimationPoseResetTime(10); CreatePlane(&m_plane, Math::Vector4f(0.3, 0.6, 0.9, 1.0)); @@ -184,18 +194,10 @@ namespace OpenVulkano m_sequenceAnimationController.Tick(); } - void Close() override - { - } + void Close() override {} }; - IGraphicsApp* MovingCubeApp::Create() - { - return new MovingCubeAppImpl(); - } + IGraphicsApp *MovingCubeApp::Create() { return new MovingCubeAppImpl(); } - std::unique_ptr MovingCubeApp::CreateUnique() - { - return std::make_unique(); - } + std::unique_ptr MovingCubeApp::CreateUnique() { return std::make_unique(); } } From a696ae2c7f3c071378306641880910fd55f3b151 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Wed, 26 Jun 2024 14:00:21 +0300 Subject: [PATCH 6/7] Using morphable and freecam controllers in example app, blending matrices using vectors, improvements regarding updating projection matrices, removed FreeCam components from MorphableController, renamed variable to m_targetMorphStatePerspective, setting target morph state using setter --- examples/ExampleApps/MovingCubeApp.cpp | 10 +++- openVulkanoCpp/Scene/MorphableCamera.cpp | 17 ++++-- .../Scene/MorphableCameraController.cpp | 58 ++----------------- .../Scene/MorphableCameraController.hpp | 13 +---- 4 files changed, 27 insertions(+), 71 deletions(-) diff --git a/examples/ExampleApps/MovingCubeApp.cpp b/examples/ExampleApps/MovingCubeApp.cpp index ba970b6..9297ee5 100644 --- a/examples/ExampleApps/MovingCubeApp.cpp +++ b/examples/ExampleApps/MovingCubeApp.cpp @@ -39,7 +39,8 @@ namespace OpenVulkano { Scene::Scene m_scene; Scene::MorphableCamera m_camera; - Scene::MorphableCameraController m_cameraControl; + Scene::MorphableCameraController m_morphableCameraControl; + FreeCamCameraController m_cameraControl; Scene::Material m_material; Scene::Shader m_shader; @@ -113,7 +114,11 @@ namespace OpenVulkano } public: - MovingCubeAppImpl() : m_camera(90, 16, 9, 0.1, 1000) { m_cameraControl.Init(&m_camera); } + MovingCubeAppImpl() : m_camera(90, 16, 9, 0.1, 1000) + { + m_morphableCameraControl.Init(&m_camera); + m_cameraControl.Init(&m_camera); + } void Init() override { @@ -190,6 +195,7 @@ namespace OpenVulkano void Tick() override { m_cameraControl.Tick(); + m_morphableCameraControl.Tick(); m_simpleAnimationController.Tick(); m_sequenceAnimationController.Tick(); } diff --git a/openVulkanoCpp/Scene/MorphableCamera.cpp b/openVulkanoCpp/Scene/MorphableCamera.cpp index 40807b6..9397058 100644 --- a/openVulkanoCpp/Scene/MorphableCamera.cpp +++ b/openVulkanoCpp/Scene/MorphableCamera.cpp @@ -15,7 +15,7 @@ namespace OpenVulkano::Scene Math::Matrix4f result; for (int i = 0; i < 4; ++i) { - for (int j = 0; j < 4; ++j) { result[i][j] = glm::mix(mat1[i][j], mat2[i][j], t); } + result[i] = Math::Utils::mix(mat1[i], mat2[i], t); } return result; } @@ -25,8 +25,17 @@ namespace OpenVulkano::Scene PerspectiveCamera::UpdateProjectionMatrix(); float aspectH = m_aspect; float aspectV = 1; - m_orthoMatrix = Math::Utils::orthoRH_ZO(-aspectH, aspectH, -aspectV, aspectV, m_nearPlane, m_farPlane); - m_projection = BlendMatrices(m_projection, m_orthoMatrix, m_morphState); - UpdateViewProjectionMatrix(); + if (m_morphState == 0) { UpdateViewProjectionMatrix(); } + else if (m_morphState == 1) + { + m_projection = Math::Utils::orthoRH_ZO(-aspectH, aspectH, -aspectV, aspectV, m_nearPlane, m_farPlane); + UpdateViewProjectionMatrix(); + } + else + { + m_orthoMatrix = Math::Utils::orthoRH_ZO(-aspectH, aspectH, -aspectV, aspectV, m_nearPlane, m_farPlane); + m_projection = BlendMatrices(m_projection, m_orthoMatrix, m_morphState); + UpdateViewProjectionMatrix(); + } } } \ No newline at end of file diff --git a/openVulkanoCpp/Scene/MorphableCameraController.cpp b/openVulkanoCpp/Scene/MorphableCameraController.cpp index cfce8fa..9ad569b 100644 --- a/openVulkanoCpp/Scene/MorphableCameraController.cpp +++ b/openVulkanoCpp/Scene/MorphableCameraController.cpp @@ -17,22 +17,16 @@ namespace OpenVulkano::Scene , m_animationDuration(1.0f) , m_currentTime(0.0f) , m_isMorphing(false) - , m_targetMorphState(false) + , m_targetMorphStatePerspective(false) { auto input = OpenVulkano::Input::InputManager::GetInstance(); m_actionMorph = input->GetAction("morph"); - m_actionForward = input->GetAction("forward"); - m_actionSide = input->GetAction("side"); - m_actionUp = input->GetAction("up"); - m_actionLookUp = input->GetAction("look up"); - m_actionLookSide = input->GetAction("look side"); - m_actionBoost = input->GetAction("boost"); } void MorphableCameraController::Init(MorphableCamera* camera) { CameraController::Init(camera); - SetDefaultKeybindings(); + m_actionMorph->BindKey(Input::InputKey::Keyboard::KEY_P); } void MorphableCameraController::Tick() @@ -44,7 +38,7 @@ namespace OpenVulkano::Scene { m_isMorphing = true; m_currentTime = 0.0f; - m_targetMorphState = !m_targetMorphState; + m_targetMorphStatePerspective = !m_targetMorphStatePerspective; } m_wasMorphingKeyDown = isMorphingDown; @@ -58,52 +52,8 @@ namespace OpenVulkano::Scene t = 1.0f; m_isMorphing = false; } - float newState = m_targetMorphState ? t : (1.0f - t); + float newState = m_targetMorphStatePerspective ? t : (1.0f - t); static_cast(GetCamera())->SetMorphState(newState); } - - // Movement logic - const float MOVEMENT_SPEED = 3.0f; - const float BOOST_MULT = 2.0f; - - Math::Vector3f_SIMD vec(input->GetAxis(m_actionSide), input->GetAxis(m_actionUp), - -input->GetAxis(m_actionForward)); - if (Math::Utils::length2(vec) > 1.0f) { vec = Math::Utils::normalize(vec); } - - float dt = CURRENT_FRAME.frameTime; - vec = vec * dt * MOVEMENT_SPEED; - if (input->GetButton(m_actionBoost)) { vec *= BOOST_MULT; } - - m_yaw -= input->GetAxis(m_actionLookSide) * dt / 2.0f; - m_pitch -= input->GetAxis(m_actionLookUp) * dt / 2.0f; - m_pitch = std::min(1.4f, std::max(-1.4f, m_pitch)); - - const Math::QuaternionF rot(Math::Vector3f(m_pitch, m_yaw, 0)); - m_position += rot * vec; - Math::Matrix4f camTransformation = Math::Utils::toMat4(rot); - camTransformation[3] = Math::Vector4f(m_position, 1); - GetCamera()->SetMatrix(camTransformation); - } - - void MorphableCameraController::SetDefaultKeybindings() - { - m_actionMorph->BindKey(Input::InputKey::Keyboard::KEY_P); - - 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_actionLookUp->BindKey(Input::InputKey::Controller::AXIS_RIGHT_Y); - m_actionLookUp->BindAxisButtons(Input::InputKey::Keyboard::KEY_DOWN, Input::InputKey::Keyboard::KEY_UP); - m_actionLookUp->BindKey(Input::InputKey::Touch::AXIS_PAN_Y, 0.10f); - m_actionLookSide->BindKey(Input::InputKey::Controller::AXIS_RIGHT_X); - m_actionLookSide->BindAxisButtons(Input::InputKey::Keyboard::KEY_RIGHT, Input::InputKey::Keyboard::KEY_LEFT); - m_actionLookSide->BindKey(Input::InputKey::Touch::AXIS_PAN_X, 0.10f); - m_actionLookUp->BindKey(Input::InputKey::Mouse::AXIS_Y); - m_actionLookSide->BindKey(Input::InputKey::Mouse::AXIS_X); - m_actionUp->BindAxisButtons(Input::InputKey::Keyboard::KEY_SPACE, Input::InputKey::Keyboard::KEY_LEFT_CONTROL); - m_actionBoost->BindKey(Input::InputKey::Keyboard::KEY_LEFT_SHIFT); } } \ No newline at end of file diff --git a/openVulkanoCpp/Scene/MorphableCameraController.hpp b/openVulkanoCpp/Scene/MorphableCameraController.hpp index 77f403c..f5c5084 100644 --- a/openVulkanoCpp/Scene/MorphableCameraController.hpp +++ b/openVulkanoCpp/Scene/MorphableCameraController.hpp @@ -18,17 +18,8 @@ namespace OpenVulkano::Scene double m_animationDuration; double m_currentTime; bool m_isMorphing; - bool m_targetMorphState; // true for perspective, false for orthographic + bool m_targetMorphStatePerspective; // true for perspective, false for orthographic - float m_yaw = 0, m_pitch = 0, m_boostFactor = 2; - Math::Vector3f_SIMD m_position = { 0, 0, 0 }; - - Input::InputAction* m_actionForward; - Input::InputAction* m_actionSide; - Input::InputAction* m_actionUp; - Input::InputAction* m_actionLookUp; - Input::InputAction* m_actionLookSide; - Input::InputAction* m_actionBoost; Input::InputAction* m_actionMorph; bool m_wasMorphingKeyDown = false; @@ -37,10 +28,10 @@ namespace OpenVulkano::Scene void Init(MorphableCamera* camera); void Tick() override; - void SetDefaultKeybindings(); void SetDuration(double duration) { m_animationDuration = duration; } double GetDuration() { return m_animationDuration; } + void SetTargetState(bool toPerspective) { m_targetMorphStatePerspective = toPerspective; } void Reset() { m_currentTime = 0; } }; } \ No newline at end of file From 656350d7906c156d87405bf71338475226268081 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Wed, 26 Jun 2024 20:59:06 +0300 Subject: [PATCH 7/7] Refactored UpdateProjectionMatrix(), SetTargetState() now launches the transition --- openVulkanoCpp/Scene/MorphableCamera.cpp | 23 ++++++++++--------- .../Scene/MorphableCameraController.cpp | 6 +++++ .../Scene/MorphableCameraController.hpp | 2 +- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/openVulkanoCpp/Scene/MorphableCamera.cpp b/openVulkanoCpp/Scene/MorphableCamera.cpp index 9397058..3643bf6 100644 --- a/openVulkanoCpp/Scene/MorphableCamera.cpp +++ b/openVulkanoCpp/Scene/MorphableCamera.cpp @@ -23,18 +23,19 @@ namespace OpenVulkano::Scene void MorphableCamera::UpdateProjectionMatrix() { PerspectiveCamera::UpdateProjectionMatrix(); - float aspectH = m_aspect; - float aspectV = 1; - if (m_morphState == 0) { UpdateViewProjectionMatrix(); } - else if (m_morphState == 1) + if (m_morphState != 0) { - m_projection = Math::Utils::orthoRH_ZO(-aspectH, aspectH, -aspectV, aspectV, m_nearPlane, m_farPlane); - UpdateViewProjectionMatrix(); - } - else - { - m_orthoMatrix = Math::Utils::orthoRH_ZO(-aspectH, aspectH, -aspectV, aspectV, m_nearPlane, m_farPlane); - m_projection = BlendMatrices(m_projection, m_orthoMatrix, m_morphState); + float aspectH = m_aspect; + float aspectV = 1; + if (m_morphState == 1) + { + m_projection = Math::Utils::orthoRH_ZO(-aspectH, aspectH, -aspectV, aspectV, m_nearPlane, m_farPlane); + } + else + { + m_orthoMatrix = Math::Utils::orthoRH_ZO(-aspectH, aspectH, -aspectV, aspectV, m_nearPlane, m_farPlane); + m_projection = BlendMatrices(m_projection, m_orthoMatrix, m_morphState); + } UpdateViewProjectionMatrix(); } } diff --git a/openVulkanoCpp/Scene/MorphableCameraController.cpp b/openVulkanoCpp/Scene/MorphableCameraController.cpp index 9ad569b..af3371c 100644 --- a/openVulkanoCpp/Scene/MorphableCameraController.cpp +++ b/openVulkanoCpp/Scene/MorphableCameraController.cpp @@ -56,4 +56,10 @@ namespace OpenVulkano::Scene static_cast(GetCamera())->SetMorphState(newState); } } + + void MorphableCameraController::SetTargetState(bool toPerspective) + { + m_targetMorphStatePerspective = toPerspective; + m_isMorphing = true; + } } \ No newline at end of file diff --git a/openVulkanoCpp/Scene/MorphableCameraController.hpp b/openVulkanoCpp/Scene/MorphableCameraController.hpp index f5c5084..be9cb87 100644 --- a/openVulkanoCpp/Scene/MorphableCameraController.hpp +++ b/openVulkanoCpp/Scene/MorphableCameraController.hpp @@ -31,7 +31,7 @@ namespace OpenVulkano::Scene void SetDuration(double duration) { m_animationDuration = duration; } double GetDuration() { return m_animationDuration; } - void SetTargetState(bool toPerspective) { m_targetMorphStatePerspective = toPerspective; } + void SetTargetState(bool toPerspective); void Reset() { m_currentTime = 0; } }; } \ No newline at end of file