Merge pull request #9366 from iwubcode/freelook_camera_quaternion

VideoCommon: allow Free Look to be manipulated by a quaternion
This commit is contained in:
Léo Lam 2021-01-28 01:34:09 +01:00 committed by GitHub
commit c386c5acc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 29 deletions

View File

@ -56,6 +56,13 @@ Quaternion Quaternion::RotateZ(float rad)
return Rotate(rad, Vec3(0, 0, 1));
}
Quaternion Quaternion::RotateXYZ(const Vec3& rads)
{
const auto length = rads.Length();
return length ? Common::Quaternion::Rotate(length, rads / length) :
Common::Quaternion::Identity();
}
Quaternion Quaternion::Rotate(float rad, const Vec3& axis)
{
const auto sin_angle_2 = std::sin(rad / 2);
@ -269,6 +276,11 @@ Matrix44 Matrix44::FromMatrix33(const Matrix33& m33)
return mtx;
}
Matrix44 Matrix44::FromQuaternion(const Quaternion& q)
{
return FromMatrix33(Matrix33::FromQuaternion(q));
}
Matrix44 Matrix44::FromArray(const std::array<float, 16>& arr)
{
Matrix44 mtx;

View File

@ -337,6 +337,10 @@ public:
static Quaternion RotateY(float rad);
static Quaternion RotateZ(float rad);
// Returns a quaternion with rotations about each axis simulatenously (e.g processing gyroscope
// input)
static Quaternion RotateXYZ(const Vec3& rads);
static Quaternion Rotate(float rad, const Vec3& axis);
Quaternion() = default;
@ -402,6 +406,7 @@ class Matrix44
public:
static Matrix44 Identity();
static Matrix44 FromMatrix33(const Matrix33& m33);
static Matrix44 FromQuaternion(const Quaternion& q);
static Matrix44 FromArray(const std::array<float, 16>& arr);
static Matrix44 Translate(const Vec3& vec);

View File

@ -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,

View File

@ -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

View File

@ -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;