Skip to content

Commit

Permalink
Add basic shader uniform setters
Browse files Browse the repository at this point in the history
  • Loading branch information
matteblair committed Sep 24, 2014
1 parent a24ce93 commit 8c0b526
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core/tangram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ const std::string vertShaderSrc =
"#ifdef GL_ES\n"
"precision mediump float;\n"
"#endif\n"
"uniform float u_time;\n"
"attribute vec4 a_position;\n"
"attribute vec4 a_color;\n"
"varying vec4 v_color;\n"
"void main() {\n"
" v_color = a_color;\n"
" gl_Position = a_position;\n"
" gl_Position.x *= sin(u_time);"
"}\n";

const std::string fragShaderSrc =
Expand Down Expand Up @@ -84,11 +86,14 @@ 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);

GLenum glError = glGetError();
if (glError) {
logMsg("%s\n", "glError!!");
logMsg("GL Error %d!!!\n", glError);
}

}
55 changes: 55 additions & 0 deletions core/util/shaderProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,58 @@ GLuint ShaderProgram::makeCompiledShader(const std::string& _src, GLenum _type)
return shader;

}

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

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

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

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

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

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

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

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

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

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

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

This comment has been minimized.

Copy link
@tallytalwar

tallytalwar Sep 24, 2014

Member

All these look good.

14 changes: 14 additions & 0 deletions core/util/shaderProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ class ShaderProgram {
*/
void use() const;

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);
void setUniformi(const std::string& _name, int _value0, int _value1, int _value2, int _value3);

void setUniformf(const std::string& _name, float _value);
void setUniformf(const std::string& _name, float _value0, float _value1);
void setUniformf(const std::string& _name, float _value0, float _value1, float _value2);
void setUniformf(const std::string& _name, float _value0, float _value1, float _value2, float _value3);

void setUniformMatrix2f(const std::string& _name, float* _value, bool transpose = false);
void setUniformMatrix3f(const std::string& _name, float* _value, bool transpose = false);
void setUniformMatrix4f(const std::string& _name, float* _value, bool transpose = false);

private:

struct ShaderLocation {
Expand Down

0 comments on commit 8c0b526

Please sign in to comment.