[GEM-dev] supporting arrays of uniforms

Claude Heiland-Allen claude at goto10.org
Mon Jun 4 22:34:20 CEST 2012


Hi,

I have a shader (2) with:

----8<----
uniform vec2 foo[8];
----8<----

which I can't fully use in vanilla Gem:  glGetActiveUniform (1) seems to 
return the name "foo[0]", and Gem uses glUniform2f(...) which only 
allows me to set the first 2 floats in the array (ie, the first element) 
with a message like "foo[0] 1.2 3.4" to [glsl_program].

Luckily a quick/hacky fix (using glUniform2fv with the m_size returned 
from glGet) I tried turned out to work very well, and could probably be 
extended to a full fix for all these array things:

----8<----

diff --git a/src/Manips/glsl_program.cpp b/src/Manips/glsl_program.cpp
index 52e80d1..4eabb8a 100644
--- a/src/Manips/glsl_program.cpp
+++ b/src/Manips/glsl_program.cpp
@@ -214,7 +214,7 @@ void glsl_program :: renderGL2()
                  glUniform1f( m_loc[i], 
static_cast<GLfloat>(m_param[i][0]) );
                 break;
                case GL_FLOAT_VEC2:
-                glUniform2f( m_loc[i], 
static_cast<GLfloat>(m_param[i][0]), static_cast<GLfloat>(m_param[i][1]) );
+                glUniform2fv( m_loc[i], m_size[i], 
static_cast<GLfloat*>(&m_param[i][0]) );
                 break;
                case GL_FLOAT_VEC3:
                  glUniform3f( m_loc[i], 
static_cast<GLfloat>(m_param[i][0]), static_cast<GLfloat>(m_param[i][1]),

----8<----

I still have to use the uniform name "foo[0]", but I can now set all the 
values I want (so far tested with 10 floats (5 vec2s), and all seems 
groovy.  I see no reason why it shouldn't work for a single vec2, but I 
haven't tested that yet.

Thanks,


Claude

(1) http://www.opengl.org/sdk/docs/man/xhtml/glGetActiveUniform.xml

(2)
WebGL:   https://gitorious.org/maximus/fractaloids
Haskell: http://archive.org/details/ClaudiusMaximus_-_Fractal_Juggling
Pd+Gem:  (coming soon)



More information about the GEM-dev mailing list