gpt4 book ai didi

iphone - 如何在 OpenGL ES (iPhone) 中批量渲染 Sprite

转载 作者:行者123 更新时间:2023-12-03 19:40:24 25 4
gpt4 key购买 nike

我有一个游戏,可以渲染一堆 Sprite (数百个),几乎所有 Sprite 都使用相同的纹理。目前,我为每一个调用 glDrawArrays(...) ,我最近发现这是非常低效的。经过一些研究后,我了解到我需要将每个 Sprite 的所有顶点放入一个大顶点缓冲区中,并仅使用一次调用 glDrawArrays(...) 。然而,当我这样做时,它只绘制第一个 Sprite ,其他 200 个 Sprite 是空白的。

blueSpriteVertices[blueBatchNum * 4] = Vertex3DMake(xloc, yloc, zloc);
blueSpriteVertices[blueBatchNum * 4 + 1] = Vertex3DMake(xloc + size, yloc, zloc);
blueSpriteVertices[blueBatchNum * 4 + 2] = Vertex3DMake(xloc, yloc + size, zloc);
blueSpriteVertices[blueBatchNum * 4 + 3] = Vertex3DMake(xloc + size, yloc + size, zloc);
blueBatchNum++;
//^^This block of code^^ is called iteratively, adding data for various sprites
//(around 200) to the vertex array. "xloc", "yloc", etc. are private members of
//this sprite class


//Draw the whole batch
glEnableClientState(GL_VERTEX_ARRAY);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glColor4f(1, 1, 1, 1);

//This code is actually in the Texture2D class implementation, hence "_name"
//and "coordinates"
glBindTexture(GL_TEXTURE_2D, _name);
glVertexPointer(3, GL_FLOAT, 0, blueSpriteVertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_VERTEX_ARRAY);

最佳答案

我最终通过使用 GL_TRIANGLES 而不是 GL_TRIANGLE_STRIP 解决了这个问题,并手动处理了三角形 strip 。通过这样做,我能够消除它在我的 Sprite 之间解释的所有“ strip ”。现在效果非常好,批处理确实极大地提高了我的游戏性能。

关于iphone - 如何在 OpenGL ES (iPhone) 中批量渲染 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4107972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com