Cascades
Cascades

Reputation: 644

OpenGL/GLSL multiple texture binding not working

There are a lot of answers for similar questions around, but I can't find a solution that works for me.

I have a scene with multiple mesh in, each with the following draw call:

shader.use();

for (unsigned int i = 0; i < textures.size(); i++)
{
    glActiveTexture(GL_TEXTURE0 + i);
    glBindTexture(GL_TEXTURE_2D, textures[i].id);
}

for (unsigned int i = 0; i < textures.size(); i++)
{
    glUniform1f(glGetUniformLocation(shader.ID, material_uniforms[i].c_str()), i);
}

glActiveTexture(GL_TEXTURE0);

// draw mesh
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);

and my frag shader uniforms (amongst others) are:

uniform sampler2D texture_ambient1;
uniform sampler2D texture_diffuse1;
uniform sampler2D texture_specular1;
uniform sampler2D texture_bump1;
uniform sampler2D texture_dissolve1;

In RenderDoc, although in the Resource Inspector all the textures seem to be listed, the "Inputs" in Texture Viewer (FS 0[0] texture_ambient1, FS 1[0] texture_diffuse1, FS 2[0] texture_dissolve1 and FS 3[0] texture_specular1 (I don't know where my other "texture_bump1" has gone?)) for each draw call seem to be exactly the same texture.

Also in RenderDoc under "Pipeline State" and the FS Stage, the Textures and Samplers each only have one bound item? Seems to contradict the fact RenderDoc sees all the above inputs?

Am I doing something obviously wrong in my code?

Upvotes: 1

Views: 280

Answers (1)

Wyck
Wyck

Reputation: 11720

Looks like you're trying to bind the texture unit using glUniform1f but the texture unit index is an integer (not a float), so you should be using glUniform1i to set that uniform.

glUniform1i(glGetUniformLocation(shader.ID, material_uniforms[i].c_str()), i);

Upvotes: 3

Related Questions