PDA

View Full Version : How to acces array by a variable in GLSL?



prince_polka
03-19-2017, 03:33 PM
Sorry if this is a stupid question,
I have an array but I can not access it unless I hard code it?


vec4 n[4];
vec4 color;
int c = 3;
if (c==0) color = n[0];
if (c==1) color = n[1];
if (c==2) color = n[2];
if (c==3) color = n[3];
// color = n[c]; doesn't work

GClements
03-19-2017, 09:19 PM
In most cases, arrays can be indexed with any integer expression.

Arrays of opaque types (e.g. samplers or images) can only be indexed with constant, uniform or dynamically-uniform expressions, depending upon GLSL version.

Arrays need to be explicitly sized if they're indexed with non-constant expressions.