diff --git a/src/gfx/CMakeLists.txt b/src/gfx/CMakeLists.txt index 2da9e0a464acac01b17819fb4eaad303a3abdf69..f77f4f3769e0465669288bd3a4c43f9f08158631 100644 --- a/src/gfx/CMakeLists.txt +++ b/src/gfx/CMakeLists.txt @@ -35,13 +35,16 @@ add_library(${THIS_TARGET_NAME} ./src/obj_control.cpp ./include/gfx/light.hpp - #./include/gfx/light.cpp + #./src/light.cpp ./include/gfx/node.hpp - #./include/gfx/node.cpp + #./src/node.cpp ./include/gfx/objectbase.hpp - #./include/gfx/objectbase.cpp + #./src/objectbase.cpp + + ./include/gfx/aabb.hpp + ./src/aabb.cpp # ./include/osi/opengl.hpp # ./include/osi/gui.hpp diff --git a/src/gfx/include/gfx/aabb.hpp b/src/gfx/include/gfx/aabb.hpp new file mode 100644 index 0000000000000000000000000000000000000000..ac43f938930849979ecf92336ab21be9c6e41818 --- /dev/null +++ b/src/gfx/include/gfx/aabb.hpp @@ -0,0 +1,24 @@ +#ifndef AABB_INCLUDED +#define AABB_INCLUDED + +#include <glad/glad.h> +#include <glm/glm.hpp> +#include <vector> +#include <array> +#include <utility> + +class AABB +{ + std::pair<glm::vec3, glm::vec3> LH; + std::array<glm::vec3, 8> full_bbox; + + void find_LH(std::vector<float> const &_vertices); + void getFullAABB(); + +public: + AABB(std::vector<float> const &_vertices); + std::vector<float> getVertices() const; + std::vector<unsigned int> getIndices() const; +}; + +#endif \ No newline at end of file diff --git a/src/gfx/include/gfx/object.hpp b/src/gfx/include/gfx/object.hpp index 2c9c15bcec9d8668cf69e5bf6e86490e09d0e9b9..4bf06506e64eb1dfb5e801fc44680b078cf7c159 100644 --- a/src/gfx/include/gfx/object.hpp +++ b/src/gfx/include/gfx/object.hpp @@ -5,21 +5,28 @@ #include <glm/glm.hpp> #include <vector> #include <memory> - +#include <utility> #include <gfx/objectbase.hpp> #include <gfx/vao.hpp> #include <gfx/vbo.hpp> #include <gfx/ebo.hpp> +#include <gfx/aabb.hpp> using VAO_ptr = std::shared_ptr<VAO>; class Object : public ObjectBase { VAO_ptr _VAO = std::make_shared<VAO>(); - glm::vec3 color; std::string shader_type; + int draw_mode = GL_TRIANGLES; + AABB aabb; + + void SendDataToVAO(std::vector<float> const &_vertices, + std::vector<unsigned int> const &_indices, + std::vector<float> const &_normals); + public: Object(std::vector<float> const &_vertices, @@ -28,9 +35,7 @@ public: glm::vec3 _color = glm::vec3(1.0f, 0.5f, 0.31f), std::string _shader_type = "lit"); - void SendDataToVAO(std::vector<float> const &_vertices, - std::vector<unsigned int> const &_indices, - std::vector<float> const &_normals); + VAO* getVAO() { return _VAO.get(); } std::vector<float> const &getVertices() const { return _VAO->getVertices(); } void setVertices(std::vector<float> const &new_vertices) { _VAO->setVertices(new_vertices); } @@ -41,8 +46,6 @@ public: std::vector<float> const &getNormals() const { return _VAO->getNormals(); } void setNormals(std::vector<float> const &new_normals) { _VAO->setNormals(new_normals); } - VAO* getVAO() { return _VAO.get(); } - std::string getShaderType() const { return shader_type; } void setShaderType(std::string new_type) { shader_type = new_type; } @@ -51,6 +54,8 @@ public: int getDrawMode() const { return draw_mode; } void setDrawMode(int _draw_mode) { draw_mode = _draw_mode; } + + const AABB& getAABB() { return aabb; } }; #endif \ No newline at end of file diff --git a/src/gfx/src/aabb.cpp b/src/gfx/src/aabb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df3d185acbc7328fe08d0dd3cb854e78ff97af11 --- /dev/null +++ b/src/gfx/src/aabb.cpp @@ -0,0 +1,64 @@ +#include <gfx/aabb.hpp> + +AABB::AABB(std::vector<float> const &_vertices) +{ + find_LH(_vertices); + getFullAABB(); +} + +void AABB::find_LH(std::vector<float> const &_vertices) +{ + LH.first = glm::vec3(_vertices[0], _vertices[1], _vertices[2]); + LH.second = glm::vec3(_vertices[0], _vertices[1], _vertices[2]); + + for (std::size_t i = 3; i < _vertices.size() - 2; i += 3) + { + LH.first = glm::min(LH.first, + glm::vec3(_vertices[i], + _vertices[i + 1], + _vertices[i + 2])); + LH.second = glm::max(LH.second, + glm::vec3(_vertices[i], + _vertices[i + 1], + _vertices[i + 2])); + } +} + +void AABB::getFullAABB() +{ + full_bbox[0] = LH.first; + + full_bbox[1] = glm::vec3(LH.second.x, LH.first.y, LH.first.z); + full_bbox[2] = glm::vec3(LH.first.x, LH.second.y, LH.first.z); + full_bbox[3] = glm::vec3(LH.first.x, LH.first.y, LH.second.z); + + full_bbox[4] = glm::vec3(LH.second.x, LH.second.y, LH.first.z); + full_bbox[5] = glm::vec3(LH.second.x, LH.first.y, LH.second.z); + full_bbox[6] = glm::vec3(LH.first.x, LH.second.y, LH.second.z); + + full_bbox[7] = LH.second; +} + +std::vector<float> AABB::getVertices() const +{ + std::vector<float> vertices; + vertices.reserve(24); + for (auto &coord : full_bbox) + { + vertices.emplace_back(coord.x); + vertices.emplace_back(coord.y); + vertices.emplace_back(coord.z); + } + + return vertices; +} + +std::vector<unsigned int> AABB::getIndices() const +{ + return { 0, 1, 0, 2, 0, 3, + 7, 4, 7, 5, 7, 6, + 1, 4, 1, 5, + 2, 4, 2, 6, + 3, 5, 3, 6 + }; +} \ No newline at end of file diff --git a/src/gfx/src/cam_control.cpp b/src/gfx/src/cam_control.cpp index 21f3163f1f11643374cbafd69e37c55592db2e70..af644990efd0c497398e358e535f41c6124f2209 100644 --- a/src/gfx/src/cam_control.cpp +++ b/src/gfx/src/cam_control.cpp @@ -12,7 +12,7 @@ void CameraController::setFrontVec(float xoffset, float yoffset) glm::vec3 rotated_y = R * bound_frame->getRotationAxisY(); auto dprod = glm::dot(rotated_y, glm::vec3(0.0f, 1.0f, 0.0f)); - std::cout << "dot: " << dprod << std::endl; + // std::cout << "dot: " << dprod << std::endl; if (dprod > 0.0f) { diff --git a/src/gfx/src/object.cpp b/src/gfx/src/object.cpp index 29d9b116f8602e75c196295d930217a93b7407a7..011a8ed04cdb4363199d49cf2b7763d9b50496f0 100644 --- a/src/gfx/src/object.cpp +++ b/src/gfx/src/object.cpp @@ -6,7 +6,7 @@ Object::Object(std::vector<float> const &_vertices, std::vector<float> const &_normals, glm::vec3 _color, std::string _shader_type) - : color{_color}, shader_type{_shader_type} + : color{_color}, shader_type{_shader_type}, aabb{_vertices} { _VAO->Bind(); SendDataToVAO(_vertices, _indices, _normals); diff --git a/src/studio/include/studio/simulator.hpp b/src/studio/include/studio/simulator.hpp index 1bb0fdae35a5db8aaa6731b88d5f8c5473a22149..423b8ee113da6dd80a388809542cd18fe5f5e85b 100644 --- a/src/studio/include/studio/simulator.hpp +++ b/src/studio/include/studio/simulator.hpp @@ -38,6 +38,8 @@ using cam_control_ptr = std::shared_ptr<CameraController>; using light_ptr = std::shared_ptr<Light>; using shader_ptr = std::shared_ptr<Shader>; +enum class state { select, move, rotate }; + struct Simulator : public osi::Simulator { Simulator(); @@ -60,6 +62,8 @@ private: int object_ctrl_type = 0; int active_camera_type = 0; + state control_state = state::select; + std::vector<obj_control_ptr> obj_controls; std::vector<cam_control_ptr> cam_controls; diff --git a/src/studio/src/simulator.cpp b/src/studio/src/simulator.cpp index 6c78bdc5ce03efff801a8046f175548e8230196f..5c8e47f202d92e167871031640e989574322b2a4 100644 --- a/src/studio/src/simulator.cpp +++ b/src/studio/src/simulator.cpp @@ -175,15 +175,27 @@ void Simulator::showUI() ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse); ImGui::Text("Object Manipulation"); - ImGui::Button("Select"); + if (ImGui::Button("Select")) + { + control_state = state::select; + std::cout << "mode: SELECT" << std::endl; + } ImGui::SameLine(); ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 10); - ImGui::Button("Move"); + if (ImGui::Button("Move")) + { + control_state = state::move; + std::cout << "mode: MOVE" << std::endl; + } ImGui::SameLine(); ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 10); - ImGui::Button("Rotate"); + if (ImGui::Button("Rotate")) + { + control_state = state::rotate; + std::cout << "mode: ROTATE" << std::endl; + } ImGui::End(); } @@ -218,14 +230,19 @@ void Simulator::find_cameras(const std::vector<node_ptr> &scene) void Simulator::create_scene() { /* ADD DEFAULT CUBE */ + Object cube(lit_cube_vertices, lit_cube_indices, lit_cube_normals); scene.emplace_back(std::make_shared<Node>(std::make_shared<Frame>(Frame{}))); - scene.back()->addObject(std::make_shared<Object>(lit_cube_vertices, lit_cube_indices, lit_cube_normals)); + scene.back()->addObject(std::make_shared<Object>(std::move(cube))); // TO DO: No move constructor? + /* ADD CUBE AABB */ + Object cube_bbox(cube.getAABB().getVertices(), cube.getAABB().getIndices(), std::vector<float>{}, glm::vec3(0.5f, 1, 0.31f), "basic"); + cube_bbox.setDrawMode(GL_LINES); + scene.back()->addObject(std::make_shared<Object>(std::move(cube_bbox))); /* ADD LIGHT CUBE AND ITS LIGHT */ scene.emplace_back(std::make_shared<Node>(std::make_shared<Frame>(Frame{glm::vec3(1.2f, 2.0f, 2.0f)}))); scene.back()->addObject(std::make_shared<Object>(lit_cube_vertices, lit_cube_indices, - lit_cube_normals, + std::vector<float>{}, glm::vec3(1.0f, 1.0f, 1.0f), "basic")); scene.back()->getFrame()->setScale(glm::vec3(0.1f, 0.1f, 0.1f)); @@ -235,7 +252,7 @@ void Simulator::create_scene() scene.emplace_back(std::make_shared<Node>(std::make_shared<Frame>(glm::vec3(0, 0, 0)))); object_ptr grid = std::make_shared<Object>(generate_grid_vertices(100), generate_grid_indices(100), - std::vector<float>{0, -1, 0}, + std::vector<float>{}, glm::vec3(0.5f, 0.5f, 0.5f), "basic"); grid->setDrawMode(GL_LINES);