If I have a mesh (such as a cube with 6 faces, each consisting individually of 4 vertices, totaling 24 vertices) and I want to apply a different texture to each face, how would I do this? Currently I draw the entire mesh (all 6 faces of开发者_运维百科 a cube) at once using glDrawElements(), supplying all of the indices into one buffer. I cannot see a way to apply a texture to a subset of the indices when drawing. Would I have to split up the indices, drawing each face one-by-one, rebinding textures between each face, or is there a more eloquent solution?
Yes, you will have to render each face differently (split it up accordingly and bind the texture for each face). It's not elegant and it's not pretty, but it's fairly simple if you have your indices in their own element buffer (a GL buffer, not a ShortBuffer
or something like that). You can just specify the offset into the buffer using glDrawElements
and the number of triangles to draw for each face. Then it goes something like this (pseudocode):
static class IndexRange
{
public int offset
public int numIndices
}
// ... later, down the hall and to the left ...
int faceTextures[] = {tex0, tex1, tex2, ...}
IndexRange faceIndices[] = {face0, face1, face2, ...}
// setup buffers and all that jazz
for (index = 0; index < numFaces; ++index)
{
IndexRange face = faceIndices[index]
int texture = faceTextures[index]
glBindTexture(GL_TEXTURE_2D, texture)
glDrawElements(GL_TRIANGLES, face.numIndices, GL_UNSIGNED_SHORT, face.offset)
}
If you're using a ShortBuffer
or something like that, I'm not entirely sure how you'd go about doing that, but I imagine you could probably slice it as needed and get the necessary buffers for each differently-textured face. Either way, the process remains relatively the same: split up the mesh and for each face, bind the texture and draw only those indices corresponding to that face.
You have to use few texture units. Check the documentation of function glClientActiveTexture()
in the specification. Also see this question.
UPDATE: You might also consider using libgdx, a very nice OpenGL ES wrapper (and more).
精彩评论