1

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).

Community
  • 1
  • 1

1 Answers1

0

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.

Community
  • 1
  • 1
Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106
  • That might be true, but remember I'm using ints so that *4 is the 4 bytes of an int, any higher or lower a number and I get bad access crashes. – user1828526 Sep 19 '13 at 04:47
  • The size of an int has nothing to do with the number of elements in your draw call. Remove the *4 and you should get proper results. – Andon M. Coleman Sep 19 '13 at 04:59
  • Darn, I think you guys are right, I think I calculated the amount of elements wrong. I'll report back when I've recalculated. – user1828526 Sep 19 '13 at 06:49
  • Thanks Andon you are a legend! I figured it out and fixed it up. http://img845.imageshack.us/img845/1971/14c3.png I need to buy you a beer one day ;) – user1828526 Sep 19 '13 at 09:26