绑定多个纹理一个网格中的OpenGL ES 1.1多个、纹理、网格、绑定

2023-09-06 04:59:34 作者:捂耳听风

如果我有一个网格(如用6个面,每个单独的4个顶点组成的立方体,共计24个顶点),我想不同的纹理应用到每个面,我将如何做到这一点?目前,我画一次使用glDrawElements整个网格(所有6个面的立方体)(),提供所有指标到一个缓冲区。我看不到的方式绘制时纹理应用到指数的子集。我将不得不分割的指数,绘制每个面一个接一个,每个面之间重新绑定纹理,或者是​​有一个更雄辩的解决方案?

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?

推荐答案

是的,你必须使每个面不同(相应的分割它,并结合每个面纹理)。这不是优雅的,它不是pretty,但如果你有你的指数在自己的元素缓冲器(GL缓冲区,而不是一个 ShortBuffer 或类似的东西,这是相当简单)。你可以只指定偏移使用 glDrawElements 缓冲和三角形的数量来绘制每个面。然后,它是这样的(伪code):

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

如果您使用的是 ShortBuffer 或类似的东西,我不是的完全的肯定你会如何去这样做,但我想你也许可以片它根据需要并获得必要的缓冲区每个不同纹理的脸。无论哪种方式,该过程保持相对的相同:分裂网格和每个面,结合的质地和绘制仅对应于该面这些索引

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.