uniform array of sampler2D only getting 1 texture

Viewed 104

I would like to make my fragment shader take in multiple sampler2D's passed in as a form of uniform sampler2D u_Textures[3]. The vertex buffer has at the end of each verticie one value representing witch texture to sample from(I call it index). I am trying to render multiple textures in the same drawcall, but the program only shows one texture for every index I give to it. My fragment shader code:

#version 450 core

layout(location = 0) out vec4 out_Color;

in vec2 v_TexCoord;
in float v_texIndex;

uniform sampler2D u_Textures[3];


void main() 
{
    int ind = int(v_texIndex);

    out_Color = texture(u_Textures[ind], v_TexCoord);
}

This is how I acces the "u_Textures" to populate it:

unsigned int loc1 = glGetUniformLocation(sh.getRendererID(), "u_Textures");
GLfloat values[3] = { 0.0f, 1.0f, 2.0f };
glUniform1fv(loc1, 3, values);

This is how I load in the textures in memory from my 'Texture' class:

    glGenTextures(1, &m_RendererID);
    glBindTexture(GL_TEXTURE_2D, m_RendererID);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer);

and how I bind the texture:

void Texture::Bind(int slot) const {
    glActiveTexture(slot + GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, m_RendererID);
}

I created 2 textures and bound them to different slots(1 and 2), and I try to draw 2 squares, 1 with each texture.

Texture tex1(path1);
Texture tex2(path2);
tex1.Bind(1);
tex2.Bind(2);

However, the output no matter how I change the texture Index or how I bind the textures is I get the same texture in both squares. I should mention that the line int ind = int(v_texIndex); works good and it passes the right value. What could be wrong here?

1 Answers

I am trying to render multiple textures in the same drawcall

Well... you can't.

The index used in an array of samplers must be a dynamically uniform expression. If the expression results in different values within the same draw call, then it's not dynamically uniform. And thus, you cannot use it as an index.

The layer index for array textures can be non-uniform. But the index into arrays of samplers cannot.

Related