Skip to main content
gamedev.net
🔒 Locked

Transparency problem

Started by polaris2013 Aug 13, 2006 at 10:22 PM 8 replies 950+ views
Original Post
polaris2013
polaris2013
First off, I did use http://members.gamedev.net/jhoxley/directx/DirectXForumFAQ.htm (and of course the DX documentation) to help me understand alpha blending. But I have a problem which seems mystical to me. I'm trying to render a sprite tile to a surface with a colorkey that is transparent. What's happening is the entirety of the sprite is about 70%-80% transparent. I set it up like this: This is how I load the texture with the transparent color key (hot pink):
bool CSpriteList::LoadTexture(const char *sz_filename, LPDIRECT3DTEXTURE8& texture)
{
    //if(FAILED(D3DXCreateTextureFromFile(m_pD3DDevice, sz_filename, &texture)))
	if(0 > D3DXCreateTextureFromFileEx(m_pD3DDevice, sz_filename,
			D3DX_DEFAULT, D3DX_DEFAULT, 1,
			0, D3DFMT_R8G8B8, D3DPOOL_MANAGED,
			D3DX_FILTER_NONE, D3DX_FILTER_BOX, 0x00FF00FF,
			NULL, NULL, &texture))
    {
        return false;
    }

    return true;
}
(Not sure if the first byte of the color key should be FF or 00, I tried both and it seemed to have no difference. The file is a 24 bit bitmap image) This is how I render the textures with the transparent color (it returns it to opaque rendering at the end):
int CSpriteList::Render()
{
	m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
	m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
	UpdateVertices();

	m_pD3DDevice->SetStreamSource(0, m_pVertexBuffer, sizeof(SPRITE_CUSTOMVERTEX));
    m_pD3DDevice->SetVertexShader(SPRITE_D3DFVF_CUSTOMVERTEX);
	if(m_pTileSet != NULL)
	{
		m_pD3DDevice->SetTexture(0, m_pTileSet);
		m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
	}
	else
	{
		m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
	}
	m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, m_numPolygons);

	m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
	m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
	m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

	return m_numPolygons;
}
I don't want to post my entire initialization code so I'll just say that my front buffer is in this format: D3DFMT_X8R8G8B8 and the back buffer is in this format: D3DFMT_A8R8G8B8 and these are the other RenderStates that are set: m_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); m_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE); m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE); Hopefully I made an obvious mistake that someone notices! edit: dang, I always get the code tags wrong on this forum!
Chase85
Chase85
m_pD3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
m_pD3DDevice->SetRenderState(D3DRS_ALPHAREF, 0x08);
m_pD3DDevice->SetRenderState(D3DRS_ALPHFUNC, D3DCMP_GREATEREQUAL);

Also those first two Nibbles in the color key Should be FF not 00 on a 24 bit bitmap
_____"C allows you to shoot yourself in the foot, C++ allows you to re-use the Bullet, Assembly allows you to Create the Bullet from Molecules"
Dookie
Dookie
Try this. Where you have this:

m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

change it to this:

m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

Hope that helps! [smile]
"The crows seemed to be calling his name, thought Caw"
Dookie
Dookie
Ohh, colorkey. Sorry, what I posted might not work for you. I've never tried transparencies using colorkeys... My idea only works if you use the alpha to affect the degree of transparency, not a colorkey. [sad]
"The crows seemed to be calling his name, thought Caw"
polaris2013
polaris2013
Wow ya'll reply fast! :thumbup:

Okay,
To the first guy:
I made those changes and it had no apparent effect. In addition, I tried 0x00 and 0xFF in the D3DRS_ALPHAREF and neither of those changes had any effect either! That seems quite strange!

To the second guy:
I made your change just to see what would happen- all transperency on the sprite was lost, and the hot pink background became black (because of the colorkey). I didn't expect it to work, but I didn't expect it to do that either!

Also I changed where some of the RenderStates are done now. Now all that's modified in the Render() are the two enables (ALPHABLENDENABLE and ALPHATESTENABLE), all the other RenderStates are only during initialization. Should I edit the first post to reflect that or post a new copy in a reply?
Dookie
Dookie
I don't know a whole lot about using color keys, but I think I've found something for you to look at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/D3DXCreateTextureFromFileEx.asp

You've probably already looked at this page a dozen times, but the part that grabbed me was the section that described the 'colorKey' parameter. It reads that the "alpha is significant and should usually be set to FF for opaque color keys", and it doesn't look like you're including the alpha in your 'colorKey' parameter. So for your D3DXCreateTextureFromFile() call, change this:

0x00FF00FF

to this:

0xFFFF00FF

and see what happens. Hope that works for you!
"The crows seemed to be calling his name, thought Caw"
polaris2013
polaris2013
I have tried everything that everyone else in this thread has suggested, and the problem is still exactly the same. Maybe I'm not describing the problem well enough so I'll post an image illustrating the problem:
http://img152.imageshack.us/img152/5676/problemys9.png

Does that help anybody diagnose what I did wrong? I tried everything I could think of to fix this...
CCF
CCF
Try this maybe,

m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
m_pD3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
Endurion
Endurion
I'd guess you need to actually tell DirectX to add an alpha component to the texture surface. Instead of D3DFMT_R8G8B8 use D3DFMT_A8R8G8B8.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
polaris2013
polaris2013
THANK YOU Everyone! I finally solved it! Here is the final code in case anyone is wondering:

Initialization:
    m_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);    m_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);    m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);	m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);	m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);	//m_pD3DDevice->SetRenderState(D3DRS_ALPHAREF, 0xFF);	//m_pD3DDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);	//m_pD3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);


Rendering:
int CSpriteList::Render(){	m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);	m_pD3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);	UpdateVertices();	m_pD3DDevice->SetStreamSource(0, m_pVertexBuffer, sizeof(SPRITE_CUSTOMVERTEX));    m_pD3DDevice->SetVertexShader(SPRITE_D3DFVF_CUSTOMVERTEX);	if(m_pTileSet != NULL)	{		m_pD3DDevice->SetTexture(0, m_pTileSet);		m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);	}	else	{		m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);	}	m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, m_numPolygons);	m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);	m_pD3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);	return m_numPolygons;}


Loading the texture:
bool CSpriteList::LoadTexture(const char *sz_filename, LPDIRECT3DTEXTURE8& texture){    //if(FAILED(D3DXCreateTextureFromFile(m_pD3DDevice, sz_filename, &texture)))	if(0 > D3DXCreateTextureFromFileEx(m_pD3DDevice, sz_filename,			D3DX_DEFAULT, D3DX_DEFAULT, 1,			0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED,			D3DX_FILTER_NONE, D3DX_FILTER_BOX, 0xFFFF00FF,			NULL, NULL, &texture))    {        return false;    }    return true;}


edit: after fiddling with it, it looks like the crucial change was Endurion's. The first byte of the color key has no change. The ALPHATESTENABLEs can be commented out. Otherwise, I think it is where I started.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.