Sign in to follow this  

Alpha blending in DirectX 9(C++)

This topic is 2846 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 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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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 );

Share this post


Link to post
Share on other sites
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);


Share this post


Link to post
Share on other sites
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...)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites

This topic is 2846 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.

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