From 630038754c1a9a04e05fc59bdbced17c06f3dab5 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Wed, 23 May 2018 20:16:09 +0200 Subject: Pass texture count to display fragment shader --- src/graphic_shader.h | 6 ++++++ src/main.cc | 9 ++++----- src/shader/display_fragment.glsl | 25 +++++-------------------- src/texture_display_buffer.h | 7 +------ 4 files changed, 16 insertions(+), 31 deletions(-) diff --git a/src/graphic_shader.h b/src/graphic_shader.h index c93e61b..c67dc01 100644 --- a/src/graphic_shader.h +++ b/src/graphic_shader.h @@ -38,6 +38,12 @@ public: return id; } + GLuint setUniform(const std::string& name, const std::vector& v) const { + GLuint id = util::getUniform(_id, name); + glUniform1iv(id, v.size(), reinterpret_cast(v.data())); + return id; + } + GLuint setUniform(const std::string& name, glm::mat4& M) const { GLuint id = util::getUniform(_id, name); glUniformMatrix4fv(id, 1, GL_FALSE, &M[0][0]); diff --git a/src/main.cc b/src/main.cc index 034d026..ceffe67 100644 --- a/src/main.cc +++ b/src/main.cc @@ -22,9 +22,9 @@ #include "shader/display_vertex.glsl" #include "shader/display_fragment.glsl" -const unsigned int particle_count = 5000; +const unsigned int particle_count = 1000; const unsigned int max_ups = 100; -const unsigned int texture_count = 10; +const unsigned int texture_count = 20; unsigned int window_width = 800; unsigned int window_height = 600; @@ -173,9 +173,8 @@ int main() { { auto guard = displayShader.use(); - for ( unsigned int i = 0; i < textures.size(); ++i ) { - displayShader.setUniform("screen_texture_" + std::to_string(i), i); - } + displayShader.setUniform("screen_textures", textures); + displayShader.setUniform("screen_textures_size", textures.size()); glClear(GL_COLOR_BUFFER_BIT); diff --git a/src/shader/display_fragment.glsl b/src/shader/display_fragment.glsl index 03986a0..b731ebe 100644 --- a/src/shader/display_fragment.glsl +++ b/src/shader/display_fragment.glsl @@ -4,27 +4,12 @@ static const std::string DISPLAY_FRAGMENT_SHADER_CODE = R"( out vec4 FragColor; in vec2 TexCoords; -uniform sampler2D screen_texture_0; -uniform sampler2D screen_texture_1; -uniform sampler2D screen_texture_2; -uniform sampler2D screen_texture_3; -uniform sampler2D screen_texture_4; -uniform sampler2D screen_texture_5; -uniform sampler2D screen_texture_6; -uniform sampler2D screen_texture_7; -uniform sampler2D screen_texture_8; -uniform sampler2D screen_texture_9; +uniform sampler2D screen_textures[64]; +uniform int screen_textures_size; void main() { - FragColor = texture(screen_texture_0, TexCoords) - + 0.9 * texture(screen_texture_1, TexCoords) - + 0.8 * texture(screen_texture_2, TexCoords) - + 0.7 * texture(screen_texture_3, TexCoords) - + 0.6 * texture(screen_texture_4, TexCoords) - + 0.5 * texture(screen_texture_5, TexCoords) - + 0.4 * texture(screen_texture_6, TexCoords) - + 0.3 * texture(screen_texture_7, TexCoords) - + 0.2 * texture(screen_texture_8, TexCoords) - + 0.1 * texture(screen_texture_9, TexCoords); + for ( int i = 0; i < screen_textures_size; ++i ) { + FragColor += (1.0 - i*1.0/screen_textures_size) * texture(screen_textures[i], TexCoords); + } } )"; diff --git a/src/texture_display_buffer.h b/src/texture_display_buffer.h index 25fd319..b38f9c0 100644 --- a/src/texture_display_buffer.h +++ b/src/texture_display_buffer.h @@ -47,12 +47,7 @@ public: void draw(const std::vector& textures) const { glBindVertexArray(_array); - - for ( unsigned int i = 0; i < textures.size(); ++i ) { - glActiveTexture(GL_TEXTURE0+i); - glBindTexture(GL_TEXTURE_2D, textures[i]); - } - + glBindTextures(textures[0], textures.size(), textures.data()); glDrawArrays(GL_TRIANGLES, 0, 6); } -- cgit v1.2.3