From 98b870e6555b855a841dd66c8ce5c28ea379dbcc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Th=C3=A9au?= <theau.baton@etu.univ-amu.fr>
Date: Mon, 14 Oct 2024 12:07:40 +0200
Subject: [PATCH] Resolve problem on shader with uniforms arrays

---
 assets/shaders/Basic.frag                       |  8 ++++++++
 assets/shaders/Basic.vert                       | 10 ++++++++++
 assets/shaders/Texture.frag                     | 10 ++++++++++
 assets/shaders/Texture.vert                     | 10 ++++++++++
 source/engine/graphics/back/shaders/Program.cpp | 10 +++++++---
 source/engine/graphics/back/shaders/Program.hpp |  4 ++--
 source/main.cpp                                 |  8 ++++++++
 7 files changed, 55 insertions(+), 5 deletions(-)
 create mode 100644 assets/shaders/Basic.frag
 create mode 100644 assets/shaders/Basic.vert
 create mode 100644 assets/shaders/Texture.frag
 create mode 100644 assets/shaders/Texture.vert

diff --git a/assets/shaders/Basic.frag b/assets/shaders/Basic.frag
new file mode 100644
index 0000000..4048d97
--- /dev/null
+++ b/assets/shaders/Basic.frag
@@ -0,0 +1,8 @@
+#version 330 core
+out vec4 FragColor;
+
+in vec3 Color;
+
+void main() {
+    FragColor = vec4(Color, 1.0);
+}
\ No newline at end of file
diff --git a/assets/shaders/Basic.vert b/assets/shaders/Basic.vert
new file mode 100644
index 0000000..fa16855
--- /dev/null
+++ b/assets/shaders/Basic.vert
@@ -0,0 +1,10 @@
+#version 330 core
+layout (location = 0) in vec3 aPos;
+layout (location = 1) in vec3 aCol;
+
+out vec3 Color;
+
+void main() {
+    Color = aCol;
+    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
+}
\ No newline at end of file
diff --git a/assets/shaders/Texture.frag b/assets/shaders/Texture.frag
new file mode 100644
index 0000000..12cf126
--- /dev/null
+++ b/assets/shaders/Texture.frag
@@ -0,0 +1,10 @@
+#version 330 core
+out vec4 FragColor;
+
+in vec2 Texture;
+
+uniform sampler2D uSamp[2];
+
+void main() {
+    FragColor = texture(uSamp[0], Texture);
+}
\ No newline at end of file
diff --git a/assets/shaders/Texture.vert b/assets/shaders/Texture.vert
new file mode 100644
index 0000000..d1fd061
--- /dev/null
+++ b/assets/shaders/Texture.vert
@@ -0,0 +1,10 @@
+#version 330 core
+layout (location = 0) in vec3 aPos;
+layout (location = 1) in vec2 aTex;
+
+out vec2 Texture;
+
+void main() {
+    Texture = aTex;
+    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
+}
\ No newline at end of file
diff --git a/source/engine/graphics/back/shaders/Program.cpp b/source/engine/graphics/back/shaders/Program.cpp
index 5a915e1..f4f9860 100644
--- a/source/engine/graphics/back/shaders/Program.cpp
+++ b/source/engine/graphics/back/shaders/Program.cpp
@@ -5,6 +5,8 @@
 
 #include "../../errors/Shader_Error.hpp"
 
+#include <iostream>
+
 namespace megu {
     Program::Program() 
     : _id(0), _usable(false), _attached(NONE) {
@@ -74,9 +76,11 @@ namespace megu {
             for(size_t i = 0; i < count; ++i) {
                 GLint length, size;
                 GLenum type;
-                GLchar* name;
-                glGetActiveUniform(this->_id, static_cast<GLuint>(i), 32, &length, &size, &type, name);
-                this->_locations[name] = glGetUniformLocation(this->_id, name);
+                GLchar name[64];
+                glGetActiveUniform(this->_id, static_cast<GLuint>(i), 64, &length, &size, &type, name);
+                
+                std::string var_name(name);
+                this->_locations[var_name.substr(0, var_name.find("["))] = glGetUniformLocation(this->_id, name);
             }
         }
         else {
diff --git a/source/engine/graphics/back/shaders/Program.hpp b/source/engine/graphics/back/shaders/Program.hpp
index ab545d3..5e4b08d 100644
--- a/source/engine/graphics/back/shaders/Program.hpp
+++ b/source/engine/graphics/back/shaders/Program.hpp
@@ -3,7 +3,7 @@
 #include <GL/glew.h>
 #include <glm/glm.hpp>
 #include <vector>
-#include <map>
+#include <unordered_map>
 
 #include "Source.hpp"
 
@@ -13,7 +13,7 @@ namespace megu {
             GLuint _id;
             bool _usable;
             uint8_t _attached;
-            std::map<std::string, GLuint> _locations;
+            mutable std::unordered_map<std::string, GLuint> _locations;
 
         protected:
             enum Bit_Categorie {
diff --git a/source/main.cpp b/source/main.cpp
index 9f510b9..c879050 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -80,6 +80,8 @@ int main(int argc, const char * argv) {
 
         vbo << vertices;
 
+        std::cout << "VBO" << std::endl;
+
         megu::VertexArray vao_2;
         megu::VerticeBuffer vbo_2(vao_2, {megu::layout::POSITION, megu::layout::COLOR}, 128, megu::EditMode::STATIC);
 
@@ -106,6 +108,8 @@ int main(int argc, const char * argv) {
 
         vbo_F << vertices_F;
 
+        std::cout << "VBO - FBO" << std::endl;
+
         //? FBO
 
         megu::FrameBuffer fbo(WINDOW_WIDTH, WINDOW_HEIGHT);
@@ -124,6 +128,8 @@ int main(int argc, const char * argv) {
             program.link();
         }
 
+        std::cout << "Shader - FBO" << std::endl;
+
 
         megu::Program program_F;
 
@@ -137,6 +143,8 @@ int main(int argc, const char * argv) {
             program_F.link();
         }
 
+        std::cout << "Shader" << std::endl;
+
 
         //? Render Loop
         glClearColor(0.0f, 0.0f, 0.0f, 0.f);
-- 
GitLab