Question

I'm following the answer from this thread: Binding Multiple Textures to One Mesh in OpenGL ES 1.1

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 this kind of result:

http://img69.imageshack.us/img69/5138/tbko.png

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:

http://img266.imageshack.us/img266/5598/c05n.png

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?

Note:(I asked a similar question yesterday, but now I've done a bit more research and still I don't know what I'm doing wrong).

Was it helpful?

Solution

The more I think about it, your index data may in fact be to blame here.

First, GL_UNSIGNED_INT is a terrible choice for vertex array element index. You rarely need 4.2 billion vertices, GL_UNSIGNED_SHORT (up to 65536 vertices) is the preferred index type - especially on embedded devices. GL_UNSIGNED_BYTE may be tempting for meshes with fewer than 256 vertices, but most hardware cannot natively support 8-bit indices so you just put more work on the driver.

Now onto what might actually be causing this problem:

You are using mesh>groupMesh[i].indexDataSize*4 for the number of vertices to draw. This will overrun your index array and indexDataSize*3-many vertices will be invalid. As weird as it sounds, since 3/4 of your drawn vertices invoke undefined behavior, this could be the cause of your texturing issues.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top