I'm writing a basic spriting engine for a blackberry app, and I want to know the best way to draw tons of sprites using the glDrawArrays() command.

We have several sprites that move around the screen, and a bunch of sprites for background tiles. Zelda-1 like tiling.

My first idea was to create a quad at the origin for every sprite that exists and glTranslatef it to wherever it needs to be. But several hundred sprites to draw per frame, you only get to call this once for every call to glDrawArrays() so its not a good idea to call glTranslatef() on each sprite as you draw it. That's the reason glVertex3f() was left out..

So I'm left with the option of maintaining a master vertex array and every time a sprite moves in the game, pushing the update to move the 4 vertices that represent its quad in the MASTERVERTEXARRAY. I don't like this but it seems my only option.

What I'd really like is to be able to somehow create an array of "translation values" to use for every quad that gets drawn..

glTranslateArray( ... ) ;

But alas, no such thing exists.

  • \$\begingroup\$ What you might be looking for is batching. Batching geometry together is the cheapest and most efficient way to get much more on screen at the same time. For a fully batched 2d engine in c++, you can have a look at phoenixgl.googlecode.com :) \$\endgroup\$ – underscorediscovery Dec 9 '10 at 23:19

I'm working on iPhone and Android but OpenGL ES is the same. So on mobile Platform (and Destop too), you must use DrawCalls carefully. I try to use < 50 to render a scene.

For my sprite engine on OpenGL ES 1.x, i use an unique coordinates system for the rendering function. So for each sprite and at each frame, i compute its positions in this coordinates system. That's a lot of operations for CPU but i can use one glDrawElement for all sprites using the same texture.

I have two categories of sprite:

  • Transparent sprites: i use a qsort
  • Solid sprites: no sort just ZBuffer

With good texture atlases, i use 10 drawcalls (glDrawElement) for my 500 sprites and have a good framerate.

Ps: for OpenGL ES 2.x, i use the gpu to do translation in the Vertex shader.


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.