D3D Alpha Blend

alpha_blend-300x237

This demo shows how to using d3d alpha blend. Before you switch on the alpha blend operation, you should specify where this alpha value come from. We could specify this alpha value come from the material diffuse color, diffuse texture alpha channel, vertex color or from a constant register. This work will be very easy if you use Pixel shader. You could get any value from any where and assign it to “output.a”(output is COLOR0). With shader technology, you could even calculate a light intensity and take such value as your own alpha value.  But without shader technology, there are only several options that you could choose. Just as the screen-shot shows, the teapot get the alpha value from the material diffuse color alpha channel, and the upper graphic get it’s alpha value from it’s diffuse texture alpha channel.

 

To select the alpha value from the material diffuse color, the code like this:

// use alpha in material’s diffuse component for alpha
device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);
device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);

 

To select the alpha value from the diffuse texture alpha channel, the code like this:

// use alpha channel in texture for alpha
device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);

 

To switch on the alpha blend operation, the code write like this:

// set blending factors so that alpha component determines transparency
device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

 

Alpha blend operation is a bit time expansive operation. So you need to switch off the alpha blend operation after the blend operation was done.

// switch off alpha blend opration
device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);

 

The full source code could be download from here.

posted @ 2012-08-24 08:40  opencoder  阅读(328)  评论(0编辑  收藏  举报