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.

转载于:https://www.cnblogs.com/open-coder/archive/2012/08/24/2653582.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值