|
From: Lasse Kärkkäi. <tr...@us...> - 2011-01-27 23:27:06
|
Module: performous
Branch: opengl2
Commit: ba00f939525b1071cc523f657bf5342157090ddf
Author: Lasse Karkkainen <tro...@tr...>
Date: Fri Jan 28 00:26:49 2011 +0100
Fix Shader::operator[] and add some error handling to it. Add setUniformMatrix.
---
game/glshader.cc | 10 +++++++---
game/glshader.hh | 9 ++++++++-
2 files changed, 15 insertions(+), 4 deletions(-)
diff --git a/game/glshader.cc b/game/glshader.cc
index 0fa3c2b..9f35fe2 100644
--- a/game/glshader.cc
+++ b/game/glshader.cc
@@ -118,8 +118,12 @@ void Shader::bind() {
GLint Shader::operator[](const std::string& uniform) {
+ // Try to use a cached value
UniformMap::iterator it = uniforms.find(uniform);
- if (it == uniforms.end())
- it->second = glGetUniformLocation(program, uniform.c_str());
- return it->second;
+ if (it != uniforms.end()) return it->second;
+ // Get the value and cache it
+ GLint var = glGetUniformLocation(program, uniform.c_str());
+ if (var == -1) throw std::logic_error("GLSL shader uniform variable not found: " + uniform);
+ return uniforms[uniform] = var;
}
+
diff --git a/game/glshader.hh b/game/glshader.hh
index 57587e9..121d6a5 100644
--- a/game/glshader.hh
+++ b/game/glshader.hh
@@ -48,6 +48,13 @@ struct Shader: public boost::noncopyable {
Shader& setUniform(const std::string& uniform, float x, float y, float z, float w) {
glUniform4f((*this)[uniform], x, y, z, w); return *this;
}
+ Shader& setUniformMatrix(const std::string& uniform, GLfloat const* m) {
+ glUniformMatrix4fv((*this)[uniform], 1, GL_FALSE, m); return *this;
+ }
+ Shader& setUniformMatrix(const std::string& uniform, GLdouble const* m) {
+ // Note: need to convert into float because glUniformMatrix4dv is NULL on my machine
+ GLfloat arr[16]; std::copy(m, m + 16, arr); return setUniformMatrix(uniform, arr);
+ }
/** Get uniform location. Uses caching internally. */
GLint operator[](const std::string& uniform);
@@ -66,7 +73,7 @@ struct Shader: public boost::noncopyable {
return shader_progs[i];
}
- private:
+private:
GLuint program; ///< shader program object id
int gl_response; ///< save last return state
|