From 8dd67e5ca965b458cd5028a623626355340621a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Wed, 7 Jan 2026 19:52:30 +0100 Subject: [PATCH] feat: triangle is perfectly draw --- CMakeLists.txt | 2 +- res/{inc => render/primitives}/p_cube.hpp | 8 +- res/render/primitives/p_triangle.frag | 8 ++ res/render/primitives/p_triangle.hpp | 37 ++++++ res/render/primitives/p_triangle.vert | 7 ++ src/core/camera.cpp | 3 +- src/core/texture.cpp | 2 +- src/cube.cpp | 101 +++++++-------- src/game.cpp | 143 ++++++++++------------ src/main.cpp | 102 ++++++++++++++- 10 files changed, 277 insertions(+), 136 deletions(-) rename res/{inc => render/primitives}/p_cube.hpp (93%) create mode 100644 res/render/primitives/p_triangle.frag create mode 100644 res/render/primitives/p_triangle.hpp create mode 100644 res/render/primitives/p_triangle.vert diff --git a/CMakeLists.txt b/CMakeLists.txt index 45c7ee8..5c42ed4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,7 @@ add_dependencies(main Shaders) target_include_directories(main PRIVATE inc # project - res/inc # ressources (primitive) + res/render # ressources lib/glad/include # glad lib/glfw/include # glfw lib/glm # glm diff --git a/res/inc/p_cube.hpp b/res/render/primitives/p_cube.hpp similarity index 93% rename from res/inc/p_cube.hpp rename to res/render/primitives/p_cube.hpp index 73abc18..45aec3a 100644 --- a/res/inc/p_cube.hpp +++ b/res/render/primitives/p_cube.hpp @@ -76,10 +76,10 @@ constexpr const std::size_t P_CUBE_INDICE_LEN = 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; + extern const unsigned char P_CUBE_VERT[]; + extern const unsigned int P_CUBE_VERT_LEN; + extern const unsigned char P_CUBE_FRAG[]; + extern const unsigned int P_CUBE_FRAG_LEN; } } // namespace mesh diff --git a/res/render/primitives/p_triangle.frag b/res/render/primitives/p_triangle.frag new file mode 100644 index 0000000..bb8afd2 --- /dev/null +++ b/res/render/primitives/p_triangle.frag @@ -0,0 +1,8 @@ +#version 330 core + +out vec4 color; + +void main() +{ + color = vec4(1.0f, 0.5f, 0.2f, 1.0f); +} \ No newline at end of file diff --git a/res/render/primitives/p_triangle.hpp b/res/render/primitives/p_triangle.hpp new file mode 100644 index 0000000..6ea374b --- /dev/null +++ b/res/render/primitives/p_triangle.hpp @@ -0,0 +1,37 @@ +#ifndef P_TRIANGLE_HPP +#define P_TRIANGLE_HPP + +#include + +#include "GLFW/glfw3.h" + +namespace mesh +{ +// clang-format off + constexpr GLfloat P_TRIANGLE_VERTICE[] = { + -0.5f, -0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + 0.0f, 0.5f, 0.0f + }; +// clang-format on + +constexpr const std::size_t P_TRIANGLE_VERTICE_LEN = + sizeof(P_TRIANGLE_VERTICE) / sizeof(GLfloat); + +constexpr unsigned int P_TRIANGLE_INDICE[] = { + 0, 1, 2, // 1 +}; + +constexpr const std::size_t P_TRIANGLE_INDICE_LEN = + sizeof(P_TRIANGLE_VERTICE) / sizeof(unsigned int); + +extern "C" +{ + extern const unsigned char P_TRIANGLE_VERT[]; + extern const unsigned int P_TRIANGLE_VERT_LEN; + extern const unsigned char P_TRIANGLE_FRAG[]; + extern const unsigned int P_TRIANGLE_FRAG_LEN; +} +} // namespace mesh + +#endif \ No newline at end of file diff --git a/res/render/primitives/p_triangle.vert b/res/render/primitives/p_triangle.vert new file mode 100644 index 0000000..63322c6 --- /dev/null +++ b/res/render/primitives/p_triangle.vert @@ -0,0 +1,7 @@ +#version 330 core + +layout (location = 0) in vec3 aPos; +void main() +{ + gl_Position = vec4(aPos , 1.0); +} \ No newline at end of file diff --git a/src/core/camera.cpp b/src/core/camera.cpp index a68c32f..2cfaeb9 100755 --- a/src/core/camera.cpp +++ b/src/core/camera.cpp @@ -1,6 +1,5 @@ -#include "camera.hpp" +#include "core/camera.hpp" -#include "glad/glad.h" #include "GLFW/glfw3.h" #include "glm/ext/matrix_transform.hpp" diff --git a/src/core/texture.cpp b/src/core/texture.cpp index 4ceb7ec..bccd357 100755 --- a/src/core/texture.cpp +++ b/src/core/texture.cpp @@ -1,4 +1,4 @@ -#include "texture.hpp" +#include "core/texture.hpp" #include #include diff --git a/src/cube.cpp b/src/cube.cpp index 6767fab..7b26ce2 100644 --- a/src/cube.cpp +++ b/src/cube.cpp @@ -1,64 +1,65 @@ -#include "res/inc/cube.hpp" +// #include "primitives/cube.hpp" -#include "ebo.hpp" -#include "glm/ext/matrix_clip_space.hpp" -#include "glm/ext/matrix_transform.hpp" -#include "glm/gtc/type_ptr.hpp" -#include "p_cube.hpp" -#include "vao.hpp" -#include "vbo.hpp" +// #include "core/vbo.hpp" +// #include "ebo.hpp" +// #include "glm/ext/matrix_clip_space.hpp" +// #include "glm/ext/matrix_transform.hpp" +// #include "glm/gtc/type_ptr.hpp" +// #include "p_cube.hpp" +// #include "vao.hpp" +// #include "vbo.hpp" -static GLsizei stride = 8 * sizeof(float); +// static GLsizei stride = 8 * sizeof(float); -Cube::Cube(Camera &camera, glm::vec3 pos, std::string texture) - : Shape(camera, pos, Shader{}, Texture{texture}) -{ - this->vao.bind(); - this->vbo.bind(); - this->ebo.bind(); +// Cube::Cube(Camera &camera, glm::vec3 pos, std::string texture) +// : Shape(camera, pos, Shader{}, Texture{texture}) +// { +// this->vao.bind(); +// this->vbo.bind(); +// this->ebo.bind(); - this->vbo.setData(P_CUBE_VERTICE, sizeof(P_CUBE_VERTICE)); +// this->vbo.setData(P_CUBE_VERTICE, sizeof(P_CUBE_VERTICE)); - // positions - this->vao.setAttributePointer(0, 3, GL_FLOAT, stride, (void *)(0)); - // normales - this->vao.setAttributePointer(1, 3, GL_FLOAT, stride, - (void *)(3 * sizeof(float))); - // texture - this->vao.setAttributePointer(2, 2, GL_FLOAT, stride, - (void *)(6 * sizeof(float))); +// // positions +// this->vao.setAttributePointer(0, 3, GL_FLOAT, stride, (void *)(0)); +// // normales +// this->vao.setAttributePointer(1, 3, GL_FLOAT, stride, +// (void *)(3 * sizeof(float))); +// // texture +// this->vao.setAttributePointer(2, 2, GL_FLOAT, stride, +// (void *)(6 * sizeof(float))); - this->ebo.setData(P_CUBE_INDICE, sizeof(P_CUBE_INDICE)); +// this->ebo.setData(P_CUBE_INDICE, sizeof(P_CUBE_INDICE)); - this->shader.compile((char *)P_CUBE_VERT, (char *)P_CUBE_FRAG); -} +// this->shader.compile((char *)P_CUBE_VERT, (char *)P_CUBE_FRAG); +// } -void Cube::render(int width, int height) -{ - shader.use(); - glActiveTexture(GL_TEXTURE0); +// void Cube::render(int width, int height) +// { +// shader.use(); +// glActiveTexture(GL_TEXTURE0); - glm::vec3 coordinate = glm::vec3(0.0f, 0.0f, -1.0f); - glm::mat4 projection = glm::perspective( - glm::radians(this->camera.getFov()), - static_cast(width) / static_cast(height), 0.1f, 100.0f); +// glm::vec3 coordinate = glm::vec3(0.0f, 0.0f, -1.0f); +// glm::mat4 projection = glm::perspective( +// glm::radians(this->camera.getFov()), +// static_cast(width) / static_cast(height), 0.1f, 100.0f); - GLint texLoc = - glGetUniformLocation(shader.getShaderProgramID(), "material.diffuse"); - glUniform1i(texLoc, 0); +// GLint texLoc = +// glGetUniformLocation(shader.getShaderProgramID(), "material.diffuse"); +// glUniform1i(texLoc, 0); - GLint projectionLoc = - glGetUniformLocation(shader.getShaderProgramID(), "projection"); - glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); +// GLint projectionLoc = +// glGetUniformLocation(shader.getShaderProgramID(), "projection"); +// glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); - GLint viewLoc = glGetUniformLocation(shader.getShaderProgramID(), "view"); - glUniformMatrix4fv(viewLoc, 1, GL_FALSE, - glm::value_ptr(camera.getViewMatrix())); +// GLint viewLoc = glGetUniformLocation(shader.getShaderProgramID(), "view"); +// glUniformMatrix4fv(viewLoc, 1, GL_FALSE, +// glm::value_ptr(camera.getViewMatrix())); - glm::mat4 model = glm::translate(glm::mat4(1.0f), coordinate); - GLint modelLoc = glGetUniformLocation(shader.getShaderProgramID(), "model"); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); +// glm::mat4 model = glm::translate(glm::mat4(1.0f), coordinate); +// GLint modelLoc = glGetUniformLocation(shader.getShaderProgramID(), "model"); +// glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); - vao.drawElement(GL_TRIANGLES, sizeof(P_CUBE_INDICE) / sizeof(unsigned int), - GL_UNSIGNED_INT, 0); -} \ No newline at end of file +// vao.drawElement(GL_TRIANGLES, sizeof(P_CUBE_INDICE) / sizeof(unsigned int), +// GL_UNSIGNED_INT, 0); +// } \ No newline at end of file diff --git a/src/game.cpp b/src/game.cpp index a451f6c..8e4db3b 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,90 +1,83 @@ -#include "glad/glad.h" -#include "GLFW/glfw3.h" -#include "game.hpp" -#include -#include -#include -#include +// #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); }); +// 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); - } +// 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) +// // 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 +// #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); +// 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); - } +// if (window == NULL) +// { +// Game::error("GLFW", 0, "Failed to create GLFW window."); +// glfwTerminate(); +// std::exit(EXIT_FAILURE); +// } - glfwMakeContextCurrent(window); +// glfwMakeContextCurrent(window); - if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) - { - Game::error("GLAD", 0, "Failed to initialize GLAD."); - glfwDestroyWindow(window); - glfwTerminate(); - std::exit(EXIT_FAILURE); - } +// if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) +// { +// Game::error("GLAD", 0, "Failed to initialize GLAD."); +// glfwDestroyWindow(window); +// glfwTerminate(); +// std::exit(EXIT_FAILURE); +// } - glfwSwapInterval(1); // activate vsync +// glfwSwapInterval(1); // activate vsync - glEnable(GL_DEPTH_TEST); - glEnable(GL_MULTISAMPLE); -} +// 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); -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); - glClearColor(0.5f, 0.2f, 0.2f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +// glfwPollEvents(); +// glfwSwapBuffers(window); - glfwPollEvents(); - glfwSwapBuffers(window); +// GLenum error = glGetError(); +// if (error != GL_NO_ERROR) +// { +// Game::error("OpenGL", error, ""); +// std::exit(EXIT_FAILURE); +// } - GLenum error = glGetError(); - if (error != GL_NO_ERROR) - { - Game::error("OpenGL", error, ""); - std::exit(EXIT_FAILURE); - } - - time.update(); - } -} \ No newline at end of file +// time.update(); +// } +// } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index fe52cb4..cd2e90c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,105 @@ #include "core/core.hpp" -#include "core/logger.hpp" +#include "core/ebo.hpp" +#include "core/shader.hpp" +#include "primitives/p_triangle.hpp" int main() { - Game game{800, 600, "window"}; - game.run(); + // Game game{800, 600, "window"}; + // game.run(); + + Time time{}; + int width = 800; + int height = 600; + const char *WINDOW_NAME = "window"; + + glfwSetErrorCallback([](int error, const char *desc) + { core::log::error(desc, error); }); + + if (glfwInit() == GLFW_FALSE) + { + core::log::error("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 + + GLFWwindow *window = + glfwCreateWindow(width, height, WINDOW_NAME, nullptr, nullptr); + + if (window == NULL) + { + core::log::error("Failed to create GLFW window."); + glfwTerminate(); + std::exit(EXIT_FAILURE); + } + + glfwMakeContextCurrent(window); + + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) + { + core::log::error("Failed to initialize GLAD."); + glfwDestroyWindow(window); + glfwTerminate(); + std::exit(EXIT_FAILURE); + } + + glfwSwapInterval(1); // activate vsync + + // glEnable(GL_DEPTH_TEST); + // glEnable(GL_MULTISAMPLE); + + core::VAO vao{}; + core::VBO vbo{mesh::P_TRIANGLE_VERTICE, sizeof(mesh::P_TRIANGLE_VERTICE)}; + core::EBO ebo{mesh::P_TRIANGLE_INDICE, sizeof(mesh::P_TRIANGLE_INDICE)}; + core::Shader shader{(char *)mesh::P_TRIANGLE_VERT, + (char *)mesh::P_TRIANGLE_FRAG}; + + vao.bind(); + vbo.bind(); + ebo.bind(); + + vao.setAttributePointer(0, 3, GL_FLOAT, 3 * sizeof(GLfloat), (void *)(0)); + + 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); + + shader.use(); + + vao.drawElement(GL_TRIANGLES, mesh::P_TRIANGLE_INDICE_LEN, GL_UNSIGNED_INT, 0); + + glfwPollEvents(); + glfwSwapBuffers(window); + + GLenum error = glGetError(); + if (error != GL_NO_ERROR) + { + core::log::error("", error); + std::exit(EXIT_FAILURE); + } + + time.update(); + } + glfwDestroyWindow(window); + glfwTerminate(); + return 0; } \ No newline at end of file