From 9b2c943e82d9a9fcdb0c631939d6dd6e7573210c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Th=C3=A9au?= <theau.baton@etu.univ-amu.fr>
Date: Mon, 28 Oct 2024 04:37:55 +0100
Subject: [PATCH] Set uSampler uniform as static vector auto builed

---
 .../graphics/front/group/FrameBufferGroup.cpp    |  5 ++++-
 .../engine/graphics/front/group/ImageGroup.cpp   |  9 +++++++--
 .../engine/graphics/utility/array_generator.hpp  | 16 ++++++++++++++++
 3 files changed, 27 insertions(+), 3 deletions(-)
 create mode 100644 source/engine/graphics/utility/array_generator.hpp

diff --git a/source/engine/graphics/front/group/FrameBufferGroup.cpp b/source/engine/graphics/front/group/FrameBufferGroup.cpp
index 80f853d..6653a7c 100644
--- a/source/engine/graphics/front/group/FrameBufferGroup.cpp
+++ b/source/engine/graphics/front/group/FrameBufferGroup.cpp
@@ -2,6 +2,7 @@
 
 #include <glm/gtc/matrix_transform.hpp>
 #include <engine/graphics/front/geometry/Plane.hpp>
+#include <engine/graphics/utility/array_generator.hpp>
 
 namespace megu {
     FrameBufferGroup::FrameBufferGroup() 
@@ -28,7 +29,9 @@ namespace megu {
             textures[i].get().bind(static_cast<GLuint>(i));
         }   
 
-        this->_program.setUniform("uSampler", std::vector<GLint>{0, 1, 2, 3, 4, 5, 6 , 7, 8, 9, 10});
+        static auto uSampler = array_generator<GLint, 32>().array;
+
+        this->_program.setUniform("uSampler", uSampler);
         this->_program.setUniform("uTexturesCount", static_cast<GLuint>(textures.size()));
         glDrawArrays(Plane::Primitive(), 0, static_cast<GLsizei>(Plane::Vertices().size()));
     }
diff --git a/source/engine/graphics/front/group/ImageGroup.cpp b/source/engine/graphics/front/group/ImageGroup.cpp
index 345e8d3..8730cb9 100644
--- a/source/engine/graphics/front/group/ImageGroup.cpp
+++ b/source/engine/graphics/front/group/ImageGroup.cpp
@@ -2,6 +2,8 @@
 
 #include <tuple>
 
+#include <engine/graphics/utility/array_generator.hpp>
+
 namespace megu {
     ImageGroup::ImageGroup() 
     : _vbo(this->_vao, Quads::Layout(), Quads::Vertices().size(), megu::EditMode::STATIC) {
@@ -39,6 +41,8 @@ namespace megu {
 
         std::vector<glm::mat4> uModels;
         std::vector<GLint> uTextures;
+
+        static auto uSampler = array_generator<GLint, 32>().array;         
     
         for(auto & image : this->_images) {
             
@@ -51,7 +55,7 @@ namespace megu {
                 if(textures.size() >= 8 || uModels.size() >= 124) {
                     this->_program.setUniform("uProj", camera.projection());
                     this->_program.setUniform("uView", camera.view());
-                    this->_program.setUniform("uSampler", std::vector<GLint>({0, 1, 2, 3, 4, 5, 6, 7}));
+                    this->_program.setUniform("uSampler", uSampler);
                     this->_program.setUniform("uModel", uModels);
                     this->_program.setUniform("uTextures", uTextures);
 
@@ -73,7 +77,8 @@ namespace megu {
             this->_program.setUniform("uProj", camera.projection());
             this->_program.setUniform("uView", camera.view());
             
-            this->_program.setUniform("uSampler", std::vector<GLint>({0, 1, 2, 3, 4, 5, 6, 7}));
+            
+            this->_program.setUniform("uSampler", uSampler);
             this->_program.setUniform("uModel", uModels);
             this->_program.setUniform("uTextures", uTextures);
 
diff --git a/source/engine/graphics/utility/array_generator.hpp b/source/engine/graphics/utility/array_generator.hpp
new file mode 100644
index 0000000..9b867f5
--- /dev/null
+++ b/source/engine/graphics/utility/array_generator.hpp
@@ -0,0 +1,16 @@
+#pragma once
+
+#include <vector>
+
+namespace megu {
+    template <typename T, T C>
+    struct array_generator {
+        public:
+            constexpr array_generator() {
+                for(T i = 0; i < C; ++i) {
+                    this->array.push_back(i);
+                }
+            }
+            std::vector<T> array;
+    };
+}
\ No newline at end of file
-- 
GitLab