# GLSL C++ Only first sampler array index is accessible

I have the following shader, which whose fragment shader contains a sampler array of 16 elements.

Fragment

#version 330 core
in vec2 _uv;
flat in int _instanceID;

out vec4 color;

uniform sampler2D sampler[16];

void main() {
color = texture(sampler[_instanceID], _uv);
}


Now when I try to set the second array element using the following method

void Shader::setUniform(const std::string& uniform, GLuint* values, size_t amount) const {
bindProgram();

GLuint loc = getUniformLocation(uniform + "[0]");
for(int i = 0; i < amount; i++){
glUniform1ui(loc+i, values[i]);
std::cout << values[i] << " "; //For debug
}
std::cout << std::endl; //For debug
}


It just sets the first element of the shader. Even though I'm passing 2 values.

This is the output of the cout's:

5 4
5 4
5 4
5 4
...


Looking up the shader variables with GDebugger, only the first element is set. It also only draws the first (index 0) image.

• Any reason why you're not using uniform sampler2DArray in your GLSL and GL_TEXTURE_2D_ARRAY in your C++? You have #version 330 core so these are supported, and they're probably what you actually want to use instead of an array of samplers. – Maximus Minimus Dec 11 '18 at 12:11

getUniformLocation isn't required to be continuous for array uniforms, so instead of just fetching the location of "sampler[0]", you also need to find the location for "sampler[1]" and so on.

• That is the problem, if I try to get the uniform location of "sampler[1]", it does not find it. Only for "sampler[0]"! – Kiryu144 Dec 11 '18 at 17:14
• That was not apparent from the code you posted, please update the code sample if that is not the code you are using. – Tyyppi_77 Dec 11 '18 at 18:34