Sign in to follow this  
limpacp

Transparency and Alpha channel!

Recommended Posts

Hello! I'm writting 2D game. I have a png image with alpha channel. My game has 3 states of blitting: normal, diffuse and additive. Right now i've added one more mode transparent: Using this states: m_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); m_pDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); m_pDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); m_pDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE); When i'm activating this mode alpha channel is lost! What's wrong ? I need to blend image with alpha channel, what states do i need to set! Please help! Thanks!

Share this post


Link to post
Share on other sites
A couple things.

Setting DESTBLEND is only part of setting up the blending mode. You should also set SRCBLEND, and possibly BLENDOP. Regular blending is:
SRCBLEND = SRCALPHA
DESTBLEND = INVSRCALPHA
BLENDOP = ADD.
ALPHABLENDENABLE = true

possibly also have D3D not waste time blending fully transparent parts.
ALPHATESTENABLE = true
ALPHAREF = 0
ALPHAFUNC = GREATER

Note that alphatest and alphablend are seperate. You can use testing to cut shapes without enabling blending. This is useful because alphatested holes aren't rendered to Z, stencil, or color buffers. This allows sprites to be non-square and use Z buffering at the same time.

Second thing, and this will be your main problem, is your state setup. It's incomplete, and you're specifically asking not to get the texture alpha, but just vertex or material alpha. A complete state setup should set not just the operations, but the arguments as well. You ask for alpha to come from DIFFUSE which is the result of the lighting system. You want the alpha to be from the texture.

0, D3DTSS_COLOROP, D3DTOP_SELECTARG1
0, D3DTSS_COLORARG1, D3DTA_TEXTURE
0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1
0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE
1, D3DTSS_COLOROP, D3DTOP_DISABLE
1, D3DTSS_ALPHAOP, D3DTOP_DISABLE

You can mix vertex/material alpha with texture alpha like so
0, D3DTSS_COLOROP, D3DTOP_SELECTARG1
0, D3DTSS_COLORARG1, D3DTA_TEXTURE
0, D3DTSS_ALPHAOP, D3DTOP_MODULATE
0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE
0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE
1, D3DTSS_COLOROP, D3DTOP_DISABLE
1, D3DTSS_ALPHAOP, D3DTOP_DISABLE

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