Sign in to follow this  
tuxbook

Alpha: Adjustable and partly transparent?

Recommended Posts

Hi All, I have adjustable alpha working using BLENDFACTOR like this:
unsigned char alpha = (int)(m_Alpha*255.0f);

union AlphaUnion
{
	DWORD dw;
	unsigned char bytes[4];
} alpha_full;

alpha_full.bytes[0] = alpha;
alpha_full.bytes[1] = alpha;
alpha_full.bytes[2] = alpha;
alpha_full.bytes[3] = alpha;

m_Device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
m_Device->SetRenderState(D3DRS_BLENDFACTOR, alpha_full.dw);
m_Device->SetRenderState(D3DRS_SRCBLEND,  D3DBLEND_BLENDFACTOR);
m_Device->SetRenderState(D3DRS_DESTBLEND,  D3DBLEND_INVBLENDFACTOR);
But I also want to use the texture's alpha channel like this:
m_Device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);

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

m_Device->SetTextureStageState(0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1);
m_Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
Both methods require setting something different to the D3DRS BLEND flags. How can I have both adjustable alpha and use texture alpha? Thanks.

Share this post


Link to post
Share on other sites
Thanks, that worked great!

For anyone interested, here's the code.


m_Device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);

m_Device->SetRenderState(D3DRS_TEXTUREFACTOR, alpha_full.dw);

// Multiply texture alpha with adjustable alpha colour
m_Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
m_Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
m_Device->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_TFACTOR);

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