2020-04-26 21:37:49 +00:00
|
|
|
// Copyright 2020 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-04-26 21:37:49 +00: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 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 "";
|
|
|
|
}
|
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
class SixAxisController final : public CameraControllerInput
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
SixAxisController() = default;
|
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
Common::Matrix44 GetView() const override { return m_mat; }
|
2020-04-26 21:37:49 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
void Reset() override
|
|
|
|
{
|
|
|
|
CameraControllerInput::Reset();
|
|
|
|
m_mat = Common::Matrix44::Identity();
|
|
|
|
}
|
2020-04-26 21:37:49 +00:00
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
void DoState(PointerWrap& p) override
|
|
|
|
{
|
|
|
|
CameraControllerInput::DoState(p);
|
|
|
|
p.Do(m_mat);
|
|
|
|
}
|
2020-04-26 21:37:49 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Common::Matrix44 m_mat = Common::Matrix44::Identity();
|
|
|
|
};
|
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
class FPSController final : public CameraControllerInput
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-04-25 03:59:35 +00:00
|
|
|
Common::Matrix44 GetView() const override
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
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
|
|
|
|
{
|
2021-04-25 03:59:35 +00:00
|
|
|
CameraControllerInput::Reset();
|
2020-04-26 21:37:49 +00:00
|
|
|
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
|
|
|
{
|
2021-04-25 03:59:35 +00:00
|
|
|
CameraControllerInput::DoState(p);
|
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{};
|
|
|
|
};
|
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
class OrbitalController final : public CameraControllerInput
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-04-25 03:59:35 +00:00
|
|
|
Common::Matrix44 GetView() const override
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
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;
|
2021-05-08 19:17:31 +00:00
|
|
|
m_distance = std::max(m_distance, MIN_DISTANCE);
|
2020-04-26 21:37:49 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2021-04-25 03:59:35 +00:00
|
|
|
CameraControllerInput::Reset();
|
2020-04-26 21:37:49 +00:00
|
|
|
m_rotation = Common::Vec3{};
|
2020-10-23 04:09:14 +00:00
|
|
|
m_rotate_quat = Common::Quaternion::Identity();
|
2021-05-08 19:17:31 +00:00
|
|
|
m_distance = MIN_DISTANCE;
|
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
|
|
|
{
|
2021-04-25 03:59:35 +00:00
|
|
|
CameraControllerInput::DoState(p);
|
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:
|
2021-05-08 19:17:31 +00:00
|
|
|
static constexpr float MIN_DISTANCE = 0.0f;
|
|
|
|
float m_distance = MIN_DISTANCE;
|
2020-04-26 21:37:49 +00:00
|
|
|
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
|
|
|
|
|
2021-05-08 19:36:44 +00:00
|
|
|
Common::Vec2 CameraControllerInput::GetFieldOfViewMultiplier() const
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
2021-05-08 19:17:31 +00:00
|
|
|
return Common::Vec2{m_fov_x_multiplier, m_fov_y_multiplier};
|
2020-04-26 21:37:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
void CameraControllerInput::DoState(PointerWrap& p)
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
2021-04-25 03:59:35 +00:00
|
|
|
p.Do(m_speed);
|
2021-05-08 19:17:31 +00:00
|
|
|
p.Do(m_fov_x_multiplier);
|
|
|
|
p.Do(m_fov_y_multiplier);
|
2020-04-26 21:37:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
void CameraControllerInput::IncreaseFovX(float fov)
|
2020-05-18 22:22:41 +00:00
|
|
|
{
|
2021-05-08 19:17:31 +00:00
|
|
|
m_fov_x_multiplier += fov;
|
|
|
|
m_fov_x_multiplier = std::max(m_fov_x_multiplier, MIN_FOV_MULTIPLIER);
|
2020-05-18 22:22:41 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
void CameraControllerInput::IncreaseFovY(float fov)
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
2021-05-08 19:17:31 +00:00
|
|
|
m_fov_y_multiplier += fov;
|
|
|
|
m_fov_y_multiplier = std::max(m_fov_y_multiplier, MIN_FOV_MULTIPLIER);
|
2020-04-26 21:37:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
float CameraControllerInput::GetFovStepSize() const
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
2021-04-25 03:59:35 +00:00
|
|
|
return 1.5f;
|
2020-04-26 21:37:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
void CameraControllerInput::Reset()
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
2021-05-08 19:17:31 +00:00
|
|
|
m_fov_x_multiplier = DEFAULT_FOV_MULTIPLIER;
|
|
|
|
m_fov_y_multiplier = DEFAULT_FOV_MULTIPLIER;
|
2020-04-26 21:37:49 +00:00
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
void CameraControllerInput::ModifySpeed(float amt)
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
2021-04-25 03:59:35 +00:00
|
|
|
m_speed += amt;
|
2021-05-08 19:17:31 +00:00
|
|
|
m_speed = std::max(m_speed, 0.0f);
|
2020-04-26 21:37:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
void CameraControllerInput::ResetSpeed()
|
2021-03-12 06:02:06 +00:00
|
|
|
{
|
2021-05-08 19:17:31 +00:00
|
|
|
m_speed = DEFAULT_SPEED;
|
2021-03-12 06:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
float CameraControllerInput::GetSpeed() const
|
2020-05-18 22:22:41 +00:00
|
|
|
{
|
2021-04-25 03:59:35 +00:00
|
|
|
return m_speed;
|
2020-05-18 22:22:41 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
FreeLookCamera::FreeLookCamera()
|
2020-05-18 22:22:41 +00:00
|
|
|
{
|
2021-04-25 03:59:35 +00:00
|
|
|
SetControlType(FreeLook::ControlType::SixAxis);
|
2020-06-23 03:09:32 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
void FreeLookCamera::SetControlType(FreeLook::ControlType type)
|
2020-06-23 03:09:32 +00:00
|
|
|
{
|
2021-04-25 03:59:35 +00:00
|
|
|
if (m_current_type && *m_current_type == type)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2020-05-18 22:22:41 +00:00
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
if (type == FreeLook::ControlType::SixAxis)
|
|
|
|
{
|
|
|
|
m_camera_controller = std::make_unique<SixAxisController>();
|
|
|
|
}
|
|
|
|
else if (type == FreeLook::ControlType::Orbital)
|
|
|
|
{
|
|
|
|
m_camera_controller = std::make_unique<OrbitalController>();
|
|
|
|
}
|
|
|
|
else if (type == FreeLook::ControlType::FPS)
|
|
|
|
{
|
|
|
|
m_camera_controller = std::make_unique<FPSController>();
|
|
|
|
}
|
2020-04-26 21:37:49 +00:00
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
m_current_type = type;
|
2020-06-12 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
Common::Matrix44 FreeLookCamera::GetView() const
|
2020-06-12 05:17:40 +00:00
|
|
|
{
|
2021-04-25 03:59:35 +00:00
|
|
|
return m_camera_controller->GetView();
|
2020-06-12 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 19:36:44 +00:00
|
|
|
Common::Vec2 FreeLookCamera::GetFieldOfViewMultiplier() const
|
2020-06-12 05:17:40 +00:00
|
|
|
{
|
2021-05-08 19:36:44 +00:00
|
|
|
return m_camera_controller->GetFieldOfViewMultiplier();
|
2020-06-12 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 21:37:49 +00:00
|
|
|
void FreeLookCamera::DoState(PointerWrap& p)
|
|
|
|
{
|
2022-05-18 05:29:05 +00:00
|
|
|
if (p.IsWriteMode() || p.IsMeasureMode())
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
|
|
|
p.Do(m_current_type);
|
|
|
|
if (m_camera_controller)
|
|
|
|
{
|
|
|
|
m_camera_controller->DoState(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const auto old_type = m_current_type;
|
|
|
|
p.Do(m_current_type);
|
|
|
|
if (old_type == m_current_type)
|
|
|
|
{
|
|
|
|
m_camera_controller->DoState(p);
|
|
|
|
}
|
2022-05-18 05:29:05 +00:00
|
|
|
else if (p.IsReadMode())
|
2020-04-26 21:37:49 +00:00
|
|
|
{
|
|
|
|
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);
|
2022-05-18 05:29:05 +00:00
|
|
|
p.SetVerifyMode();
|
2020-04-26 21:37:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
bool FreeLookCamera::IsActive() const
|
2020-06-17 17:07:40 +00:00
|
|
|
{
|
2021-04-25 03:59:35 +00:00
|
|
|
return FreeLook::GetActiveConfig().enabled;
|
2020-06-17 17:07:40 +00:00
|
|
|
}
|
2020-06-19 04:49:07 +00:00
|
|
|
|
2021-04-25 03:59:35 +00:00
|
|
|
CameraController* FreeLookCamera::GetController() const
|
2020-06-19 04:49:07 +00:00
|
|
|
{
|
2021-04-25 03:59:35 +00:00
|
|
|
return m_camera_controller.get();
|
2020-06-19 04:49:07 +00:00
|
|
|
}
|