From d08230a7c3dc5bf0d7a8486c1c7901d41a6638b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20=C5=A0toura=C4=8D?= <525032@mail.muni.cz> Date: Mon, 27 Mar 2023 23:17:18 +0200 Subject: [PATCH] incorporated 'hints' branch changes --- src/gfx/include/gfx/camera.hpp | 2 +- src/gfx/include/gfx/ebo.hpp | 3 +- src/gfx/include/gfx/object.hpp | 18 ++++---- src/gfx/include/gfx/render.hpp | 4 +- src/gfx/include/gfx/shader.hpp | 6 +-- src/gfx/include/gfx/vao.hpp | 3 +- src/gfx/include/gfx/vbo.hpp | 3 +- src/gfx/src/camera.cpp | 2 +- src/gfx/src/ebo.cpp | 18 ++++++-- src/gfx/src/object.cpp | 21 +++------- src/gfx/src/render.cpp | 18 ++++---- src/gfx/src/shader.cpp | 44 +++++++++++++++++-- src/gfx/src/vao.cpp | 18 ++++++-- src/gfx/src/vbo.cpp | 19 +++++++-- src/studio/src/simulator.cpp | 56 ++++++++++++++++++++----- src/utils/include/utils/assumptions.hpp | 2 +- 16 files changed, 170 insertions(+), 67 deletions(-) diff --git a/src/gfx/include/gfx/camera.hpp b/src/gfx/include/gfx/camera.hpp index 52b8b1a..a5db7fc 100644 --- a/src/gfx/include/gfx/camera.hpp +++ b/src/gfx/include/gfx/camera.hpp @@ -26,7 +26,7 @@ class Camera glm::u32vec2 window_size = glm::u32vec2{0, 0}; public: - Camera(glm::vec3 pos, glm::vec3 direction, float fov); + Camera(glm::vec3 const &pos, glm::vec3 const &direction, float fov); void forward(); void backward(); diff --git a/src/gfx/include/gfx/ebo.hpp b/src/gfx/include/gfx/ebo.hpp index 45dbb98..d366bb3 100644 --- a/src/gfx/include/gfx/ebo.hpp +++ b/src/gfx/include/gfx/ebo.hpp @@ -11,10 +11,11 @@ class EBO public: EBO(); + ~EBO(); void Bind(); void Unbind(); void Delete(); - void SendData(std::vector<unsigned int> &indices); //TO DO: remove size + void SendData(std::vector<unsigned int> const &indices); }; diff --git a/src/gfx/include/gfx/object.hpp b/src/gfx/include/gfx/object.hpp index 0332984..72f4ab0 100644 --- a/src/gfx/include/gfx/object.hpp +++ b/src/gfx/include/gfx/object.hpp @@ -22,18 +22,20 @@ class Object bool buffer_object(); public: - Object(glm::vec3 position, std::vector<float> &_vertices, std::vector<unsigned int> &_indices); + Object(glm::vec3 const &position, + std::vector<float> const &_vertices, + std::vector<unsigned int> const &_indices); - glm::vec3 getPosition(); - void setPosition(glm::vec3 new_pos); + glm::vec3 const &getPosition() const { return positionVec; } + void setPosition(glm::vec3 const &new_pos) { positionVec = new_pos; } - std::vector<float>& getVertices(); - void setVertices(std::vector<float> &vertices); + std::vector<float> const &getVertices() const { return vertices; } + void setVertices(std::vector<float> const &new_vertices) { vertices = new_vertices; } - std::vector<unsigned int>& getIndices(); - void setIndices(std::vector<unsigned int> &indices); + std::vector<unsigned int> const &getIndices() const { return indices; } + void setIndices(std::vector<unsigned int> const &new_indices) { indices = new_indices; } - VAO& getVAO(); + VAO &getVAO() { return _VAO; } }; #endif \ No newline at end of file diff --git a/src/gfx/include/gfx/render.hpp b/src/gfx/include/gfx/render.hpp index 8d788cf..1887a23 100644 --- a/src/gfx/include/gfx/render.hpp +++ b/src/gfx/include/gfx/render.hpp @@ -11,7 +11,7 @@ #include <iostream> #include <vector> -Shader create_shader_program(); -void gfx_draw(Shader &myShader, Camera camera, Object object); +void send_matrices_to_shader(Shader &myShader, glm::mat4 &model, glm::mat4 &view, glm::mat4 &projection); +void gfx_draw(Shader &myShader, Camera &camera, Object &object); #endif \ No newline at end of file diff --git a/src/gfx/include/gfx/shader.hpp b/src/gfx/include/gfx/shader.hpp index 655f257..2255d8f 100644 --- a/src/gfx/include/gfx/shader.hpp +++ b/src/gfx/include/gfx/shader.hpp @@ -12,11 +12,11 @@ class Shader unsigned int ID; // utility function for checking shader compilation/linking errors. // ------------------------------------------------------------------------ - void checkCompileErrors(unsigned int shader, std::string type); + void checkCompileErrors(unsigned int shader, std::string const& type); public: //constructor - Shader(std::filesystem::path vertexPath, std::filesystem::path fragmentPath); + Shader(std::filesystem::path const& vertexPath, std::filesystem::path const& fragmentPath); //shader activation void use(); @@ -26,7 +26,7 @@ public: void setInt(const std::string &name, int value) const; void setFloat(const std::string &name, float value) const; - unsigned int getID() {return ID;} + unsigned int getID() {return ID;} // Why do we need to expose ID??? ~Shader(); }; diff --git a/src/gfx/include/gfx/vao.hpp b/src/gfx/include/gfx/vao.hpp index 78c0ab1..78f21ff 100644 --- a/src/gfx/include/gfx/vao.hpp +++ b/src/gfx/include/gfx/vao.hpp @@ -11,11 +11,12 @@ class VAO public: VAO(); + ~VAO(); void Bind(); void Unbind(); void Delete(); - void LinkVBO(VBO &vbo); // , unsigned int layout + void LinkVBO(); // , unsigned int layout }; #endif \ No newline at end of file diff --git a/src/gfx/include/gfx/vbo.hpp b/src/gfx/include/gfx/vbo.hpp index 770904d..f132bfe 100644 --- a/src/gfx/include/gfx/vbo.hpp +++ b/src/gfx/include/gfx/vbo.hpp @@ -11,10 +11,11 @@ class VBO public: VBO(); + ~VBO(); void Bind(); void Unbind(); void Delete(); - void SendData(std::vector<float> &vertices); + void SendData(std::vector<float> const &vertices); }; diff --git a/src/gfx/src/camera.cpp b/src/gfx/src/camera.cpp index 703c754..7a86fe3 100644 --- a/src/gfx/src/camera.cpp +++ b/src/gfx/src/camera.cpp @@ -1,6 +1,6 @@ #include <gfx/camera.hpp> -Camera::Camera(glm::vec3 pos, glm::vec3 direction, float fov) : +Camera::Camera(glm::vec3 const &pos, glm::vec3 const &direction, float fov) : posVec{pos}, frontVec{direction}, FOV{fov} {} void Camera::forward() diff --git a/src/gfx/src/ebo.cpp b/src/gfx/src/ebo.cpp index da23ee0..1156c62 100644 --- a/src/gfx/src/ebo.cpp +++ b/src/gfx/src/ebo.cpp @@ -5,10 +5,17 @@ EBO::EBO() { glGenBuffers(1, &ID); ASSUMPTION(glGetError() == GL_NO_ERROR); + ASSUMPTION(ID != 0); +} + +EBO::~EBO() +{ + Delete(); } void EBO::Bind() { + ASSUMPTION(ID != 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ID); ASSUMPTION(glGetError() == GL_NO_ERROR); } @@ -21,12 +28,17 @@ void EBO::Unbind() void EBO::Delete() { - glDeleteBuffers(1, &ID); - ASSUMPTION(glGetError() == GL_NO_ERROR); + if (ID != 0) + { + glDeleteBuffers(1, &ID); + ASSUMPTION(glGetError() == GL_NO_ERROR); + ID = 0; + } } -void EBO::SendData(std::vector<unsigned int> &indices) +void EBO::SendData(std::vector<unsigned int> const &indices) { + ASSUMPTION(ID != 0); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices.front(), GL_STATIC_DRAW); ASSUMPTION(glGetError() == GL_NO_ERROR); } diff --git a/src/gfx/src/object.cpp b/src/gfx/src/object.cpp index 5f45afb..087f168 100644 --- a/src/gfx/src/object.cpp +++ b/src/gfx/src/object.cpp @@ -1,7 +1,7 @@ #include <gfx/object.hpp> #include <vector> -Object::Object(glm::vec3 position, std::vector<float> &_vertices, std::vector<unsigned int> &_indices) : +Object::Object(glm::vec3 const &position, std::vector<float> const &_vertices, std::vector<unsigned int> const &_indices) : positionVec{position}, vertices{_vertices}, indices{_indices} { buffer_object(); @@ -14,21 +14,10 @@ bool Object::buffer_object() _EBO.Bind(); _VBO.SendData(getVertices()); _EBO.SendData(getIndices()); - _VAO.LinkVBO(_VBO); + _VAO.LinkVBO(); // Either unbind all three or none, otherwise no cube - _VAO.Unbind(); - _VBO.Unbind(); - _EBO.Unbind(); + // _VAO.Unbind(); + // _VBO.Unbind(); + // _EBO.Unbind(); return true; } - -glm::vec3 Object::getPosition() { return positionVec; } -void Object::setPosition(glm::vec3 new_pos) { positionVec = new_pos; } - -std::vector<float>& Object::getVertices() { return vertices; } -void Object::setVertices(std::vector<float> &new_vertices) { vertices = new_vertices; } - -std::vector<unsigned int>& Object::getIndices() { return indices; } -void Object::setIndices(std::vector<unsigned int> &new_indices) { indices = new_indices; } - -VAO& Object::getVAO() { return _VAO; } \ No newline at end of file diff --git a/src/gfx/src/render.cpp b/src/gfx/src/render.cpp index fc6e810..f5b5d14 100644 --- a/src/gfx/src/render.cpp +++ b/src/gfx/src/render.cpp @@ -19,29 +19,29 @@ //#include <SDL2/sdl.h> #include <SDL2/SDL.h> -Shader create_shader_program() -{ - Shader myShader("./data/shaders/basicshader.vert", - "./data/shaders/basicshader.frag"); - return myShader; -} - void send_matrices_to_shader(Shader &myShader, glm::mat4 &model, glm::mat4 &view, glm::mat4 &projection) { int modelLoc = glGetUniformLocation(myShader.getID(), "model"); + ASSUMPTION(glGetError() == GL_NO_ERROR); glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); + ASSUMPTION(glGetError() == GL_NO_ERROR); int viewLoc = glGetUniformLocation(myShader.getID(), "view"); + ASSUMPTION(glGetError() == GL_NO_ERROR); glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); + ASSUMPTION(glGetError() == GL_NO_ERROR); int projectionLoc = glGetUniformLocation(myShader.getID(), "projection"); + ASSUMPTION(glGetError() == GL_NO_ERROR); glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); + ASSUMPTION(glGetError() == GL_NO_ERROR); } -void gfx_draw(Shader &myShader, Camera camera, Object object) +void gfx_draw(Shader &myShader, Camera &camera, Object &object) { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + ASSUMPTION(glGetError() == GL_NO_ERROR); myShader.use(); @@ -52,7 +52,7 @@ void gfx_draw(Shader &myShader, Camera camera, Object object) // glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform)); glm::mat4 model = glm::mat4(1.0f); - model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f)); + //model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f)); model = glm::translate(model, object.getPosition()); //model = glm::rotate(model, ((float)SDL_GetTicks() / 1000) * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f)); diff --git a/src/gfx/src/shader.cpp b/src/gfx/src/shader.cpp index 52df1bb..a19654e 100644 --- a/src/gfx/src/shader.cpp +++ b/src/gfx/src/shader.cpp @@ -1,4 +1,5 @@ #include <gfx/shader.hpp> +#include <utils/assumptions.hpp> #include <glad/glad.h> #include <string> @@ -8,8 +9,11 @@ //constructor -Shader::Shader(std::filesystem::path vertexPath, std::filesystem::path fragmentPath) +Shader::Shader(std::filesystem::path const& vertexPath, std::filesystem::path const& fragmentPath) { + ASSUMPTION(std::filesystem::is_regular_file(vertexPath)); + ASSUMPTION(std::filesystem::is_regular_file(fragmentPath)); + // 1. retrieve the vertex/fragment source code from filePath std::string vertexCode; std::string fragmentCode; @@ -46,75 +50,107 @@ Shader::Shader(std::filesystem::path vertexPath, std::filesystem::path fragmentP // vertex Shader vertex = glCreateShader(GL_VERTEX_SHADER); + ASSUMPTION(glGetError() == GL_NO_ERROR); + ASSUMPTION(vertex != 0); glShaderSource(vertex, 1, &vShaderCode, NULL); + ASSUMPTION(glGetError() == GL_NO_ERROR); glCompileShader(vertex); + ASSUMPTION(glGetError() == GL_NO_ERROR); // print compile errors if any checkCompileErrors(vertex, "VERTEX"); // fragment Shader fragment = glCreateShader(GL_FRAGMENT_SHADER); + ASSUMPTION(glGetError() == GL_NO_ERROR); + ASSUMPTION(fragment != 0); glShaderSource(fragment, 1, &fShaderCode, NULL); + ASSUMPTION(glGetError() == GL_NO_ERROR); glCompileShader(fragment); + ASSUMPTION(glGetError() == GL_NO_ERROR); checkCompileErrors(fragment, "FRAGMENT"); // shader Program ID = glCreateProgram(); + ASSUMPTION(glGetError() == GL_NO_ERROR); + ASSUMPTION(ID != 0); glAttachShader(ID, vertex); + ASSUMPTION(glGetError() == GL_NO_ERROR); glAttachShader(ID, fragment); + ASSUMPTION(glGetError() == GL_NO_ERROR); glBindFragDataLocation(ID, 3, "FragColor"); // NOT NECESSARY ? + ASSUMPTION(glGetError() == GL_NO_ERROR); glLinkProgram(ID); + ASSUMPTION(glGetError() == GL_NO_ERROR); // print linking errors if any - checkCompileErrors(ID, "SHADER PROGRAM"); + checkCompileErrors(ID, "PROGRAM"); // delete shaders; they’re linked into our program and no longer necessary glDeleteShader(vertex); + ASSUMPTION(glGetError() == GL_NO_ERROR); glDeleteShader(fragment); + ASSUMPTION(glGetError() == GL_NO_ERROR); } void Shader::use() { + ASSUMPTION(ID != 0); glUseProgram(ID); + ASSUMPTION(glGetError() == GL_NO_ERROR); } void Shader::setBool(const std::string &name, bool value) const { + ASSUMPTION(ID != 0); glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value); + ASSUMPTION(glGetError() == GL_NO_ERROR); } void Shader::setInt(const std::string &name, int value) const { + ASSUMPTION(ID != 0); glUniform1i(glGetUniformLocation(ID, name.c_str()), value); + ASSUMPTION(glGetError() == GL_NO_ERROR); } void Shader::setFloat(const std::string &name, float value) const { + ASSUMPTION(ID != 0); glUniform1f(glGetUniformLocation(ID, name.c_str()), value); + ASSUMPTION(glGetError() == GL_NO_ERROR); } Shader::~Shader() { - glDeleteProgram(ID); + if (ID != 0) + { + glDeleteProgram(ID); + ASSUMPTION(glGetError() == GL_NO_ERROR); + } } // utility function for checking shader compilation/linking errors. // ------------------------------------------------------------------------ -void Shader::checkCompileErrors(unsigned int shader, std::string type) +void Shader::checkCompileErrors(unsigned int shader, std::string const& type) { int success; char infoLog[1024]; if (type != "PROGRAM") { glGetShaderiv(shader, GL_COMPILE_STATUS, &success); + ASSUMPTION(glGetError() == GL_NO_ERROR); if (!success) { glGetShaderInfoLog(shader, 1024, NULL, infoLog); + ASSUMPTION(glGetError() == GL_NO_ERROR); std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl; } } else { glGetProgramiv(shader, GL_LINK_STATUS, &success); + ASSUMPTION(glGetError() == GL_NO_ERROR); if (!success) { glGetProgramInfoLog(shader, 1024, NULL, infoLog); + ASSUMPTION(glGetError() == GL_NO_ERROR); std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl; } } diff --git a/src/gfx/src/vao.cpp b/src/gfx/src/vao.cpp index e07eb1b..ad0cc45 100644 --- a/src/gfx/src/vao.cpp +++ b/src/gfx/src/vao.cpp @@ -4,10 +4,17 @@ VAO::VAO() { glGenVertexArrays(1, &ID); ASSUMPTION(glGetError() == GL_NO_ERROR); + ASSUMPTION(ID != 0); } -void VAO::LinkVBO(VBO &vbo) // , unsigned int layout +VAO::~VAO() { + Delete(); +} + +void VAO::LinkVBO() // , unsigned int layout +{ + ASSUMPTION(ID != 0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); ASSUMPTION(glGetError() == GL_NO_ERROR); glEnableVertexAttribArray(0); // layout instead of 0s at the start @@ -16,6 +23,7 @@ void VAO::LinkVBO(VBO &vbo) // , unsigned int layout void VAO::Bind() { + ASSUMPTION(ID != 0); glBindVertexArray(ID); ASSUMPTION(glGetError() == GL_NO_ERROR); } @@ -28,6 +36,10 @@ void VAO::Unbind() void VAO::Delete() { - glDeleteVertexArrays(1, &ID); - ASSUMPTION(glGetError() == GL_NO_ERROR); + if (ID != 0) + { + glDeleteVertexArrays(1, &ID); + ASSUMPTION(glGetError() == GL_NO_ERROR); + ID = 0; + } } diff --git a/src/gfx/src/vbo.cpp b/src/gfx/src/vbo.cpp index 1f07736..3b98739 100644 --- a/src/gfx/src/vbo.cpp +++ b/src/gfx/src/vbo.cpp @@ -5,28 +5,41 @@ VBO::VBO() { glGenBuffers(1, &ID); ASSUMPTION(glGetError() == GL_NO_ERROR); + ASSUMPTION(ID != 0); +} + +VBO::~VBO() +{ + Delete(); } void VBO::Bind() { + ASSUMPTION(ID != 0); glBindBuffer(GL_ARRAY_BUFFER, ID); ASSUMPTION(glGetError() == GL_NO_ERROR); } void VBO::Unbind() { + ASSUMPTION(ID != 0); glBindBuffer(GL_ARRAY_BUFFER, 0); ASSUMPTION(glGetError() == GL_NO_ERROR); } void VBO::Delete() { - glDeleteBuffers(1, &ID); - ASSUMPTION(glGetError() == GL_NO_ERROR); + if (ID != 0) + { + glDeleteBuffers(1, &ID); + ASSUMPTION(glGetError() == GL_NO_ERROR); + ID = 0; + } } -void VBO::SendData(std::vector<float> &vertices) +void VBO::SendData(std::vector<float> const &vertices) { + ASSUMPTION(ID != 0); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices.front(), GL_STATIC_DRAW); ASSUMPTION(glGetError() == GL_NO_ERROR); } diff --git a/src/studio/src/simulator.cpp b/src/studio/src/simulator.cpp index 2e038dc..f946afa 100644 --- a/src/studio/src/simulator.cpp +++ b/src/studio/src/simulator.cpp @@ -1,6 +1,7 @@ #include <studio/simulator.hpp> #include <osi/opengl.hpp> #include <osi/gui.hpp> +#include <utils/assumptions.hpp> #include <glad/glad.h> #include <gfx/shader.hpp> @@ -54,20 +55,54 @@ float FOV = 60.0f; Simulator::Simulator() : osi::Simulator() // camera frame, object frame, camera, object - , myShader{create_shader_program()} - , camera{cam_position, cam_direction, FOV} - , object{obj_position, obj_vertices, obj_indices} + , myShader{"./data/shaders/basicshader.vert", + "./data/shaders/basicshader.frag"} + , camera{{0.0f, 0.0f, 3.0f}, {0.0f, 1.0f, 0.0f}, 60.0f} + , object{{ 0.0f, 0.0f, 0.0f }, + { + 0, 0, 0, 1, -0.2f, 0, 1, 0.2f, 0, + 0, 0, 0, -0.2f, 2, 0, 0.2f, 2, 0, + 0, 0, 0, -0.2f, 0, 3, 0.2f, 0, 3, + }, + { + 0, 1, 2, + 3, 4, 5, + 6, 7, 8 + } + // , object{{ 1.0f, -1.0f, 0.0f }, + // { + // // positions + // //front + // 0.2f, 0.2f, 0.2f, // top right + // 0.2f, -0.2f, 0.2f, // bottom right + // -0.2f, -0.2f, 0.2f, // bottom left + // -0.2f, 0.2f, 0.2f, // top left + // //back + // 0.2f, 0.2f, -0.2f, // top right + // 0.2f, -0.2f, -0.2f, // bottom right + // -0.2f, -0.2f, -0.2f, // bottom left + // -0.2f, 0.2f, -0.2f // top left + // }, + // { + // // front + // 0, 1, 3, 1, 2, 3, + // // back + // 4, 5, 7, 5, 6, 7, + // // right + // 0, 1, 4, 1, 4, 5, + // // left + // 2, 3, 7, 2, 6, 7, + // // top + // 0, 3, 4, 3, 4, 7, + // // bottom + // 1, 2, 5, 2, 5, 6 + // } + } { camera.setWindowSize(window().size()); } -Simulator::~Simulator() -{ - // TO DO: in one function - myVAO.Delete(); - myVBO.Delete(); - myEBO.Delete(); -} +Simulator::~Simulator() {} void Simulator::update() { @@ -117,6 +152,7 @@ void Simulator::process_input() int fill_mode = keyboard().down().contains("X") ? GL_FILL : GL_LINE; // TO DO: change glPolygonMode(GL_FRONT_AND_BACK, fill_mode); + ASSUMPTION(glGetError() == GL_NO_ERROR); } void Simulator::process_mouse() diff --git a/src/utils/include/utils/assumptions.hpp b/src/utils/include/utils/assumptions.hpp index 721c783..1be4785 100644 --- a/src/utils/include/utils/assumptions.hpp +++ b/src/utils/include/utils/assumptions.hpp @@ -13,7 +13,7 @@ explicit assumption_failure(std::string const& msg) : std::logic_error(msg) {} }; # define ASSUMPTION(C) do { if (!(C)) { LOG(LSL_ERROR,"Assumption failure.");\ - [](){ throw assumption_failure(FAIL_MSG("Assumption failure.")); }; }\ + [](){ throw assumption_failure(FAIL_MSG("Assumption failure.")); }(); }\ } while (false) # else # define ASSUMPTION(C) -- GitLab