How to render sprites in OpenGL ES (iPhone)

I have a game that creates a bunch of sprites (several hundred), almost all of which use the same texture. I am currently calling glDrawArrays (...) for each of them that I recently discovered was very inefficient. After some research, I found out that I need to put all my vertices for each sprite into one large vertex buffer and call glDrawArrays (...) only once with this. However, when I do this, it only draws the first sprite, and the remaining 200 are empty.

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);
+3
source share
3 answers

, , , GL_TRIANGLES GL_TRIANGLE_STRIP, . , "", . , .

+3

(GL_TRIANGLES GL_TRIANGLE_STRIP ( Android)

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

The last parameter glDrawArrays () should contain the number of vertices (in your case you have only 4). You must also have the same number of texture coordinates so that they correspond to the drawn vertices!

0
source

Source: https://habr.com/ru/post/1773271/


All Articles