Jump to content
  • Advertisement
Sign in to follow this  
ms291052

2-D Z Coordinates

This topic is 4949 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having trouble figuring out how the Z-Coordinate of the Position parameter of ID3DXSprite::Draw works. My experimentation shows that it doesn't like values outside of [0, 1), but I was under the impression that you could assign any Z-value to a sprite and it would sort them based on their Z-coordinates. Is this incorrect? The SDK doesn't mention this, and only says (0, 0, 0) is the upper-left coordinate (dismissing the Z-value altogether). As an afterthought, is there any way to control the Z-value of the call to ID3DXFont::DrawText? I'm just working in plain 2D space, with world and view both set to the Identity, no billboards or any such 3D uses of ID3DXSprite or ID3DXFont. Thanks, ms

Share this post


Link to post
Share on other sites
Advertisement
I also realized the same thing, however since the z value is floating-point, you can use the entire range between 0 and 1 and D3DXSprite will sort your sprites accordingly. So an object with z coordinate 0.01 will be rendered before (or after, depending on which flag you set) one with 0.02.

Share this post


Link to post
Share on other sites
If you really want a z-coord, implement one in your own object or sprite class. Then when rendering, use something like qsort to sort the wanted sprites into a render order by using the z-coord.

Here is the code I use when I want to render sprites in a 2.5D fasion, it can easyily by modded to use the z-coord instead of the y-coord:


/*********************************************************************************************/
//
// STRUCT: tagSpriteSort
//
// DESC: This aids in the sorting of the sprites by carrying the sprite index and y-coord
//
/*********************************************************************************************/

typedef struct tagSpriteSort : public IMemObject {

int m_nKey; // the sprite key
float m_fY; // the y-coord

QUICK_SIZE;
}SPRITE_SORT, *LPSPRITE_SORT;

// this sorts the sprites into a 2.5D fasion using the y value
int __cdecl SortSprites25D( const void* pSprite1, const void* pSprite2 );
inline int __cdecl SortSprites25D( const void* pSprite1, const void* pSprite2 )
{
// get the sprites
LPSPRITE_SORT p1 = (LPSPRITE_SORT)pSprite1;
LPSPRITE_SORT p2 = (LPSPRITE_SORT)pSprite2;

// sort the sprites
if ( p1->m_fY > p2->m_fY )
return -1;
else
if ( p1->m_fY == p2->m_fY )
return 0;
else
return 1;
} // end SortSprites25D

// this renders all of the sprites
// it takes nothing
// it returns true upon success else returns false
bool CSpriteManager::Render( void )
{
int nKey = 0; // the sprite key
int nSize = ms_Sprites.size( ); // sprite list size
CSprite* pSprite = NULL; // to speed up rendering, saves on CPointer operator calls

LPSPRITE_SORT pSprites = new SPRITE_SORT[ nSize ]; // the sorted sprite list

// create the list to be sorted
for ( int i = 0; i < nSize; i++ ) {
pSprites[ i ].m_fY = ms_Sprites[ i ]->GetScreenYPos( );
pSprites[ i ].m_nKey = ms_Sprites[ i ]->GetKey( );
} // end for

// sort the list
qsort( pSprites, nSize, sizeof( SPRITE_SORT ), SortSprites25D );

// render the sprites
for ( i = 0; i < nSize; i++ ) {
nKey = pSprites[ i ].m_nKey;
pSprite = ms_Sprites[ nKey ].GetData( );
pSprite->Render( );
} // end for

// free up
pSprite = NULL;
delete [ ] pSprites;
pSprites = NULL;

// return success
return true;
} // end CSpriteManager::Render

Share this post


Link to post
Share on other sites
DirectX uses the z-value of pretransformed vertices (and the sprite is such) for z-buffer-testing. It assumes a value between 0 and 1.

If you want to have your sprites z-sorted automagically you need to have a z-buffer and zbuffer write and zbuffer enabled.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!