Browse Source

Attributes-map in shader, and bug fix.

master
Joshua Moerman 12 years ago
parent
commit
988683baee
  1. 50
      J/shader.h

50
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 { void set_attribute(const std::string& name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid* ptr) const {
auto it = attributes.find(name); auto it = attributes.find(name);
if(it == attributes.end()) throw std::runtime_error(name + " attribute could not be found"); if(it == attributes.end()) throw std::runtime_error(name + " attribute could not be found");
glVertexAttribPointer(it->second, size, type, normalized, stride, ptr);
glEnableVertexAttribArray(it->second); glEnableVertexAttribArray(it->second);
glVertexAttribPointer(it->second, size, type, normalized, stride, ptr);
} }
// *************** // ***************
@ -117,88 +117,88 @@ public:
// float vectors (multiple values) // float vectors (multiple values)
void set_uniform(char const* name, float x) const { 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 { 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 { 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 { 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) // integer vectors (multiple values)
void set_uniform(char const* name, int x) const { 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 { 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 { 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 { 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) // Float vectors (array)
void set_uniform(char const* name, const GLfloat (&value)[4]) const { 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 { 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 { 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 { 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) // Integer vectors (array)
void set_uniform(char const* name, const GLint (&value)[4]) const { 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 { 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 { 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 { 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 // matrices
// NOTE: in ES2 there are only square matrices // NOTE: in ES2 there are only square matrices
void set_uniform(char const* name, const GLfloat (&value)[16], GLboolean trans) const { 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<GLfloat, 16> & value, GLboolean trans) const { void set_uniform(char const* name, const std::array<GLfloat, 16> & 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 { 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 { 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 // textures
@ -227,6 +227,14 @@ public:
} }
private: private:
mutable std::map<std::string, GLint> 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 // compiling / linking