Sign in to follow this  

Alpha Blending With DirectX 9

This topic is 4733 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 am a little new to graphics programming with Direct3d. What I am designing is a 2D game that uses the Direct3d9 libraries. Because I am using Direct3D I want to be able to take advantage of the alpha blending aspects. I was wondering if someone could maybe explain a few things to help clarify the concept of Alpha Blending with me. I understand that all of the different blitting effects are done by setting the renderstate properties. For example, below is the code I use for setting the renderstate for Alpha Blended textures. //------------ this->d3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_DESTALPHA); this->d3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_SRCALPHA); //------------ Right now this works, but only when I blit using a vertexcolor with an alpha value of 'FF'(255). What I want is to be able to lower the alpha value. The problem is, when i lower the alpha value the section that is supposed to be transparent is no longer transparent. Is there a way I can have a threshold of transparent colors for all alpha values? So that the parts of the texture that are black are always transparent with any alpha value? I did a little research and I think the following render states might be what I need but I couldn't find enough information about them to understand how to use them. this->d3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, ?); this->d3dDevice->SetRenderState(D3DRS_ALPHAREF, ?); this->d3dDevice->SetRenderState(D3DRS_ALPHAFUNC, ?);

Share this post


Link to post
Share on other sites
If you want the alpha to control the opacity (0 = Fully transparent, 255 = 100% opaque), then you should use:

D3DRS_ALPHABLENDENABLE -> TRUE
D3DRS_SRCBLEND -> D3DBLEND_SRCALPHA
D3DRS_DESTBLEND -> D3DBLEND_INVSRCALPHA

You'll get: FinalColor = (Color * Alpha) + (BackbufferColor * (255 - Alpha))

Alpha Testing

When alpha testing is enabled, it will compare the current alpha to the AlphaRef using the AlphaFunc. If the test succeed, then the pixel will be rendered.

For example, if you set AlphaFunc = GreaterEqual and AlphaRef = 128, then only the pixels with an alpha value >= 128 will be rendered.

Share this post


Link to post
Share on other sites

This topic is 4733 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