PDA

View Full Version : How to draw a mesh with multiple textures



stilianos
09-18-2013, 06:23 AM
My implementation doesn't seem to be working and I don't know why.

Here are the facts of my code:

-textureArray is an NSMutableArray populated by GLKTextureInfo objects
-groupMesh is an array of a struct that contains:
-a pointer to the place in the index array that we want to get indices from.
-the size of the index data

-I have one element array buffer for my vertices and one for my indices

I decided to make a for loop. In each iteration I bind a different texture from the GLKTextureInfo array, and I change the pointer to the area of memory of the index data I want to draw with the texture that I just bound.


for (int i = 0; i<mesh->numMeshes-1; i++)
{
glBindTexture(GL_TEXTURE_2D,
[(GLKTextureInfo *)[textureArray objectAtIndex:i] name]);


glDrawElements(GL_TRIANGLES,
mesh->groupMesh[i].indexDataSize*4,
GL_UNSIGNED_INT,
mesh->groupMesh[i].indexPointer);

}

The first texture in the array is a tree bark texture, the second texture is tree leaves.

The textures aren't binding after the first iteration however. Which is giving the result of all the mesh pieces using the same texture, basically the whole tree and the leaves are using the bark texture.

I forced the loop to test if my theory was correct and changed objectAtIndex:i to objectAtIndex:1, and the leaf texture appeared all over the tree this time.

So it just seems to be glBindTexture that isn't working, is it because opengl is already in the draw state? Is there a way around this?

Thanks.