0
$\begingroup$

Background:

I am extracting the textures for a model with Assimp and sampling those textures in the fragment shader. I have multiple uniform sampler2D textureN declarations in my fragment shader and call glUniform1i(location, textureN), glActiveTexture(GL_TEXTUREN) and glBindTexture(GL_TEXTURE_2D, textureN_ref) for each texture. This is working correctly.

The following Mesh drawing function from learnopengl.com binds multiple textures. However, as shown below, the fragment shader only contains one sampler.

void Draw(Shader &shader) 
{
    unsigned int diffuseNr = 1;
    unsigned int specularNr = 1;
    for(unsigned int i = 0; i < textures.size(); i++)
    {
        glActiveTexture(GL_TEXTURE0 + i); // activate proper texture unit before binding
        // retrieve texture number (the N in diffuse_textureN)
        string number;
        string name = textures[i].type;
        if(name == "texture_diffuse")
            number = std::to_string(diffuseNr++);
        else if(name == "texture_specular")
            number = std::to_string(specularNr++);

        shader.setFloat(("material." + name + number).c_str(), i);
        glBindTexture(GL_TEXTURE_2D, textures[i].id);
    }
    glActiveTexture(GL_TEXTURE0);

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

Fragment shader:

#version 330 core
out vec4 FragColor;

in vec2 TexCoords;

uniform sampler2D texture_diffuse1;

void main()
{    
    FragColor = texture(texture_diffuse1, TexCoords);
}

Question:

Surely for the void Draw(Shader &shader) function to work you would have to define multiple samplers in the fragment shader? The linked article does in fact seem to state this in the paragraph prior to the function.

Thanks.

$\endgroup$
4
  • $\begingroup$ "The linked article does in fact seem to state this in the paragraph prior to the function." So... what's the problem? $\endgroup$ – Nicol Bolas Feb 5 at 17:15
  • $\begingroup$ Thanks for the reply. The actual shader used in the tutorial (shown above) doesn't follow the advice given in that paragraph. This seems contradictory? $\endgroup$ – MasterReDWinD Feb 6 at 9:21
  • $\begingroup$ Yes, it does. When I go to that page and search for "sampler", I see a shader that has 5 sampler variables in it. $\endgroup$ – Nicol Bolas Feb 6 at 14:34
  • $\begingroup$ That's true, in the text of the article he does describe a shader with 5 samplers but then two articles further on (titled 'Model') when we actually get the complete ('model_loading.cpp') code he creates the fragment shader ('1.model_loading.fs') with the conents shown above. I would have expected to see a fragment shader with the 5 samplers in. $\endgroup$ – MasterReDWinD Feb 6 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.