From ef4d10a46b9488429205c51ba83e963dd2005451 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20=C5=A0toura=C4=8D?= <525032@mail.muni.cz>
Date: Sat, 14 Oct 2023 19:42:17 +0200
Subject: [PATCH] added changing grid levels (+visual cue)

---
 src/controls/include/controls/control.hpp |  6 ++++-
 src/controls/src/control.cpp              | 29 +++++++++++++++++------
 src/controls/src/control_hub.cpp          |  4 ++--
 src/edit/include/edit/editor.hpp          |  2 ++
 src/edit/include/edit/scene.hpp           |  2 +-
 src/edit/src/editor.cpp                   | 20 +++++++++++-----
 src/edit/src/scene.cpp                    |  6 ++---
 src/gfx/include/gfx/render.hpp            |  4 +++-
 src/gfx/src/render.cpp                    | 21 ++++++++++++----
 src/studio/src/simulator.cpp              |  6 ++++-
 10 files changed, 73 insertions(+), 27 deletions(-)

diff --git a/src/controls/include/controls/control.hpp b/src/controls/include/controls/control.hpp
index 6d15654..d5ddda2 100644
--- a/src/controls/include/controls/control.hpp
+++ b/src/controls/include/controls/control.hpp
@@ -42,7 +42,8 @@ public:
     void setPosY(float coord) { bound_frame->setPosVec(glm::vec3(bound_frame->getPosition().x, coord, bound_frame->getPosition().z)); }
     void setPosZ(float coord) { bound_frame->setPosVec(glm::vec3(bound_frame->getPosition().x, bound_frame->getPosition().y, coord)); }    
 
-    void keyboardMove(const std::unordered_set<std::string> &pressed_keys);
+    void keyboardMove(const std::unordered_set<std::string> &held_keys, 
+                      const std::unordered_set<std::string> &pressed_keys);
     void forward();
     void backward();
     void left();
@@ -50,6 +51,9 @@ public:
     void up();
     void down();
     void rotate(glm::vec3 rotational_difference);
+
+    void stepForward(float step_size);
+    void stepBackward(float step_size);
 };
 
 #endif
\ No newline at end of file
diff --git a/src/controls/src/control.cpp b/src/controls/src/control.cpp
index 71c295f..26ebd0b 100644
--- a/src/controls/src/control.cpp
+++ b/src/controls/src/control.cpp
@@ -15,20 +15,26 @@ void Controller::setup(float dt, frame_ptr frame)
     setStep(dt);
 }
 
-void Controller::keyboardMove(const std::unordered_set<std::string> &pressed_keys)
+void Controller::keyboardMove(const std::unordered_set<std::string> &held_keys, 
+                              const std::unordered_set<std::string> &pressed_keys)
 {
-    if (pressed_keys.contains("W"))
+    if (held_keys.contains("W"))
         forward();
-    if (pressed_keys.contains("S"))
+    if (held_keys.contains("S"))
         backward();
-    if (pressed_keys.contains("A"))
+    if (held_keys.contains("A"))
         left();
-    if (pressed_keys.contains("D"))
+    if (held_keys.contains("D"))
         right();
-    if (pressed_keys.contains("R"))
+    if (held_keys.contains("R"))
         up();
-    if (pressed_keys.contains("F"))
+    if (held_keys.contains("F"))
         down();
+
+    if (pressed_keys.contains("P"))
+        stepForward(1.0f);
+    if (pressed_keys.contains("O"))
+        stepBackward(1.0f);    
 }
 
 void Controller::forward()
@@ -60,3 +66,12 @@ void Controller::rotate(glm::vec3 rotational_difference)
 {
     bound_frame->rotateRotationQuat(rotational_difference);
 }
+
+void Controller::stepForward(float step_size)
+{
+    bound_frame->translatePosVec(step_size * getFrontVec());
+}
+void Controller::stepBackward(float step_size)
+{
+    bound_frame->translatePosVec(-step_size * getFrontVec());
+}
\ No newline at end of file
diff --git a/src/controls/src/control_hub.cpp b/src/controls/src/control_hub.cpp
index 3b240db..9c4d86e 100644
--- a/src/controls/src/control_hub.cpp
+++ b/src/controls/src/control_hub.cpp
@@ -36,13 +36,13 @@ void ControlHub::processInput(const scene_ptr &scene, const osi::Keyboard &keybo
         for (auto &controller : obj_controls)
         {
             controller->setup(timer.dt(), scene->getSelection()->getFrame());
-            controller->keyboardMove(keyboard.down());
+            controller->keyboardMove(keyboard.down(), keyboard.just_released());
         }
     }
     else
     {
         active_camera_type->setStep(timer.dt());
-        active_camera_type->keyboardMove(keyboard.down());
+        active_camera_type->keyboardMove(keyboard.down(), keyboard.just_pressed());
     }
 
     if (keyboard.just_pressed().contains("C"))
diff --git a/src/edit/include/edit/editor.hpp b/src/edit/include/edit/editor.hpp
index bdff623..4b7ce92 100644
--- a/src/edit/include/edit/editor.hpp
+++ b/src/edit/include/edit/editor.hpp
@@ -69,6 +69,7 @@ public:
     void setRunning(bool _editor_running) { editor_running = _editor_running; }
 
     state &getControlState() { return control_state; }
