Sign in to follow this  
Proudest

2D z-buffer sorting (2D in D3D9 C#)

Recommended Posts

I'm creating a tile-based RPG with Direct3D9 and C# using the Sprite interface and have size 32x48 sprites and 32x32 tiles. I would like to draw the sprites based off their y value (bottom draws first) so I can support overlapping sprites. Until I heard of the z-buffer sorting method, I manually compared all sprites and sorted them which is tedious. I enabled the z-buffer and passed the y value as the z value in my draw function and noticed that the buffer only handles values from 0 to 1. I also noticed the higher the z value, the further it's drawn, which is the opposite of what I need. I heard that transparency wouldn't work if I sorted my sprites in this way (not sure if it's true). If anyone knows how to solve this, or a better way to accomplish this, I'm all ears. Thanks.

Share this post


Link to post
Share on other sites
Hi

I'm not using z buffer sorting. You can achieve same effect drawing further tiles first and closest tiles last. Same for sprites.
This you can get with two for loops. However, disabling z-buffer in directx you get noticable speed improvements. Just try to disable z-buffering in any D3D application and look at fps counter.



....
int tilex = 48, tiley =12; // size in pixel of our tile

// tiles visible onscreen
#define MAP_DRAW_WIDTH (19)
#define MAP_DRAW_HEIGHT (50)

//tile coordinate on map
int tMapDrawX = 0;
int tMapDrawY = 0;

for(int y = 0; y<MAP_DRAW_HEIGHT;y++)
{

for(int x = 0; x<=MAP_DRAW_WIDTH;x++)
{

// POINT g_ptDrawOffset is scroll offset of tile 0,0
int tMapX = g_ptDrawOffset.x+tMapDrawX+x; //draw coordinates
int tMapY = g_ptDrawOffset.y+tMapDrawY+x;

int index = 0;
index = (tMapY * m_Width) + tMapX;
// MAPDATA[index] store values of tiles

int draw_xpos, draw_ypos;
if(CHECKEVEN(y))
draw_xpos = g_ptScrollOffset.x+x*xoffset;
else
draw_xpos = g_ptScrollOffset.x+x*xoffset-(xoffset/2);

draw_ypos = g_ptScrollOffset.y+y*yoffset;

// custom iso draw funcion
BlitIso(m_pDevice, draw_xpos,draw_ypos,48,24,0xffffffff);

}
if(CHECKEVEN(tMapDrawX+tMapDrawY))
tMapDrawX--;
else
tMapDrawY++;
}


and....

static inline bool CHECKEVEN(int num)
{
if(num/2 == (float)num/2.0f)
return true;
return false;
}





Hope this helps...

Share this post


Link to post
Share on other sites
thats not a good idea,
z buffering is inefficent and the blending wont work right with zbuffering alone

Share this post


Link to post
Share on other sites
Wait... Comparing/sorting out sprites isn't a tedious task, if you know what to use.
All I did was simply use STL's "sort()" method, and created an operator overload for the "graphical" objects. (However, I used STL's "vector" also)

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