新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论高级C/C++编程、代码重构(Refactoring)、极限编程(XP)、泛型编程等话题
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 C/C++编程思想 』 → 深度测试与alpha混合 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 17225 个阅读者浏览上一篇主题  刷新本主题   平板显示贴子 浏览下一篇主题
     * 贴子主题: 深度测试与alpha混合 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 C/C++编程思想 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客楼主
    发贴心情 

    深度测试与alpha混合(2)
    前面的一些例子中,已经遇到了alpha值的概念,如结构体D3DCOLORVALUE中的成员变量a,但它并没有被使用,因为还没有激活alpha混合(alpha blending),并且alpha总是被赋值为1.0f(默认渲染状态下alpha为1.0f,表示完全不透明)。

    alpha混合原理

    在前面介绍的示例程序中,绘制图形的颜色总是替换当前颜色缓冲区中存在的颜色,这样后面的物体总是覆盖在原有的物体上。但是当想要绘制类似于玻璃、水等具有透明效果的物体时,这种方法显然满足不了要求。通过定义一个表示物体半透明度的alpha值和一个半透明计算公式,可以将要绘制的物体颜色与颜色缓冲区中存在的颜色相混合,从而绘制出具有半透明效果的物体。Direct3D计算alpha颜色混合的方法如下:

    color = (RGBsrc * Ksrc) OP (RGBdst * Kdst)

    其中color表示alpha混合后的颜色值,RGBsrc表示源颜色值,即将要绘制的图元的颜色值;Ksrc表示源混合系数,通常赋值为表示半透明程度的alpha值,也可以是属于枚举类型D3DBLEND的任意值,用来和RGBsrc相乘。RGBdst表示目标颜色值,即当前颜色缓冲区中的颜色值,Kdst表示目标混合系数,可以是属于枚举D3DBLEND的任意值,用来和RGBdst相乘。OP表示源计算结果与颜色缓冲区计算结果的混合方法,默认状态下OP为D3DBLEND_ADD,即源计算结果与颜色缓冲区计算结果相加。

    图形显示中,对alpha混合最普遍的用法是:把Ksrc赋值为D3DBLEND_SRCALPHA,即当前绘制像素的alpha值;把Kdst赋值为D3DBLEND_INVSRCALPHA,即1减去当前绘制像素的alpha值;把OP赋值为D3DBLEND_ADD,使源计算结果和颜色缓冲区计算结果相加,这样一来,alpha混合颜色的公式变为:

    color = (RGBsrc * Ksrc) + (RGBdst * Kdst)

    上面的设置可以较好地模拟大多数半透明物体的效果。

    启用alpha混合

    想要绘制半透明物体,首先需要激活Direct3D的alpha混合运算,调用Direct3D渲染状态设置函数IDirect3DDevice9:::SetRenderState(),将第一个参数设置为D3DRS_ALPHABLENDENABLE,第二个参数设置为TRUE,可以激活alpha混合,代码如下:

    g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);

    设置alpha混合系数

    在上面介绍的alpha混合原理中提到的源混合系数和目标混合系数,也是通过Direct3D渲染状态设置函数IDirect3DDevice9::SetRenderState()设置的。若将第一个参数设置为D3DRS_SRCBLEND,则用于设置源混合系数,若将第一个参数设置为D3DRS_DESTBLEND,则用于设置目标混合系数,第二个参数可以设置为D3DBLEND枚举常量,各具体枚举常量的含义如下:

    Defines the supported blend mode.

    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;
    Constants
    D3DBLEND_ZERO
    Blend factor is (0, 0, 0, 0).
    D3DBLEND_ONE
    Blend factor is (1, 1, 1, 1).
    D3DBLEND_SRCCOLOR
    Blend factor is (Rs, Gs, Bs, As).
    D3DBLEND_INVSRCCOLOR
    Blend factor is (1 - Rs, 1 - Gs, 1 - Bs, 1 - As).
    D3DBLEND_SRCALPHA
    Blend factor is (As, As, As, As).
    D3DBLEND_INVSRCALPHA
    Blend factor is ( 1 - As, 1 - As, 1 - As, 1 - As).
    D3DBLEND_DESTALPHA
    Blend factor is (Ad Ad Ad Ad).
    D3DBLEND_INVDESTALPHA
    Blend factor is (1 - Ad 1 - Ad 1 - Ad 1 - Ad).
    D3DBLEND_DESTCOLOR
    Blend factor is (Rd, Gd, Bd, Ad).
    D3DBLEND_INVDESTCOLOR
    Blend factor is (1 - Rd, 1 - Gd, 1 - Bd, 1 - Ad).
    D3DBLEND_SRCALPHASAT
    Blend factor is (f, f, f, 1); where f = min(As, 1 - Ad).
    D3DBLEND_BOTHSRCALPHA
    Obsolete. 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_BOTHINVSRCALPHA
    Source 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_BLENDFACTOR
    Constant 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 of D3DCAPS9.
    D3DBLEND_INVBLENDFACTOR
    Inverted 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 DestBlendCaps members of D3DCAPS9.
    D3DBLEND_SRCCOLOR2
    Blend 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_INVSRCCOLOR2
    Blend 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_DWORD
    Forces 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.
    Remarks
    In 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 Blending
    Direct3D 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.

    设置alpha混合系数的代码示例如下:

    g_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    g_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

    设置alpha混合方法

    alpha混合方法指定源颜色和目标颜色的混合方法,通过Direct3D渲染状态设置函数IDirect3DDevice9::SetRenderState()设置,其中第一个参数设置为 D3DRS_BLENDOP,第二个参数设置为D3DBLENDOP枚举常量,各常量的含义如下:

    Defines the supported blend operations. See Remarks for definitions of terms.

    typedef enum D3DBLENDOP{    D3DBLENDOP_ADD = 1,    D3DBLENDOP_SUBTRACT = 2,    D3DBLENDOP_REVSUBTRACT = 3,    D3DBLENDOP_MIN = 4,    D3DBLENDOP_MAX = 5,    D3DBLENDOP_FORCE_DWORD = 0x7fffffff,} D3DBLENDOP, *LPD3DBLENDOP;
    Constants
    D3DBLENDOP_ADD
    The result is the destination added to the source. Result = Source + Destination
    D3DBLENDOP_SUBTRACT
    The result is the destination subtracted from to the source. Result = Source - Destination
    D3DBLENDOP_REVSUBTRACT
    The result is the source subtracted from the destination. Result = Destination - Source
    D3DBLENDOP_MIN
    The result is the minimum of the source and destination. Result = MIN(Source, Destination)
    D3DBLENDOP_MAX
    The result is the maximum of the source and destination. Result = MAX(Source, Destination)
    D3DBLENDOP_FORCE_DWORD
    Forces 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.
    Remarks
    Source, Destination, and Result are defined as:

    Term Type Description
    Source Input Color of the source pixel before the operation.
    Destination Input Color of the pixel in the destination buffer before the operation.
    Result Output Returned value that is the blended color resulting from the operation.

    This enumerated type defines values used by the following render states:

    D3DRS_BLENDOP
    D3DRS_BLENDOPALPHA

    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/3/9 9:59:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 C/C++编程思想 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/19 9:32:04

    本主题贴数9,分页: [1]

     *树形目录 (最近20个回帖) 顶端 
    主题:  深度测试与alpha混合(5638字) - 卷积内核,2009年3月9日
        回复:  [B]深度测试与alpha混合(5)[/B]透过那些透明度非常高的物体看其他物体,例如透过几乎完..(8947字) - 卷积内核,2009年4月27日
        回复:  源程序: #include <d3dx9.h>#pragma warning(disabl..(6331字) - 卷积内核,2009年4月27日
        回复:  [B]深度测试与alpha混合(4)[/B][B]材质alpha[/B]顶点alpha是没有..(2490字) - 卷积内核,2009年4月27日
        回复:  源程序:#include <d3dx9.h>#pragma warning(disabl..(6950字) - 卷积内核,2009年4月27日
        回复:  深度测试与alpha混合(3) alpha源混合系数通常设置为D3DBLEND_SRCALPHA..(1183字) - 卷积内核,2009年4月27日
        回复:  [B]示例程序:[/B]该示例程序模拟了直升飞机螺旋桨的半透明效果。在程序的初始化阶段,载入H..(9414字) - 卷积内核,2009年3月9日
        回复:  深度测试与alpha混合(2) 前面的一些例子中,已经遇到了alpha值的概念,如结构体D3DC..(7799字) - 卷积内核,2009年3月9日
        回复:  [B]示例程序[/B]该示例程序绘制了一个矩形和一个坦克模型,其中先绘制矩形,再绘制坦克模型,..(8669字) - 卷积内核,2009年3月9日

    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    156.250ms