Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  danijar Apr 9 '13 at 8:41

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
2  
Terse, but accurate =) Also highlights the downsides of creating elaborate data structures before knowing who and how that data will get used. –  Patrick Hughes Apr 9 '13 at 6:20
    
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... –  Danicco Apr 9 '13 at 15:08
1  
@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. –  Nicol Bolas Apr 9 '13 at 15:10
    
I concur, you need to do some basic research on just what vertex/pixel shaders do before coming back with a more focused question. –  Patrick Hughes Apr 9 '13 at 15:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.