+    const Level &getLevel() { return build_level; }
 
     bool isObjectMoving() const { return object_moving; }
     void setObjectMoving(bool _object_moving) { object_moving = _object_moving; } 
@@ -78,6 +79,7 @@ public:
 
     void doObjectAction(scene_ptr scene, ctrlhub_ptr controls);
     void updateCamera(scene_ptr scene, ctrlhub_ptr controls);
+    void changeLevel(scene_ptr scene, ctrlhub_ptr controls);
 
     void selectObject(scene_ptr scene, ctrlhub_ptr controls);
     void rotateObject(scene_ptr scene, ctrlhub_ptr controls);
diff --git a/src/edit/include/edit/scene.hpp b/src/edit/include/edit/scene.hpp
index b806063..59695d4 100644
--- a/src/edit/include/edit/scene.hpp
+++ b/src/edit/include/edit/scene.hpp
@@ -68,7 +68,7 @@ public:
 
     void addCameraNode(float FOV, frame_ptr frame);
     void addCameraNode(float FOV, Frame frame);
-    void addCameraNode(float FOV, glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f));
+    void addCameraNode(float FOV, glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 rotation = glm::vec3(0.0f, 0.0f, 0.0f));
 
     void addMesh(node_ptr node, const std::string &file_name);
     void addMesh(node_ptr node, mesh_ptr mesh);
diff --git a/src/edit/src/editor.cpp b/src/edit/src/editor.cpp
index 44c7653..b79d270 100644
--- a/src/edit/src/editor.cpp
+++ b/src/edit/src/editor.cpp
@@ -31,10 +31,10 @@ void Editor::updateCamera(scene_ptr scene, ctrlhub_ptr controls)
         && !keyboard.just_pressed().contains("L") )
         return;
 
-    std::array<float, 3> levels = build_level.getAll();
-    std::array<glm::vec3, 3> rotations = { glm::vec3(0.0f, 90.0f, 0.0f), // L(x-depth)
-                                           glm::vec3(-89.0f, 0.0f, 0.0f), // I (y-depth) // FIX - should be -90.0f
-                                           glm::vec3(0.0f, 00.0f, 0.0f)  }; // K (z-depth)
+    std::array<float, 3> cam_levels = build_level.getAll();
+    std::array<glm::vec3, 3> rotations = { glm::vec3(0.0f, -90.0f, 0.0f), // L(x-depth)
+                                           glm::vec3(-90.0f, 0.0f, 0.0f), // I (y-depth) // FIX - should be -90.0f
+                                           glm::vec3(0.0f, 180.0f, 0.0f)  }; // K (z-depth)
     std::uint8_t new_active = 0;
 
     if (keyboard.just_pressed().contains("I"))
@@ -46,10 +46,18 @@ void Editor::updateCamera(scene_ptr scene, ctrlhub_ptr controls)
     if (keyboard.just_pressed().contains("L"))
         new_active = 0;
 
-    levels[new_active] += 5.0f;
+    cam_levels[new_active] += new_active == 1 ? 5.0f : -5.0f; // +5 for up-down, else -5 
     build_level.setActiveIndex(new_active);
     controls->getActiveCamCtrl()->setRotation(rotations[new_active]);
-    controls->getActiveCamCtrl()->setPos(levels[0], levels[1], levels[2]);
+    controls->getActiveCamCtrl()->setPos(cam_levels[0], cam_levels[1], cam_levels[2]);
+}
+void Editor::changeLevel(scene_ptr scene, ctrlhub_ptr controls)
+{
+    if (keyboard.just_pressed().contains("P"))
+        ++build_level;
+        
+    if (keyboard.just_pressed().contains("O"))
+        --build_level;
 }
 void Editor::selectObject(scene_ptr scene, ctrlhub_ptr controls)
 {
diff --git a/src/edit/src/scene.cpp b/src/edit/src/scene.cpp
index d22c1ce..be53543 100644
--- a/src/edit/src/scene.cpp
+++ b/src/edit/src/scene.cpp
@@ -75,7 +75,7 @@ void Scene::createScene()
     // addMeshNode("./data/models/final_giraffe.obj", glm::vec3(-5, 1, 3));
 
     /* ADD CAMERA */
-    addCameraNode(60.0f, glm::vec3(0.0f, 1.0f, 6.0f));
+    addCameraNode(60.0f, glm::vec3(0.0f, 1.0f, -6.0f), glm::vec3(0.0f, 180.0f, 0.0f));
 }
 
 void Scene::removeActiveCamera(camera_ptr camera) // TO DO: fix
@@ -164,9 +164,9 @@ void Scene::addCameraNode(float FOV, Frame frame)
     scene.back()->addObject(std::make_shared<Camera>(FOV, scene.back()->getFrame()));
 }
 
