D3D Alpha Blend
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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了