Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2D engine with OpenGL: Use Z buffer or own implementation for sprite sorting?

Tags:

c++

layer

opengl

2d

If I was making a 3D engine, the answer to this question would be clear: I'd go for using the depth buffer instead of thinking of sorting all my polygons on my own.

However, this is a different situation with 2D, because here layers can be implemented easily without the help of OpenGL - and you then could even sort and move sprites within layers. (Which isn't possible in OpenGL afaik)

  • (Why) should I use the OpenGL depth buffer instead of a C++ layer system running on the CPU?
  • How much slower would the depth buffer version be?

It is clear to me that making a layer system in C++ would impose as good as no performance impact at all, as I have to iterate over the sprites for rendering in any case.

like image 874
KLa Avatar asked Apr 07 '11 15:04

KLa


2 Answers

I would suggest you to do it in software since you probably want to use transparency on your sprites and that implies you render them from back to front. Also sorting a couple of sprites shouldn't be that CPU demanding.

like image 134
tibur Avatar answered Oct 02 '22 14:10

tibur


Use both, if you can.

Depth information is nice for post-processing and stuff like 3D-glasses, so you shouldn't throw it away. These kinds of effects can be very nice for 2D games. Also, if you draw your (opaque) layers front to back, you can save fill-rate because the Z-Buffer can do the clipping for you (Depth tests are faster than actual drawing). Depth testing is usually almost free, especially when you got hierarchical Z info. Because of this and the fill-rate savings, using depth testing will probably be even faster.

On the other hand, the software sorting is nice so you can actually do front to back rendering for opaque sprites and it's mandatory to do alpha-blending right (of course, you draw these sprites back to front).

like image 34
ltjax Avatar answered Oct 02 '22 14:10

ltjax