I am creating a 2D program in OpenGL 3.2 Now to render what I do is have a VBO for each texture and add the elements of the objects that use that texture to this vbo. Now as you know the order you render objects will be the (fliped) order you see them on the screen.
Here is the problem, as you know its possible to sort objects by their Z value in the same VBO (by inserting them to the vbo according to their Z value). But when you want to sort them between VBOs thats impossible because they have to be in the correct VBO to have the correct texture, Meaning the VBOs that are drawn last will have positive Z value priority over VBOs that are drawn first.
This causes the Z value to transfer from the actual objects, to their textures and makes the Z value be grouped for all objects that use the same texture.
That was kinda confusing so to recap, because of the way VBOs work this is what is going to happen:
Turtule texture id 1
Box texture id 2
(Each textures has its own VBO)
There is one turtle and one box, the turtle (z = -1) is behind the box (z = 0) . if I render the box VBO then the Turtle VBO What will happen is we will see the turtle over the box even though it has a z value that is less then the box.
Here is some art I drew to help you visualize the problem:
^^^ That makes the Z value of the actual turtle meaning less compared to the " Z " value of the VBO. This can be worked around by having a Z value for each VBO but that limits the program because if the Z of the boxes is bigger than turtles no matter what every turtles is gonna be hidden behind every box. And its a stupid way of doing it.
So finally to the question:
What can I do to fix this, to have a meaning full Z value to each OBJECT.
- I cannot draw the object individually because I use drawElements and because I add them to texture coresponding VBOs to reduce the VBO count and texture binding count.
Stuff I have tried* : I have tried using the actual opengl Z-buffer, but I cannot blend layers because of that so its not a solution.