From ff7a82f646b6cba5d2e2934b49a5736a1bb52f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Sun, 15 Feb 2026 20:10:17 +0100 Subject: [PATCH] feat: replace triangle test by cube --- CMakeLists.txt | 4 +- inc/core/camera.hpp | 4 +- inc/core/shader.hpp | 2 + inc/cube.hpp | 22 +++---- res/render/primitives/p_cube.frag | 22 +------ res/render/primitives/p_cube.vert | 10 +-- res/render/primitives/p_triangle.frag | 3 +- res/render/primitives/p_triangle.hpp | 1 - glsl2c.py => scripts/glsl2c.py | 5 +- scripts/glsl2pak.py | 20 ++++++ src/core/camera.cpp | 22 +++---- src/cube.cpp | 93 +++++++++++++-------------- src/main.cpp | 58 +++++++++++++---- 13 files changed, 149 insertions(+), 117 deletions(-) rename glsl2c.py => scripts/glsl2c.py (91%) create mode 100755 scripts/glsl2pak.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 5c42ed4..9b1360a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,11 +40,11 @@ foreach(SHADER ${SHADERS}) add_custom_command( OUTPUT ${OUTPUT_C} COMMAND python3 - ${CMAKE_SOURCE_DIR}/glsl2c.py + ${CMAKE_SOURCE_DIR}/scripts/glsl2c.py ${SHADER} ${OUTPUT_C} DEPENDS ${SHADER} - ${CMAKE_SOURCE_DIR}/glsl2c.py + ${CMAKE_SOURCE_DIR}/scripts/glsl2c.py COMMENT "Converting shader ${NAME_WE}${EXT} to C file" VERBATIM ) diff --git a/inc/core/camera.hpp b/inc/core/camera.hpp index 17ca019..e5bc0b8 100755 --- a/inc/core/camera.hpp +++ b/inc/core/camera.hpp @@ -5,6 +5,8 @@ #include "glm/ext/matrix_float4x4.hpp" #include "glm/ext/vector_float3.hpp" +namespace core +{ class Camera { private: @@ -63,5 +65,5 @@ class Camera void setFov(float newFov); void setPosition(const glm::vec3& newPosition); }; - +} // namespace core #endif diff --git a/inc/core/shader.hpp b/inc/core/shader.hpp index 09ee2a0..d967335 100755 --- a/inc/core/shader.hpp +++ b/inc/core/shader.hpp @@ -29,6 +29,8 @@ class Shader // Activates the shader program for rendering // All subsequent draw calls will use this program void use() const; + + GLuint getId() const { return id; } }; } // namespace core diff --git a/inc/cube.hpp b/inc/cube.hpp index 2f7bd84..223c5f3 100644 --- a/inc/cube.hpp +++ b/inc/cube.hpp @@ -1,14 +1,14 @@ -#ifndef CUBE_HPP -#define CUBE_HPP +// #ifndef CUBE_HPP +// #define CUBE_HPP -#include "glm/ext/vector_float3.hpp" -#include "shape.hpp" +// #include "glm/ext/vector_float3.hpp" +// #include "shape.hpp" -class Cube : public Shape -{ - public: - Cube(Camera &camera, glm::vec3 pos, const char *texture); - void render(int width, int height) override; -}; +// class Cube : public Shape +// { +// public: +// Cube(Camera &camera, glm::vec3 pos, const char *texture); +// void render(int width, int height) override; +// }; -#endif \ No newline at end of file +// #endif \ No newline at end of file diff --git a/res/render/primitives/p_cube.frag b/res/render/primitives/p_cube.frag index b2f5048..7145a27 100644 --- a/res/render/primitives/p_cube.frag +++ b/res/render/primitives/p_cube.frag @@ -1,31 +1,11 @@ #version 330 core out vec4 FragColor; -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; -uniform Light light; - void main() { - vec4 texColor = texture(material.diffuse , TexCoords); - FragColor = texColor; + FragColor = vec4(0.8, 0.8, 1.0, 1.0); } diff --git a/res/render/primitives/p_cube.vert b/res/render/primitives/p_cube.vert index c65d705..9abe006 100644 --- a/res/render/primitives/p_cube.vert +++ b/res/render/primitives/p_cube.vert @@ -8,15 +8,7 @@ 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; + gl_Position = projection * view * model * vec4(aPos, 1.0); } diff --git a/res/render/primitives/p_triangle.frag b/res/render/primitives/p_triangle.frag index bb8afd2..05e7b12 100644 --- a/res/render/primitives/p_triangle.frag +++ b/res/render/primitives/p_triangle.frag @@ -1,8 +1,7 @@ #version 330 core out vec4 color; - void main() { - color = vec4(1.0f, 0.5f, 0.2f, 1.0f); + color = vec4(0.2, 0.51, 1.0, 1.0); } \ No newline at end of file diff --git a/res/render/primitives/p_triangle.hpp b/res/render/primitives/p_triangle.hpp index 6ea374b..521ce33 100644 --- a/res/render/primitives/p_triangle.hpp +++ b/res/render/primitives/p_triangle.hpp @@ -13,7 +13,6 @@ namespace mesh 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); diff --git a/glsl2c.py b/scripts/glsl2c.py similarity index 91% rename from glsl2c.py rename to scripts/glsl2c.py index a660782..894e398 100644 --- a/glsl2c.py +++ b/scripts/glsl2c.py @@ -5,6 +5,7 @@ from pathlib import Path line: int = 12 + def write_binary(content: bytes) -> str: text = "" for i, byte in enumerate(content): @@ -16,6 +17,7 @@ def write_binary(content: bytes) -> str: text += "0x00\n" return text + def main() -> int: # check arguments if len(sys.argv) < 2 or len(sys.argv) > 3: @@ -37,7 +39,8 @@ def main() -> int: content = src.read_bytes() with open(dst, "w") as f: - f.write(f"const unsigned char {varname}[] = {{\n{write_binary(content)}}};\n") + f.write( + f"const unsigned char {varname}[] = {{\n{write_binary(content)}}};\n") f.write(f"const unsigned int {varname}_LEN = {len(content)};\n") return 0 diff --git a/scripts/glsl2pak.py b/scripts/glsl2pak.py new file mode 100755 index 0000000..77124f0 --- /dev/null +++ b/scripts/glsl2pak.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 + +from scripts.glsl2c import write_binary +from pathlib import Path +import sys + + +def main() -> int: + if len(sys.argv) < 2: + print("glsl2pak.py ... output") + return 1 + + output = sys.argv.pop() + print(sys.argv) + + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/src/core/camera.cpp b/src/core/camera.cpp index 2cfaeb9..0aa2706 100755 --- a/src/core/camera.cpp +++ b/src/core/camera.cpp @@ -5,7 +5,7 @@ #include "glm/ext/matrix_transform.hpp" #include "glm/ext/matrix_clip_space.hpp" -Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity) +core::Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity) : width(width), height(height), window(window), @@ -15,7 +15,7 @@ Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity) updateCameraVectors(); } -void Camera::update(float deltaTime) +void core::Camera::update(float deltaTime) { processInput(deltaTime); processMouseMovement(); @@ -23,7 +23,7 @@ void Camera::update(float deltaTime) } // TODO: callback management -void Camera::processInput(float deltaTime) +void core::Camera::processInput(float deltaTime) { float velocity = speed * deltaTime; @@ -51,7 +51,7 @@ void Camera::processInput(float deltaTime) : 45.f; } -void Camera::processMouseMovement() +void core::Camera::processMouseMovement() { double mouseX, mouseY; glfwGetCursorPos(window, &mouseX, &mouseY); @@ -80,7 +80,7 @@ void Camera::processMouseMovement() if (cameraPitch < -89.0f) cameraPitch = -89.0f; } -void Camera::updateCameraVectors() +void core::Camera::updateCameraVectors() { glm::vec3 front; front.x = cos(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch)); @@ -92,12 +92,12 @@ void Camera::updateCameraVectors() cameraUp = glm::normalize(glm::cross(cameraRight, cameraFront)); } -glm::mat4 Camera::getViewMatrix() const +glm::mat4 core::Camera::getViewMatrix() const { return glm::lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp); } -glm::mat4 Camera::getProjectionMatrix() const +glm::mat4 core::Camera::getProjectionMatrix() const { return glm::perspective( glm::radians(this->fov), @@ -105,7 +105,7 @@ glm::mat4 Camera::getProjectionMatrix() const 100.0f); } -void Camera::setSpeed(float newSpeed) +void core::Camera::setSpeed(float newSpeed) { if (newSpeed > 0.0f) { @@ -113,7 +113,7 @@ void Camera::setSpeed(float newSpeed) } } -void Camera::setCameraSensitivity(float newSensitivity) +void core::Camera::setCameraSensitivity(float newSensitivity) { if (newSensitivity > 0.0f) { @@ -121,7 +121,7 @@ void Camera::setCameraSensitivity(float newSensitivity) } } -void Camera::setFov(float newFov) +void core::Camera::setFov(float newFov) { if (newFov > 1.0f && newFov < 179.0f) { @@ -129,7 +129,7 @@ void Camera::setFov(float newFov) } } -void Camera::setPosition(const glm::vec3& newPosition) +void core::Camera::setPosition(const glm::vec3& newPosition) { cameraPosition = newPosition; } \ No newline at end of file diff --git a/src/cube.cpp b/src/cube.cpp index 7b26ce2..0b6c498 100644 --- a/src/cube.cpp +++ b/src/cube.cpp @@ -1,65 +1,64 @@ -// #include "primitives/cube.hpp" +// #include "cube.hpp" +// #include "core/ebo.hpp" +// #include "core/vao.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" +// #include "primitives/p_cube.hpp" -// static GLsizei stride = 8 * sizeof(float); +// constexpr 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/main.cpp b/src/main.cpp index cd2e90c..b23e695 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,13 @@ #include "core/core.hpp" -#include "core/ebo.hpp" -#include "core/shader.hpp" +#include "glm/ext/matrix_clip_space.hpp" +#include "glm/ext/matrix_transform.hpp" +#include "glm/fwd.hpp" +#include "glm/gtc/type_ptr.hpp" +#include "primitives/p_cube.hpp" #include "primitives/p_triangle.hpp" +using namespace mesh; + int main() { // Game game{800, 600, "window"}; @@ -57,34 +62,65 @@ int main() glfwSwapInterval(1); // activate vsync - // glEnable(GL_DEPTH_TEST); - // glEnable(GL_MULTISAMPLE); + glEnable(GL_DEPTH_TEST); + glEnable(GL_MULTISAMPLE); + // CUBE 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}; + core::VBO vbo{P_CUBE_VERTICE, sizeof(P_CUBE_VERTICE)}; + core::EBO ebo{P_CUBE_INDICE, sizeof(P_CUBE_INDICE)}; + core::Shader shader{(char *)P_CUBE_VERT, (char *)P_CUBE_FRAG}; + // CAMERA + core::Camera camera(width, height, window, .1f); + + // BINDING vao.bind(); vbo.bind(); ebo.bind(); - vao.setAttributePointer(0, 3, GL_FLOAT, 3 * sizeof(GLfloat), (void *)(0)); + // positions + vao.setAttributePointer(0, 3, GL_FLOAT, 8 * sizeof(GLfloat), (void *)(0)); + // normales + vao.setAttributePointer(1, 3, GL_FLOAT, 8 * sizeof(GLfloat), + (void *)(3 * sizeof(float))); + // texture + vao.setAttributePointer(2, 2, GL_FLOAT, 8 * sizeof(GLfloat), + (void *)(6 * sizeof(float))); + + glm::vec3 coordinate = glm::vec3(0.0f, 0.0f, -10.0f); 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); + camera.update(time.getDeltaTime()); + shader.use(); - vao.drawElement(GL_TRIANGLES, mesh::P_TRIANGLE_INDICE_LEN, GL_UNSIGNED_INT, 0); + // projection matrix + const glm::float32 *view = glm::value_ptr(camera.getViewMatrix()); + glm::mat4 model = glm::translate(glm::mat4(1.0f), coordinate); + glm::mat4 projection = glm::perspective( + glm::radians(camera.getFov()), + static_cast(width) / static_cast(height), 0.1f, 100.f); + + glUniformMatrix4fv(glGetUniformLocation(shader.getId(), "projection"), 1, + GL_FALSE, glm::value_ptr(projection)); + + glUniformMatrix4fv(glGetUniformLocation(shader.getId(), "view"), 1, + GL_FALSE, glm::value_ptr(camera.getViewMatrix())); + + glUniformMatrix4fv(glGetUniformLocation(shader.getId(), "model"), 1, + GL_FALSE, glm::value_ptr(model)); + + vao.drawElement(GL_TRIANGLES, sizeof(P_TRIANGLE_VERTICE), GL_UNSIGNED_INT, + 0); glfwPollEvents(); glfwSwapBuffers(window);