Sign in to follow this  
Endemoniada

Z-Buffer for 2D ?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this