-void Scene::addCameraNode(float FOV, glm::vec3 position)
+void Scene::addCameraNode(float FOV, glm::vec3 position, glm::vec3 rotation)
 {
-    scene.emplace_back(std::make_shared<Node>(std::make_shared<Frame>(position)));
+    scene.emplace_back(std::make_shared<Node>(std::make_shared<Frame>(position, rotation)));
     scene.back()->addObject(std::make_shared<Camera>(FOV, scene.back()->getFrame()));
 }
 
diff --git a/src/gfx/include/gfx/render.hpp b/src/gfx/include/gfx/render.hpp
index 7276519..45842c7 100644
--- a/src/gfx/include/gfx/render.hpp
+++ b/src/gfx/include/gfx/render.hpp
@@ -3,6 +3,7 @@
 
 #include <common/frame.hpp>
 #include <common/node.hpp>
+#include <edit/editor.hpp>
 #include <gfx/camera.hpp>
 #include <gfx/light.hpp>
 #include <gfx/mesh.hpp>
@@ -28,6 +29,7 @@ glm::mat4 get_projection_matrix(camera_ptr camera);
 void gfx_draw(std::map<std::string, shader_ptr> &my_shaders, 
               std::vector<light_ptr> lights,
               camera_ptr camera,
-              const std::vector<node_ptr> &scene);
+              const std::vector<node_ptr> &scene,
+              const edit::Level &build_level);
 
 #endif
\ No newline at end of file
diff --git a/src/gfx/src/render.cpp b/src/gfx/src/render.cpp
index 4d808eb..2825e39 100644
--- a/src/gfx/src/render.cpp
+++ b/src/gfx/src/render.cpp
@@ -81,10 +81,18 @@ glm::mat4 get_projection_matrix(camera_ptr camera)
                        100.0f);
 }
 
+bool node_in_active(camera_ptr camera, frame_ptr frame, const edit::Level &build_level) // will be more complicated in the future
+{
+    if (camera->isPerspective())
+        return true;
+    return frame->getPosition()[build_level.getActiveIndex()] == build_level.getActive();
+}
+
 void draw_objects(std::map<std::string, shader_ptr> &my_shaders, std::vector<light_ptr> lights,
                   camera_ptr camera,
                   const std::vector<node_ptr> &scene,
-                  glm::mat4 &view, glm::mat4 &projection)
+                  glm::mat4 &view, glm::mat4 &projection,
+                  const edit::Level &build_level)
 {
     for (auto &node : scene)
     {
@@ -115,21 +123,24 @@ void draw_objects(std::map<std::string, shader_ptr> &my_shaders, std::vector<lig
 
             obj->getVAO()->bind();
 
-            glDrawElements(obj->getDrawMode(),
+            glPolygonMode(GL_FRONT_AND_BACK, node_in_active(camera, node->getFrame(), build_level) ? GL_FILL : GL_LINE);
+
+            glDrawElements(obj->getDrawMode() ,
                         static_cast<GLsizei>(obj->getIndices().size()), 
                         GL_UNSIGNED_INT, 
                         0);
             ASSUMPTION(glGetError() == GL_NO_ERROR);
         }
 
-        draw_objects(my_shaders, lights, camera, node->getChildren(), view, projection);
+        draw_objects(my_shaders, lights, camera, node->getChildren(), view, projection, build_level);
     }
 }
 
 void gfx_draw(std::map<std::string, shader_ptr> &my_shaders, 
               std::vector<light_ptr> lights,
               camera_ptr camera,
-              const std::vector<node_ptr> &scene)
+              const std::vector<node_ptr> &scene,
+              const edit::Level &build_level)
 {
     glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
@@ -160,5 +171,5 @@ void gfx_draw(std::map<std::string, shader_ptr> &my_shaders,
     ASSUMPTION(glGetError() == GL_NO_ERROR);
 
     /* DRAW OBJECTS */
-    draw_objects(my_shaders, lights, camera, scene, view, projection);
+    draw_objects(my_shaders, lights, camera, scene, view, projection, build_level);
 }
diff --git a/src/studio/src/simulator.cpp b/src/studio/src/simulator.cpp
index e6e3d1b..49a338c 100644
--- a/src/studio/src/simulator.cpp
+++ b/src/studio/src/simulator.cpp
@@ -39,6 +39,10 @@ void Simulator::update()
 
     editor->updateCamera(scene, controls);
     editor->doObjectAction(scene, controls);
+    editor->changeLevel(scene, controls);
+
+    auto cam_frame = scene->getActiveCameras()[0].lock()->getFrame()->getPosition();
+    std::cout << "Camera coords: X " << cam_frame.x << " | Y " << cam_frame.y << " | Z " << cam_frame.z << std::endl;
 
     /* Rotation around x-axis demo */
     // controls->rotate_obj(glm::vec3(1.0f, 0.0f, 0.0f));
@@ -57,7 +61,7 @@ void Simulator::present()
         {
             active_cam.lock()->setWindowSize(window().size());
         }
-        gfx_draw(my_shaders, scene->getLights(), active_cam.lock(), scene->getScene());
+        gfx_draw(my_shaders, scene->getLights(), active_cam.lock(), scene->getScene(), editor->getLevel());
     }
     
 }
-- 
GitLab