More on shaders to classes

This commit is contained in:
Vitor Fernandes 2020-06-21 22:17:44 +01:00
parent a42eabd694
commit a5c84e31d2
No known key found for this signature in database
GPG key ID: EBFB4EE09F348A26
3 changed files with 30 additions and 10 deletions

View file

@ -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;
}
/**

View file

@ -1,6 +1,7 @@
#pragma once
#include <string>
#include <unordered_map>
struct ShaderProgramSource
{
@ -14,6 +15,7 @@ struct ShaderProgramSource
class Shader
{
private:
std::unordered_map<std::string,unsigned int> m_UniformLocationCache;
unsigned int m_RendererID;
std::string m_FilePath;
ShaderProgramSource ParseShader(const std::string &filepath);

View file

@ -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)