本帖最后由 夜行的猫仔 于 2013-10-23 15:34 编辑
首先要明白的是,半透明混合都来源于这个公式: Color = TexelColor x SourceBlend + CurrentPixelColor x DestBlend
也就是
Color=RGBs * As +RGBd * Ad
其中Color是最终显示的颜色,RGBs是渲染源的颜色,也就是模型的颜色,As是渲染源的半透明系数,RGBd是渲染目标的颜色,也就是背景色, Ad渲染目标(背景)的混合系数
Syntax- typedef enum D3DBLEND {
- D3DBLEND_ZERO = 1,
- D3DBLEND_ONE = 2,
- D3DBLEND_SRCCOLOR = 3,
- D3DBLEND_INVSRCCOLOR = 4,
- D3DBLEND_SRCALPHA = 5,
- D3DBLEND_INVSRCALPHA = 6,
- D3DBLEND_DESTALPHA = 7,
- D3DBLEND_INVDESTALPHA = 8,
- D3DBLEND_DESTCOLOR = 9,
- D3DBLEND_INVDESTCOLOR = 10,
- D3DBLEND_SRCALPHASAT = 11,
- D3DBLEND_BOTHSRCALPHA = 12,
- D3DBLEND_BOTHINVSRCALPHA = 13,
- D3DBLEND_BLENDFACTOR = 14,
- D3DBLEND_INVBLENDFACTOR = 15,
- D3DBLEND_SRCCOLOR2 = 16,
- D3DBLEND_INVSRCCOLOR2 = 17,
- D3DBLEND_FORCE_DWORD = 0x7fffffff
- } D3DBLEND, *LPD3DBLEND;
复制代码 D3DBLEND_ZEROBlend factor is (0, 0, 0, 0). D3DBLEND_ONEBlend factor is (1, 1, 1, 1). D3DBLEND_SRCCOLORBlend factor is (Rs, Gs, Bs, As). D3DBLEND_INVSRCCOLORBlend factor is (1 - Rs, 1 - Gs, 1 - Bs, 1 - As). D3DBLEND_SRCALPHABlend factor is (As, As, As, As). D3DBLEND_INVSRCALPHABlend factor is ( 1 - As, 1 - As, 1 - As, 1 - As). D3DBLEND_DESTALPHABlend factor is (Ad Ad Ad Ad). D3DBLEND_INVDESTALPHABlend factor is (1 - Ad 1 - Ad 1 - Ad 1 - Ad). D3DBLEND_DESTCOLORBlend factor is (Rd, Gd, Bd, Ad). D3DBLEND_INVDESTCOLORBlend factor is (1 - Rd, 1 - Gd, 1 - Bd, 1 - Ad). D3DBLEND_SRCALPHASATBlend factor is (f, f, f, 1); where f = min(As, 1 - Ad). D3DBLEND_BOTHSRCALPHAObsolete. Starting with DirectX 6, you can achieve the same effect by setting the source and destination blend factors to D3DBLEND_SRCALPHA and D3DBLEND_INVSRCALPHA in separate calls. D3DBLEND_BOTHINVSRCALPHASource blend factor is (1 - As, 1 - As, 1 - As, 1 - As), and destination blend factor is (As, As, As, As); the destination blend selection is overridden. This blend mode is supported only for the D3DRS_SRCBLEND render state. D3DBLEND_BLENDFACTORConstant color blending factor used by the frame-buffer blender. This blend mode is supported only if D3DPBLENDCAPS_BLENDFACTOR is set in the SrcBlendCaps or DestBlendCaps members ofD3DCAPS9. D3DBLEND_INVBLENDFACTORInverted constant color-blending factor used by the frame-buffer blender. This blend mode is supported only if the D3DPBLENDCAPS_BLENDFACTOR bit is set in the SrcBlendCaps or DestBlendCapsmembers of D3DCAPS9. D3DBLEND_SRCCOLOR2Blend factor is (PSOutColor[1]r, PSOutColor[1]g, PSOutColor[1]b, not used). See Render Target Blending. Differences between Direct3D 9 and Direct3D 9Ex: This flag is available in Direct3D 9Ex only. |
D3DBLEND_INVSRCCOLOR2Blend factor is (1 - PSOutColor[1]r, 1 - PSOutColor[1]g, 1 - PSOutColor[1]b, not used)). See Render Target Blending. Differences between Direct3D 9 and Direct3D 9Ex: This flag is available in Direct3D 9Ex only. |
D3DBLEND_FORCE_DWORDForces this enumeration to compile to 32 bits in size. Without this value, some compilers would allow this enumeration to compile to a size other than 32 bits. This value is not used. RemarksIn the preceding member descriptions, the RGBA values of the source and destination are indicated by the s and d subscripts. The values in this enumerated type are used by the following render states: - D3DRS_DESTBLEND
- D3DRS_SRCBLEND
- D3DRS_DESTBLENDALPHA
- D3DRS_SRCBLENDALPHA
See D3DRENDERSTATETYPE Render Target BlendingDirect3D 9Ex has improved text rendering capabilities. Rendering clear-type fonts would normally require two passes. To eliminate the second pass, a pixel shader can be used to output two colors, which we can call PSOutColor[0] and PSOutColor[1]. The first color would contain the standard 3 color components (RGB). The second color would contain 3 alpha components (one for each component of the first color). These new blending modes are only used for text rendering on the first render target. -------------------------------------------------------------------------------------------------------------------------------------------------以上是微软的D3DBLEND文档,接下来通过程序来测试里面的混合效果。
此次使用例题的源代码下载:
该示例程序模拟了直升飞机螺旋桨的半透明效果。在程序的初始化阶段,载入Heli.x文件,它是一个包含直升飞机的三维模型文件,其中螺旋桨的材质漫反射属性为(R, G, B, A) = (0.183700; 0.183700; 0.183700; 0.500000; ),可以用文本方式打开Heli.x查看它的材质属性,Heli.x是一个文本格式的.x文件。
示例程序中没有设置alpha混合方法,所以应用程序将采用默认的alpha混合方法D3DBLEND_ADD,即将源计算结果与颜色缓冲区计算结果相加。由于直升机螺旋桨的材质的alpha值为0.5f,因此它的最终颜色就是50%的玻璃颜色加上50%的背景颜色。
以下分别对不同的参数做了渲染测试:
|