Browse Source

Add shader uniform data setters

master
Riyyi 4 years ago
parent
commit
3a0a6bc610
  1. 64
      inferno/src/inferno/render/shader.cpp
  2. 14
      inferno/src/inferno/render/shader.h

64
inferno/src/inferno/render/shader.cpp

@ -1,6 +1,7 @@
#include <vector> // std::vector #include <vector> // std::vector
#include <glad/glad.h> #include <glad/glad.h>
#include <glm/gtc/type_ptr.hpp> // glm::value_ptr
#include "inferno/assertions.h" #include "inferno/assertions.h"
#include "inferno/core.h" #include "inferno/core.h"
@ -40,21 +41,72 @@ namespace Inferno {
// ----------------------------------------- // -----------------------------------------
void Shader::bind() const void Shader::setInt(const std::string &name, int value)
{ {
glUseProgram(m_program); // Set unifrom int
GLint location = glGetUniformLocation(m_program, name.c_str());
glUniform1i(location, value);
} }
void Shader::unbind() const void Shader::setFloat(const std::string &name, float f1) const
{ {
glUseProgram(0); // Set uniform float
GLint location = glGetUniformLocation(m_program, name.c_str());
glUniform1f(location, f1);
}
void Shader::setFloat(const std::string &name, float f1, float f2, float f3, float f4) const
{
// Set uniform vec4 data
GLint location = glGetUniformLocation(m_program, name.c_str());
glUniform4f(location, f1, f2, f3, f4);
}
void Shader::setFloat(const std::string &name, glm::vec2 value) const
{
// Set uniform vec2 data
GLint location = glGetUniformLocation(m_program, name.c_str());
glUniform2f(location, value.x, value.y);
}
void Shader::setFloat(const std::string &name, glm::vec3 value) const
{
// Set uniform vec3 data
GLint location = glGetUniformLocation(m_program, name.c_str());
glUniform3f(location, value.x, value.y, value.z);
}
void Shader::setFloat(const std::string &name, glm::vec4 value) const
{
// Set uniform vec4 data
GLint location = glGetUniformLocation(m_program, name.c_str());
glUniform4f(location, value.x, value.y, value.z, value.w);
}
void Shader::setFloat(const std::string &name, glm::mat3 matrix) const
{
// Set uniform mat3 data
GLint location = glGetUniformLocation(m_program, name.c_str());
glUniformMatrix3fv(location, 1, GL_FALSE, glm::value_ptr(matrix));
}
void Shader::setFloat(const std::string &name, glm::mat4 matrix) const
{
// Set uniform mat4 data
GLint location = glGetUniformLocation(m_program, name.c_str());
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(matrix));
} }
// ----------------------------------------- // -----------------------------------------
uint32_t Shader::getProgram() const void Shader::bind() const
{ {
return m_program; glUseProgram(m_program);
}
void Shader::unbind() const
{
glUseProgram(0);
} }
// ----------------------------------------- // -----------------------------------------

14
inferno/src/inferno/render/shader.h

@ -13,6 +13,18 @@ namespace Inferno {
Shader(const std::string &vertexSource, const std::string &fragmentSource); Shader(const std::string &vertexSource, const std::string &fragmentSource);
~Shader(); ~Shader();
// -----------------------------------------
// Set uniform data
void setInt(const std::string& name, int value);
void setFloat(const std::string &name, float f1) const;
void setFloat(const std::string &name, float f1, float f2, float f3, float f4) const;
void setFloat(const std::string &name, glm::vec2 v) const;
void setFloat(const std::string &name, glm::vec3 v) const;
void setFloat(const std::string &name, glm::vec4 v) const;
void setFloat(const std::string &name, glm::mat3 m) const;
void setFloat(const std::string &name, glm::mat4 m) const;
// ----------------------------------------- // -----------------------------------------
void bind() const; void bind() const;
@ -20,7 +32,7 @@ namespace Inferno {
// ----------------------------------------- // -----------------------------------------
uint32_t getProgram() const; inline uint32_t getProgram() const { return m_program; }
// ----------------------------------------- // -----------------------------------------

Loading…
Cancel
Save