glsl - Opengl error 1282 (invalid operation) when using texture() -


i have following fragment shader:

#version 330 core  layout (location = 0) out vec4 color;  uniform vec4 colour; uniform vec2 light_pos;  in data {     vec4 position;     vec2 texcoord;     float tid;     vec4 color; } fs_in;  uniform sampler2d textures[32];  void main() {     float intensity = 1.0 / length(fs_in.position.xy - light_pos);      vec4 texcolor = fs_in.color;     if (fs_in.tid > 0.0)     {         int tid = int(fs_in.tid + 0.5);         texcolor = texture(textures[tid], fs_in.texcoord);     }      color = texcolor * intensity; } 

if run program, opengl error 1282, invalid operation. if don't use texture(), write texcoord = vec4(...) works perfectly. i'm passing in tid (texture id) 0 (no texture) part shouldn't run. i've set textures uniform placeholder, far know shouldn't matter. cause invalid operation then?

your shader compilation has failed. make sure check compile status after trying compile shader, using:

glint val = gl_false; glgetshaderiv(shaderid, gl_compile_status, &val); if (val != gl_true) {     // compilation failed } 

in case, shader illegal because you're trying access array of samplers variable index:

texcolor = texture(textures[tid], fs_in.texcoord); 

this not supported in glsl 3.30. spec (emphasis added):

samplers aggregated arrays within shader (using square brackets [ ]) can indexed integral constant expressions (see section 4.3.3 “constant expressions”).

this restriction relaxed in later opengl versions. example, glsl 4.50 spec:

when aggregated arrays within shader, samplers can indexed dynamically uniform integral expression, otherwise results undefined.

this change introduced in glsl 4.00. still not sufficient case, since you're trying use in variable index, not dynamically uniform.

if textures same size, may want consider using array texture instead. allow sample 1 of layers in array texture based on dynamically calculated index.


Comments