Z-Buffer for 2D ?

Started by
6 comments, last by Raymond_Porter420 18 years, 7 months ago
Hi guys, For my 2D game I have basically three layers: background, sprites, particles. Do I just set those to different Z-values, say 0.6, 0.5, 0.4 respectevely, and not worry about it ? Does it matter what order they go in to the vertex buffer ? Also, if my background takes up the whole screen do I need to Clear() anything ? Does the Z-Buffer have to get cleared even though I'm specifying the Z-values in the vertices ? Thanks.
Advertisement
You can do that if you like. Unless you're expecting significant overdraw, though, you may as well just turn z-buffering off and render in back-to-front order.

Oh, and if you use the Z-buffer, you should always clear it between frames, even if you don't have to. It's an operation that is essentially free on modern GPUs, and not doing so can actually prevent certain optimizations from taking place.
But you definitely don't have to clear the backbuffer between frames if you are redrawing the entire background anyways.
Quote:Original post by MasterWorks
But you definitely don't have to clear the backbuffer between frames if you are redrawing the entire background anyways.

No, you don't have to, but YOU STILL SHOULD. Again, not doing so will prevent certain optimizations from taking place.
Hi guys,

So if I don't use a Z-buffer whatever is in the vertex buffer gets drawn sequentially ? Like this:

v[0]...v[2] // red triangle
v[10]...v[12] // blue triangle
v[100]...v[102] // black triangle

So red always gets drawn first, then blue, then black; so if all three overlap the black one will be on the top and the red on the bottom, is that right ?

Now, if my sprites take up the whole display (like a tiled background for example) and I don't enable the Z-buffer should I still call Clear() to enable the optimizations ?

Thanks.
Quote:Original post by Endemoniada
Hi guys,

So if I don't use a Z-buffer whatever is in the vertex buffer gets drawn sequentially ? Like this:

v[0]...v[2] // red triangle
v[10]...v[12] // blue triangle
v[100]...v[102] // black triangle

So red always gets drawn first, then blue, then black; so if all three overlap the black one will be on the top and the red on the bottom, is that right ?

Yep.

EDIT: Well, actually. I'm not sure if the order is guaranteed if they're being drawn as part of the same primitive. If they're different primitives, though, definitely.

Quote:Now, if my sprites take up the whole display (like a tiled background for example) and I don't enable the Z-buffer should I still call Clear() to enable the optimizations ?

Well, since you won't have a Z-buffer, there won't be a way to clear it. You don't need to clear the color buffer, and I'm not sure whether not clearing it would make any difference, either to allowing optimizations or changing overall performance. It's a simple thing to change, so you may as well try it both ways and see what the effects are.
Quote:Original post by Sneftel
Quote:Original post by MasterWorks
But you definitely don't have to clear the backbuffer between frames if you are redrawing the entire background anyways.

No, you don't have to, but YOU STILL SHOULD. Again, not doing so will prevent certain optimizations from taking place.


Not to make this a different subject, but the dx sdk samples that render a skybox instead of clearing are inefficient in that aspect.

And yes, rendering objects in back to front order probably will speed things up :)

ProgrammingNerd
if you draw a big vertex buffer full of verts then theirs no guarantee whats on top. Im writing a 2 renderer and i still dont have a good solution to this because if i disable z buffering im basicly disableing batching as well. that was probably no help to you but if you get a clean solution to depth based on z values if the z buffer is on and doing overlay drawing when the z buffer is off id like to know. When i use my z buffer ( no support for overlay drawing yet ) i store the vertices in an array and when the array gets full i copy it to a VB and DrawPrimitive the whole thing. For overlay i could draw every time i add a primitive but im afraid it will be slow...

This topic is closed to new replies.

Advertisement