My problem is that everything renders in order: newest vertices appear on top of older ones. I want vertices with that are pointing out of the screen (Z negative?) to be rendered on top of vertices that are into the screen (Z positive?)

It has few bugs, but they don't bother me. (Not flushing when spritesheet is different and some others maybe) and also it is not very efficient, but I don't really need efficiency here.For now I just want to resolve the Z axis.

You generally don't want to use depth testing with 2D due to the transparency problem. The most common solution for rendering transparent stuff with a depth buffer (regardless of 2D or 3D) is to disable depth writes and sort your objects. If you only need additive blending you don't need to sort your objects, but this is rarely the case.

Concerning sorting performance, I wouldn't worry that much. The order of your objects will not change much between frames, so I'd strongly recommend implementing a simple insertion sort. Insertion sort has the advantage of being O(n) if the list is already sorted and requires no temporary memory. Since we can assume that the order does not change much from frame to frame insertion sort will be extremely fast (except for the first frame when your objects are completely unsorted).

As you can see, insertion sort scales horribly with the number of objects when they are unsorted, but linearly when they are already sorted. Although Java's Arrays.sort() method is faster in all cases, it generates a large amount of garbage which may cause problems with garbage collection pauses.

The performance isn't the issue for me.. Its the messiness of the code.. I just implemented something like you said (I had done in the past), but I'm not content with the results. Since I'm using shaders and sprite batch, I still need to keep the order of the drawing, even though I implemented this, since you sometimes need to flush more than once per frame, like changing projection/modelview matrix.

You generally don't want to use depth testing with 2D due to the transparency problem. The most common solution for rendering transparent stuff with a depth buffer (regardless of 2D or 3D) is to disable depth writes and sort your objects. If you only need additive blending you don't need to sort your objects, but this is rarely the case.

What if I have two seperate lists: 1. Sprites that only have pixels with alpha 1 or alpha 0 or Sprites that are supposed to be rendered with additive blending,2. Sprites that have pixels with other alpha values between 1 and 0.

Now, all sprites have some kind of "layer" variable, or in other words: A Z-position.I don't need to sort list 1. I can simply render it non-sorted with additive blending, alpha test and depth testing enabled.I sort list 2, then render it with normal blending and depth testing disabled.

You generally don't want to use depth testing with 2D due to the transparency problem. The most common solution for rendering transparent stuff with a depth buffer (regardless of 2D or 3D) is to disable depth writes and sort your objects. If you only need additive blending you don't need to sort your objects, but this is rarely the case.

What if I have two seperate lists: 1. Sprites that only have pixels with alpha 1 or alpha 0 or Sprites that are supposed to be rendered with additive blending,2. Sprites that have pixels with other alpha values between 1 and 0.

Now, all sprites have some kind of "layer" variable, or in other words: A Z-position.I don't need to sort list 1. I can simply render it non-sorted with additive blending, alpha test and depth testing enabled.I sort list 2, then render it with normal blending and depth testing disabled.

Transparency should work and everything should be faster, no?

The problem with this approach is that usually you'd want blending on all your sprites to get nice and smooth edges as alpha testing leads to clearly visible aliasing. You could enable multisampling and alpha-to-coverage to improve that, but that'd make the second pass do blending into a multisampled buffer which is significantly slower.

Funny enough, I was messing around with GLSL yesterday and I found an interesting little tweak. If you set gl_Position.z to equal 0.1f then you get all the vertices rendering, retaining perspetive. I would think that this would create some sort of flat shader, but now that i think of it, it makes perfect sense. This may or may not relate to what you were asking, I just thought I'd put it out there

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org