diff --git a/engine/include/Shader.h b/engine/include/Shader.h index b6cbdcd..8152b84 100644 --- a/engine/include/Shader.h +++ b/engine/include/Shader.h @@ -9,6 +9,8 @@ #include #include #include +#include + class Shader { @@ -19,15 +21,11 @@ public: static Shader Create(const std::string& vPath, const std::string& fPath); - unsigned int GetID() const; - void Use() const; void Shutdown() const; - void SetBool(const char* name, bool value) const; - void SetInt(const char* name, int value) const; - void SetFloat(const char* name, float value) const; - void SetMat4(const char* name, glm::mat4 value) const; + int GetUniformLocation(const std::string& name); private: unsigned int m_ID; + std::unordered_map m_Uniforms{}; }; \ No newline at end of file diff --git a/engine/src/Renderer.cpp b/engine/src/Renderer.cpp index deff9e5..71d4343 100644 --- a/engine/src/Renderer.cpp +++ b/engine/src/Renderer.cpp @@ -78,8 +78,8 @@ void Renderer::Render() { s_Data.m_Shader->Use(); auto transform = glm::mat4(1.0f); transform = glm::translate(transform, glm::vec3(UI::GetData().m_Position[0], UI::GetData().m_Position[1], UI::GetData().m_Position[2])); - s_Data.m_Shader->SetMat4("transform", transform); - glUniform3fv(glGetUniformLocation(s_Data.m_Shader->GetID(), "color"), 1, UI::GetData().m_Color); + glUniformMatrix4fv(s_Data.m_Shader->GetUniformLocation("transform"), 1, GL_FALSE, glm::value_ptr(transform)); + glUniform3fv(s_Data.m_Shader->GetUniformLocation("color"), 1, UI::GetData().m_Color); s_Data.m_VAO->Bind(); glDrawArrays(GL_TRIANGLES, 0, 3); FrameBuffer::Unbind(); diff --git a/engine/src/Shader.cpp b/engine/src/Shader.cpp index f114571..a47d404 100644 --- a/engine/src/Shader.cpp +++ b/engine/src/Shader.cpp @@ -94,24 +94,12 @@ void Shader::Shutdown() const glDeleteProgram(m_ID); } -unsigned int Shader::GetID() const +int Shader::GetUniformLocation(const std::string& name) { - return m_ID; -} + if(m_Uniforms.find(name) == m_Uniforms.end()) + { + m_Uniforms[name] = glGetUniformLocation(m_ID, name.c_str()); + } -void Shader::SetBool(const char* name, bool value) const -{ - glUniform1i(glGetUniformLocation(m_ID, name), (int)value); -} -void Shader::SetInt(const char* name, int value) const -{ - glUniform1i(glGetUniformLocation(m_ID, name), value); -} -void Shader::SetFloat(const char* name, float value) const -{ - glUniform1f(glGetUniformLocation(m_ID, name), value); -} -void Shader::SetMat4(const char* name, glm::mat4 value) const -{ - glUniformMatrix4fv(glGetUniformLocation(m_ID, name), 1, GL_FALSE, glm::value_ptr(value)); + return m_Uniforms.at(name); }