2D layering in OpenGL

Having worked in a default Java 2D graphics for a good while, I’m used to the concept that the last thing that is rendered goes on top.

I’d like to simulate this similar layering in a 2D OpenGL game.

Up until now, I have been setting z values of the individual objects to decide what gets rendered on top, but this is getting very awkward and impractical especially when adding transparent objects.

I had the idea of just increasing the z value that each object is rendered on as objects are rendered each frame, but I’m using buffer objects that only get generated once and then rendered each frame. Also, the buffer objects have differing z values within them so it becomes difficult to layer them individually.

I realize a lot of this probably doesn’t make any sense but any help would be greatly appreciated :smiley:

As long as depth test is disabled (which it should be by default) then you will get the same behaviour. Somewhere in your code you must be calling glEnable(GL_DEPTH_TEST) - don’t. For 2D graphics there is rarely any use for depth test and you certainly shouldn’t be working in 3D points.

I’m already using the depth buffer to layer objects. Is there any way i can easily layer objects without using the depth buffer?

I’m saying DO NOT use the depth buffer. Then you will get them being ordered according to the order you render them. That’s what you want isn’t it?

unfortunately OpenGL seems to render object the other way round, objects rendered first at the front

only if you [icode]glEnable(GL_DEPTH_TEST);[/icode].

If you [icode]glDisable(GL_DEPTH_TEST);[/icode] it works the same as with awt.

Btw, awt (java2D) uses OpenGL under the hood (as one of their backends).

Ok, thanks, will try this and get back to you when i have a moment :smiley:

That isn’t true. If you render one primitive then render another primitive on top of it, as long as there is no depth/stencil/alpha/scissor test, the second will be rendered over the top.

Front-to-back blending can be useful for two reasons.

  1. It can be faster in some cases, similarly to how front-to-back sorting improves depth testing performance.
  2. It produces correct alpha values in the render target, so the resulting texture (if rendering to a texture of course) has correct alpha values.

Right but in a 2d game like this, with distinct layers (I presume), you’d be better off using the stencil buffer for front to back rendering. It must be quicker than the depth buffer and you can have as many layers as you like without worrying about depth buffer precision.

The stencil buffer won’t work with transparent objects though. Same problem as transparency with depth testing.

I’m now having another issue, because of layering, I want to render each tile and then render the entities within that tile. This means now instead of generated one VBO for each chunk of tiles, I have to generate a VBO for each individual tile and render it with its entities. This, although still playable, has had some big performance impacts that i could really do without.

I have tried giving each type of tile an idividual VBO and then just translating it to its wanted position each time, but this is also way too slow.

Any ideas?

No one have any ideas for this ???

What kind of entity are you rendering for each tile?!
Normally entities are drawn in another seperate VBO or if there are only a few in their own VBOs or even immediatly…

Each tile is a quad with a texture applied to it. Before I had this depth issue, I was adding each chunk (16 * 16) of tiles to one large buffer object, but now I need to render a row of tiles and all of the entities in that row of tiles which will mean making 16 buffer objects for each row in the chunk which seems very inefficient and has caused large frame drops. The entities in that tile usually are made up of around 6 VBO’s themselves as each individual part of the entity needs to be manipulated

It would be very helpful if you explain what you are doing - not just what you are doing but what are you using it for, maybe a screenshot and some code example or something…
I do not get why you switched from rendering your tiles chunkwise to rendering them individually…
What are you doing?

The reason why I switched them to rendering them individually because I needed the entities within the tiles the be rendered on top of each tile, but not rendered on top of the tiles below the entity so the layering is correct. I’m sorry if I’m being to vague I’m really bad at explaining things :P.

If you really want the code i can try and paste it but it is a very long chunk of code with a lot of additional variables and functions not to do with this topic

This makes it a bit clearer…
Maybe you could trick a little bit with depth testing?
Code is not necessary, but describing your game could be helpful.

That was the original plan, but managing all the z values got very messy :S

You should make a complete plan where to use which z values then.
The only other way to reduce your VBOs I can imagine is using a shader, but this is your decision.