• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

How to pass a vec4 array to a shader? (GLSL)

Kaliam

Member
Hello, I'm just trying to use a shader uniform that is declared as such:
Code:
uniform vec4 lightMap[32];
This compiles just fine in GLSL, however, there isn't a GMS2 function that is specifically for a vec4. I've tried using a float array of arrays that represent the vec4's in an array but can't seem to get it to work. This is the code that I tried.
Code:
//create event
u_lightMap = shader_get_uniform(shdr_depthShadow,"lightMap")
testVec4 = [1.0, 1.0, 1.0, 1.0];
vec4Array = array_create(32,testVec4);

//draw event
shader_set(shdr_lightingShader);
shader_set_uniform_f_array(u_lightMap,vec4Array);
/*Draw all the things*/
shader_reset();
Which function would I use for this? Is it even possible to do in GMS2?
Code:
shader_set_uniform_f_array()
shader_set_uniform_i_array()
shader_set_uniform_matrix_array()
Any information is appreciated, I could represent the vec4 data in just a regular float array, but it would be awesome if we could just use a vec4[ ] array and not have to pass through everything in one messy float array.

Thanks!
 

Kaliam

Member
Well I managed to figure it out, basically you just send in an array that has all the vector data in order, depending on the vector you chose. The syntax is like this if doing an vec4 array uniform:

Code:
//create event
u_lightMap = shader_get_uniform(shdr_depthShadow,"lightMap")

//draw event
shader_set(shdr_lightingShader);

/*
Each row in the code is a vec4, this will be different if 
using a vec2 or vec3. You can build your array earlier too
and just pass in the variable.
*/
shader_set_uniform_f_array(u_lightMap, [1.0,1.0,1.0,1.0,
                                        1.0,1.0,1.0,1.0,
                                        1.0,1.0,1.0,1.0,
                                        1.0,1.0,1.0,1.0,
                                        1.0,1.0,1.0,1.0,
                                        1.0,1.0,1.0,1.0]);
                                      //R    G    B    A
                                      //X    Y    Z    W
shader_reset();

//Fragment Shader
uniform vec4 lightMap[6];

//reference like this:
lightMap[Index].rgba
It's a little bit annoying as you need build everything into one array before you can pass it to the shader uniform, but will help lots by organizing shader code into vectors.
 
Top