From 6c97c029b58c35cf4ab20e39a772f77dd8b8837d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20=C5=A0toura=C4=8D?= <525032@mail.muni.cz>
Date: Sun, 23 Apr 2023 22:39:16 +0200
Subject: [PATCH] normal pos calc, camera and objects fully use quat

---
 data/shaders/lightshader.vert       |  3 ++-
 src/gfx/include/gfx/cam_control.hpp |  3 +--
 src/gfx/include/gfx/control.hpp     | 12 +++---------
 src/gfx/include/gfx/frame.hpp       |  9 +++++----
 src/gfx/include/gfx/obj_control.hpp |  6 ------
 src/gfx/src/cam_control.cpp         |  7 ++++++-
 src/gfx/src/obj_control.cpp         |  5 -----
 src/studio/src/simulator.cpp        |  4 ++--
 8 files changed, 19 insertions(+), 30 deletions(-)

diff --git a/data/shaders/lightshader.vert b/data/shaders/lightshader.vert
index 022a844..1e1282a 100644
--- a/data/shaders/lightshader.vert
+++ b/data/shaders/lightshader.vert
@@ -11,7 +11,8 @@ uniform mat4 projection;
 
 void main()
 {
-    Normal = aNormal;
+    //Normal = aNormal;
+    Normal = vec3(model * vec4(aNormal, 0.0));
     FragPos = vec3(model * vec4(aPos, 1.0));
     
     gl_Position = projection * view * vec4(FragPos, 1.0);
diff --git a/src/gfx/include/gfx/cam_control.hpp b/src/gfx/include/gfx/cam_control.hpp
index 372dbe8..516bf5e 100644
--- a/src/gfx/include/gfx/cam_control.hpp
+++ b/src/gfx/include/gfx/cam_control.hpp
@@ -10,7 +10,6 @@
 
 class CameraController : public Controller
 {
-    // TO DO: change to use quaternion
     float pitch = 0.0f;
     float yaw = -90.0f; 
 
@@ -25,7 +24,7 @@ public:
     void setPitch(float yoffset);
     void setYaw(float xoffset);
 
-    void setFrontVec() override;
+    void setFrontVec();
 
     void turn(float xoffset, float yoffset);
 
diff --git a/src/gfx/include/gfx/control.hpp b/src/gfx/include/gfx/control.hpp
index 40379aa..e3520a3 100644
--- a/src/gfx/include/gfx/control.hpp
+++ b/src/gfx/include/gfx/control.hpp
@@ -13,11 +13,6 @@
 class Controller
 {
 protected:
-    // TO DO: zrusit, pouzivat frame, camera tez
-    glm::vec3 frontVec = glm::vec3(0.0f, 0.0f, -1.0f); 
-    glm::vec3 upVec = glm::vec3(0.0f, 1.0f, 0.0f);
-
-
     float speed = 2.5f;
     float step = 0;
 
@@ -28,10 +23,9 @@ public:
     Controller(Frame &frame) 
     : bound_frame{frame} {}
 
-    virtual glm::vec3 getFrontVec() { return frontVec; }
-    virtual glm::vec3 getUpVec() { return upVec; }
-    virtual void setFrontVec() = 0;
-    // void setUpVec(); // TO DO
+    virtual glm::vec3 getFrontVec() { return -bound_frame.getRotationAxisZ(); }
+    virtual glm::vec3 getUpVec() { return glm::vec3(0.0f, 1.0f, 0.0f); }
+    // virtual void setUpVec();
 
     void setStep(float dt);
     void setSpeed(float new_speed);
diff --git a/src/gfx/include/gfx/frame.hpp b/src/gfx/include/gfx/frame.hpp
index e59c5e3..bdb6688 100644
--- a/src/gfx/include/gfx/frame.hpp
+++ b/src/gfx/include/gfx/frame.hpp
@@ -7,6 +7,7 @@
 #include <glm/gtc/type_ptr.hpp>
 #include <glm/gtc/quaternion.hpp>
 #include <glm/gtx/quaternion.hpp>
+#include <glm/gtc/matrix_access.hpp>
 #include <vector>
 
 class Frame
@@ -26,10 +27,9 @@ public:
     glm::quat getRotation() { return rotationQuat; }
     glm::vec3 getScale() { return scale; }
 
-    // ???
-    glm::vec3 getRotationAxisX() { return glm::vec3(glm::axis(rotationQuat).x, 0, 0); }
-    glm::vec3 getRotationAxisY() { return glm::vec3(0, glm::axis(rotationQuat).y, 0); }
-    glm::vec3 getRotationAxisZ() { return glm::vec3(0, 0, glm::axis(rotationQuat).z); }
+    glm::vec3 getRotationAxisX() { return glm::column(glm::toMat3(rotationQuat), 0); }
+    glm::vec3 getRotationAxisY() { return glm::column(glm::toMat3(rotationQuat), 1); }
+    glm::vec3 getRotationAxisZ() { return glm::column(glm::toMat3(rotationQuat), 2); }
 
     glm::mat4 getTranslationMat() { return glm::translate(glm::mat4(1.0f), positionVec); }
     glm::mat4 getRotationMat() { return glm::toMat4(rotationQuat); }
@@ -41,6 +41,7 @@ public:
     void setPosVec(glm::vec3 new_position) { positionVec = new_position; }
     void translatePosVec(glm::vec3 direction) { positionVec += direction; }
     void setRotationQuat(glm::vec3 new_rotation) { rotationQuat = glm::tquat(glm::radians(new_rotation)); }
+    void setRotationQuat(glm::quat new_rotation) { rotationQuat = new_rotation; }
     void rotateRotationQuat(glm::vec3 rotation) { rotationQuat *= glm::tquat(glm::radians(rotation)); }
     void setScale(glm::vec3 _scale) { scale = _scale; }
 };
diff --git a/src/gfx/include/gfx/obj_control.hpp b/src/gfx/include/gfx/obj_control.hpp
index 730ed2e..7b16596 100644
--- a/src/gfx/include/gfx/obj_control.hpp
+++ b/src/gfx/include/gfx/obj_control.hpp
@@ -13,14 +13,8 @@
 
 class ObjectController : public Controller
 {
-    // QUESTION: set automatically relative to camera or axis movement?
-    // Currently: object axis
-    // glm::vec3 frontVec = glm::vec3(0.0f, 0.0f, -1.0f); 
-    // glm::vec3 upVec = glm::vec3(0.0f, 1.0f, 0.0f);
-
 public:
     using Controller::Controller;
-    void setFrontVec() override;
 };
 
 
diff --git a/src/gfx/src/cam_control.cpp b/src/gfx/src/cam_control.cpp
index 17bdbf0..a843531 100644
--- a/src/gfx/src/cam_control.cpp
+++ b/src/gfx/src/cam_control.cpp
@@ -22,7 +22,12 @@ void CameraController::setFrontVec()
     front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
     front.y = sin(glm::radians(pitch));
     front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
-    frontVec = glm::normalize(front);
+    // front.x = glm::degrees(acos(cos(glm::radians(yaw)) * cos(glm::radians(pitch))));
+    // front.y = glm::degrees(asin(sin(glm::radians(pitch))));
+    // front.z = glm::degrees(sin(glm::radians(yaw)) * cos(glm::radians(pitch)));
+    front = glm::normalize(front);
+    glm::quat front_quat = glm::quatLookAt(front, getUpVec());
+    bound_frame.setRotationQuat(front_quat);
 }
 
 void CameraController::turn(float xoffset, float yoffset)
diff --git a/src/gfx/src/obj_control.cpp b/src/gfx/src/obj_control.cpp
index d167cea..6a4504d 100644
--- a/src/gfx/src/obj_control.cpp
+++ b/src/gfx/src/obj_control.cpp
@@ -1,6 +1 @@
 #include <gfx/obj_control.hpp>
-
-void ObjectController::setFrontVec() // temporary implementation
-{
-    frontVec = glm::vec3(0.0f, 0.0f, -1.0f);
-}
\ No newline at end of file
diff --git a/src/studio/src/simulator.cpp b/src/studio/src/simulator.cpp
index 877a477..7ad8cf7 100644
--- a/src/studio/src/simulator.cpp
+++ b/src/studio/src/simulator.cpp
@@ -117,7 +117,7 @@ Simulator::Simulator()
                 std::make_shared<Shader>("./data/shaders/basicshader.vert",
                                          "./data/shaders/basicshader.frag")}
     , camera{60.0f}
-    , cam_frame{{0.0f, 1.0f, 6.0f}, {0.0f, -90.0f, 0.0f}} // Setting camera rotation currently does nothing
+    , cam_frame{{0.0f, 1.0f, 6.0f}, {0.0f, 0.0f, 0.0f}}
     , cam_control{cam_frame}
     , objects{std::make_shared<Object>(lit_cube_vertices, lit_cube_indices, lit_cube_normals),
               std::make_shared<Object>(lit_cube_vertices, lit_cube_indices, lit_cube_normals, glm::vec3(1.0f, 1.0f, 1.0f))}
@@ -129,7 +129,7 @@ Simulator::Simulator()
     objects[1]->setToLight(true);
     obj_frames[1]->setScale(glm::vec3(0.1f, 0.1f, 0.1f));
     camera.setWindowSize(window().size()); 
-    // obj_controls[0]->rotate(glm::vec3(0.0f, 50.0f, 0.0f));
+    obj_controls[0]->rotate(glm::vec3(0.0f, 50.0f, 0.0f));
 }
 
 Simulator::~Simulator() {}
-- 
GitLab