Skip to content

Commit

Permalink
Automatically bind shader program when setting uniforms
Browse files Browse the repository at this point in the history
  • Loading branch information
matteblair committed Sep 24, 2014
1 parent a40ad09 commit 2f8067d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 0 additions & 1 deletion core/tangram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ void renderFrame()
glClearColor(0.8f * sintsqr, 0.32f * sintsqr, 0.3f * sintsqr, 1.0f);
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

shader->use();
shader->setUniformf("u_time", t);

mesh->draw(shader);
Expand Down
11 changes: 11 additions & 0 deletions core/util/shaderProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,56 +164,67 @@ GLuint ShaderProgram::makeCompiledShader(const std::string& _src, GLenum _type)
}

void ShaderProgram::setUniformi(const std::string& _name, int _value) {
use();
GLint location = getUniformLocation(_name);
glUniform1i(location, _value);
}

void ShaderProgram::setUniformi(const std::string& _name, int _value0, int _value1) {
use();
GLint location = getUniformLocation(_name);
glUniform2i(location, _value0, _value1);
}

void ShaderProgram::setUniformi(const std::string& _name, int _value0, int _value1, int _value2) {
use();
GLint location = getUniformLocation(_name);
glUniform3i(location, _value0, _value1, _value2);
}

void ShaderProgram::setUniformi(const std::string& _name, int _value0, int _value1, int _value2, int _value3) {
use();
GLint location = getUniformLocation(_name);
glUniform4i(location, _value0, _value1, _value2, _value3);
}

void ShaderProgram::setUniformf(const std::string& _name, float _value) {
use();
GLint location = getUniformLocation(_name);
glUniform1f(location, _value);
}

void ShaderProgram::setUniformf(const std::string& _name, float _value0, float _value1) {
use();
GLint location = getUniformLocation(_name);
glUniform2f(location, _value0, _value1);
}

void ShaderProgram::setUniformf(const std::string& _name, float _value0, float _value1, float _value2) {
use();
GLint location = getUniformLocation(_name);
glUniform3f(location, _value0, _value1, _value2);
}

void ShaderProgram::setUniformf(const std::string& _name, float _value0, float _value1, float _value2, float _value3) {
use();
GLint location = getUniformLocation(_name);
glUniform4f(location, _value0, _value1, _value2, _value3);
}

void ShaderProgram::setUniformMatrix2f(const std::string& _name, float* _value, bool _transpose) {
use();
GLint location = getUniformLocation(_name);
glUniformMatrix2fv(location, 1, _transpose, _value);
}

void ShaderProgram::setUniformMatrix3f(const std::string& _name, float* _value, bool _transpose) {
use();
GLint location = getUniformLocation(_name);
glUniformMatrix3fv(location, 1, _transpose, _value);
}

void ShaderProgram::setUniformMatrix4f(const std::string& _name, float* _value, bool _transpose) {
use();
GLint location = getUniformLocation(_name);
glUniformMatrix4fv(location, 1, _transpose, _value);
}
3 changes: 3 additions & 0 deletions core/util/shaderProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class ShaderProgram {
*/
void use() const;

/*
* setUniform - ensures the program is bound and then sets the named uniform to the given value(s)
*/
void setUniformi(const std::string& _name, int _value);
void setUniformi(const std::string& _name, int _value0, int _value1);
void setUniformi(const std::string& _name, int _value0, int _value1, int _value2);
Expand Down

0 comments on commit 2f8067d

Please sign in to comment.