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?