2020-04-26 21:37:49 +00:00
|
|
|
// Copyright 2020 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "VideoCommon/FreeLookCamera.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
|
|
#include "Common/MathUtil.h"
|
|
|
|
|
|
|
|
#include "Common/ChunkFile.h"
|
|
|
|
#include "Core/ConfigManager.h"
|
|
|
|
#include "Core/Core.h"
|
2020-06-19 04:44:17 +00:00
|
|
|
|
2020-04-26 21:37:49 +00:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
|
|
|
|
|
|
|
FreeLookCamera g_freelook_camera;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2020-06-19 04:44:17 +00:00
|
|
|
std::string to_string(FreeLook::ControlType type)
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2020-06-19 04:44:17 +00:00
|
|
|
case FreeLook::ControlType::SixAxis:
|
2020-04-26 21:37:49 +00:00
|
|
|
return "Six Axis";
|
2020-06-19 04:44:17 +00:00
|
|
|
case FreeLook::ControlType::FPS:
|
2020-04-26 21:37:49 +00:00
|
|
|
return "First Person";
|
2020-06-19 04:44:17 +00:00
|
|
|
case FreeLook::ControlType::Orbital:
|
2020-04-26 21:37:49 +00:00
|
|
|
return "Orbital";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2020-10-23 04:09:14 +00:00
|
|
|
class SixAxisController final : public CameraController
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
SixAxisController() = default;
|
|
|
|
|
|
|
|
Common::Matrix44 GetView() override { return m_mat; }
|
|
|
|
|
|
|
|
void MoveVertical(float amt) override
|
|
|
|
{
|
|
|
|
m_mat = Common::Matrix44::Translate(Common::Vec3{0, amt, 0}) * m_mat;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MoveHorizontal(float amt) override
|
|
|
|
{
|
|
|
|
m_mat = Common::Matrix44::Translate(Common::Vec3{amt, 0, 0}) * m_mat;
|
|
|
|
}
|
|
|
|
|
2020-06-12 05:21:50 +00:00
|
|
|
void MoveForward(float amt) override
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
|
|
|
m_mat = Common::Matrix44::Translate(Common::Vec3{0, 0, amt}) * m_mat;
|
|
|
|
}
|
|
|
|
|
2020-10-23 04:09:14 +00:00
|
|
|
void Rotate(const Common::Vec3& amt) override { Rotate(Common::Quaternion::RotateXYZ(amt)); }
|
|
|
|
|
|
|
|
void Rotate(const Common::Quaternion& quat) override
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
2020-10-23 04:09:14 +00:00
|
|
|
m_mat = Common::Matrix44::FromQuaternion(quat) * m_mat;
|
2020-04-26 21:37:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Reset() override { m_mat = Common::Matrix44::Identity(); }
|
|
|
|
|
2021-02-13 16:42:20 +00:00
|
|
|
void DoState(PointerWrap& p) override { p.Do(m_mat); }
|
2020-04-26 21:37:49 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Common::Matrix44 m_mat = Common::Matrix44::Identity();
|
|
|
|
};
|
|
|
|
|
2020-10-23 04:09:14 +00:00
|
|
|
class FPSController final : public CameraController
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Common::Matrix44 GetView() override
|
|
|
|
{
|
2020-10-23 04:09:14 +00:00
|
|
|
return Common::Matrix44::FromQuaternion(m_rotate_quat) *
|
|
|
|
Common::Matrix44::Translate(m_position);
|
2020-04-26 21:37:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MoveVertical(float amt) override
|
|
|
|
{
|
2020-10-23 04:09:14 +00:00
|
|
|
const Common::Vec3 up = m_rotate_quat.Conjugate() * Common::Vec3{0, 1, 0};
|
2020-04-26 21:37:49 +00:00
|
|
|
m_position += up * amt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MoveHorizontal(float amt) override
|
|
|
|
{
|
2020-10-23 04:09:14 +00:00
|
|
|
const Common::Vec3 right = m_rotate_quat.Conjugate() * Common::Vec3{1, 0, 0};
|
2020-04-26 21:37:49 +00:00
|
|
|
m_position += right * amt;
|
|
|
|
}
|
|
|
|
|
2020-06-12 05:21:50 +00:00
|
|
|
void MoveForward(float amt) override
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
2020-10-23 04:09:14 +00:00
|
|
|
const Common::Vec3 forward = m_rotate_quat.Conjugate() * Common::Vec3{0, 0, 1};
|
2020-04-26 21:37:49 +00:00
|
|
|
m_position += forward * amt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rotate(const Common::Vec3& amt) override
|
|
|
|
{
|
2021-03-12 06:02:06 +00:00
|
|
|
if (amt.Length() == 0)
|
|
|
|
return;
|
|
|
|
|
2020-04-26 21:37:49 +00:00
|
|
|
m_rotation += amt;
|
|
|
|
|
2020-10-23 04:09:14 +00:00
|
|
|
using Common::Quaternion;
|
2021-03-12 06:02:06 +00:00
|
|
|
m_rotate_quat =
|
2020-10-23 04:09:14 +00:00
|
|
|
(Quaternion::RotateX(m_rotation.x) * Quaternion::RotateY(m_rotation.y)).Normalized();
|
2020-04-26 21:37:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 06:02:06 +00:00
|
|
|
void Rotate(const Common::Quaternion& quat) override
|
|
|
|
{
|
|
|
|
Rotate(Common::FromQuaternionToEuler(quat));
|
|
|
|
}
|
2020-10-23 04:09:14 +00:00
|
|
|
|
2020-04-26 21:37:49 +00:00
|
|
|
void Reset() override
|
|
|
|
{
|
|
|
|
m_position = Common::Vec3{};
|
|
|
|
m_rotation = Common::Vec3{};
|
2020-10-23 04:09:14 +00:00
|
|
|
m_rotate_quat = Common::Quaternion::Identity();
|
2020-04-26 21:37:49 +00:00
|
|
|
}
|
|
|
|
|
2021-02-13 16:42:20 +00:00
|
|
|
void DoState(PointerWrap& p) override
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
|
|
|
p.Do(m_rotation);
|
2020-10-23 04:09:14 +00:00
|
|
|
p.Do(m_rotate_quat);
|
2020-04-26 21:37:49 +00:00
|
|
|
p.Do(m_position);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Common::Vec3 m_rotation = Common::Vec3{};
|
2020-10-23 04:09:14 +00:00
|
|
|
Common::Quaternion m_rotate_quat = Common::Quaternion::Identity();
|
2020-04-26 21:37:49 +00:00
|
|
|
Common::Vec3 m_position = Common::Vec3{};
|
|
|
|
};
|
|
|
|
|
2020-10-23 04:09:14 +00:00
|
|
|
class OrbitalController final : public CameraController
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Common::Matrix44 GetView() override
|
|
|
|
{
|
2020-10-23 04:09:14 +00:00
|
|
|
return Common::Matrix44::Translate(Common::Vec3{0, 0, -m_distance}) *
|
|
|
|
Common::Matrix44::FromQuaternion(m_rotate_quat);
|
2020-04-26 21:37:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MoveVertical(float) override {}
|
|
|
|
|
|
|
|
void MoveHorizontal(float) override {}
|
|
|
|
|
2020-06-12 05:21:50 +00:00
|
|
|
void MoveForward(float amt) override
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
|
|
|
m_distance += -1 * amt;
|
|
|
|
m_distance = std::clamp(m_distance, 0.0f, m_distance);
|
|
|
|
}
|
|
|
|
|
2020-10-23 04:09:14 +00:00
|
|
|
void Rotate(const Common::Vec3& amt) override
|
|
|
|
{
|
2021-03-12 06:02:06 +00:00
|
|
|
if (amt.Length() == 0)
|
|
|
|
return;
|
|
|
|
|
2020-10-23 04:09:14 +00:00
|
|
|
m_rotation += amt;
|
|
|
|
|
|
|
|
using Common::Quaternion;
|
2021-03-12 06:02:06 +00:00
|
|
|
m_rotate_quat =
|
2020-10-23 04:09:14 +00:00
|
|
|
(Quaternion::RotateX(m_rotation.x) * Quaternion::RotateY(m_rotation.y)).Normalized();
|
|
|
|
}
|
|
|
|
|
2021-03-12 06:02:06 +00:00
|
|
|
void Rotate(const Common::Quaternion& quat) override
|
|
|
|
{
|
|
|
|
Rotate(Common::FromQuaternionToEuler(quat));
|
|
|
|
}
|
2020-04-26 21:37:49 +00:00
|
|
|
|
|
|
|
void Reset() override
|
|
|
|
{
|
|
|
|
m_rotation = Common::Vec3{};
|
2020-10-23 04:09:14 +00:00
|
|
|
m_rotate_quat = Common::Quaternion::Identity();
|
2020-04-26 21:37:49 +00:00
|
|
|
m_distance = 0;
|
|
|
|
}
|
|
|
|
|
2021-02-13 16:42:20 +00:00
|
|
|
void DoState(PointerWrap& p) override
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
|
|
|
p.Do(m_rotation);
|
2020-10-23 04:09:14 +00:00
|
|
|
p.Do(m_rotate_quat);
|
2020-04-26 21:37:49 +00:00
|
|
|
p.Do(m_distance);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
float m_distance = 0;
|
|
|
|
Common::Vec3 m_rotation = Common::Vec3{};
|
2020-10-23 04:09:14 +00:00
|
|
|
Common::Quaternion m_rotate_quat = Common::Quaternion::Identity();
|
2020-04-26 21:37:49 +00:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2020-12-26 04:19:09 +00:00
|
|
|
FreeLookCamera::FreeLookCamera()
|
|
|
|
{
|
|
|
|
SetControlType(FreeLook::ControlType::SixAxis);
|
|
|
|
}
|
|
|
|
|
2020-06-19 04:44:17 +00:00
|
|
|
void FreeLookCamera::SetControlType(FreeLook::ControlType type)
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
|
|
|
if (m_current_type && *m_current_type == type)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-19 04:44:17 +00:00
|
|
|
if (type == FreeLook::ControlType::SixAxis)
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
|
|
|
m_camera_controller = std::make_unique<SixAxisController>();
|
|
|
|
}
|
2020-06-19 04:44:17 +00:00
|
|
|
else if (type == FreeLook::ControlType::Orbital)
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
|
|
|
m_camera_controller = std::make_unique<OrbitalController>();
|
|
|
|
}
|
2020-06-19 04:44:17 +00:00
|
|
|
else if (type == FreeLook::ControlType::FPS)
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
|
|
|
m_camera_controller = std::make_unique<FPSController>();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_current_type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::Matrix44 FreeLookCamera::GetView()
|
|
|
|
{
|
|
|
|
return m_camera_controller->GetView();
|
|
|
|
}
|
|
|
|
|
2020-05-18 22:22:41 +00:00
|
|
|
Common::Vec2 FreeLookCamera::GetFieldOfView() const
|
|
|
|
{
|
|
|
|
return Common::Vec2{m_fov_x, m_fov_y};
|
|
|
|
}
|
|
|
|
|
2020-04-26 21:37:49 +00:00
|
|
|
void FreeLookCamera::MoveVertical(float amt)
|
|
|
|
{
|
|
|
|
m_camera_controller->MoveVertical(amt);
|
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreeLookCamera::MoveHorizontal(float amt)
|
|
|
|
{
|
|
|
|
m_camera_controller->MoveHorizontal(amt);
|
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
2020-06-12 05:21:50 +00:00
|
|
|
void FreeLookCamera::MoveForward(float amt)
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
2020-06-12 05:21:50 +00:00
|
|
|
m_camera_controller->MoveForward(amt);
|
2020-04-26 21:37:49 +00:00
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreeLookCamera::Rotate(const Common::Vec3& amt)
|
|
|
|
{
|
|
|
|
m_camera_controller->Rotate(amt);
|
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
2021-03-12 06:02:06 +00:00
|
|
|
void FreeLookCamera::Rotate(const Common::Quaternion& amt)
|
|
|
|
{
|
|
|
|
m_camera_controller->Rotate(amt);
|
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
2020-05-18 22:22:41 +00:00
|
|
|
void FreeLookCamera::IncreaseFovX(float fov)
|
|
|
|
{
|
|
|
|
m_fov_x += fov;
|
2021-03-18 01:51:35 +00:00
|
|
|
m_fov_x = std::clamp(m_fov_x, m_min_fov_multiplier, m_fov_x);
|
2020-05-18 22:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FreeLookCamera::IncreaseFovY(float fov)
|
|
|
|
{
|
|
|
|
m_fov_y += fov;
|
2021-03-18 01:51:35 +00:00
|
|
|
m_fov_y = std::clamp(m_fov_y, m_min_fov_multiplier, m_fov_y);
|
2020-06-23 03:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float FreeLookCamera::GetFovStepSize() const
|
|
|
|
{
|
2021-03-18 01:51:35 +00:00
|
|
|
return 1.5f;
|
2020-05-18 22:22:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 21:37:49 +00:00
|
|
|
void FreeLookCamera::Reset()
|
|
|
|
{
|
|
|
|
m_camera_controller->Reset();
|
2020-05-18 22:22:41 +00:00
|
|
|
m_fov_x = 1.0f;
|
|
|
|
m_fov_y = 1.0f;
|
2020-04-26 21:37:49 +00:00
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
2021-03-18 01:51:35 +00:00
|
|
|
void FreeLookCamera::ModifySpeed(float amt)
|
2020-06-12 05:17:40 +00:00
|
|
|
{
|
2021-03-18 01:51:35 +00:00
|
|
|
m_speed += amt;
|
|
|
|
m_speed = std::clamp(m_speed, 0.0f, m_speed);
|
2020-06-12 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FreeLookCamera::ResetSpeed()
|
|
|
|
{
|
2021-03-18 01:51:35 +00:00
|
|
|
m_speed = 60.0f;
|
2020-06-12 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float FreeLookCamera::GetSpeed() const
|
|
|
|
{
|
|
|
|
return m_speed;
|
|
|
|
}
|
|
|
|
|
2020-04-26 21:37:49 +00:00
|
|
|
void FreeLookCamera::DoState(PointerWrap& p)
|
|
|
|
{
|
|
|
|
if (p.mode == PointerWrap::MODE_WRITE || p.mode == PointerWrap::MODE_MEASURE)
|
|
|
|
{
|
2020-06-12 05:17:40 +00:00
|
|
|
p.Do(m_speed);
|
2020-04-26 21:37:49 +00:00
|
|
|
p.Do(m_current_type);
|
2020-05-18 22:22:41 +00:00
|
|
|
p.Do(m_fov_x);
|
|
|
|
p.Do(m_fov_y);
|
2020-04-26 21:37:49 +00:00
|
|
|
if (m_camera_controller)
|
|
|
|
{
|
|
|
|
m_camera_controller->DoState(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const auto old_type = m_current_type;
|
2020-06-12 05:17:40 +00:00
|
|
|
p.Do(m_speed);
|
2020-04-26 21:37:49 +00:00
|
|
|
p.Do(m_current_type);
|
2020-06-28 00:28:01 +00:00
|
|
|
p.Do(m_fov_x);
|
|
|
|
p.Do(m_fov_y);
|
2020-04-26 21:37:49 +00:00
|
|
|
if (old_type == m_current_type)
|
|
|
|
{
|
|
|
|
m_camera_controller->DoState(p);
|
|
|
|
}
|
|
|
|
else if (p.GetMode() == PointerWrap::MODE_READ)
|
|
|
|
{
|
|
|
|
const std::string old_type_name = old_type ? to_string(*old_type) : "";
|
|
|
|
const std::string loaded_type_name = m_current_type ? to_string(*m_current_type) : "";
|
|
|
|
const std::string message =
|
|
|
|
fmt::format("State needs same free look camera type. Settings value '{}', loaded value "
|
|
|
|
"'{}'. Aborting load state",
|
|
|
|
old_type_name, loaded_type_name);
|
|
|
|
Core::DisplayMessage(message, 5000);
|
|
|
|
p.SetMode(PointerWrap::MODE_VERIFY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FreeLookCamera::IsDirty() const
|
|
|
|
{
|
|
|
|
return m_dirty;
|
|
|
|
}
|
2020-06-17 17:07:40 +00:00
|
|
|
|
|
|
|
void FreeLookCamera::SetClean()
|
|
|
|
{
|
|
|
|
m_dirty = false;
|
|
|
|
}
|
2020-06-19 04:49:07 +00:00
|
|
|
|
|
|
|
bool FreeLookCamera::IsActive() const
|
|
|
|
{
|
|
|
|
return FreeLook::GetActiveConfig().enabled;
|
|
|
|
}
|