More on shaders to classes
This commit is contained in:
parent
a42eabd694
commit
a5c84e31d2
3 changed files with 30 additions and 10 deletions
|
@ -11,29 +11,41 @@
|
||||||
Shader::Shader(const std::string &filepath)
|
Shader::Shader(const std::string &filepath)
|
||||||
: m_FilePath(filepath), m_RendererID(0)
|
: m_FilePath(filepath), m_RendererID(0)
|
||||||
{
|
{
|
||||||
//ShaderProgramSource source = ParseShader(m_FilePath);
|
ShaderProgramSource source = ParseShader(m_FilePath);
|
||||||
//m_RendererID = CreateShader(source.VertexSource, source.FragmentSource);
|
m_RendererID = CreateShader(source.VertexSource, source.FragmentSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Shader::~Shader()
|
Shader::~Shader()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::Bind()
|
void Shader::Bind()
|
||||||
{
|
{
|
||||||
|
GLCall(glUseProgram(m_RendererID));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::Unbind()
|
void Shader::Unbind()
|
||||||
{
|
{
|
||||||
|
GLCall(glUseProgram(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
//Set uniforms
|
//Set uniforms
|
||||||
void Shader::SetUniform4f(const std::string &name, float v0, float v1, float v2, float v3)
|
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)
|
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;
|
int length;
|
||||||
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
|
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
|
||||||
|
|
||||||
LOG("Length: " << length);
|
LOG("Length: " << length);
|
||||||
|
|
||||||
char *message = (char *)malloc(length * sizeof(char));
|
char *message = (char *)malloc(length * sizeof(char));
|
||||||
glGetShaderInfoLog(id, length, &length, message);
|
glGetShaderInfoLog(id, length, &length, message);
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
struct ShaderProgramSource
|
struct ShaderProgramSource
|
||||||
{
|
{
|
||||||
|
@ -14,6 +15,7 @@ struct ShaderProgramSource
|
||||||
class Shader
|
class Shader
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
std::unordered_map<std::string,unsigned int> m_UniformLocationCache;
|
||||||
unsigned int m_RendererID;
|
unsigned int m_RendererID;
|
||||||
std::string m_FilePath;
|
std::string m_FilePath;
|
||||||
ShaderProgramSource ParseShader(const std::string &filepath);
|
ShaderProgramSource ParseShader(const std::string &filepath);
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
#include "VertexArray.h"
|
#include "VertexArray.h"
|
||||||
#include "utils.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)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
@ -70,11 +70,14 @@ int main(void)
|
||||||
|
|
||||||
IndexBuffer ib(indexes,6);
|
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
|
va.Unbind();
|
||||||
//int uniform_location = glGetUniformLocation(shader, "u_color");
|
vb.Unbind();
|
||||||
|
ib.Unbind();
|
||||||
//if (uniform_location == -1) LOG("Error fetching uniform location");
|
shader.Unbind();
|
||||||
|
|
||||||
float red = 0.0f;
|
float red = 0.0f;
|
||||||
float increment = 0.05f;
|
float increment = 0.05f;
|
||||||
|
@ -104,6 +107,9 @@ int main(void)
|
||||||
|
|
||||||
//GLCall(glDrawElements(GL_TRIANGLES, 6 * sizeof(uint), GL_UNSIGNED_INT, nullptr));
|
//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)
|
if (red > 1.0f)
|
||||||
increment = -0.05f;
|
increment = -0.05f;
|
||||||
else if (red < 0.0f)
|
else if (red < 0.0f)
|
||||||
|
|
Loading…
Reference in a new issue