From d5451aa5215bff05eb8f389ff37554dbd88853f8 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 18 Feb 2021 00:28:36 +0000 Subject: Allow setting any uniform in shaders Move less commonly used (lightDirection) to a named one --- application/main.cpp | 2 +- gfx/gl/shader.cpp | 12 ++++++------ gfx/gl/shader.h | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/application/main.cpp b/application/main.cpp index 41bc15a..7e23a16 100644 --- a/application/main.cpp +++ b/application/main.cpp @@ -84,7 +84,7 @@ public: camera.Pitch(0.24); camera.RotateY(0.7854); shader.setView(camera.GetViewProjection()); - shader.setLight(glm::normalize(glm::vec3 {1, -1, 0})); + shader.setUniform("lightDirection", glm::normalize(glm::vec3 {1, -1, 0})); auto t_start = std::chrono::high_resolution_clock::now(); const auto framelen = std::chrono::milliseconds {1000} / 120; diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp index 979d57e..8c58c39 100644 --- a/gfx/gl/shader.cpp +++ b/gfx/gl/shader.cpp @@ -6,8 +6,7 @@ #include #include -Shader::ProgramHandle::ProgramHandle(std::initializer_list srcs) : - viewProjection_uniform {}, model_uniform {}, lightDir_uniform {} +Shader::ProgramHandle::ProgramHandle(std::initializer_list srcs) : viewProjection_uniform {}, model_uniform {} { for (const auto & srcId : srcs) { glAttachShader(m_program, srcId); @@ -25,7 +24,6 @@ Shader::ProgramHandle::ProgramHandle(std::initializer_list srcs) : viewProjection_uniform = glGetUniformLocation(m_program, "viewProjection"); model_uniform = glGetUniformLocation(m_program, "model"); - lightDir_uniform = glGetUniformLocation(m_program, "lightDirection"); } Shader::Shader() : @@ -46,11 +44,13 @@ Shader::setView(glm::mat4 proj) const } void -Shader::setLight(glm::vec3 lightDir) const +Shader::setUniform(const GLchar * uniform, glm::vec3 v) const { for (const auto & prog : programs) { - glUseProgram(prog.m_program); - glUniform3fv(prog.lightDir_uniform, 1, &lightDir[0]); + if (auto loc = glGetUniformLocation(prog.m_program, uniform); loc >= 0) { + glUseProgram(prog.m_program); + glUniform3fv(loc, 1, &v[0]); + } } } diff --git a/gfx/gl/shader.h b/gfx/gl/shader.h index 98e0348..0282f21 100644 --- a/gfx/gl/shader.h +++ b/gfx/gl/shader.h @@ -15,7 +15,7 @@ public: void setView(glm::mat4 view) const; void setModel(glm::mat4 model, Program = Program::Basic) const; - void setLight(glm::vec3 dir) const; + void setUniform(const GLchar *, glm::vec3 dir) const; private: class Source { @@ -36,7 +36,7 @@ private: using ProgramRef = glRef; ProgramRef m_program; - GLint viewProjection_uniform, model_uniform, lightDir_uniform; + GLint viewProjection_uniform, model_uniform; }; std::array programs; -- cgit v1.2.3