feat(close #8): Implement uniform location caching
This commit is contained in:
parent
0ced6c452f
commit
9a866d16ef
@ -9,6 +9,8 @@
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <unordered_map>
|
||||
|
||||
|
||||
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<std::string, int> m_Uniforms{};
|
||||
};
|
@ -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();
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user