diff --git a/J/shader.h b/J/shader.h index 9f6ca94..3d2eed1 100644 --- a/J/shader.h +++ b/J/shader.h @@ -108,8 +108,8 @@ public: void set_attribute(const std::string& name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid* ptr) const { auto it = attributes.find(name); if(it == attributes.end()) throw std::runtime_error(name + " attribute could not be found"); + glEnableVertexAttribArray(it->second); glVertexAttribPointer(it->second, size, type, normalized, stride, ptr); - glEnableVertexAttribArray(it->second); } // *************** @@ -117,88 +117,88 @@ public: // float vectors (multiple values) void set_uniform(char const* name, float x) const { - glUniform1f(glGetUniformLocation(program, name), x); + glUniform1f(get_uniform_location(name), x); } void set_uniform(char const* name, float x, float y) const { - glUniform2f(glGetUniformLocation(program, name), x, y); + glUniform2f(get_uniform_location(name), x, y); } void set_uniform(char const* name, float x, float y, float z) const { - glUniform3f(glGetUniformLocation(program, name), x, y, z); + glUniform3f(get_uniform_location(name), x, y, z); } void set_uniform(char const* name, float x, float y, float z, float w) const { - glUniform4f(glGetUniformLocation(program, name), x, y, z, w); + glUniform4f(get_uniform_location(name), x, y, z, w); } // integer vectors (multiple values) void set_uniform(char const* name, int x) const { - glUniform1i(glGetUniformLocation(program, name), x); + glUniform1i(get_uniform_location(name), x); } void set_uniform(char const* name, int x, int y) const { - glUniform2i(glGetUniformLocation(program, name), x, y); + glUniform2i(get_uniform_location(name), x, y); } void set_uniform(char const* name, int x, int y, int z) const { - glUniform3i(glGetUniformLocation(program, name), x, y, z); + glUniform3i(get_uniform_location(name), x, y, z); } void set_uniform(char const* name, int x, int y, int z, int w) const { - glUniform4i(glGetUniformLocation(program, name), x, y, z, w); + glUniform4i(get_uniform_location(name), x, y, z, w); } // Float vectors (array) void set_uniform(char const* name, const GLfloat (&value)[4]) const { - glUniform4fv(glGetUniformLocation(program, name), 1, value); + glUniform4fv(get_uniform_location(name), 1, value); } void set_uniform(char const* name, const GLfloat (&value)[3]) const { - glUniform3fv(glGetUniformLocation(program, name), 1, value); + glUniform3fv(get_uniform_location(name), 1, value); } void set_uniform(char const* name, const GLfloat (&value)[2]) const { - glUniform2fv(glGetUniformLocation(program, name), 1, value); + glUniform2fv(get_uniform_location(name), 1, value); } void set_uniform(char const* name, const GLfloat (&value)[1]) const { - glUniform1fv(glGetUniformLocation(program, name), 1, value); + glUniform1fv(get_uniform_location(name), 1, value); } // Integer vectors (array) void set_uniform(char const* name, const GLint (&value)[4]) const { - glUniform4iv(glGetUniformLocation(program, name), 1, value); + glUniform4iv(get_uniform_location(name), 1, value); } void set_uniform(char const* name, const GLint (&value)[3]) const { - glUniform3iv(glGetUniformLocation(program, name), 1, value); + glUniform3iv(get_uniform_location(name), 1, value); } void set_uniform(char const* name, const GLint (&value)[2]) const { - glUniform2iv(glGetUniformLocation(program, name), 1, value); + glUniform2iv(get_uniform_location(name), 1, value); } void set_uniform(char const* name, const GLint (&value)[1]) const { - glUniform1iv(glGetUniformLocation(program, name), 1, value); + glUniform1iv(get_uniform_location(name), 1, value); } // matrices // NOTE: in ES2 there are only square matrices void set_uniform(char const* name, const GLfloat (&value)[16], GLboolean trans) const { - glUniformMatrix4fv(glGetUniformLocation(program, name), 1, trans, value); + glUniformMatrix4fv(get_uniform_location(name), 1, trans, value); } void set_uniform(char const* name, const std::array & value, GLboolean trans) const { - glUniformMatrix4fv(glGetUniformLocation(program, name), 1, trans, &value[0]); + glUniformMatrix4fv(get_uniform_location(name), 1, trans, &value[0]); } void set_uniform(char const* name, const GLfloat (&value)[9], GLboolean trans) const { - glUniformMatrix3fv(glGetUniformLocation(program, name), 1, trans, value); + glUniformMatrix3fv(get_uniform_location(name), 1, trans, value); } void set_uniform(char const* name, const GLfloat (&value)[4], GLboolean trans) const { - glUniformMatrix2fv(glGetUniformLocation(program, name), 1, trans, value); + glUniformMatrix2fv(get_uniform_location(name), 1, trans, value); } // textures @@ -227,6 +227,14 @@ public: } private: + mutable std::map uniforms; + GLint get_uniform_location(char const * name) const { + auto it = uniforms.find(name); + if(it != uniforms.end()) + return it->second; + + return uniforms[name] = glGetUniformLocation(program, name); + } // ******************* // compiling / linking