2020-04-26 23:37:49 +02:00
|
|
|
// Copyright 2020 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-04-26 23:37:49 +02:00
|
|
|
|
|
|
|
#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 06:44:17 +02:00
|
|
|
|
2020-04-26 23:37:49 +02:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
|
|
|
|
|
|
|
FreeLookCamera g_freelook_camera;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2020-06-19 06:44:17 +02:00
|
|
|
std::string to_string(FreeLook::ControlType type)
|
2020-04-26 23:37:49 +02:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2020-06-19 06:44:17 +02:00
|
|
|
case FreeLook::ControlType::SixAxis:
|
2020-04-26 23:37:49 +02:00
|
|
|
return "Six Axis";
|
2020-06-19 06:44:17 +02:00
|
|
|
case FreeLook::ControlType::FPS:
|
2020-04-26 23:37:49 +02:00
|
|
|
return "First Person";
|
2020-06-19 06:44:17 +02:00
|
|
|
case FreeLook::ControlType::Orbital:
|
2020-04-26 23:37:49 +02:00
|
|
|
return "Orbital";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2020-10-23 06:09:14 +02:00
|
|
|
class SixAxisController final : public CameraController
|
2020-04-26 23:37:49 +02: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 07:21:50 +02:00
|
|
|
void MoveForward(float amt) override
|
2020-04-26 23:37:49 +02:00
|
|
|
{
|
|
|
|
m_mat = Common::Matrix44::Translate(Common::Vec3{0, 0, amt}) * m_mat;
|
|
|
|
}
|
|
|
|
|
2020-10-23 06:09:14 +02:00
|
|
|
void Rotate(const Common::Vec3& amt) override { Rotate(Common::Quaternion::RotateXYZ(amt)); }
|
|
|
|
|
|
|
|
void Rotate(const Common::Quaternion& quat) override
|
2020-04-26 23:37:49 +02:00
|
|
|
{
|
2020-10-23 06:09:14 +02:00
|
|
|
m_mat = Common::Matrix44::FromQuaternion(quat) * m_mat;
|
2020-04-26 23:37:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Reset() override { m_mat = Common::Matrix44::Identity(); }
|
|
|
|
|
2021-02-13 17:42:20 +01:00
|
|
|
void DoState(PointerWrap& p) override { p.Do(m_mat); }
|
2020-04-26 23:37:49 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
Common::Matrix44 m_mat = Common::Matrix44::Identity();
|
|
|
|
};
|
|
|
|
|
2020-10-23 06:09:14 +02:00
|
|
|
class FPSController final : public CameraController
|
2020-04-26 23:37:49 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Common::Matrix44 GetView() override
|
|
|
|
{
|
2020-10-23 06:09:14 +02:00
|
|
|
return Common::Matrix44::FromQuaternion(m_rotate_quat) *
|
|
|
|
Common::Matrix44::Translate(m_position);
|
2020-04-26 23:37:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MoveVertical(float amt) override
|
|
|
|
{
|
2020-10-23 06:09:14 +02:00
|
|
|
const Common::Vec3 up = m_rotate_quat.Conjugate() * Common::Vec3{0, 1, 0};
|
2020-04-26 23:37:49 +02:00
|
|
|
m_position += up * amt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MoveHorizontal(float amt) override
|
|
|
|
{
|
2020-10-23 06:09:14 +02:00
|
|
|
const Common::Vec3 right = m_rotate_quat.Conjugate() * Common::Vec3{1, 0, 0};
|
2020-04-26 23:37:49 +02:00
|
|
|
m_position += right * amt;
|
|
|
|
}
|
|
|
|
|
2020-06-12 07:21:50 +02:00
|
|
|
void MoveForward(float amt) override
|
2020-04-26 23:37:49 +02:00
|
|
|
{
|
2020-10-23 06:09:14 +02:00
|
|
|
const Common::Vec3 forward = m_rotate_quat.Conjugate() * Common::Vec3{0, 0, 1};
|
2020-04-26 23:37:49 +02:00
|
|
|
m_position += forward * amt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rotate(const Common::Vec3& amt) override
|
|
|
|
{
|
2021-03-12 07:02:06 +01:00
|
|
|
if (amt.Length() == 0)
|
|
|
|
return;
|
|
|
|
|
2020-04-26 23:37:49 +02:00
|
|
|
m_rotation += amt;
|
|
|
|
|
2020-10-23 06:09:14 +02:00
|
|
|
using Common::Quaternion;
|
2021-03-12 07:02:06 +01:00
|
|
|
m_rotate_quat =
|
2020-10-23 06:09:14 +02:00
|
|
|
(Quaternion::RotateX(m_rotation.x) * Quaternion::RotateY(m_rotation.y)).Normalized();
|
2020-04-26 23:37:49 +02:00
|
|
|
}
|
|
|
|
|
2021-03-12 07:02:06 +01:00
|
|
|
void Rotate(const Common::Quaternion& quat) override
|
|
|
|
{
|
|
|
|
Rotate(Common::FromQuaternionToEuler(quat));
|
|
|
|
}
|
2020-10-23 06:09:14 +02:00
|
|
|
|
2020-04-26 23:37:49 +02:00
|
|
|
void Reset() override
|
|
|
|
{
|
|
|
|
m_position = Common::Vec3{};
|
|
|
|
m_rotation = Common::Vec3{};
|
2020-10-23 06:09:14 +02:00
|
|
|
m_rotate_quat = Common::Quaternion::Identity();
|
2020-04-26 23:37:49 +02:00
|
|
|
}
|
|
|
|
|
2021-02-13 17:42:20 +01:00
|
|
|
void DoState(PointerWrap& p) override
|
2020-04-26 23:37:49 +02:00
|
|
|
{
|
|
|
|
p.Do(m_rotation);
|
2020-10-23 06:09:14 +02:00
|
|
|
p.Do(m_rotate_quat);
|
2020-04-26 23:37:49 +02:00
|
|
|
p.Do(m_position);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Common::Vec3 m_rotation = Common::Vec3{};
|
2020-10-23 06:09:14 +02:00
|
|
|
Common::Quaternion m_rotate_quat = Common::Quaternion::Identity();
|
2020-04-26 23:37:49 +02:00
|
|
|
Common::Vec3 m_position = Common::Vec3{};
|
|
|
|
};
|
|
|
|
|
2020-10-23 06:09:14 +02:00
|
|
|
class OrbitalController final : public CameraController
|
2020-04-26 23:37:49 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Common::Matrix44 GetView() override
|
|
|
|
{
|
2020-10-23 06:09:14 +02:00
|
|
|
return Common::Matrix44::Translate(Common::Vec3{0, 0, -m_distance}) *
|
|
|
|
Common::Matrix44::FromQuaternion(m_rotate_quat);
|
2020-04-26 23:37:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MoveVertical(float) override {}
|
|
|
|
|
|
|
|
void MoveHorizontal(float) override {}
|
|
|
|
|
2020-06-12 07:21:50 +02:00
|
|
|
void MoveForward(float amt) override
|
2020-04-26 23:37:49 +02:00
|
|
|
{
|
|
|
|
m_distance += -1 * amt;
|
|
|
|
m_distance = std::clamp(m_distance, 0.0f, m_distance);
|
|
|
|
}
|
|
|
|
|
2020-10-23 06:09:14 +02:00
|
|
|
void Rotate(const Common::Vec3& amt) override
|
|
|
|
{
|
2021-03-12 07:02:06 +01:00
|
|
|
if (amt.Length() == 0)
|
|
|
|
return;
|
|
|
|
|
2020-10-23 06:09:14 +02:00
|
|
|
m_rotation += amt;
|
|
|
|
|
|
|
|
using Common::Quaternion;
|
2021-03-12 07:02:06 +01:00
|
|
|
m_rotate_quat =
|
2020-10-23 06:09:14 +02:00
|
|
|
(Quaternion::RotateX(m_rotation.x) * Quaternion::RotateY(m_rotation.y)).Normalized();
|
|
|
|
}
|
|
|
|
|
2021-03-12 07:02:06 +01:00
|
|
|
void Rotate(const Common::Quaternion& quat) override
|
|
|
|
{
|
|
|
|
Rotate(Common::FromQuaternionToEuler(quat));
|
|
|
|
}
|
2020-04-26 23:37:49 +02:00
|
|
|
|
|
|
|
void Reset() override
|
|
|
|
{
|
|
|
|
m_rotation = Common::Vec3{};
|
2020-10-23 06:09:14 +02:00
|
|
|
m_rotate_quat = Common::Quaternion::Identity();
|
2020-04-26 23:37:49 +02:00
|
|
|
m_distance = 0;
|
|
|
|
}
|
|
|
|
|
2021-02-13 17:42:20 +01:00
|
|
|
void DoState(PointerWrap& p) override
|
2020-04-26 23:37:49 +02:00
|
|
|
{
|
|
|
|
p.Do(m_rotation);
|
2020-10-23 06:09:14 +02:00
|
|
|
p.Do(m_rotate_quat);
|
2020-04-26 23:37:49 +02:00
|
|
|
p.Do(m_distance);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
float m_distance = 0;
|
|
|
|
Common::Vec3 m_rotation = Common::Vec3{};
|
2020-10-23 06:09:14 +02:00
|
|
|
Common::Quaternion m_rotate_quat = Common::Quaternion::Identity();
|
2020-04-26 23:37:49 +02:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2020-12-26 05:19:09 +01:00
|
|
|
FreeLookCamera::FreeLookCamera()
|
|
|
|
{
|
|
|
|
SetControlType(FreeLook::ControlType::SixAxis);
|
|
|
|
}
|
|
|
|
|
2020-06-19 06:44:17 +02:00
|
|
|
void FreeLookCamera::SetControlType(FreeLook::ControlType type)
|
2020-04-26 23:37:49 +02:00
|
|
|
{
|
|
|
|
if (m_current_type && *m_current_type == type)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-19 06:44:17 +02:00
|
|
|
if (type == FreeLook::ControlType::SixAxis)
|
2020-04-26 23:37:49 +02:00
|
|
|
{
|
|
|
|
m_camera_controller = std::make_unique<SixAxisController>();
|
|
|
|
}
|
2020-06-19 06:44:17 +02:00
|
|
|
else if (type == FreeLook::ControlType::Orbital)
|
2020-04-26 23:37:49 +02:00
|
|
|
{
|
|
|
|
m_camera_controller = std::make_unique<OrbitalController>();
|
|
|
|
}
|
2020-06-19 06:44:17 +02:00
|
|
|
else if (type == FreeLook::ControlType::FPS)
|
2020-04-26 23:37:49 +02:00
|
|
|
{
|
|
|
|
m_camera_controller = std::make_unique<FPSController>();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_current_type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::Matrix44 FreeLookCamera::GetView()
|
|
|
|
{
|
|
|
|
return m_camera_controller->GetView();
|
|
|
|
}
|
|
|
|
|
2020-05-19 00:22:41 +02:00
|
|
|
Common::Vec2 FreeLookCamera::GetFieldOfView() const
|
|
|
|
{
|
|
|
|
return Common::Vec2{m_fov_x, m_fov_y};
|
|
|
|
}
|
|
|
|
|
2020-04-26 23:37:49 +02: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 07:21:50 +02:00
|
|
|
void FreeLookCamera::MoveForward(float amt)
|
2020-04-26 23:37:49 +02:00
|
|
|
{
|
2020-06-12 07:21:50 +02:00
|
|
|
m_camera_controller->MoveForward(amt);
|
2020-04-26 23:37:49 +02:00
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreeLookCamera::Rotate(const Common::Vec3& amt)
|
|
|
|
{
|
|
|
|
m_camera_controller->Rotate(amt);
|
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
2021-03-12 07:02:06 +01:00
|
|
|
void FreeLookCamera::Rotate(const Common::Quaternion& amt)
|
|
|
|
{
|
|
|
|
m_camera_controller->Rotate(amt);
|
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
2020-05-19 00:22:41 +02:00
|
|
|
void FreeLookCamera::IncreaseFovX(float fov)
|
|
|
|
{
|
|
|
|
m_fov_x += fov;
|
2021-03-18 02:51:35 +01:00
|
|
|
m_fov_x = std::clamp(m_fov_x, m_min_fov_multiplier, m_fov_x);
|
2020-05-19 00:22:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FreeLookCamera::IncreaseFovY(float fov)
|
|
|
|
{
|
|
|
|
m_fov_y += fov;
|
2021-03-18 02:51:35 +01:00
|
|
|
m_fov_y = std::clamp(m_fov_y, m_min_fov_multiplier, m_fov_y);
|
2020-06-23 05:09:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
float FreeLookCamera::GetFovStepSize() const
|
|
|
|
{
|
2021-03-18 02:51:35 +01:00
|
|
|
return 1.5f;
|
2020-05-19 00:22:41 +02:00
|
|
|
}
|
|
|
|
|
2020-04-26 23:37:49 +02:00
|
|
|
void FreeLookCamera::Reset()
|
|
|
|
{
|
|
|
|
m_camera_controller->Reset();
|
2020-05-19 00:22:41 +02:00
|
|
|
m_fov_x = 1.0f;
|
|
|
|
m_fov_y = 1.0f;
|
2020-04-26 23:37:49 +02:00
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
2021-03-18 02:51:35 +01:00
|
|
|
void FreeLookCamera::ModifySpeed(float amt)
|
2020-06-12 07:17:40 +02:00
|
|
|
{
|
2021-03-18 02:51:35 +01:00
|
|
|
m_speed += amt;
|
|
|
|
m_speed = std::clamp(m_speed, 0.0f, m_speed);
|
2020-06-12 07:17:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FreeLookCamera::ResetSpeed()
|
|
|
|
{
|
2021-03-18 02:51:35 +01:00
|
|
|
m_speed = 60.0f;
|
2020-06-12 07:17:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
float FreeLookCamera::GetSpeed() const
|
|
|
|
{
|
|
|
|
return m_speed;
|
|
|
|
}
|
|
|
|
|
2020-04-26 23:37:49 +02:00
|
|
|
void FreeLookCamera::DoState(PointerWrap& p)
|
|
|
|
{
|
|
|
|
if (p.mode == PointerWrap::MODE_WRITE || p.mode == PointerWrap::MODE_MEASURE)
|
|
|
|
{
|
2020-06-12 07:17:40 +02:00
|
|
|
p.Do(m_speed);
|
2020-04-26 23:37:49 +02:00
|
|
|
p.Do(m_current_type);
|
2020-05-19 00:22:41 +02:00
|
|
|
p.Do(m_fov_x);
|
|
|
|
p.Do(m_fov_y);
|
2020-04-26 23:37:49 +02:00
|
|
|
if (m_camera_controller)
|
|
|
|
{
|
|
|
|
m_camera_controller->DoState(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const auto old_type = m_current_type;
|
2020-06-12 07:17:40 +02:00
|
|
|
p.Do(m_speed);
|
2020-04-26 23:37:49 +02:00
|
|
|
p.Do(m_current_type);
|
2020-06-28 02:28:01 +02:00
|
|
|
p.Do(m_fov_x);
|
|
|
|
p.Do(m_fov_y);
|
2020-04-26 23:37:49 +02: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 19:07:40 +02:00
|
|
|
|
|
|
|
void FreeLookCamera::SetClean()
|
|
|
|
{
|
|
|
|
m_dirty = false;
|
|
|
|
}
|
2020-06-19 06:49:07 +02:00
|
|
|
|
|
|
|
bool FreeLookCamera::IsActive() const
|
|
|
|
{
|
|
|
|
return FreeLook::GetActiveConfig().enabled;
|
|
|
|
}
|