Making a 2D sprites in D3D

Started by
5 comments, last by yanuart 21 years, 9 months ago
Hi, how can I make a 2d sprites in D3D 8 from a quad which is always in front of my scene (for a game menu) or always behind my game scene ?? I remembered that you can do this just by messing with the projection matrices but I forgot and I can''t figure it out myself. Thx..
Ride the thrill of your life
Play Motorama
Advertisement
Try this:


      define D3DFVF_SPRITEVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)struct SPRITE_VERTEX{   f32 x, y, z, rhw;	// The transformed position for the vertex.   u32 ARGB;		    // The vertex color.   f32 tu, tv;			// tex coords};void DrawSprite( IDirect3DTexture8 *Texture, u32 ARGB,                   f32 sx, f32 sy, f32 sw, f32 sh,                   f32 tu, f32 tv, f32 tw, f32 th )    /////////////////////////////////////////    //// copy data    SPRITE_VERTEX ASprite[4];    ASprite[0].x = sx;      ASprite[0].y = sy;      ASprite[0].rhw = 0.5f; ASprite[0].ARGB = ARGB;  ASprite[0].tu = tu;     ASprite[0].tv = tv;    ASprite[1].x = sx+sw;   ASprite[1].y = sy;      ASprite[1].rhw = 0.5f; ASprite[1].ARGB = ARGB;  ASprite[1].tu = tu+tw;  ASprite[1].tv = tv;    ASprite[2].x = sx;      ASprite[2].y = sy+sh;   ASprite[2].rhw = 0.5f; ASprite[2].ARGB = ARGB;  ASprite[2].tu = tu;     ASprite[2].tv = tv+th;    ASprite[3].x = sx+sw;   ASprite[3].y = sy+sh;   ASprite[3].rhw = 0.5f; ASprite[3].ARGB = ARGB;  ASprite[3].tu = tu+tw;  ASprite[3].tv = tv+th;            /////////////////////////////////////////    // draw Sprite    DXDisplay->SetTexture( 0, Texture);    DXDisplay->SetStreamSource( 0, SpriteBuff[BuffInd], sizeof(SPRITE_VERTEX) );    DXDisplay->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, ASprite, sizeof(SPRITE_VERTEX) );}  


All you have to have setup is your viewport, the projection can be left alone As far as I remember. Also turn of zbuffer read/write for sprites.

To draw behind the 3d geom

1) draw behind sprites
2) draw 3d geom
3) draw infront sprites

This uses transformed+lit vertices, see the sdk on vertex formats.
Also using DrawPrimitiveUP() is not the most efficent way to do this, using a dynamic vertex buffer would improve things.

[edit, damn that #define messing up the source, and damn my formating ;]
Mark Duffill[The Jackal]
Eurocom Entertainment Software



[edited by - mark duffill on July 17, 2002 7:08:59 PM]
for always in front, disable the depth test. it will be in front and you will save a few cycles too.

Author, "Real Time Rendering Tricks and Techniques in DirectX", "Focus on Curves and Surfaces", A third book on advanced lighting and materials
I''ve tried using TL vertices for my quad but the sprite will show up always in front of my 3d scene.
How can I make sure that my quad sprites will render in the furthest distance (as a background??)
Ride the thrill of your life
Play Motorama
Change the rhw member. the rhw member stands for "Reciprocal Homogenous W" [sic?] - i.e. the depth of the quad.


Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)
To draw a sprite in the back of the scene set its vertices'' Z components to 1 and don''t turn off depth testing.

void Signature(void* Pointer)
{
PObject(Pointer)->ShowMessage("Why do we need so many pointers?");
};
void Signature(void* Pointer){PObject(Pointer)->ShowMessage("Why do we need so many pointers?");};
After I render my 2d sripte, I lost all of my lighting ??? I swear I didn''t change the lighting''s render states when I render my sprite and I''ve forced to set set ligthing render states everytime I draw my 3d geom. What happened ???
Ride the thrill of your life
Play Motorama

This topic is closed to new replies.

Advertisement