Shaders & VBO Spritebatcher

Hello there! I’ve started developing using opengl and learned the basics of 2d and now I know how to create a game and manage all the main stuff, but if for an example I am trying to draw 10000 32x32 textures I barely get FPS and that is because I need to implement a SpriteBatcher to fix my performance.

Correct me if I’m wrong, but a spritebatch simply stores all the vertex data of all the rendering on the screen, same for textures and then simply draws them at once? I need to do this with VBOs, then how can I do this? (Just give me a little push and I’ll figure out myself)

Another thing is that I saw and read about shaders and I questioned my self, are shaders useful for 2d projects?

you collect all vertices of your “drawing objects” and give them at once to the gpu.
In addition you can use a spritesheet/textureatlas to reduce the amount of texturebinding.

So you mean I create a FloatBuffer and put all the vertices in it? I need to create a FloatBuffer for the texture coords too, no?

Well yes, it’s the same process of having one buffer object per object, but just modifying the process to store many objects to decrease the amount of time it takes to send all your geometry to its destination (GPU). But you need to find the balance; too much data per batch and it’ll be less efficient than throwing in one buffer per time. Too little and you’ll starve it, which isn’t good either.

Poke around the tutorials here, a few discuss a sprite batcher and how it’s made. If you’re using the modern pipeline you’ll probably want shader, matrix, texture, and VBO utils to make things easy and clean.

See the API and it’s source as an example of how it might be implemented. Lots is inspired by LibGDX, which is another good source reference for sprite batching.

In my very limited OpenGL experience so far, I’ve found them useful. I very recently created a little 2d program where different coloured ameoba-like cells wander around the screen and eat eachother. The whole “wobbly moving cell” appearance was implemented in shaders, using only a single 128 x 128 texture; and the wobble effect varied slightly & randomly for each cell. I’m sure you could imagine how hard that effect would be to implement in a more traditional, no-shader 2d approach.

I guess if you are using fixed-pipeline and rendering a texture on screen, as-is, then they probably aren’t so useful. But don’t limit yourself to this thinking; you can acheive some awesome things with shaders.