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.