I want to specify textures to be used when I render an array of sprites. So I put a texture index in their vertex data, and pass it as a flat value from my vertex shader to my fragment shader, but can't use it to index an array of samplers as expected because compiler sees it as "non-constant". Instead I have to resort to the disgusting code below. Can anyone explain what is going on here?
const int numTextures = 2;
uniform sampler2D textures[numTextures];
in vec2 uv;
flat in int tex;
out vec4 colour;
void main(void)
{
// this caused the compiler error
/// "sampler arrays indexed with non-constant expressions"
// colour = texture( textures[ tex ], uv );
// hence this (ugh) ...
switch ( tex )
{
case 0:
colour = texture( textures[0], uv );
break;
case 1:
colour = texture( textures[1], uv );
break;
default:
colour = vec4( 0.3f, 0.3f, 0.3f, 1.0f );
break;
};
}