From a7db0a1354aa81c4f98386fe99f4be65e3971200 Mon Sep 17 00:00:00 2001 From: Conni Bilham Date: Fri, 20 Oct 2023 13:17:51 +0100 Subject: [PATCH] Remake temp phys/collision engine Create and use Shader::link_shaders Use Shader::compile_shader within Shader::load_shader function Small cleaning and minor fixes in window.cpp --- CMakeLists.txt | 2 +- include/CollisionEngine.h | 7 ++- include/PhysicsEngine.h | 3 ++ include/Scene.h | 5 +- include/Shader.h | 20 ++++---- src/PhysicsEngine.cpp | 23 +++++++-- src/Shader.cpp | 102 +++++++++++++++++++++----------------- src/window.cpp | 39 +++++++-------- 8 files changed, 116 insertions(+), 85 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d0ed68b..818188a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.25) -project("2D-Engine") +project("3D-Engine") set(CMAKE_CXX_STANDARD 11) diff --git a/include/CollisionEngine.h b/include/CollisionEngine.h index 4451b6d..2baeb67 100644 --- a/include/CollisionEngine.h +++ b/include/CollisionEngine.h @@ -40,21 +40,24 @@ class BoxCollider { auto a = CollisionEngine::side(A, B, P); if(a != aUV) { logging::info("Side check for A to B failed"); + return false; } auto b = CollisionEngine::side(B, C, P); if(b != bUV) { logging::info("Side check for B to C failed"); + return false; } auto c = CollisionEngine::side(C, D, P); if(c != cUV) { logging::info("Side check for C to D failed"); + return false; } auto d = CollisionEngine::side(D, A, P); if(d != dUV) { logging::info("Side check for D to A failed"); + return false; } - - // TODO: Return a value + return true; } }; diff --git a/include/PhysicsEngine.h b/include/PhysicsEngine.h index 64d3a27..4ae5087 100644 --- a/include/PhysicsEngine.h +++ b/include/PhysicsEngine.h @@ -13,6 +13,9 @@ class PhysicsEngine { public: Entity_t* entity{}; + glm::vec3 velocity = glm::vec3(0.0f, 0.0f, 0.0f); + glm::vec3 acceleration = glm::vec3(0.0f, 0.0f, 0.0f); + PhysicsEngine(Entity_t* entity); void ApplyPhysics(double deltaTime); diff --git a/include/Scene.h b/include/Scene.h index 9d2915a..1ded631 100644 --- a/include/Scene.h +++ b/include/Scene.h @@ -8,11 +8,10 @@ #include "Camera.h" #include "DebugRendering/DebugEntry.h" #include "Entity_t.h" -#include "Shader.h" -#include "Camera.h" -#include "DebugRendering.h" #include "GLFW/glfw3.h" +#include "Shader.h" #include "Storage.h" +#include class Window; class Scene { diff --git a/include/Shader.h b/include/Shader.h index 8ef331d..7084b86 100644 --- a/include/Shader.h +++ b/include/Shader.h @@ -5,16 +5,19 @@ #ifndef INC_2D_ENGINE_SHADER_H #define INC_2D_ENGINE_SHADER_H -#include -#include #include "glad/glad.h" -#include "logging.h" #include "glm.hpp" +#include "logging.h" +#include +#include class Shader { private: - static unsigned int compile_shader(unsigned int type, const std::string& source); + static unsigned int compile_shader(unsigned int type, const std::string &source); + static unsigned int link_shaders(unsigned int fragment_id, unsigned int vertex_id); + static bool check_compile_status(unsigned int shader_id); + public: unsigned int id; @@ -24,16 +27,15 @@ class Shader { std::string fragment_path; std::string fragment_raw; - Shader(const std::string& vertex_path, - const std::string& fragment_path - ); + Shader(const std::string &vertex_path, + const std::string &fragment_path); ~Shader(); void reload(); - void load_shader(const std::string& vertex_path_r, const std::string& fragment_path_r); + void load_shader(const std::string &vertex_path_r, const std::string &fragment_path_r); void apply(glm::mat4 view = glm::mat4(0.0f), glm::mat4 projection = glm::mat4(0.0f)) const; }; -#endif //INC_2D_ENGINE_SHADER_H +#endif//INC_2D_ENGINE_SHADER_H diff --git a/src/PhysicsEngine.cpp b/src/PhysicsEngine.cpp index 0e8d41d..e364515 100644 --- a/src/PhysicsEngine.cpp +++ b/src/PhysicsEngine.cpp @@ -2,6 +2,8 @@ // Created by Conni Bilham on 24/07/2023. // +#define G -9.81 + #include "PhysicsEngine.h" PhysicsEngine::PhysicsEngine(Entity_t *entity) { @@ -9,9 +11,22 @@ PhysicsEngine::PhysicsEngine(Entity_t *entity) { } void PhysicsEngine::ApplyPhysics(double deltaTime) { -// std::cout << "PhysicsEngine::ApplyPhysics(Entity_t* entity) Called" << std::endl; +// logging::verbose("PhysicsEngine::ApplyPhysics()"); + + auto* acceleration = &this->entity->physics.engine->acceleration; + auto* velocity = &this->entity->physics.engine->velocity; + if(!this->entity->physics.enabled) + return; + + acceleration->y = G; + + // Hate this but glm::vec3 doesn't have operators setup correctly + velocity->x += acceleration->x * (float)deltaTime; + velocity->y += acceleration->y * (float)deltaTime; + velocity->z += acceleration->z * (float)deltaTime; - if(entity->physics.enabled) { - this->entity->position.y = this->entity->position.y - (9.81 * deltaTime); // Haha gravity - } + // Hate this but glm::vec3 doesn't have operators setup correctly + this->entity->position.x += velocity->x; + this->entity->position.y += velocity->y; + this->entity->position.z += velocity->z; } \ No newline at end of file diff --git a/src/Shader.cpp b/src/Shader.cpp index f12d843..bead376 100644 --- a/src/Shader.cpp +++ b/src/Shader.cpp @@ -44,50 +44,16 @@ void Shader::load_shader(const std::string &vertex_path_r, const std::string &fr this->fragment_raw = std::string((std::istreambuf_iterator(fragment_shader_file)), std::istreambuf_iterator()); - const char* vertex_raw = this->vertex_raw.c_str(); - const char* fragment_raw = this->fragment_raw.c_str(); - // Compile vertex shader - logging::verbose("Compiling vertex shader"); - unsigned int vertex_shader_id = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertex_shader_id, 1, &vertex_raw, nullptr); - glCompileShader(vertex_shader_id); - if(!check_compile_status(vertex_shader_id)) { - logging::error("ERROR::SHADER::VERTEX::COMPILATION_FAILED"); - std::cout << "Press enter to continue..." << std::endl; - std::cin.get(); - } - // Compile fragment shader - logging::verbose("Compiling fragment shader"); - unsigned int fragment_shader_id = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragment_shader_id, 1, &fragment_raw, nullptr); - glCompileShader(fragment_shader_id); - if(!check_compile_status(fragment_shader_id)) { - logging::error("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED"); - std::cout << "Press enter to continue..." << std::endl; - std::cin.get(); - } - // Link shaders - logging::verbose("Linking vertex and fragment shaders"); - this->id = glCreateProgram(); - glAttachShader(this->id, vertex_shader_id); - glAttachShader(this->id, fragment_shader_id); - glLinkProgram(this->id); + logging::debug("Compiling vertex shader"); + unsigned int vertex_shader_id = compile_shader(GL_VERTEX_SHADER, this->vertex_raw); - // Check for linking errors - int success; - char info_log[512]; - glGetProgramiv(this->id, GL_LINK_STATUS, &success); - if (!success) { - glGetProgramInfoLog(this->id, 512, nullptr, info_log); - logging::error("ERROR::SHADER::PROGRAM::LINKING_FAILED"); - std::cout << info_log << std::endl; - } - logging::debug("Shaders linked successfully"); + // Compile fragment shader + logging::debug("Compiling fragment shader"); + unsigned int fragment_shader_id = compile_shader(GL_FRAGMENT_SHADER, this->fragment_raw); - // Delete shaders - glDeleteShader(vertex_shader_id); - glDeleteShader(fragment_shader_id); + // Link shaders + this->id = Shader::link_shaders(fragment_shader_id, vertex_shader_id); } bool Shader::check_compile_status(unsigned int shader_id) { @@ -118,24 +84,68 @@ void Shader::apply(glm::mat4 view, glm::mat4 projection) const { int phaseID = glGetUniformLocation(this->id, "phase"); glUniform1f(phaseID, value + 1); - if(view != glm::mat4(0)) + if (view != glm::mat4(0)) glUniformMatrix4fv(glGetUniformLocation(this->id, "view"), 1, GL_FALSE, &view[0][0]); - if(projection != glm::mat4(0)) + if (projection != glm::mat4(0)) glUniformMatrix4fv(glGetUniformLocation(this->id, "projection"), 1, GL_FALSE, &projection[0][0]); } +std::string ShaderTypeRaw(unsigned int type) { + switch (type) { + case (GL_VERTEX_SHADER): + return "VERTEX"; + case (GL_FRAGMENT_SHADER): + return "FRAGMENT"; + default: + return "UNKNOWN_TYPE"; + } +} unsigned int Shader::compile_shader(unsigned int type, const std::string &source) { - logging::debug("Shader::compile_shader() - " + std::to_string(type) + " - " + source); + auto type_e = ShaderTypeRaw(type); + logging::verbose("Shader::compile_shader() - " + type_e); unsigned int shader_id = glCreateShader(type); - const char* CSource = source.c_str(); + const char *CSource = source.c_str(); glShaderSource(shader_id, 1, &CSource, nullptr); glCompileShader(shader_id); - check_compile_status(shader_id); + if (!check_compile_status(shader_id)) { + logging::error("ERROR::SHADER::" + type_e + "::COMPILATION_FAILED"); + + std::cout << "Press enter to continue..." << std::endl; + std::cin.get(); + } return shader_id; } +unsigned int Shader::link_shaders(unsigned int fragment_id, unsigned int vertex_id) { + // Link shaders + logging::verbose("Linking vertex and fragment shaders"); + auto id = glCreateProgram(); + glAttachShader(id, vertex_id); + glAttachShader(id, fragment_id); + glLinkProgram(id); + + // Check for linking errors + int success; + char info_log[512]; + glGetProgramiv(id, GL_LINK_STATUS, &success); + if (!success) { + glGetProgramInfoLog(id, 512, nullptr, info_log); + logging::error("ERROR::SHADER::PROGRAM::LINKING_FAILED"); + std::cout << info_log << std::endl; + + std::cout << "Press enter to continue..." << std::endl; + std::cin.get(); + } else + logging::debug("Shaders linked successfully"); + + // Delete shaders + glDeleteShader(vertex_id); + glDeleteShader(fragment_id); + return id; +} + void Shader::reload() { logging::debug("Shader::reload()"); glDeleteProgram(this->id); diff --git a/src/window.cpp b/src/window.cpp index c9e210d..6f110b3 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -2,9 +2,9 @@ // Created by Conni Bilham on 10/07/2023. // -#include -#include "Storage.h" #include "../include/Window.h" +#include "Storage.h" +#include void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { if (storage->loaded_scene) @@ -69,7 +69,7 @@ Window::Window(int width, int height, std::string title, int fullscreen, Camera Window::~Window() { logging::info("Destroying window"); - for (auto* scene : storage->scene_list) { + for (auto *scene: storage->scene_list) { delete scene; } @@ -104,7 +104,7 @@ static bool vsync = true; float fps_history[100]; static bool full = false; -static int fps_history_index = 0; +static int frame_number = 0; // timing float deltaTime = 0.0f; @@ -137,25 +137,25 @@ void Window::window_loop_callback(GLFWwindow *window) { glfwSwapBuffers(window); - if (!full && fps_history_index + 1 == 100) { + if (!full && frame_number + 1 == 100) { full = true; - logging::info("FPS history full"); + logging::verbose("FPS history full"); } - fps_history[fps_history_index] = 1.0f / deltaTime; - fps_history_index = (fps_history_index + 1) % 100; + fps_history[frame_number] = 1.0f / deltaTime; + frame_number = (frame_number + 1) % 100; const float fps = 30.0f; auto currentFrameDuation = glfwGetTime() - currentFrame; if (vsync) { glfwSwapInterval(1); if (currentFrameDuation < 1.0 / fps) { - std::this_thread::sleep_for(std::chrono::milliseconds((int)((1.0 / fps - currentFrameDuation) * 1000))); + std::this_thread::sleep_for(std::chrono::milliseconds((int) ((1.0 / fps - currentFrameDuation) * 1000))); } auto newFrameDuration = glfwGetTime() - currentFrame; // If lagging if (newFrameDuration > 1.0) { - logging::error("Lagging behind: " + std::to_string(1/newFrameDuration)); + logging::error("Lagging behind: " + std::to_string(1 / newFrameDuration)); } } else glfwSwapInterval(0); @@ -193,19 +193,18 @@ void Window::handle_input(GLFWwindow *window) { glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); toggle = !toggle; ready = false; - } - else if(glfwGetKey(window, GLFW_KEY_C) == GLFW_RELEASE) { + } else if (glfwGetKey(window, GLFW_KEY_C) == GLFW_RELEASE) { ready = true; } - if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) + if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera->HandleKeyboard(Camera::CameraMovement::FORWARD, deltaTime, sprinting); - if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) + if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) camera->HandleKeyboard(Camera::CameraMovement::BACKWARD, deltaTime); - if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) + if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) camera->HandleKeyboard(Camera::CameraMovement::LEFT, deltaTime); - if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) + if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) camera->HandleKeyboard(Camera::CameraMovement::RIGHT, deltaTime); @@ -233,7 +232,7 @@ void Window::handle_input(GLFWwindow *window) { last_click = current_time; } - if(glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) { + if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) { logging::info("Wireframe: " + std::to_string(wireframe)); if (wireframe) @@ -245,12 +244,12 @@ void Window::handle_input(GLFWwindow *window) { } // -/+ for FOV -// std::cout << "FOV: " << camera->fov << std::endl; - if(glfwGetKey(window, GLFW_KEY_EQUAL) == GLFW_PRESS) { + // std::cout << "FOV: " << camera->fov << std::endl; + if (glfwGetKey(window, GLFW_KEY_EQUAL) == GLFW_PRESS) { camera->HandleMouseScroll(10); last_click = current_time; } - if(glfwGetKey(window, GLFW_KEY_MINUS) == GLFW_PRESS) { + if (glfwGetKey(window, GLFW_KEY_MINUS) == GLFW_PRESS) { camera->HandleMouseScroll(-10); last_click = current_time; }