Z buffer when drawing 2D sprites

Started by
6 comments, last by Evil Steve 14 years, 11 months ago
Hi, Im trying to do my first game in DirectX, and I cant get the z-buffer to work. Basicly what Im doing is : drawing a background and then a character and a couple of objects on it. The character and the object have different z values in their D3DXVECTOR3 z value (all legal between 0.0f and 1.0f) but when presenting, it does not seem like the z value does any effect, the only thing that seem to matter is the order the sprites are drawn. The code Im using in order to draw:

//----------------------------CODE
d3dspt->Begin(D3DXSPRITE_SORT_DEPTH_BACKTOFRONT);    // begin sprite drawing	
// draw the sprite
D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);    // center at the upper-left corner
D3DXVECTOR3 position(50.0f, 50.0f, 0.6f);  
d3dspt->Draw(sprite, NULL, NULL, &position, D3DCOLOR_XRGB(255, 255, 255));
d3dspt->End();    // end sprite drawing
position.x=200.0f;
position.z = 0.8f; // change z position
d3dspt2->Begin(D3DXSPRITE_SORT_DEPTH_BACKTOFRONT);// begin second sprite drawing
d3dspt2->Draw(sprite, NULL, NULL, &position, D3DCOLOR_XRGB(255, 255, 255));
d3dspt2->End();    // end sprite drawing
//---------------------------ENDCODE

Any help will be appriciated, Thanks... :) [Edited by - jitterjaw on May 18, 2009 11:31:23 AM]
Advertisement
is there a special reason you are using 2 sprites?you can draw two diffrent objects with the same sprite(its even faster if you use 1 begin and end calls). and well i am not sure zbuffer affects sprites(i think they are drawn on screen space, and not world space).
anyway, to turn zbuffer on you put this when you create your present params :
[schnitzel] PresParams.EnableAutoDepthStencil = TRUE; PresParams.AutoDepthStencilFormat = D3DFMT_D16;[/schnitzel]
Quote:Original post by Bru
is there a special reason you are using 2 sprites?you can draw two diffrent objects with the same sprite(its even faster if you use 1 begin and end calls). and well i am not sure zbuffer affects sprites(i think they are drawn on screen space, and not world space).
anyway, to turn zbuffer on you put this when you create your present params :
[schnitzel] PresParams.EnableAutoDepthStencil = TRUE; PresParams.AutoDepthStencilFormat = D3DFMT_D16;[/schnitzel]

Thanks for the replay. This does not seem to create the effect I wanted, even though it has an effect on the display, it does not effect the order of the layers being drawn. Maybe the answer does not lay in the z-buffer as it is defined in the 3D, but the tutorial I worked with claimed it is possible to control the way the layers are drawn using just the z value.
The reason Im using several sprites is that I have very large textures (2048X2048) each of a character in the game, and I need to draw several characters on the screen at the same time.
Thanks
You must use

d3dspt->End();

only once at the end of your Draw() calls.

Sprite->End() does all the sorting and drawing depending on the
Sprite->Begin() statement.
Quote:Original post by MichaelH55
You must use

d3dspt->End();

only once at the end of your Draw() calls.

Sprite->End() does all the sorting and drawing depending on the
Sprite->Begin() statement.

Hi and Thanks for the replay,
Im not 100% sure about what am I supposed to do, since I am using 2 sprite pointers, and each of them need a Begin and an End. Do you mean it only sorts the sprites that come out of the same sprite pointer ?
Thanks
They are saying do ALL of the drawing between Begin() and End(), right now you have a begin and end for each sprite / object, draw ALL objects between 1 Begin() and End() otherwise each Begin and End should be using a different ZBuffer making Z position pretty much irrelevant.

Draw()
{
Begin()
Draw Calls here for all objects
End()

}
Quote:Original post by JonConley
They are saying do ALL of the drawing between Begin() and End(), right now you have a begin and end for each sprite / object, draw ALL objects between 1 Begin() and End() otherwise each Begin and End should be using a different ZBuffer making Z position pretty much irrelevant.

Draw()
{
Begin()
Draw Calls here for all objects
End()

}


Hi,
I think there is a misunderstanding, because the Begin and End are functions of the sprite pointer, in order to draw into the screen a begin and an end will have to be declared per sprite pointer.
To sum up all of what was said, there is no way to order by z depth when using several sprites ? is there a better way to show 2D graphics in DirectX not using sprites ?
Thanks...
Quote:Original post by jitterjaw
I think there is a misunderstanding, because the Begin and End are functions of the sprite pointer, in order to draw into the screen a begin and an end will have to be declared per sprite pointer.
You should only have one ID3DXSprite in your entire application. Think of it as a sprite renderer, rather than an individual sprite.
Then you should only have a single Begin()..End() pair for all of your sprites.

This topic is closed to new replies.

Advertisement