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 "";
|
|
|
|
}
|
|
|
|
|
|
|
|
class SixAxisController : public CameraController
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rotate(const Common::Vec3& amt) override
|
|
|
|
{
|
|
|
|
using Common::Matrix33;
|
|
|
|
m_mat = Common::Matrix44::FromMatrix33(Matrix33::RotateX(amt.x) * Matrix33::RotateY(amt.y) *
|
|
|
|
Matrix33::RotateZ(amt.z)) *
|
|
|
|
m_mat;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reset() override { m_mat = Common::Matrix44::Identity(); }
|
|
|
|
|
|
|
|
void DoState(PointerWrap& p) { p.Do(m_mat); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Common::Matrix44 m_mat = Common::Matrix44::Identity();
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr double HalfPI = MathUtil::PI / 2;
|
|
|
|
|
|
|
|
class FPSController : public CameraController
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Common::Matrix44 GetView() override
|
|
|
|
{
|
|
|
|
return m_rotate_mat * 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]};
|
|
|
|
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]};
|
|
|
|
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
|
|
|
{
|
|
|
|
Common::Vec3 forward{m_rotate_mat.data[8], m_rotate_mat.data[9], m_rotate_mat.data[10]};
|
|
|
|
m_position += forward * amt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rotate(const Common::Vec3& amt) override
|
|
|
|
{
|
|
|
|
m_rotation += amt;
|
|
|
|
|
|
|
|
using Common::Matrix33;
|
|
|
|
using Common::Matrix44;
|
|
|
|
m_rotate_mat =
|
|
|
|
Matrix44::FromMatrix33(Matrix33::RotateX(m_rotation.x) * Matrix33::RotateY(m_rotation.y));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reset() override
|
|
|
|
{
|
|
|
|
m_position = Common::Vec3{};
|
|
|
|
m_rotation = Common::Vec3{};
|
|
|
|
m_rotate_mat = Common::Matrix44::Identity();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoState(PointerWrap& p)
|
|
|
|
{
|
|
|
|
p.Do(m_rotation);
|
|
|
|
p.Do(m_rotate_mat);
|
|
|
|
p.Do(m_position);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Common::Vec3 m_rotation = Common::Vec3{};
|
|
|
|
Common::Matrix44 m_rotate_mat = Common::Matrix44::Identity();
|
|
|
|
Common::Vec3 m_position = Common::Vec3{};
|
|
|
|
};
|
|
|
|
|
|
|
|
class OrbitalController : 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rotate(const Common::Vec3& amt) override { m_rotation += amt; }
|
|
|
|
|
|
|
|
void Reset() override
|
|
|
|
{
|
|
|
|
m_rotation = Common::Vec3{};
|
|
|
|
m_distance = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoState(PointerWrap& p)
|
|
|
|
{
|
|
|
|
p.Do(m_rotation);
|
|
|
|
p.Do(m_distance);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
float m_distance = 0;
|
|
|
|
Common::Vec3 m_rotation = Common::Vec3{};
|
|
|
|
};
|
|
|
|
} // 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;
|
|
|
|
}
|
|
|
|
|
2020-05-18 22:22:41 +00:00
|
|
|
void FreeLookCamera::IncreaseFovX(float fov)
|
|
|
|
{
|
|
|
|
m_fov_x += fov;
|
2020-06-23 03:09:32 +00:00
|
|
|
m_fov_x = std::clamp(m_fov_x, m_fov_step_size, m_fov_x);
|
2020-05-18 22:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FreeLookCamera::IncreaseFovY(float fov)
|
|
|
|
{
|
|
|
|
m_fov_y += fov;
|
2020-06-23 03:09:32 +00:00
|
|
|
m_fov_y = std::clamp(m_fov_y, m_fov_step_size, m_fov_y);
|
|
|
|
}
|
|
|
|
|
|
|
|
float FreeLookCamera::GetFovStepSize() const
|
|
|
|
{
|
|
|
|
return m_fov_step_size;
|
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;
|
|
|
|
}
|
|
|
|
|
2020-06-12 05:17:40 +00:00
|
|
|
void FreeLookCamera::ModifySpeed(float multiplier)
|
|
|
|
{
|
|
|
|
m_speed *= multiplier;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreeLookCamera::ResetSpeed()
|
|
|
|
{
|
|
|
|
m_speed = 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|