3
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ For 2., the issue is that textures aren't passed by texture id but by the active texture position they are bound to. I don't like that, too, but that's how OpenGL works. \$\endgroup\$
    – danijar
    Apr 9, 2013 at 8:41

2 Answers 2

7
\$\begingroup\$

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?

You don't.

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?

Again, you don't.

The correct way to handle this is to create a distinction between "object" and "mesh". An "object" is a house. An object can be composed of multiple "mesh"es. Each mesh represents a single rendering command. Thus, it is the vertex data, shader, and texture(s) needed to render that mesh.

So your house would have a mesh for the roof and a mesh for the walls.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Terse, but accurate =) Also highlights the downsides of creating elaborate data structures before knowing who and how that data will get used. \$\endgroup\$ Apr 9, 2013 at 6:20
  • \$\begingroup\$ If I can't get any dynamic allocation, how is the lightning done when I have multiple light sources? Currently my lightning code only has a single "omni" light calculation, but I'd like it to be able to handle n directional lights... \$\endgroup\$
    – Danicco
    Apr 9, 2013 at 15:08
  • 1
    \$\begingroup\$ @Danicco: That is a new question. So ask it with the "Ask a Question" button. But I can tell you right now that shaders don't have "dynamic allocation". Nor is that needed for lighting. Do some research on how lighting is typically handled before asking us. \$\endgroup\$ Apr 9, 2013 at 15:10
  • \$\begingroup\$ I concur, you need to do some basic research on just what vertex/pixel shaders do before coming back with a more focused question. \$\endgroup\$ Apr 9, 2013 at 15:24
0
\$\begingroup\$

You can probably have up to 16 activated textures in a shader program. And from the application, they should each be bound to their own texture unit, that is

glActivateTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, object->textures[i]->textureID);
glUniform1i(TextureLoc, 0);

glActivateTexture(GL_TEXTURE1);
...

glActivateTexture(GL_TEXTURE2);
...

You can also use a Uniform Buffer Object so you don't have to rebind all the textures every time you run the shader program.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .