在 OpenGL ES 1.1 中将多个纹理绑定到一个网格
如果我有一个网格(例如有 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您必须以不同的方式渲染每个面(相应地将其分割并绑定每个面的纹理)。它并不优雅,也不漂亮,但如果您的索引位于自己的元素缓冲区(GL 缓冲区,而不是
ShortBuffer
或类似的东西)中,那么它相当简单。您只需使用glDrawElements 指定缓冲区中的偏移量以及为每个面绘制的三角形数量即可。然后它会像这样(伪代码):如果您使用
ShortBuffer
或类似的东西,我不完全确定您会如何去做,但我想你可能可以根据需要切片它并获得必要的每个不同纹理的面的缓冲区。无论哪种方式,过程都保持相对相同:分割网格并为每个面绑定纹理并仅绘制与该面相对应的索引。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 usingglDrawElements
and the number of triangles to draw for each face. Then it goes something like this (pseudocode):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.您必须使用很少的纹理单元。检查 中函数
glClientActiveTexture()
的文档规范。另请参阅此问题。更新:您也可以考虑使用 libgdx,一个非常好的 OpenGL ES 包装器(等等) 。
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).