From 92e190c1a45c14763ed3c535b10ee287e84de1c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Tue, 6 Jan 2026 22:13:09 +0100 Subject: [PATCH] feat(p_cube.hpp): add vertice & indice length --- CMakeLists.txt | 4 +- res/inc/p_cube.hpp | 129 +++++++++++++++++++++---------------- src/{ => core}/camera.cpp | 0 src/{ => core}/ebo.cpp | 0 src/{ => core}/shader.cpp | 2 +- src/{ => core}/texture.cpp | 0 src/{ => core}/vao.cpp | 0 src/{ => core}/vbo.cpp | 0 src/cube.cpp | 2 +- src/game.cpp | 90 ++++++++++++++++++++++++++ src/main.cpp | 117 ++------------------------------- 11 files changed, 175 insertions(+), 169 deletions(-) rename src/{ => core}/camera.cpp (100%) rename src/{ => core}/ebo.cpp (100%) rename src/{ => core}/shader.cpp (95%) rename src/{ => core}/texture.cpp (100%) rename src/{ => core}/vao.cpp (100%) rename src/{ => core}/vbo.cpp (100%) create mode 100644 src/game.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 97ff0c4..45c7ee8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,12 +61,12 @@ add_dependencies(main Shaders) target_include_directories(main PRIVATE inc # project - res/render # ressources (primitive) + res/inc # ressources (primitive) lib/glad/include # glad lib/glfw/include # glfw lib/glm # glm lib/imgui # imgui - "lib/imgui/backends # imgui backends" + lib/imgui/backends # imgui backends lib/stb # stb ) diff --git a/res/inc/p_cube.hpp b/res/inc/p_cube.hpp index 643ac4d..73abc18 100644 --- a/res/inc/p_cube.hpp +++ b/res/inc/p_cube.hpp @@ -1,65 +1,86 @@ +#ifndef P_CUBE_HPP +#define P_CUBE_HPP + +#include + #include "GLFW/glfw3.h" -constexpr GLfloat P_CUBE_VERTICE[] = { - // Positions // normales // texture coordinate - // front side - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, +namespace mesh +{ +// clang-format off + constexpr GLfloat P_CUBE_VERTICE[] = { + // Positions // normales // texture coordinate + // front side + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, + + // back side + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, + + // left side + -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, + -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, + -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, + + // right side + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, + + // bottom side + -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, + -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, + + // up side + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, + }; +// clang-format on - // back side - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, - - // left side - -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, - -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, - -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, - -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, - - // right side - 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, - - // bottom side - -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, - 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, - -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, - - // up side - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, - 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, -}; +constexpr const std::size_t P_CUBE_VERTICE_LEN = + sizeof(P_CUBE_VERTICE) / sizeof(GLfloat); constexpr unsigned int P_CUBE_INDICE[] = { - 0, 1, 2, // 1 - 2, 3, 0, // 2 + 0, 1, 2, // 1 + 2, 3, 0, // 2 - 4, 5, 6, // 3 - 6, 7, 4, // 4 + 4, 5, 6, // 3 + 6, 7, 4, // 4 - 8, 9, 10, // 5 - 10, 11, 8, // 6 + 8, 9, 10, // 5 + 10, 11, 8, // 6 - 12, 13, 14, // 7 - 14, 15, 12, // 8 + 12, 13, 14, // 7 + 14, 15, 12, // 8 - 16, 17, 18, // 9 - 18, 19, 16, // 10 + 16, 17, 18, // 9 + 18, 19, 16, // 10 - 20, 21, 22, // 11 - 22, 23, 20 // 12 -}; + 20, 21, 22, // 11 + 22, 23, 20 // 12 +}; -extern "C" const unsigned char P_CUBE_FRAG[]; -extern "C" const unsigned int P_CUBE_FRAG_LEN; -extern "C" const unsigned char P_CUBE_VERT[]; -extern "C" const unsigned int P_CUBE_VERT_LEN; \ No newline at end of file +constexpr const std::size_t P_CUBE_INDICE_LEN = + sizeof(P_CUBE_VERTICE) / sizeof(unsigned int); + +extern "C" +{ + extern const unsigned char CUBE_VERT[]; + extern const unsigned int CUBE_VERT_LEN; + extern const unsigned char CUBE_FRAG[]; + extern const unsigned int CUBE_FRAG_LEN; +} +} // namespace mesh + +#endif \ No newline at end of file diff --git a/src/camera.cpp b/src/core/camera.cpp similarity index 100% rename from src/camera.cpp rename to src/core/camera.cpp diff --git a/src/ebo.cpp b/src/core/ebo.cpp similarity index 100% rename from src/ebo.cpp rename to src/core/ebo.cpp diff --git a/src/shader.cpp b/src/core/shader.cpp similarity index 95% rename from src/shader.cpp rename to src/core/shader.cpp index ebc6915..5ee3fa5 100755 --- a/src/shader.cpp +++ b/src/core/shader.cpp @@ -1,4 +1,4 @@ -#include "shader.hpp" +#include "core/shader.hpp" #include diff --git a/src/texture.cpp b/src/core/texture.cpp similarity index 100% rename from src/texture.cpp rename to src/core/texture.cpp diff --git a/src/vao.cpp b/src/core/vao.cpp similarity index 100% rename from src/vao.cpp rename to src/core/vao.cpp diff --git a/src/vbo.cpp b/src/core/vbo.cpp similarity index 100% rename from src/vbo.cpp rename to src/core/vbo.cpp diff --git a/src/cube.cpp b/src/cube.cpp index 2218834..6767fab 100644 --- a/src/cube.cpp +++ b/src/cube.cpp @@ -1,4 +1,4 @@ -#include "cube.hpp" +#include "res/inc/cube.hpp" #include "ebo.hpp" #include "glm/ext/matrix_clip_space.hpp" diff --git a/src/game.cpp b/src/game.cpp new file mode 100644 index 0000000..a451f6c --- /dev/null +++ b/src/game.cpp @@ -0,0 +1,90 @@ +#include "glad/glad.h" +#include "GLFW/glfw3.h" +#include "game.hpp" +#include +#include +#include +#include + +Game::Game(int width, int height, const char *window_name) + : width(width), height(height), WINDOW_NAME(window_name) +{ + glfwSetErrorCallback([](int error, const char *desc) + { Game::error("GLFW", error, desc); }); + + if (glfwInit() == GLFW_FALSE) + { + Game::error("GLFW", 0, "Failed to initialize GLFW."); + std::exit(EXIT_FAILURE); + } + + // OpenGL CORE 4.1 + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + // window properties + glfwWindowHint(GLFW_DEPTH_BITS, 24); // request a 24 bits depth buffer + glfwWindowHint(GLFW_STENCIL_BITS, 8); // request an 8 bits stencil buffer + glfwWindowHint(GLFW_SAMPLES, 4); // activate MSAA (x4) + +#ifdef __APPLE__ // disable deprecated functionalities on Apple devices + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); +#endif + + this->window = glfwCreateWindow(width, height, WINDOW_NAME, nullptr, nullptr); + + if (window == NULL) + { + Game::error("GLFW", 0, "Failed to create GLFW window."); + glfwTerminate(); + std::exit(EXIT_FAILURE); + } + + glfwMakeContextCurrent(window); + + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) + { + Game::error("GLAD", 0, "Failed to initialize GLAD."); + glfwDestroyWindow(window); + glfwTerminate(); + std::exit(EXIT_FAILURE); + } + + glfwSwapInterval(1); // activate vsync + + glEnable(GL_DEPTH_TEST); + glEnable(GL_MULTISAMPLE); +} + +void Game::error(const char *type, int errortype, const char *desc) +{ + std::cerr << type << "ERROR" + << ((errortype) ? std::format("({})", errortype) : "") << ": " + << desc << std::endl; +} + +void Game::run() +{ + time.start(); + while (!glfwWindowShouldClose(window)) + { + glfwGetFramebufferSize(window, &width, &height); + glfwGetWindowSize(window, &width, &height); + glViewport(0, 0, width, height); + + glClearColor(0.5f, 0.2f, 0.2f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glfwPollEvents(); + glfwSwapBuffers(window); + + GLenum error = glGetError(); + if (error != GL_NO_ERROR) + { + Game::error("OpenGL", error, ""); + std::exit(EXIT_FAILURE); + } + + time.update(); + } +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 73ff648..fe52cb4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,114 +1,9 @@ -#include -#include -#include +#include "core/core.hpp" +#include "core/logger.hpp" -#include "glad/glad.h" -#include "GLFW/glfw3.h" -// #include "imgui.h" -// #include "backends/imgui_impl_glfw.h" -// #include "backends/imgui_impl_opengl3.h" - -#include "camera.hpp" -#include "cube.hpp" - -constexpr const char *WINDOW_NAME = "window"; - -int width = 800; -int height = 600; - -int main(void) +int main() { - // define error callback - glfwSetErrorCallback( - [](int error, const char *desc) - { std::cerr << "GLFW ERROR (" << error << "): " << desc << std::endl; }); - - if (glfwInit() == GLFW_FALSE) - { - std::cerr << "GLFW ERROR: Failed to initialize glfw." << std::endl; - return EXIT_FAILURE; - } - - // OpenGL CORE 4.1 - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - // window properties - glfwWindowHint(GLFW_DEPTH_BITS, 24); // set depth buffer to 3 bytes (24 bits) - glfwWindowHint(GLFW_STENCIL_BITS, 8); // set stencil buffer to 1 byte (8 bits) - glfwWindowHint(GLFW_SAMPLES, 4); // activate MSAA (4 samples per pixels) -#ifdef __APPLE__ - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, - GL_TRUE); // disable deprecated functionalities -#endif - - GLFWwindow *window = - glfwCreateWindow(width, height, WINDOW_NAME, nullptr, nullptr); - - if (window == NULL) - { - std::cerr << "GLFW ERROR: Failed to create GLFW window." << std::endl; - glfwTerminate(); - return EXIT_FAILURE; - } - - glfwMakeContextCurrent(window); - - if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) - { - std::cerr << "GLAD ERROR: Failed to initialize GLAD" << std::endl; - glfwDestroyWindow(window); - glfwTerminate(); - return EXIT_FAILURE; - } - - glfwSwapInterval(1); - - // IMGUI_CHECKVERSION(); - // ImGui::CreateContext(); - // ImGuiIO &io = ImGui::GetIO(); - // (void)io; - - // ImGui::StyleColorsDark(); - - // ImGui_ImplGlfw_InitForOpenGL(window, true); - // ImGui_ImplOpenGL3_Init("#version 410"); - - Camera camera(width, height, window, 0.1f); - Cube cube{camera, glm::vec3{0.f, 0.f, 0.f}, "stone.png"}; - - glEnable(GL_DEPTH_TEST); - glEnable(GL_MULTISAMPLE); - - double deltaTime = 0.f; - double lastTime = glfwGetTime(); - while (!glfwWindowShouldClose(window)) - { - glfwGetFramebufferSize(window, &width, &height); - glViewport(0, 0, width, height); - - glClearColor(0.5f, 0.2f, 0.2f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - double currentTime = glfwGetTime(); - deltaTime = currentTime - lastTime; - lastTime = currentTime; - - camera.update(deltaTime); - cube.render(width, height); - - glfwPollEvents(); - glfwSwapBuffers(window); - - GLenum error = glGetError(); - if (error != GL_NO_ERROR) - { - std::cerr << "error:" << error << std::endl; - exit(1); - } - } - - glfwDestroyWindow(window); - glfwTerminate(); - return EXIT_SUCCESS; + Game game{800, 600, "window"}; + game.run(); + return 0; } \ No newline at end of file