簡體   English   中英

如何在OpenGL ES(iPhone)中批量渲染精靈

[英]How to batch render sprites in OpenGL ES (iPhone)

我有一個游戲,渲染一堆精靈(幾百),幾乎所有精靈都使用相同的紋理。 目前,我正在為每一個調用glDrawArrays(...),我最近發現這是非常低效的。 在做了一些研究之后,我了解到我需要將每個精靈的所有頂點放入一個大的頂點緩沖區,並使用它調用glDrawArrays(...)一次。 但是,當我這樣做時它只繪制第一個精靈,而另外200個是空白的。

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解決了這個問題,並手動處理了三角形條。 通過這樣做,我能夠消除它我的精靈之間解釋的所有“條帶”。 現在就像魅力一樣,配料肯定會提升我的游戲性能。

使用(GL_TRIANGLES而不是GL_TRIANGLE_STRIP適用於我(在Android上)

glDrawElements(GL_TRIANGLES, 6 * mSpriteCounter, GL_UNSIGNED_SHORT, (char*) NULL);

glDrawArrays()最后一個參數應該包含頂點數(在你的情況下你只有4個)。 此外,您必須具有相同數量的紋理坐標才能匹配繪制的頂點!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM