3

I'm working on a game in a 3D world with 2D sprites only (like Don't Starve game). (OpenGL ES2 with C++)

Currently, I'm ordering elements back to front before drawing them without batch (so 1 element = 1 drawcall). I would like to implement batching in my framework to decrease draw calls.

Here is what I've got for the moment:

  • Order all elements of my scene back to front.
  • Send order list of elements to the Renderer.
  • Renderer look in his batch manager if a batch exist for the given element with his Material.

    • Batch didn't exist: create a new one.
    • Batch exist for element with this Material: Add sprite to the batch.
  • Compute big mesh with all sprite for each batch (1 material type = 1 batch).

  • When all batches are ok, the batch manager compute draw commands for the renderer.

  • Renderer process draw commands (bind shader, bind textures, bind buffers, draw element)

Image to show my problem here: ImageImage

But I've got some problems because objects can be behind another objects inside another batch.

How can I do something like that?

genpfault
  • 51,148
  • 11
  • 85
  • 139
user41765
  • 41
  • 1
  • what exactly are you asking (how to do what)? how to do Z-Sorting? For rendering without blendig is Z-Buffer enough. With transparent objects you need to Z-sort all objects respect to camera view axis and draw from back to front. – Spektre Feb 04 '14 at 12:45
  • These are all planar sprites aligned to the XY-plane from what I am seeing. Sorting should be trivial, the only complicated thing is the grass and that is opaque so just draw that first. If your question is how to do this while minimizing the number of draw calls/texture swaps, the answer is to use a sprite sheet. – Andon M. Coleman Feb 04 '14 at 22:53

1 Answers1

1

It sounds like you're trying to optimize rendering for two mutually exclusive goals.

  • Rendering in sorted Z-order to produce correct transparency results
  • Rendering grouped by the material type, presumably to reduce the number of GL state changes incurred from switching materials.

One of them has to take priority over the other. Since Z-ordering is critical for proper appearance of transparency you'll likely have to have that take precedence. Once you've done that you're still free to batch up items of the same material within the Z-ordering.

Jherico
  • 28,584
  • 8
  • 61
  • 87