I'm working on a 3d particle emitter. It's going pretty good but I'm still having a problem with triangle sorting.
As you can see in the photos, there are some particles that are not blending properly presumably because of sorting. I do sort my quads before I draw them back to front. And they are all being drawn in a single glDrawElements(GL_TRIANGLES...
call.
Blending is on & depth buffer is on. Depth buffer has to be on because of the other 3d objects in the scene that are sometimes in front and sometimes behind the particles.
When they're moving in a z space they're sorting better and you don't see it as much:
I'm using a sort with the following compare:
int compare (const void *a, const void *b) {
Particle *ia = (Particle*)a;
Particle *ib = (Particle*)b;
float t = ib->distanceToCamera - ia->distanceToCamera;
if (t<0) {
return -1;
}
return (t>0);
}
But I think there are times when some particles have the same z and then they're not blending nicely.
One thought was to somehow force them to not be the same z. But with them moving on their own I'm not sure about that.
QUESTION - if overlapping triangles are close in z space but not equal, and still drawn back to front within the draw command, could they still overlap like this?
ANSWER - I set a break point and printed out all my structures to find that there are a ton of quads in the same z. In groups.
Using the advice from the checked answer I was able to fix it. By making sure to render the solids first and the particles last I don't need to worry about writing to the depth buffer while rendering the particles. In my game they're all like gases, smoke and air. So they all need to blend with each other and on to things deeper in the scene than them. When I start to render the particles I set:
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_FALSE);
And when I'm done rendering them I return it to:
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
And look at the result:
What I believe was happening was that as particle triangles were drawn if they were on the same plane they were writing to the depth buffer and then the next one on that z if larger would get masked. By turning off depth mask it doesn't write to the mask as it's drawing the particles so they're not masking each other any more. Since I draw them last they don't need to mask other things.
UPDATE: Another benefit I just figured out. Now I don't have to sort the particles at all. The sort was only to make them draw properly with the depth test on. And that's no longer needed. So I have that savings as well.