0
\$\begingroup\$

What's the best way to handle adding and removing 2D sprites (quads) in a VBO?

I am working on a mobile board game and sometimes game pieces (represented by quads graphically) need to be added to or removed from the screen. Currently, I store all of my quad vertices in an array. When I need to add new quads to the screen, I append this list with the new vertices. However, removing quads is more cumbersome. I track the positions of each quad in the VBO array with a dictionary, and when I comes time to remove it, I remove that block of four vertices from the array. I then have to update the values stored inside the dictionary since the VBO array has been resized.

I am not satisfied with this solution, and I feel like there could be a better way to achieve what I want. I couldn't find any related questions about storing 2D sprites which need to be added or removed during runtime like this. Since the game is really simple graphically, I am thinking it may be easiest to make a draw call per sprite. I can't use instancing since I am restricted to OpenGL ES 2.

\$\endgroup\$
2
\$\begingroup\$

You don't need a dictionary for this, just use an infinite array (c++ - vector, java - arraylist, c# - list) and store an id in the order they can be found in the vbo. If you want to remove one of them, find the location of this id, multiply it with the amount of vertices per quad, remove the quad and shift everything back 1 (a lot easier than shifting a dictionary)

If you only have quads, then storing one for each piece is unnecessary.

You should only put the data for 1 quad in the vbo, then when you need to render the scene you should bind the vbos and load them in the shader at the beginning of the rendering, then when you want to render one you just liad the necessary uniforms and call the render function.

If you want to keep the current approach with all the quads in the vbo you don't need to remove any of them from the array, just put it somewhere far away where it can't be seen.

\$\endgroup\$
  • \$\begingroup\$ Could you explain what you mean by "bins the vbos"? \$\endgroup\$ – Rishub Nagpal Jan 6 '17 at 6:59
  • \$\begingroup\$ @Rishub mistype, it wanted to be "bind" \$\endgroup\$ – Bálint Jan 6 '17 at 7:47
  • \$\begingroup\$ if I were to store 1 quad as you mentioned, wouldn't I have to make a draw call for each separate quad? I've read that it is better to batch quads in one vbo and draw them at once than to do separate calls one at a time. \$\endgroup\$ – Rishub Nagpal Jan 6 '17 at 8:24
  • \$\begingroup\$ @Rishub You're making a puzzle game where you render quads. Don't make your life harder and do it in the convenient way. The solution you used also uses more memory and changing vbos is costy. But if you really want to use just 1 draw call, then look into instancing \$\endgroup\$ – Bálint Jan 6 '17 at 8:43
  • \$\begingroup\$ Ya you're right. I was too focused on minimizing draw calls, but didn't realize it wouldn't make much of a difference for my use case. Thanks! \$\endgroup\$ – Rishub Nagpal Jan 7 '17 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.