From a5c84e31d21756d02032be88532f0909f8cee476 Mon Sep 17 00:00:00 2001 From: Vitor Fernandes Date: Sun, 21 Jun 2020 22:17:44 +0100 Subject: [PATCH] More on shaders to classes --- src/opengl/Shader.cpp | 22 +++++++++++++++++----- src/opengl/Shader.h | 2 ++ src/opengl/gl1.cpp | 16 +++++++++++----- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/opengl/Shader.cpp b/src/opengl/Shader.cpp index 30535dd..4ea007c 100644 --- a/src/opengl/Shader.cpp +++ b/src/opengl/Shader.cpp @@ -11,29 +11,41 @@ Shader::Shader(const std::string &filepath) : m_FilePath(filepath), m_RendererID(0) { - //ShaderProgramSource source = ParseShader(m_FilePath); - //m_RendererID = CreateShader(source.VertexSource, source.FragmentSource); + ShaderProgramSource source = ParseShader(m_FilePath); + m_RendererID = CreateShader(source.VertexSource, source.FragmentSource); } - Shader::~Shader() { } void Shader::Bind() { + GLCall(glUseProgram(m_RendererID)); } void Shader::Unbind() { + GLCall(glUseProgram(0)); } //Set uniforms void Shader::SetUniform4f(const std::string &name, float v0, float v1, float v2, float v3) { + GLCall(glUniform4f(GetUniformLocation(name), v0, v1, v2, v3)); } unsigned int Shader::GetUniformLocation(const std::string &name) { + if (m_UniformLocationCache.find(name) != m_UniformLocationCache.end()) + return m_UniformLocationCache[name]; + + GLCall(unsigned int location = glGetUniformLocation(m_RendererID, name.c_str())); + + if (location == -1) + LOG("Warning: uniform" << name << " doesn't exist"); + + m_UniformLocationCache[name] = location; + return location; } /** @@ -72,9 +84,9 @@ unsigned int Shader::CompileShader(unsigned int type, const std::string &source) { int length; glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length); - + LOG("Length: " << length); - + char *message = (char *)malloc(length * sizeof(char)); glGetShaderInfoLog(id, length, &length, message); diff --git a/src/opengl/Shader.h b/src/opengl/Shader.h index 679d2b6..ca2c34b 100644 --- a/src/opengl/Shader.h +++ b/src/opengl/Shader.h @@ -1,6 +1,7 @@ #pragma once #include +#include struct ShaderProgramSource { @@ -14,6 +15,7 @@ struct ShaderProgramSource class Shader { private: + std::unordered_map m_UniformLocationCache; unsigned int m_RendererID; std::string m_FilePath; ShaderProgramSource ParseShader(const std::string &filepath); diff --git a/src/opengl/gl1.cpp b/src/opengl/gl1.cpp index 6204462..664253f 100644 --- a/src/opengl/gl1.cpp +++ b/src/opengl/gl1.cpp @@ -12,7 +12,7 @@ #include "VertexArray.h" #include "utils.h" -//const std::string SHADERS_PATH = "src/opengl/res/shaders/Basic.shader"; +const std::string SHADERS_PATH = "src/opengl/res/shaders/Basic.shader"; int main(void) { @@ -70,11 +70,14 @@ int main(void) IndexBuffer ib(indexes,6); + Shader shader(SHADERS_PATH); + shader.Bind(); + shader.SetUniform4f("u_Color",0.2f, 0.3f, 0.8f, 1.0f); - //Find the shader uniform color - //int uniform_location = glGetUniformLocation(shader, "u_color"); - - //if (uniform_location == -1) LOG("Error fetching uniform location"); + va.Unbind(); + vb.Unbind(); + ib.Unbind(); + shader.Unbind(); float red = 0.0f; float increment = 0.05f; @@ -104,6 +107,9 @@ int main(void) //GLCall(glDrawElements(GL_TRIANGLES, 6 * sizeof(uint), GL_UNSIGNED_INT, nullptr)); + shader.Bind(); + shader.SetUniform4f("u_Color",red, 0.3f, 0.8f, 1.0f); + if (red > 1.0f) increment = -0.05f; else if (red < 0.0f)