'array of sampler2D uses last bound texture [duplicate]

I am trying to implement a simple batchrenderer. I am using an array of sampler2D for it. however, the array is using the last bound texture instead of using the texture bound to the texture unit. I would really appreciate if anyone can help me out. here's my code:

Edit: the sampler2d array index is working fine. when i used FragColor = vec4(t, 0, 0, 0); it gave me a black quad and a red quad. But for some, it is using the last bound texture. both the quads are using texture2.

main.cpp

int main()
{
    Window window("DJROXZHIA", 800, 600, true);
    Shader shader("res/shaders/vertex.glsl", "res/shaders/fragment.glsl");
    shader.enable();

    float vertices[] = {
        0.0f, 0.0f, 0.0f,       0.0f,   0.0f, 0.0f,     1.0f, 1.0f, 1.0f, 1.0f,
        1.0f, 0.0f, 0.0f,       0.0f,   1.0f, 0.0f,     1.0f, 1.0f, 1.0f, 1.0f,
        1.0f, 1.0f, 0.0f,       0.0f,   1.0f, 1.0f,     1.0f, 1.0f, 1.0f, 1.0f,
        0.0f, 1.0f, 0.0f,       0.0f,   0.0f, 1.0f,     1.0f, 1.0f, 1.0f, 1.0f,

        -0.5f, -0.5f, 0.0f,     1.0f,   0.0f, 0.0f,     1.0f, 1.0f, 1.0f, 1.0f,
        -1.0f, -0.5f, 0.0f,     1.0f,   1.0f, 0.0f,     1.0f, 1.0f, 1.0f, 1.0f,
        -1.0f, -1.0f, 0.0f,     1.0f,   1.0f, 1.0f,     1.0f, 1.0f, 1.0f, 1.0f,
        -0.5f, -1.0f, 0.0f,     1.0f,   0.0f, 1.0f,     1.0f, 1.0f, 1.0f, 1.0f
    };

    unsigned int indices[] = {
        0, 1, 2, 2, 3, 0,
        4, 5, 6, 6, 7, 4
    };

    Texture texture("res/texture/DodgerCover.png");
    Texture texture2("res/texture/game-play-image.png"); //both the quads are using texture2

    GLuint vao, vbo, ibo;
    glGenVertexArrays(1, &vao);
    glGenBuffers(1, &vbo);
    glGenBuffers(1, &ibo);

    glBindVertexArray(vao);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);
    glEnableVertexAttribArray(3);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(DJROXZHIA::maths::Vertex2D), (void*)offsetof(DJROXZHIA::maths::Vertex2D, pos));
    glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, sizeof(DJROXZHIA::maths::Vertex2D), (void*)offsetof(DJROXZHIA::maths::Vertex2D, texture));
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(DJROXZHIA::maths::Vertex2D), (void*)offsetof(DJROXZHIA::maths::Vertex2D, texCoords));
    glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(DJROXZHIA::maths::Vertex2D), (void*)offsetof(DJROXZHIA::maths::Vertex2D, color));

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    GLfloat t[2] = { 0.0f, 1.0f };
    glUniform1fv(glGetUniformLocation(shader.getID(), "u_texture"), 2, t);

    while (!window.closed())
    {
        window.clear(glm::vec4(0.0f));

        glActiveTexture(GL_TEXTURE + 0);
        glBindTexture(GL_TEXTURE_2D, texture.getTextureID());
        glActiveTexture(GL_TEXTURE + 1);
        glBindTexture(GL_TEXTURE_2D, texture2.getTextureID());

        glBindVertexArray(vao);
        glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);

        window.tick();
    }

    return 0;
}

vertex.glsl
#version 330 core

layout (location = 0) in vec3 aPos;
layout (location = 1) in float aTexture;
layout (location = 2) in vec2 aTexCoords;
layout (location = 3) in vec4 aColor;

out float tex;
out vec2 texCoords;
out vec4 color;
out vec3 pos;

//uniform mat4 u_ProjView;

void main()
{
    gl_Position = vec4(aPos, 1.0);
    tex = aTexture;
    texCoords = aTexCoords;
    color = aColor;
    pos = aPos;
}

fragment.glsl
#version 330 core

in float tex;
in vec2 texCoords;
in vec4 color;
in vec3 pos;


uniform sampler2D u_texture[2];

out vec4 FragColor;

void main()
{
    int t = int(tex);
    FragColor = texture(u_texture[t], texCoords) * color;
}

Thanks in advance


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source