From 7f70d00e4c0ec6faf48665f6546d29d9928b7c11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Tue, 30 Dec 2025 14:27:11 +0100 Subject: [PATCH] feat: add a few improvements of cube logic --- inc/cube.hpp | 24 +--- inc/ebo.hpp | 2 +- inc/shape.hpp | 5 +- inc/vbo.hpp | 2 +- res/render/primitives/cube.hpp | 8 +- src/cube.cpp | 212 +++++++++------------------------ src/ebo.cpp | 2 +- src/main.cpp | 5 +- src/vbo.cpp | 2 +- 9 files changed, 77 insertions(+), 185 deletions(-) diff --git a/inc/cube.hpp b/inc/cube.hpp index ecfa5e5..0d1b172 100644 --- a/inc/cube.hpp +++ b/inc/cube.hpp @@ -1,29 +1,15 @@ #ifndef CUBE_HPP #define CUBE_HPP -#include - #include "camera.hpp" -#include "ebo.hpp" -#include "shader.hpp" -#include "texture.hpp" -#include "vao.hpp" -#include "vbo.hpp" +#include "glm/ext/vector_float3.hpp" +#include "shape.hpp" -class Cube +class Cube : public Shape { - private: - VBO vbo; - EBO ebo; - VAO vao; - - Camera &camera; - Shader shader; - Texture texture; - public: - Cube(Camera &camera); - void loop(int width, int height); + Cube(Camera &camera, glm::vec3 pos, std::string texture); + void render(int width, int height) override; }; #endif \ No newline at end of file diff --git a/inc/ebo.hpp b/inc/ebo.hpp index 26b30c7..6fd6e3a 100755 --- a/inc/ebo.hpp +++ b/inc/ebo.hpp @@ -14,7 +14,7 @@ class EBO EBO(); ~EBO(); - void setData(unsigned int* indices, size_t size); + void setData(const unsigned int* indices, size_t size); void bind(); void unbind(); diff --git a/inc/shape.hpp b/inc/shape.hpp index 0fc4eb6..fcb5651 100644 --- a/inc/shape.hpp +++ b/inc/shape.hpp @@ -25,7 +25,10 @@ class Shape Texture texture; public: - Shape(Camera &camera, glm::vec3 pos, Shader shader, Texture texture); + Shape(Camera &camera, glm::vec3 pos, Shader shader, Texture texture) + : camera(camera), pos(pos), shader(shader), texture(texture) + { + } virtual void render(int width, int height) = 0; diff --git a/inc/vbo.hpp b/inc/vbo.hpp index a28d6fe..cd9c5bb 100755 --- a/inc/vbo.hpp +++ b/inc/vbo.hpp @@ -14,7 +14,7 @@ class VBO VBO(); ~VBO(); - void setData(GLfloat* vertices, size_t size); + void setData(const GLfloat* vertices, size_t size); void bind(); void unbind(); diff --git a/res/render/primitives/cube.hpp b/res/render/primitives/cube.hpp index 716fc46..7e682cc 100644 --- a/res/render/primitives/cube.hpp +++ b/res/render/primitives/cube.hpp @@ -57,4 +57,10 @@ constexpr unsigned int INDICE[] = { 20, 21, 22, // 11 22, 23, 20 // 12 -}; \ No newline at end of file +}; + +extern unsigned char *__res_render_primitives_cube_frag; +extern unsigned char *__res_render_primitives_cube_vert; + +extern unsigned int __res_render_primitives_cube_frag_len; +extern unsigned int __res_render_primitives_cube_vert_len; \ No newline at end of file diff --git a/src/cube.cpp b/src/cube.cpp index 51ae6f5..0a7ba46 100644 --- a/src/cube.cpp +++ b/src/cube.cpp @@ -1,174 +1,70 @@ #include "cube.hpp" -#include "glm/gtc/type_ptr.hpp" +#include "ebo.hpp" +#include "render/primitives/cube.hpp" #include "vao.hpp" #include "vbo.hpp" -GLfloat cubeVertices[] = { - // 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, -}; - -unsigned int cubeIndices[] = { - 0, 1, 2, // Triangle 1 - 2, 3, 0, // Triangle 2 - - 4, 5, 6, // Triangle 3 - 6, 7, 4, // Triangle 4 - - 8, 9, 10, // Triangle 5 - 10, 11, 8, // Triangle 6 - - 12, 13, 14, // Triangle 7 - 14, 15, 12, // Triangle 8 - - 16, 17, 18, // Triangle 9 - 18, 19, 16, // Triangle 10 - - 20, 21, 22, // Triangle 11 - 22, 23, 20 // Triangle 12 -}; - -const char* cubeVertexShader = R"( - #version 330 core - - layout (location = 0) in vec3 aPos; - layout (location = 1) in vec3 aNormal; - layout (location = 2) in vec2 aTexCoords; - - uniform mat4 projection; - uniform mat4 model; - uniform mat4 view; - - out vec3 Normal; - out vec3 FragPos; - out vec2 TexCoords; - - void main() { - FragPos = vec3(model * vec4(aPos, 1.0)); - gl_Position = projection * view * vec4(FragPos, 1.0); - - Normal = mat3(transpose(inverse(model))) * aNormal; - TexCoords = aTexCoords; - } -)"; - -const char* cubeFragShader = R"( - #version 330 core - out vec4 FragColor; - - // Définitions de struct non utilisées pour l'éclairage mais laissées pour référence - struct Material { - sampler2D diffuse; - vec3 specular; - float shininess; - }; - struct Light { - vec3 position; - - vec3 ambient; - vec3 diffuse; - vec3 specular; - }; - - in vec3 FragPos; - in vec3 Normal; - in vec2 TexCoords; - - uniform vec3 viewPos; - uniform Material material; // Utilisé uniquement pour material.diffuse - uniform Light light; // Non utilisé - - void main() - { - // 1. Lire la couleur de la texture - vec4 texColor = texture(material.diffuse, TexCoords); - - // 2. Afficher la couleur de la texture directement - FragColor = texColor; - - // Toutes les lignes de calcul d'éclairage (ambient, diffuse, specular) sont retirées. - } -)"; - -Cube::Cube(Camera& camera) - : vbo(cubeVertices, sizeof(cubeVertices)), - ebo(cubeIndices, sizeof(cubeIndices)), - texture("stone.png"), - camera(camera), - shader(cubeVertexShader, cubeFragShader) +Cube::Cube(Camera &camera, glm::vec3 pos, std::string texture) + : Shape(camera, pos, Shader{}, Texture{texture}) { - vao.bind(); - vbo.bind(); + this->vao.bind(); + this->vbo.bind(); + this->ebo.bind(); - GLsizei stride = 8 * sizeof(float); + this->vbo.setData(VERTICE, sizeof(VERTICE)); + this->ebo.setData(INDICE, sizeof(INDICE)); - vao.setAttributePointer(0, 3, GL_FLOAT, stride, (void*)0); - vao.setAttributePointer(1, 3, GL_FLOAT, stride, (void*)(3 * sizeof(float))); - vao.setAttributePointer(2, 2, GL_FLOAT, stride, (void*)(6 * sizeof(float))); - - ebo.bind(); + this->shader.compile((const char *)__res_render_primitives_cube_vert, + (const char *)__res_render_primitives_cube_frag); } -void Cube::loop(int width, int height) -{ - shader.use(); - glActiveTexture(GL_TEXTURE0); +void Cube::render(int width, int height) {} +// Cube::Cube(Camera& camera) +// : vbo(cubeVertices, sizeof(cubeVertices)), +// ebo(cubeIndices, sizeof(cubeIndices)), +// texture("stone.png"), +// camera(camera), +// shader(cubeVertexShader, cubeFragShader) +// { +// vao.bind(); +// vbo.bind(); - glm::vec3 coordinate = glm::vec3(0.0f, 0.0f, -1.0f); - glm::mat4 projection = glm::perspective( - glm::radians(camera.fov), - static_cast(width) / static_cast(height), 0.1f, 100.0f); +// GLsizei stride = 8 * sizeof(float); - GLint texLoc = glGetUniformLocation(shader.getProgram(), "material.diffuse"); - glUniform1i(texLoc, 0); +// vao.setAttributePointer(0, 3, GL_FLOAT, stride, (void*)0); +// vao.setAttributePointer(1, 3, GL_FLOAT, stride, (void*)(3 * +// sizeof(float))); vao.setAttributePointer(2, 2, GL_FLOAT, stride, (void*)(6 +// * sizeof(float))); - GLint projectionLoc = glGetUniformLocation(shader.getProgram(), "projection"); - glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); +// ebo.bind(); +// } - GLint viewLoc = glGetUniformLocation(shader.getProgram(), "view"); - glUniformMatrix4fv(viewLoc, 1, GL_FALSE, - glm::value_ptr(camera.getViewMatrix())); +// void Cube::loop(int width, int height) +// { +// shader.use(); +// glActiveTexture(GL_TEXTURE0); - glm::mat4 model = glm::translate(glm::mat4(1.0f), coordinate); - GLint modelLoc = glGetUniformLocation(shader.getProgram(), "model"); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); +// glm::vec3 coordinate = glm::vec3(0.0f, 0.0f, -1.0f); +// glm::mat4 projection = glm::perspective( +// glm::radians(camera.fov), +// static_cast(width) / static_cast(height), 0.1f, 100.0f); - vao.drawElement(GL_TRIANGLES, sizeof(cubeIndices) / sizeof(unsigned int), - GL_UNSIGNED_INT, 0); -} \ No newline at end of file +// GLint texLoc = glGetUniformLocation(shader.getProgram(), +// "material.diffuse"); glUniform1i(texLoc, 0); + +// GLint projectionLoc = glGetUniformLocation(shader.getProgram(), +// "projection"); glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, +// glm::value_ptr(projection)); + +// GLint viewLoc = glGetUniformLocation(shader.getProgram(), "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.getProgram(), "model"); +// glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); + +// vao.drawElement(GL_TRIANGLES, sizeof(cubeIndices) / sizeof(unsigned int), +// GL_UNSIGNED_INT, 0); +// } \ No newline at end of file diff --git a/src/ebo.cpp b/src/ebo.cpp index bfc917c..7503517 100755 --- a/src/ebo.cpp +++ b/src/ebo.cpp @@ -2,7 +2,7 @@ EBO::EBO() : id(0) {} -void EBO::setData(unsigned int* indices, size_t size) +void EBO::setData(const unsigned int* indices, size_t size) { if (this->id == 0) glGenBuffers(1, &this->id); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->id); diff --git a/src/main.cpp b/src/main.cpp index 5514bcd..3147fa4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,7 @@ #include "camera.hpp" #include "cube.hpp" +#include "glm/ext/vector_float3.hpp" static void glfwErrorCallback(int error, const char *description) { @@ -59,7 +60,7 @@ int main() glfwSwapInterval(1); Camera camera(width, height, window, 0.1f); - Cube cube{camera}; + Cube cube{camera, glm::vec3 {0.f, 0.f, 0.f}, "stone.png"}; glEnable(GL_DEPTH_TEST); glEnable(GL_MULTISAMPLE); @@ -79,7 +80,7 @@ int main() lastTime = currentTime; camera.update(deltaTime); - cube.loop(width, height); + cube.render(width, height); glfwPollEvents(); glfwSwapBuffers(window); diff --git a/src/vbo.cpp b/src/vbo.cpp index e056745..996b532 100755 --- a/src/vbo.cpp +++ b/src/vbo.cpp @@ -2,7 +2,7 @@ VBO::VBO() : id(0) {} -void VBO::setData(GLfloat* vertices, size_t size) +void VBO::setData(const GLfloat* vertices, size_t size) { if (this->id == 0) glGenBuffers(1, &this->id); glBindBuffer(GL_ARRAY_BUFFER, this->id);