Sign in to follow this  
matthughson

Manual Alpha Blend + Additive Blend

Recommended Posts

Hey guys, I've run into a bit of a problem with my render/texture states. A while back I implemented the ability to manually fade out textures via the diffuse component of its material. It's worked fine ever since. Recently I added the ability to turn on addative blending on for textures. That worked fine as well. That is until I tried using them both at once. If I try to fade a texture out while it is being addativly blended, it becomes basically a binary alpha blend; that is it's either full on (when its alpha is greater than zero) or completely transparent (less than or equal to zero). Here is is the code I use for setting up those states:
	// Make it so the object can fade away
	m_pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
	m_pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
	m_pD3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
	m_pD3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
        
        // Turn on additive blending
	m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_ONE);
	m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ONE);

I imagive the problem is that one is over riding the other, but I'm not sure how I might fix it. Any suggestions? Thanks guys, Matt Hughson

Share this post


Link to post
Share on other sites
Sorry, but your setup makes no sense [smile]

Why do you use the same setting on both of your texture blending stages? If you don't use D3DTA_CURRENT as argument on stage 1 then you're discarding the data from stage 0.

Remember, all texture blending stages will be blended with each other to produce the final texture to use in the rendering.

Also, by setting both the source and destination blending factor to one you exclude the your final texture alpha value from the blending algorithm, so it shouldn't affect the visibility at all. What happens with negative alpha seems like unexpected behavior by the device.

Try this setup instead using one stage (haven't tried it but...)

m_pD3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
m_pD3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); /* Default */
m_pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
m_pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); /* Default */
m_pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_CURRENT ); /* Default, translates to D3DTA_DIFFUSE for stage 0 */

m_pD3DDevice->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD ); /* Default */
m_pD3DDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
m_pD3DDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );





If you want to add another texture stage then make sure you keep alphaarg2 at the default value so that the previous stage result is not excluded from the blending.

Stay sharp.

[EDIT] The binary alpha blend you speak of sounds a lot like what happens if alpha testing (D3DRS_ALPHATESTENABLE render state) is turned on and set to reject pixels with alpha equal to or below 0. This is not on by default so if it is on it has to have been turned on manually.

Share this post


Link to post
Share on other sites
Quote:
Original post by staaf
Sorry, but your setup makes no sense [smile]


That doesn't surprise me really. I'm still pretty new to DirectX, and had that code pretty much handed to me, so I never really took the time to figure it our completly.

Thanks for the suggestions though, I'll give it a try tommorow probably.

Matt Hughson

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