Alpha blending in DirectX 9(C++)

Started by
8 comments, last by Bru 14 years, 1 month ago
i dont quite understand how to make alpha blending work. i have this awesome energy ball sprite : the backround is transperent(made sure by using photoshop) and i enable alpha blending using :

Device->SetRenderState(D3DRS_ALPHABLENDENABLE,true);
by setting the source and destination,i only managed to make the whole quad go transperent and not the invidual pixels in which are suppoused to be. what am i doing wrong?
Advertisement
The render state is ok, but that image hasn't got any alpha channel at all, if that was your intention. Here is an example image you can play around with (cyan circle, border alpha-ed out):



You definitively did something wrong during creation with Photoshop. Can't help you there, though, I use Paint.NET.
Your image doesnt have alpha channel.

Try this image edited using Paint.NET.


http://img202.imageshack.us/img202/6706/projectile.png

My Game Development Blog : chetanjags.wordpress.com

hey,thanks for the answer :)
i directly replaced my image with this one to test it,and the border seems to be black ingame.
i am also using these source and destination defitions :
Device->SetRenderState(D3DRS_SRCBLEND , D3DBLEND_SRCCOLOR);Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCCOLOR);

Try these. Note that these also enable additional tinting with the diffuse color:

Device->SetRenderState( D3DRS_ALPHABLENDENABLE, true );Device->SetRenderState( D3DRS_ALPHATESTABLE, false );Device->SetRenderState( D3DRS_SRCBLEND , D3DBLEND_SRCALPHA );Device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA) ;Device->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );Device->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );Device->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );Device->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );Device->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );Device->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Quote:Original post by Bru
hey,thanks for the answer :)
i directly replaced my image with this one to test it,and the border seems to be black ingame.
i am also using these source and destination defitions :
Device->SetRenderState(D3DRS_SRCBLEND , D3DBLEND_SRCCOLOR);Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCCOLOR);


Those blending states are working with the colour (RGB) component of the source texture so wont give the effect you desire (the alpha channel doesnt even have any effect there). Instead you need to use the source alpha components so just change those two render states to this and you should be good.

Device->SetRenderState(D3DRS_SRCBLEND , D3DBLEND_SRCALPHA);Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
i understand now. but it still seems to give me a black border,even when i load the fixed image and use states like this :
Device->SetRenderState(D3DRS_ALPHABLENDENABLE,true);Device->SetRenderState(D3DRS_SRCBLEND , D3DBLEND_SRCALPHA);Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

(when i say black border, i mean everything around the ball that is pretty much suppoused to be invisible.)

perhaps it's the way i draw it? it's for a billboard class i made. i draw like this :
 Device->SetTransform(D3DTS_WORLD, &billboard ); Device->SetTexture(0,*texture); Device->SetFVF(D3DFVF_XYZ|D3DFVF_TEX1); Device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST,0,4,2,&indices,D3DFMT_INDEX16,&vertices,sizeof(BillboardVertex));


and the vertex struct is
struct BillboardVertex{D3DXVECTOR3 location;float u,v;};


could it perhaps be there?

(offtopic,if you wonder why i dont create a vertex buffer,that's because due to animation i would need to change the UV cordinates every frame,which would be ineffiecient if i lock and unlock the buffer every frame...)
bah, i just dont get it. i even downloaded a sample of alpha blending to inspect,and it seems like in that example,loading my texture works,and the key is just enabling it,and setting the source and destination once.
i downloaded "alpha texture blending" sample from http://www.codesampler.com/dx9src/dx9src_4.htm#dx9_alpha_blending_texture

the FVF in that sample and in my project are the same,even if i load the texture exacly the same way like in the sample it's still the same.
i am seriously confused,what's the problem?
You might want to try setting D3DRS_BLENDOP to D3DBLENDOP_ADD in case it's been changed from the default.

If that doesn't help I'd suggest using PIX to debug it.
oh... oh it hurts so much. i wasted like 5 hours just working on it and thanks to your advice i found what the problem was. i render my billboard before i render anything else,and since my backround is empty and black,it get's sorted that way that it's transperent pixels are also black,and i thought it as because it didnt work...

oh well,thanks a lot for the help :)

This topic is closed to new replies.

Advertisement