Not logged in, Join Here! or Log In Below:  
News Articles Search    

 Home / 3D Theory & Graphics / d3d & blend factor Account Manager
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.

April 05, 2005, 05:55 AM

How to use alpha blending with blend factor? I'm writing something like this:

  2. g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
  3. g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  4. g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_CURRENT );
  5. g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
  6. g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
  7. g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
  8. g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
  10. g_pd3dDevice->SetRenderState(D3DRS_BLENDFACTOR, 0x07ffffff);
  11. g_pd3dDevice->SetRenderState(D3DRS_TEXTUREFACTOR, 0x07ffffff);
  13. g_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE,  FALSE );
  14. g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
  15. g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_BLENDFACTOR );
  16. g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVBLENDFACTOR );
  18. // render mesh
  20. g_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE,  TRUE );
  21. g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );

but it doesn't work (mesh isn't transparent at all). I have no idea why. It seems that blendfactor affects mesh color but alpha channel is ignored. Anyone know how to fix this?


April 05, 2005, 12:41 PM

Don't forget that the blend operation is :
srccolor * srcblend + destcolor * destblend

since the RGB of your blendfactor is white, srccolor is multiplied by white and destcolor by black. the alpha channel of the blendfactor is not used when computing the blended rgb

I supposed it's used when blending the alpha values that are written to the render target, but normally it doesn't matter.

in a normal alpha blending situation the alpha is used because srcalpha and invsrcalpha are used as srcblend and destblend.

note : I need to start using the shift key more...


April 06, 2005, 01:10 AM

Hymm... I suppose you're right. I thought that it'll replace the vertex color of all verticles drawn next... I don't know why I didn't notice that it's set for srcblend not srccolor ;). Anyway, can anyone tell me how to do alpha blending without changing every vertex color in vertex buffer? In other words - how to set a global alpha (or color) value for blending? Is it possible?


April 06, 2005, 01:16 AM

You can do that via materials. This in turn needs lighting enabled.
In the material set the diffuse alpha (and other needed alphas)to a value below 1.0.


April 06, 2005, 01:42 AM

if you are using the fixed function pipeline you can use texture factor to set your alpha value. And in fact you have done that in your first post, so what's the problem? you can set alpha and color channel operations separately so you can take the color from the vertices, but the alpha from the texture factor.



April 06, 2005, 03:37 AM

Endurion: yes, I know that :) The problem is, that i don't want to use build-in lighting ;)
juhnu: The problem is that it doesn't work ;) Maybe it's becouse the blend factor. I'll check texture factor only. Thanks


April 06, 2005, 07:16 AM

I didn't read your example very carefully.b You can either use alpha to control the blending or use the blend factor as you have. g_pd3dDevice->SetRenderState(D3DRS_BLENDFACTOR, 0x07ffffff); use 0x00070707 instead
or something.



April 06, 2005, 08:37 AM

Might seem obvious, but have you checked that your testbed graphics card supports both texture factor and blend factor operations? Its easy enough to do...



April 07, 2005, 07:24 AM

It works in the way juhnu suggested (with value 0x00070707 etc.).
scoot: yes, of course i've checked ;)

Thanks a lot.

This thread contains 9 messages.
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.