From 61929e61d3d1c36d69241f9b3790f11b1b97c573 Mon Sep 17 00:00:00 2001 From: Georg Hagen Date: Mon, 24 Jun 2024 16:49:56 +0200 Subject: [PATCH] Update ArcballCameraController --- .../Controller/ArcballCameraController.cpp | 31 ++++++++++--------- .../Controller/ArcballCameraController.hpp | 2 +- openVulkanoCpp/Scene/Camera.hpp | 9 +++++- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/openVulkanoCpp/Controller/ArcballCameraController.cpp b/openVulkanoCpp/Controller/ArcballCameraController.cpp index a4dcecd..3d512bd 100644 --- a/openVulkanoCpp/Controller/ArcballCameraController.cpp +++ b/openVulkanoCpp/Controller/ArcballCameraController.cpp @@ -25,21 +25,18 @@ namespace OpenVulkano void ArcballCameraController::Tick() { if (!GetCamera()) return; - auto input = Input::InputManager::GetInstance(); + Input::InputManager* input = Input::InputManager::GetInstance(); Math::Vector4f_SIMD position = GetCamera()->GetPosition(); - float yaw = input->GetAxis(m_actionLookSide); - float pitch = input->GetAxis(m_actionLookUp); + yaw += input->GetAxis(m_actionLookSide); + pitch += input->GetAxis(m_actionLookUp); - // Handle issue with camera direction being the same as the up vector - float cosAngle = dot(Math::Vector3f_SIMD(GetCamera()->GetViewDirection()), m_upVector); - if (cosAngle * Math::Utils::sign(pitch) > 0.99f) pitch = 0; + pitch = std::min(1.5f, std::max(-1.5f, pitch)); - // Rotate the camera around the pivot point - Math::Matrix4f rotationMatrixX = Math::Utils::rotate(yaw, m_upVector); - position = (rotationMatrixX * (position - m_pivotPoint)) + m_pivotPoint; - m_rightVector = Math::Vector3f_SIMD(rotationMatrixX * Math::Vector4f(m_rightVector, 0)); - Math::Matrix4f rotationMatrixY = Math::Utils::rotate(pitch, m_rightVector); - position = (rotationMatrixY * (position - m_pivotPoint)) + m_pivotPoint; + Math::Matrix4f rotateY = Math::Utils::rotate(yaw, Math::Vector3f_SIMD(0, 1, 0)); + Math::Vector4f x = rotateY * Math::Vector4f(1, 0, 0, 0); + Math::Matrix4f rotateX = Math::Utils::rotate(pitch, Math::Vector3f_SIMD(x)); + position = rotateX * rotateY * Math::Vector4f_SIMD(0, 0, 3, 0); + position += m_pivotPoint; // Move the camera and the pivot point Math::Vector3f_SIMD vec(input->GetAxis(m_actionSide), input->GetAxis(m_actionUp), -input->GetAxis(m_actionForward)); @@ -60,12 +57,16 @@ namespace OpenVulkano // Update the camera view GetCamera()->SetViewMatrix(Math::Utils::lookAt(reinterpret_cast(position), reinterpret_cast(m_pivotPoint), - m_upVector)); + Math::Vector3f_SIMD(0,1,0))); } void ArcballCameraController::SetActive() { - m_pivotPoint = GetCamera()->GetPosition() + Math::Vector4f(GetCamera()->GetViewDirection() * 3.0f, 0); + Math::Vector3f viewDir = Math::Utils::normalize(GetCamera()->GetViewDirection()); + m_pivotPoint = GetCamera()->GetPosition() + Math::Vector4f(viewDir * 3.0f, 0); + + pitch = Math::Utils::asin(viewDir.y); + yaw = Math::Utils::atan(-viewDir.x, -viewDir.z); } void ArcballCameraController::SetDefaultKeybindings() @@ -80,7 +81,7 @@ namespace OpenVulkano m_actionUp->BindKey(Input::InputKey::Touch::AXIS_PAN_TWO_FINGERS_Y, 0.03f); 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.001f); + m_actionLookUp->BindKey(Input::InputKey::Touch::AXIS_PAN_Y, -0.001f); 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.001f); diff --git a/openVulkanoCpp/Controller/ArcballCameraController.hpp b/openVulkanoCpp/Controller/ArcballCameraController.hpp index a9f3a01..8f4fa72 100644 --- a/openVulkanoCpp/Controller/ArcballCameraController.hpp +++ b/openVulkanoCpp/Controller/ArcballCameraController.hpp @@ -18,8 +18,8 @@ namespace OpenVulkano class ArcballCameraController final : public CameraController { - Math::Vector3f_SIMD m_upVector{ 0, 1, 0 }, m_rightVector{ 1, 0, 0 }; Math::Vector4f_SIMD m_pivotPoint{ 0, 0, 0, 1 }; + float yaw = 0, pitch = 0; Input::InputAction* m_actionForward = nullptr; Input::InputAction* m_actionSide = nullptr; diff --git a/openVulkanoCpp/Scene/Camera.hpp b/openVulkanoCpp/Scene/Camera.hpp index 0ae069c..612f6b3 100644 --- a/openVulkanoCpp/Scene/Camera.hpp +++ b/openVulkanoCpp/Scene/Camera.hpp @@ -120,11 +120,18 @@ namespace OpenVulkano::Scene [[nodiscard]] Math::Vector3f GetViewDirection() const { - return { m_view[0][2], m_view[1][2], m_view[2][2] }; + return { -m_view[0][2], -m_view[1][2], -m_view[2][2] }; + } + + [[nodiscard]] Math::Vector3f GetUpVector() const + { + return { m_view[0][1], m_view[1][1], m_view[1][2] }; } [[nodiscard]] auto GetForward() const { return GetViewDirection(); } + [[nodiscard]] const Math::Matrix4f& GetViewMatrix() const { return m_view; } + [[nodiscard]] Math::Frustum GetFrustum() const { return {m_viewProjection};