From 586c689d63ad30f12eba0a15161ea5070989751f Mon Sep 17 00:00:00 2001 From: iwubcode Date: Thu, 22 Oct 2020 23:09:14 -0500 Subject: [PATCH] VideoCommon: use Quaternion for Free Look orientation --- Source/Core/Core/State.cpp | 2 +- Source/Core/VideoCommon/FreeLookCamera.cpp | 65 ++++++++++++---------- Source/Core/VideoCommon/FreeLookCamera.h | 1 + 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/Source/Core/Core/State.cpp b/Source/Core/Core/State.cpp index d86da80cfe..7d9afc5327 100644 --- a/Source/Core/Core/State.cpp +++ b/Source/Core/Core/State.cpp @@ -74,7 +74,7 @@ static Common::Event g_compressAndDumpStateSyncEvent; static std::thread g_save_thread; // Don't forget to increase this after doing changes on the savestate system -constexpr u32 STATE_VERSION = 127; // Last changed in PR 9300 +constexpr u32 STATE_VERSION = 128; // Last changed in PR 9366 // Maps savestate versions to Dolphin versions. // Versions after 42 don't need to be added to this list, diff --git a/Source/Core/VideoCommon/FreeLookCamera.cpp b/Source/Core/VideoCommon/FreeLookCamera.cpp index c40b5e3dd2..9380424c04 100644 --- a/Source/Core/VideoCommon/FreeLookCamera.cpp +++ b/Source/Core/VideoCommon/FreeLookCamera.cpp @@ -36,7 +36,7 @@ std::string to_string(FreeLook::ControlType type) return ""; } -class SixAxisController : public CameraController +class SixAxisController final : public CameraController { public: SixAxisController() = default; @@ -58,12 +58,11 @@ public: m_mat = Common::Matrix44::Translate(Common::Vec3{0, 0, amt}) * m_mat; } - void Rotate(const Common::Vec3& amt) override + void Rotate(const Common::Vec3& amt) override { Rotate(Common::Quaternion::RotateXYZ(amt)); } + + void Rotate(const Common::Quaternion& quat) override { - using Common::Matrix33; - m_mat = Common::Matrix44::FromMatrix33(Matrix33::RotateX(amt.x) * Matrix33::RotateY(amt.y) * - Matrix33::RotateZ(amt.z)) * - m_mat; + m_mat = Common::Matrix44::FromQuaternion(quat) * m_mat; } void Reset() override { m_mat = Common::Matrix44::Identity(); } @@ -74,31 +73,30 @@ private: Common::Matrix44 m_mat = Common::Matrix44::Identity(); }; -constexpr double HalfPI = MathUtil::PI / 2; - -class FPSController : public CameraController +class FPSController final : public CameraController { public: Common::Matrix44 GetView() override { - return m_rotate_mat * Common::Matrix44::Translate(m_position); + return Common::Matrix44::FromQuaternion(m_rotate_quat) * + Common::Matrix44::Translate(m_position); } void MoveVertical(float amt) override { - Common::Vec3 up{m_rotate_mat.data[4], m_rotate_mat.data[5], m_rotate_mat.data[6]}; + const Common::Vec3 up = m_rotate_quat.Conjugate() * Common::Vec3{0, 1, 0}; m_position += up * amt; } void MoveHorizontal(float amt) override { - Common::Vec3 right{m_rotate_mat.data[0], m_rotate_mat.data[1], m_rotate_mat.data[2]}; + const Common::Vec3 right = m_rotate_quat.Conjugate() * Common::Vec3{1, 0, 0}; m_position += right * amt; } void MoveForward(float amt) override { - Common::Vec3 forward{m_rotate_mat.data[8], m_rotate_mat.data[9], m_rotate_mat.data[10]}; + const Common::Vec3 forward = m_rotate_quat.Conjugate() * Common::Vec3{0, 0, 1}; m_position += forward * amt; } @@ -106,43 +104,41 @@ public: { m_rotation += amt; - using Common::Matrix33; - using Common::Matrix44; - m_rotate_mat = - Matrix44::FromMatrix33(Matrix33::RotateX(m_rotation.x) * Matrix33::RotateY(m_rotation.y)); + using Common::Quaternion; + const auto quat = + (Quaternion::RotateX(m_rotation.x) * Quaternion::RotateY(m_rotation.y)).Normalized(); + Rotate(quat); } + void Rotate(const Common::Quaternion& quat) override { m_rotate_quat = quat; } + void Reset() override { m_position = Common::Vec3{}; m_rotation = Common::Vec3{}; - m_rotate_mat = Common::Matrix44::Identity(); + m_rotate_quat = Common::Quaternion::Identity(); } void DoState(PointerWrap& p) { p.Do(m_rotation); - p.Do(m_rotate_mat); + p.Do(m_rotate_quat); p.Do(m_position); } private: Common::Vec3 m_rotation = Common::Vec3{}; - Common::Matrix44 m_rotate_mat = Common::Matrix44::Identity(); + Common::Quaternion m_rotate_quat = Common::Quaternion::Identity(); Common::Vec3 m_position = Common::Vec3{}; }; -class OrbitalController : public CameraController +class OrbitalController final : public CameraController { public: Common::Matrix44 GetView() override { - Common::Matrix44 result = Common::Matrix44::Identity(); - result *= Common::Matrix44::Translate(Common::Vec3{0, 0, -m_distance}); - result *= Common::Matrix44::FromMatrix33(Common::Matrix33::RotateX(m_rotation.x)); - result *= Common::Matrix44::FromMatrix33(Common::Matrix33::RotateY(m_rotation.y)); - - return result; + return Common::Matrix44::Translate(Common::Vec3{0, 0, -m_distance}) * + Common::Matrix44::FromQuaternion(m_rotate_quat); } void MoveVertical(float) override {} @@ -155,23 +151,36 @@ public: m_distance = std::clamp(m_distance, 0.0f, m_distance); } - void Rotate(const Common::Vec3& amt) override { m_rotation += amt; } + void Rotate(const Common::Vec3& amt) override + { + m_rotation += amt; + + using Common::Quaternion; + const auto quat = + (Quaternion::RotateX(m_rotation.x) * Quaternion::RotateY(m_rotation.y)).Normalized(); + Rotate(quat); + } + + void Rotate(const Common::Quaternion& quat) override { m_rotate_quat = quat; } void Reset() override { m_rotation = Common::Vec3{}; + m_rotate_quat = Common::Quaternion::Identity(); m_distance = 0; } void DoState(PointerWrap& p) { p.Do(m_rotation); + p.Do(m_rotate_quat); p.Do(m_distance); } private: float m_distance = 0; Common::Vec3 m_rotation = Common::Vec3{}; + Common::Quaternion m_rotate_quat = Common::Quaternion::Identity(); }; } // namespace diff --git a/Source/Core/VideoCommon/FreeLookCamera.h b/Source/Core/VideoCommon/FreeLookCamera.h index e2ca268a71..d9c9fcbc83 100644 --- a/Source/Core/VideoCommon/FreeLookCamera.h +++ b/Source/Core/VideoCommon/FreeLookCamera.h @@ -32,6 +32,7 @@ public: virtual void MoveForward(float amt) = 0; virtual void Rotate(const Common::Vec3& amt) = 0; + virtual void Rotate(const Common::Quaternion& quat) = 0; virtual void Reset() = 0;