I have this (pseudo) code:
unsigned int TextureLoc = glGetUniformLocation(programID, "objectTexture");
for(int i = 0; i < object->texturesCount; i++)
{
glActivateTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, object->textures[i]->textureID);
glUniform1i(TextureLoc, 0);
}
And Fragment Shader code:
in vec2 UV;
out vec3 color;
uniform sampler2D objectTexture;
void main()
{
color = texture2D(objectTexture, UV).rgb;
}
My example is that of a house model that composes a single object, but the house walls are of Texture#1 and the roof is of Texture#2.
At first I coded that each object had it's own material/textures, but then I noticed it's a waste of memory if I'm using the same texture in multiple places, so now I have a list of objects and a list of textures. An object can have n textures.
1. How can I get a "new" in GLSL for dynamic textures count? Some objects might have 1, some might have 10, etc... How can I control that?
2. I have an array specifying which textureID each triangle in the object uses, but how can I use this information to properly draw the object? I send the array in the glEnableVertexAttribArray(int) function, but how can I know which texture I'm supposed to use when I'm in the shader code?
Another option I thought of was to divide all objects into smaller objects if they don't have the same texture, but I'm not sure what's the best approach to this.