0
\$\begingroup\$

I am using OpenGL to render sprites, using an orthographic projection.

Each sprite has a position: (x, y, z). I need to be able to set the order of rendering, so that certain sprites are rendered on top of the others. For this reason, I also have a z value in the position;

The rendering pseudocode looks like this:

Renderer.begin(camera.projection)
Renderer.draw(sprite1)
Renderer.draw(sprite2)
...
Renderer.end()

For the correct layering of sprites, I would need to sort them by their position.z value.

I would love to find a way around that: I plan to extend my system so that sprites can have custom materials, which will use custom shaders. Therefore, I would like to sort by their material index, in order to minimize shader switches.

Is there a way to utilize the depth buffer in order to make the layering work, so that I can just sort by material index?

\$\endgroup\$
2
  • \$\begingroup\$ The depth buffer can help if your sprites have hard edges and you don't mind stairstep aliasing there (or you can clean it up with an antialiasing pass). If your sprites have soft feathered edges or partial translucency, then it gets more complicated. Order independent transparency is expensive and limited — cheap versions are only approximate and can have strange artifacts. \$\endgroup\$
    – DMGregory
    Commented Mar 11 at 2:43
  • \$\begingroup\$ You're right, I forgot to take into account translucent sprites and sprites with feathered edges; In that case, even manually changing the z-buffer will produce incorrect results without z-sorting first. Order independent transparency is out of the scope of this current iteration and from what I remember, the performance hit outweighs the benefits of the occasional shader switch. TL;DR I'll just sort by [z-value, shader.index] \$\endgroup\$ Commented Mar 11 at 4:50

0

You must log in to answer this question.

Browse other questions tagged .