| 1 | // Copyright 2020 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/FreeLookCamera.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | |
| 8 | #include <fmt/format.h> |
| 9 | |
| 10 | #include "Common/ChunkFile.h" |
| 11 | #include "Common/Config/Config.h" |
| 12 | #include "Core/Core.h" |
| 13 | |
| 14 | FreeLookCamera g_freelook_camera; |
| 15 | |
| 16 | namespace |
| 17 | { |
| 18 | std::string to_string(FreeLook::ControlType type) |
| 19 | { |
| 20 | switch (type) |
| 21 | { |
| 22 | case FreeLook::ControlType::SixAxis: |
| 23 | return "Six Axis"; |
| 24 | case FreeLook::ControlType::FPS: |
| 25 | return "First Person"; |
| 26 | case FreeLook::ControlType::Orbital: |
| 27 | return "Orbital"; |
| 28 | } |
| 29 | |
| 30 | return ""; |
| 31 | } |
| 32 | |
| 33 | class SixAxisController final : public CameraControllerInput |
| 34 | { |
| 35 | public: |
| 36 | SixAxisController() = default; |
| 37 | |
| 38 | Common::Matrix44 GetView() const override { return m_mat; } |
| 39 | |
| 40 | void MoveVertical(float amt) override |
| 41 | { |
| 42 | m_mat = Common::Matrix44::Translate(Common::Vec3{0, amt, 0}) * m_mat; |
| 43 | } |
| 44 | |
| 45 | void MoveHorizontal(float amt) override |
| 46 | { |
| 47 | m_mat = Common::Matrix44::Translate(Common::Vec3{amt, 0, 0}) * m_mat; |
| 48 | } |
| 49 | |
| 50 | void MoveForward(float amt) override |
| 51 | { |
| 52 | m_mat = Common::Matrix44::Translate(Common::Vec3{0, 0, amt}) * m_mat; |
| 53 | } |
| 54 | |
| 55 | void Rotate(const Common::Vec3& amt) override { Rotate(Common::Quaternion::RotateXYZ(amt)); } |
| 56 | |
| 57 | void Rotate(const Common::Quaternion& quat) override |
| 58 | { |
| 59 | m_mat = Common::Matrix44::FromQuaternion(quat) * m_mat; |
| 60 | } |
| 61 | |
| 62 | void Reset() override |
| 63 | { |
| 64 | CameraControllerInput::Reset(); |
| 65 | m_mat = Common::Matrix44::Identity(); |
| 66 | } |
| 67 | |
| 68 | void DoState(PointerWrap& p) override |
| 69 | { |
| 70 | CameraControllerInput::DoState(p); |
| 71 | p.Do(m_mat); |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | Common::Matrix44 m_mat = Common::Matrix44::Identity(); |
| 76 | }; |
| 77 | |
| 78 | class FPSController final : public CameraControllerInput |
| 79 | { |
| 80 | public: |
| 81 | Common::Matrix44 GetView() const override |
| 82 | { |
| 83 | return Common::Matrix44::FromQuaternion(m_rotate_quat) * |
| 84 | Common::Matrix44::Translate(m_position); |
| 85 | } |
| 86 | |
| 87 | void MoveVertical(float amt) override |
| 88 | { |
| 89 | const Common::Vec3 up = m_rotate_quat.Conjugate() * Common::Vec3{0, 1, 0}; |
| 90 | m_position += up * amt; |
| 91 | } |
| 92 | |
| 93 | void MoveHorizontal(float amt) override |
| 94 | { |
| 95 | const Common::Vec3 right = m_rotate_quat.Conjugate() * Common::Vec3{1, 0, 0}; |
| 96 | m_position += right * amt; |
| 97 | } |
| 98 | |
| 99 | void MoveForward(float amt) override |
| 100 | { |
| 101 | const Common::Vec3 forward = m_rotate_quat.Conjugate() * Common::Vec3{0, 0, 1}; |
| 102 | m_position += forward * amt; |
| 103 | } |
| 104 | |
| 105 | void Rotate(const Common::Vec3& amt) override |
| 106 | { |
| 107 | if (amt.Length() == 0) |
| 108 | return; |
| 109 | |
| 110 | m_rotation += amt; |
| 111 | |
| 112 | using Common::Quaternion; |
| 113 | m_rotate_quat = |
| 114 | (Quaternion::RotateX(m_rotation.x) * Quaternion::RotateY(m_rotation.y)).Normalized(); |
| 115 | } |
| 116 | |
| 117 | void Rotate(const Common::Quaternion& quat) override |
| 118 | { |
| 119 | Rotate(Common::FromQuaternionToEuler(quat)); |
| 120 | } |
| 121 | |
| 122 | void Reset() override |
| 123 | { |
| 124 | CameraControllerInput::Reset(); |
| 125 | m_position = Common::Vec3{}; |
| 126 | m_rotation = Common::Vec3{}; |
| 127 | m_rotate_quat = Common::Quaternion::Identity(); |
| 128 | } |
| 129 | |
| 130 | void DoState(PointerWrap& p) override |
| 131 | { |
| 132 | CameraControllerInput::DoState(p); |
| 133 | p.Do(m_rotation); |
| 134 | p.Do(m_rotate_quat); |
| 135 | p.Do(m_position); |
| 136 | } |
| 137 | |
| 138 | private: |
| 139 | Common::Vec3 m_rotation = Common::Vec3{}; |
| 140 | Common::Quaternion m_rotate_quat = Common::Quaternion::Identity(); |
| 141 | Common::Vec3 m_position = Common::Vec3{}; |
| 142 | }; |
| 143 | |
| 144 | class OrbitalController final : public CameraControllerInput |
| 145 | { |
| 146 | public: |
| 147 | Common::Matrix44 GetView() const override |
| 148 | { |
| 149 | return Common::Matrix44::Translate(Common::Vec3{0, 0, -m_distance}) * |
| 150 | Common::Matrix44::FromQuaternion(m_rotate_quat); |
| 151 | } |
| 152 | |
| 153 | void MoveVertical(float) override {} |
| 154 | |
| 155 | void MoveHorizontal(float) override {} |
| 156 | |
| 157 | void MoveForward(float amt) override |
| 158 | { |
| 159 | m_distance += -1 * amt; |
| 160 | m_distance = std::max(m_distance, MIN_DISTANCE); |
| 161 | } |
| 162 | |
| 163 | void Rotate(const Common::Vec3& amt) override |
| 164 | { |
| 165 | if (amt.Length() == 0) |
| 166 | return; |
| 167 | |
| 168 | m_rotation += amt; |
| 169 | |
| 170 | using Common::Quaternion; |
| 171 | m_rotate_quat = |
| 172 | (Quaternion::RotateX(m_rotation.x) * Quaternion::RotateY(m_rotation.y)).Normalized(); |
| 173 | } |
| 174 | |
| 175 | void Rotate(const Common::Quaternion& quat) override |
| 176 | { |
| 177 | Rotate(Common::FromQuaternionToEuler(quat)); |
| 178 | } |
| 179 | |
| 180 | void Reset() override |
| 181 | { |
| 182 | CameraControllerInput::Reset(); |
| 183 | m_rotation = Common::Vec3{}; |
| 184 | m_rotate_quat = Common::Quaternion::Identity(); |
| 185 | m_distance = MIN_DISTANCE; |
| 186 | } |
| 187 | |
| 188 | void DoState(PointerWrap& p) override |
| 189 | { |
| 190 | CameraControllerInput::DoState(p); |
| 191 | p.Do(m_rotation); |
| 192 | p.Do(m_rotate_quat); |
| 193 | p.Do(m_distance); |
| 194 | } |
| 195 | |
| 196 | private: |
| 197 | static constexpr float MIN_DISTANCE = 0.0f; |
| 198 | float m_distance = MIN_DISTANCE; |
| 199 | Common::Vec3 m_rotation = Common::Vec3{}; |
| 200 | Common::Quaternion m_rotate_quat = Common::Quaternion::Identity(); |
| 201 | }; |
| 202 | } // namespace |
| 203 | |
| 204 | Common::Vec2 CameraControllerInput::GetFieldOfViewMultiplier() const |
| 205 | { |
| 206 | return Common::Vec2{m_fov_x_multiplier, m_fov_y_multiplier}; |
| 207 | } |
| 208 | |
| 209 | void CameraControllerInput::DoState(PointerWrap& p) |
| 210 | { |
| 211 | p.Do(m_speed); |
| 212 | p.Do(m_fov_x_multiplier); |
| 213 | p.Do(m_fov_y_multiplier); |
| 214 | } |
| 215 | |
| 216 | void CameraControllerInput::IncreaseFovX(float fov) |
| 217 | { |
| 218 | m_fov_x_multiplier += fov; |
| 219 | m_fov_x_multiplier = std::max(m_fov_x_multiplier, MIN_FOV_MULTIPLIER); |
| 220 | } |
| 221 | |
| 222 | void CameraControllerInput::IncreaseFovY(float fov) |
| 223 | { |
| 224 | m_fov_y_multiplier += fov; |
| 225 | m_fov_y_multiplier = std::max(m_fov_y_multiplier, MIN_FOV_MULTIPLIER); |
| 226 | } |
| 227 | |
| 228 | float CameraControllerInput::GetFovStepSize() const |
| 229 | { |
| 230 | return 1.5f; |
| 231 | } |
| 232 | |
| 233 | void CameraControllerInput::Reset() |
| 234 | { |
| 235 | m_fov_x_multiplier = DEFAULT_FOV_MULTIPLIER; |
| 236 | m_fov_y_multiplier = DEFAULT_FOV_MULTIPLIER; |
| 237 | m_dirty = true; |
| 238 | } |
| 239 | |
| 240 | void CameraControllerInput::ModifySpeed(float amt) |
| 241 | { |
| 242 | m_speed += amt; |
| 243 | m_speed = std::max(m_speed, 0.0f); |
| 244 | } |
| 245 | |
| 246 | void CameraControllerInput::ResetSpeed() |
| 247 | { |
| 248 | m_speed = DEFAULT_SPEED; |
| 249 | } |
| 250 | |
| 251 | float CameraControllerInput::GetSpeed() const |
| 252 | { |
| 253 | return m_speed; |
| 254 | } |
| 255 | |
| 256 | FreeLookCamera::FreeLookCamera() |
| 257 | { |
| 258 | RefreshConfig(); |
| 259 | } |
| 260 | |
| 261 | void FreeLookCamera::RefreshConfig() |
| 262 | { |
| 263 | m_is_enabled = Config::Get(Config::FREE_LOOK_ENABLED); |
| 264 | const auto type = Config::Get(Config::FL1_CONTROL_TYPE); |
| 265 | |
| 266 | if (m_current_type == type) |
| 267 | return; |
| 268 | |
| 269 | if (type == FreeLook::ControlType::Orbital) |
| 270 | { |
| 271 | m_camera_controller = std::make_unique<OrbitalController>(); |
| 272 | } |
| 273 | else if (type == FreeLook::ControlType::FPS) |
| 274 | { |
| 275 | m_camera_controller = std::make_unique<FPSController>(); |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | m_camera_controller = std::make_unique<SixAxisController>(); |
| 280 | } |
| 281 | |
| 282 | m_current_type = type; |
| 283 | } |
| 284 | |
| 285 | Common::Matrix44 FreeLookCamera::GetView() const |
| 286 | { |
| 287 | return m_camera_controller->GetView(); |
| 288 | } |
| 289 | |
| 290 | Common::Vec2 FreeLookCamera::GetFieldOfViewMultiplier() const |
| 291 | { |
| 292 | return m_camera_controller->GetFieldOfViewMultiplier(); |
| 293 | } |
| 294 | |
| 295 | void FreeLookCamera::DoState(PointerWrap& p) |
| 296 | { |
| 297 | if (p.IsWriteMode() || p.IsMeasureMode()) |
| 298 | { |
| 299 | p.Do(m_current_type); |
| 300 | if (m_camera_controller) |
| 301 | { |
| 302 | m_camera_controller->DoState(p); |
| 303 | } |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | const auto old_type = m_current_type; |
| 308 | p.Do(m_current_type); |
| 309 | if (old_type == m_current_type) |
| 310 | { |
| 311 | m_camera_controller->DoState(p); |
| 312 | } |
| 313 | else if (p.IsReadMode()) |
| 314 | { |
| 315 | const std::string old_type_name = old_type ? to_string(*old_type) : ""; |
| 316 | const std::string loaded_type_name = m_current_type ? to_string(*m_current_type) : ""; |
| 317 | const std::string message = |
| 318 | fmt::format("State needs same free look camera type. Settings value '{}', loaded value " |
| 319 | "'{}'. Aborting load state", |
| 320 | old_type_name, loaded_type_name); |
| 321 | Core::DisplayMessage(message, 5000); |
| 322 | p.SetVerifyMode(); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | bool FreeLookCamera::IsActive() const |
| 328 | { |
| 329 | return m_is_enabled; |
| 330 | } |
| 331 | |
| 332 | CameraController* FreeLookCamera::GetController() const |
| 333 | { |
| 334 | return m_camera_controller.get(); |
| 335 | } |