Sign in to follow this  
_TT_

Problem with transparency in direct3D.

Recommended Posts

I am working a bit on a 2d - direct3D project, and ran into a little problem. I got the following code for rendering sprites:
bool GEP_Core::Graphics_RenderSprite( GEP_Sprite sprite )
{
CUSTOMVERTEX vertices[6];

//Fill vertices with data from sprite class.
Graphics_FillVertex( &vertices, &sprite );

//Add vertice data to buffer.
VOID* pVertices;
if( FAILED( pVertexBuffer->Lock( 0, sizeof(vertices), (VOID**)&pVertices, 0 ) ) )
 return false;
memcpy( pVertices, vertices, sizeof(vertices) );
pVertexBuffer->Unlock();

//Recieve texture to use.
LPDIRECT3DTEXTURE9 tex = sprite.GetTexture()->GetTexturePointer();

//Set texture to use, and draw the sprite.
pD3DDevice->SetTexture( 0, tex );
pD3DDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );

//Completed successfully.
return true;
}







And the following code for loading textures:
bool GEP_Core::Texture_Load( GEP_Texture *texture, std::string filename )
{
	//GEP_Texture texture;
	LPDIRECT3DTEXTURE9 tex;	

	//Get image info and validate existence.
	D3DXIMAGE_INFO imageInfo;
	if( FAILED(D3DXGetImageInfoFromFile( filename.c_str(), &imageInfo )) )
		return false;

	//Load texture from file.
	D3DXCreateTextureFromFileEx( pD3DDevice,
				filename.c_str(),
				imageInfo.Width,
				imageInfo.Height,
				1,
				0,
				D3DFMT_A8R8G8B8,
				D3DPOOL_MANAGED,
				D3DX_DEFAULT,
				D3DX_DEFAULT,
				Settings_GetTransparentColor(),
				NULL,
				NULL,
				&tex);
	
	//Store texture data in texture storage.
	texture->SetTextureAreaSize( imageInfo.Width, imageInfo.Height );
	texture->SetTexture( tex );

	//Completed successfully.
	return true;
}







In the rendering function, I receive the spritedata from the spriteclass passed to it, like positioning, size, texture to use and z-value. My problem is related to transparency and the z-value, which I use to specify top-priority for sprites. If I first render a sprite with z-value 0 (closest to the screen), and afterwards render a new sprite with z-value 1 (furthest away), at the same position as the first, the transparency color of the first sprite will cover up the graphics from the sprite with z-value 1. To try to illustrate it, it is looking somewhat like this: .___ |1 _|__ |..|...| |__|..0| ...|___| When it should have looked something like this: .___ |1..|__ |...|..| |__|..0| ...|___| If I render the sprite with z-value 1 first, and afterwards render the one with z-value 0, this problem does not occur. So, my problem in all simplicy is that when I render a sprite with z-value 1, I can't render over its transparency color with a sprite with z-value 0. I hope theres someone out there who understand what I'm trying to explain, and that someone can tell my why this is happening, and if there is a way to solve it. - Thanks, TT

Share this post


Link to post
Share on other sites
Any alphablended needs to be rendered back to front. That's a normal process common to 3d engines and 2D engines alike. With 2d engines, you usually get the sorting for free because your graphics are sorted by layers (background, tiles, objects, bullets, particles) anyways.

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