From 1227a74d15f32d35a4bed6cdc36d461e21018f56 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20=C5=A0toura=C4=8D?= <525032@mail.muni.cz>
Date: Wed, 25 Oct 2023 13:14:11 +0200
Subject: [PATCH] basic orbit camera working

---
 src/controls/include/controls/cam_control.hpp | 24 ++++++++-
 src/controls/include/controls/control.hpp     |  4 +-
 src/controls/src/cam_control.cpp              | 51 ++++++++++++++++++-
 src/controls/src/control_hub.cpp              |  9 +++-
 4 files changed, 82 insertions(+), 6 deletions(-)

diff --git a/src/controls/include/controls/cam_control.hpp b/src/controls/include/controls/cam_control.hpp
index 650c5b4..c6bca11 100644
--- a/src/controls/include/controls/cam_control.hpp
+++ b/src/controls/include/controls/cam_control.hpp
@@ -20,7 +20,29 @@ public:
     void setSens(float x_size, float y_size);
     void setSensMultiplier(float _sens_multiplier) { sens_multiplier = _sens_multiplier; }
 
-    void processMouse(const osi::Mouse &mouse, const osi::Window &window, bool &editor_running);
+    virtual void mouseMove(const osi::Mouse &mouse, const osi::Window &window, bool &editor_running);
+};
+
+class FreeFlyCameraController : public CameraController
+{
+public:
+    using CameraController::CameraController;
+    // void keyboardMove(const std::unordered_set<std::string> &held_keys, 
+    //                   const std::unordered_set<std::string> &pressed_keys) override;
+    // void mouseMove(const osi::Mouse &mouse, const osi::Window &window, bool &editor_running) override
+};
+
+class OrbitCameraController : public CameraController
+{
+    glm::vec3 target = glm::vec3(0.0f, 0.0f, 0.0f);
+    float distance = 5.0f;
+
+public:
+    using CameraController::CameraController;
+    void keyboardMove(const std::unordered_set<std::string> &held_keys, 
+                      const std::unordered_set<std::string> &pressed_keys) override;
+
+    void mouseMove(const osi::Mouse &mouse, const osi::Window &window, bool &editor_running) override;
 };
 
 #endif
\ No newline at end of file
diff --git a/src/controls/include/controls/control.hpp b/src/controls/include/controls/control.hpp
index b299522..7fb8b1d 100644
--- a/src/controls/include/controls/control.hpp
+++ b/src/controls/include/controls/control.hpp
@@ -45,8 +45,8 @@ public:
 
     void translate(glm::vec3 direction) { bound_frame->translatePosVec(direction); }
 
-    void keyboardMove(const std::unordered_set<std::string> &held_keys, 
-                      const std::unordered_set<std::string> &pressed_keys);
+    virtual void keyboardMove(const std::unordered_set<std::string> &held_keys, 
+                              const std::unordered_set<std::string> &pressed_keys);
     void forward();
     void backward();
     void left();
diff --git a/src/controls/src/cam_control.cpp b/src/controls/src/cam_control.cpp
index 0ed0aaf..66e7a7e 100644
--- a/src/controls/src/cam_control.cpp
+++ b/src/controls/src/cam_control.cpp
@@ -27,7 +27,7 @@ void CameraController::setSens(float x_size, float y_size)
 }
 
 
-void CameraController::processMouse(const osi::Mouse &mouse, const osi::Window &window, bool &editor_running)
+void CameraController::mouseMove(const osi::Mouse &mouse, const osi::Window &window, bool &editor_running)
 {
     if (window.is_minimized())
         return;
@@ -53,4 +53,53 @@ void CameraController::processMouse(const osi::Mouse &mouse, const osi::Window &
     }
 
     turn(xoffset, yoffset);
+}
+void OrbitCameraController::keyboardMove(const std::unordered_set<std::string> &held_keys, const std::unordered_set<std::string> &pressed_keys)
+{
+    // these should also move the target
+    if (held_keys.contains("W")) // redo
+        forward();
+    if (held_keys.contains("S")) // redo
+        backward();
+    if (held_keys.contains("A"))
+        left();
+    if (held_keys.contains("D"))
+        right();
+    if (held_keys.contains("R")) // redo
+        up();
+    if (held_keys.contains("F")) // redo
+        down();
+
+    // if (pressed_keys.contains("P"))
+    //     stepForward(1.0f);
+    // if (pressed_keys.contains("O"))
+    //     stepBackward(1.0f);
+}
+void OrbitCameraController::mouseMove(const osi::Mouse &mouse, const osi::Window &window, bool &editor_running)
+{
+    if (window.is_minimized())
+        return;
+
+    float theta = 0.0f; // azimuthal angle
+    float phi = 0.0f;   // polar angle
+
+    if (mouse.down().contains("MouseRight")
+        && window.is_mouse_in_window())
+    {
+        if (!editor_running || (editor_running && window.is_resized()))
+        {
+            setSens(window.pixel_size_in_meters().x,
+                                window.pixel_size_in_meters().y);
+            editor_running = true;
+        }
+
+        glm::i32vec2 offset = mouse.pos_delta();
+        
+        theta = -static_cast<float>(offset.x) * getSens().x; 
+        phi = -static_cast<float>(-offset.y) * getSens().y;
+
+        turn(theta, phi);
+        auto ahead = -bound_frame->getRotationAxisZ();
+        bound_frame->setPosVec(target - (distance * ahead));
+    }
 }
\ No newline at end of file
diff --git a/src/controls/src/control_hub.cpp b/src/controls/src/control_hub.cpp
index 761146c..a0ff669 100644
--- a/src/controls/src/control_hub.cpp
+++ b/src/controls/src/control_hub.cpp
@@ -2,7 +2,12 @@
 
 ControlHub::ControlHub()
 {
-    cam_controls.emplace_back(std::make_shared<CameraController>());
+    auto orbit_cam = std::make_shared<OrbitCameraController>();
+    auto free_cam = std::make_shared<FreeFlyCameraController>();
+
+    cam_controls.emplace_back(orbit_cam); // BROKEN
+    cam_controls.emplace_back(free_cam);
+
     obj_controls.emplace_back(std::make_shared<ObjectController>());
 
     active_camera_type = cam_controls[0];
@@ -54,7 +59,7 @@ void ControlHub::processInput(const scene_ptr &scene, const osi::Keyboard &keybo
 }
 void ControlHub::processMouse(const osi::Mouse &mouse, const osi::Window &window, bool &editor_running)
 {
-    active_camera_type->processMouse(mouse, window, editor_running);
+    active_camera_type->mouseMove(mouse, window, editor_running);
 }
 
 void ControlHub::rotate_cam(glm::vec3 rotation)
-- 
GitLab