From 03108a282fd32b479e9bb17d4c5eb66a34eca1ee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20=C5=A0toura=C4=8D?= <525032@mail.muni.cz>
Date: Sun, 2 Apr 2023 19:22:33 +0200
Subject: [PATCH] fixed object rotation correctness

---
 src/gfx/include/gfx/control.hpp     |  5 ++---
 src/gfx/include/gfx/frame.hpp       | 10 +++++++---
 src/gfx/include/gfx/obj_control.hpp |  2 --
 src/gfx/src/control.cpp             |  5 +++++
 src/gfx/src/render.cpp              |  2 +-
 src/studio/src/simulator.cpp        |  5 +++--
 6 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/src/gfx/include/gfx/control.hpp b/src/gfx/include/gfx/control.hpp
index 14a64aa..818e3ce 100644
--- a/src/gfx/include/gfx/control.hpp
+++ b/src/gfx/include/gfx/control.hpp
@@ -24,9 +24,6 @@ protected:
 public:
     Controller(Frame &frame) 
     : bound_frame{frame} {}
-    //   frontVec{glm::normalize(glm::vec3(frame.getRotation().x, 
-    //                                     frame.getRotation().y, 
-    //                                     frame.getRotation().z))} {}
 
     glm::vec3 getFrontVec() { return frontVec; }
     glm::vec3 getUpVec() { return upVec; }
@@ -45,6 +42,8 @@ public:
     void right();
     void up();
     void down();
+
+    void rotate(glm::vec3 rotational_difference);
 };
 
 #endif
\ No newline at end of file
diff --git a/src/gfx/include/gfx/frame.hpp b/src/gfx/include/gfx/frame.hpp
index 5d8dffb..fc45884 100644
--- a/src/gfx/include/gfx/frame.hpp
+++ b/src/gfx/include/gfx/frame.hpp
@@ -7,22 +7,26 @@
 #include <glm/gtc/type_ptr.hpp>
 #include <glm/gtc/quaternion.hpp>
 #include <glm/gtx/quaternion.hpp>
+#include <vector>
 
 class Frame
 {
     glm::vec3 positionVec = glm::vec3(0.0f, 0.0f, 0.0f);
-    glm::quat rotationQuat;
+    glm::quat rotationQuat = glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
 
 public:
     Frame(glm::vec3 position, glm::vec3 rotation)
-     : positionVec{position}, rotationQuat{glm::quat(rotation)} {}
+     : positionVec{position}
+    {    
+        setrotationQuat(rotation);
+    }
     
     glm::vec3 getPosition() { return positionVec; }
     glm::quat getRotation() { return rotationQuat; }
     glm::mat4 getRotationMat() { return glm::toMat4(rotationQuat); }
 
     void setPositionVec(glm::vec3 new_position) { positionVec = new_position; }
-    void setrotationQuat(glm::vec3 new_rotation) { rotationQuat = glm::quat(new_rotation); }
+    void setrotationQuat(glm::vec3 new_rotation) { rotationQuat *= glm::tquat(glm::radians(new_rotation)); }
 };
 
 #endif
\ No newline at end of file
diff --git a/src/gfx/include/gfx/obj_control.hpp b/src/gfx/include/gfx/obj_control.hpp
index 3f6ad4a..a16edc2 100644
--- a/src/gfx/include/gfx/obj_control.hpp
+++ b/src/gfx/include/gfx/obj_control.hpp
@@ -21,8 +21,6 @@ class ObjectController : public Controller
 public:
     using Controller::Controller;
     void setFrontVec() override;
-
-    //void rotate() TO DO
 };
 
 
diff --git a/src/gfx/src/control.cpp b/src/gfx/src/control.cpp
index 2b12273..3dfd2d7 100644
--- a/src/gfx/src/control.cpp
+++ b/src/gfx/src/control.cpp
@@ -48,4 +48,9 @@ void Controller::up()
 void Controller::down()
 {
     bound_frame.setPositionVec(bound_frame.getPosition() - step * upVec);
+}
+
+void Controller::rotate(glm::vec3 rotational_difference)
+{
+    bound_frame.setrotationQuat(rotational_difference);
 }
\ No newline at end of file
diff --git a/src/gfx/src/render.cpp b/src/gfx/src/render.cpp
index 1371bb6..9d7305b 100644
--- a/src/gfx/src/render.cpp
+++ b/src/gfx/src/render.cpp
@@ -64,7 +64,7 @@ void gfx_draw(Shader &myShader,
     /* ---------------------------------------------------------------- */
 
     glm::mat4 model = glm::mat4(1.0f);
-    model *= obj_frame.getRotationMat(); // QUESTION: Is this correct?
+    model = obj_frame.getRotationMat() * model;
     model = glm::translate(model, obj_frame.getPosition());
 
     
diff --git a/src/studio/src/simulator.cpp b/src/studio/src/simulator.cpp
index 50c96da..ccd573e 100644
--- a/src/studio/src/simulator.cpp
+++ b/src/studio/src/simulator.cpp
@@ -58,8 +58,8 @@ Simulator::Simulator()
               6, 7, 8
              }
             }
-    , cam_frame{{0.0f, 0.0f, 3.0f}, {0.0f, -90.0f, 0.0f}} // Setting rotation currently does nothing
-    , obj_frame{{ 0.0f, 0.0f, 0.0f }, { -50.0f, 0.0f, 0.0f }}
+    , cam_frame{{0.0f, 0.0f, 3.0f}, {0.0f, -90.0f, 0.0f}} // Setting camera rotation currently does nothing
+    , obj_frame{{ 0.0f, 0.0f, 0.0f }, { 90.0f, 0.0f, 0.0f }}
     , cam_control{cam_frame}
     , obj_control{obj_frame}
     // , object{{ 1.0f, -1.0f, 0.0f }, 
@@ -102,6 +102,7 @@ void Simulator::update()
     
     process_input();
     process_mouse(); 
+    //obj_control.rotate(glm::vec3(1.0f, 0.0f, 0.0f)); // Rotation around x-axis demo
 }
 
 void Simulator::present()
-- 
GitLab