深度测试与alpha混合

Comment:做粒子系统的时候发现,如果用alpha blend的话,先渲染的在前面的物体会挡住后面的物体产生一个图片的黑边,最常被提到的做法是按离摄像机从远到近的顺序把物体sort一遍,然后按从远到近的顺序渲染。但是超级不喜欢sort这种nlogn的算法,而且还要加重cpu的负担,在加上要转换到视口才能sort,不用写就觉得会很ugly。找了下是否有其它办法取代,查到了下面的方法。其实很简单,就是渲染粒子系统的时候,SetRenderState(D3DRS_ZENABLE,false)。这样后渲染的在远处的粒子就会叠加在先渲染的物体上面,融合在一起。效果还不错。但是只适用于粒子这种允许乱序的物体,模型的话顺序则会乱掉。

在绘制复杂的三维场景时,不可避免地会出现物体间的相互遮挡,在这种情况下,为了正确地绘制场景需要使用深度测试。半透明物体的绘制不同于不透明物体,Direct3D通过alpha混合实现半透明物体的绘制。深度测试可以简化复杂场景的绘制,alpha混合可以使绘制的三维场景更完整、更逼真。

在复杂的场景中,通常有多个物体需要绘制,这些物体之间通常会存在遮挡关系,离观察点较远的物体会因为近处物体的者的遮挡而不可见或只有部分可见,Direct3D图形系统提供了深度测试功能来实现这种效果。


 

深度缓冲区与深度测试

要理解深度测试,首先需要理解深度缓冲区。深度缓冲区是Direct3D用来存储绘制到屏幕上的每个像素点的深度信息的一块内存缓冲区。当Direct3D将一个场景渲染到目标表面上时,它使用深度缓冲区来决定光栅化后各个多边形的像素的前后遮挡关系,最终决定哪个颜色值被绘制出来。也就是说,Direct3D通过比较当前绘制的像素点的深度和对应深度缓冲区的点的深度值来决定是否绘制当前像素。如果深度测试结果为TRUE,则绘制当前像素,并用当前像素点的深度来更新深度缓冲区,反之则不予绘制。通常情况下,深度缓冲区对应于屏幕大小的一块二维区域。

对一个启用了深度缓冲区的场景进行光栅化操作时,渲染表面上的每个点都要进行深度测试。在深度测试开始时,深度缓冲区的深度值被设置为该场景可能出现的最大值,渲染表面上的颜色值被设置为背景颜色值。然后测试场景内即将绘制的每个多边形,看它是否小于存储在深度缓冲区中的深度值,如果该多边形的深度值更小,则该深度值被更新到深度缓冲区中,并将渲染表面上当前点的颜色值替换为该多边形的颜色。如果多边形在这一点的深度值更大,将继续测试列表中的下一个多边形。

 

创建深度缓冲区

若要在Direct3D图形程序中应用深度测试,首先必须在创建Direct3D渲染设备时创建深度缓冲区,示例代码如下:

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE; // 表示由Direct3D创建并管理一个深度缓冲区
d3dpp.AutoDepthStencilFormat = D3DFMT_D16; // 表示深度缓冲区中每一个像素的深度值由16位的二进制数表示if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_device)))
{
return false;
}

 

激活深度测试

深度缓冲区随Direct3D渲染设备创建好后,调用Direct3D渲染状态设置函数IDirect3DDevice9::SetRenderState(),将第一个参数设为D3DRS_ZENABLE,第二个参数设为TRUE,激活深度测试:

g_device->SetRenderState(D3DRS_ZENABLE, TRUE);

 

设置深度测试函数

接下来依然调用IDirect3DDevice9::SetRenderState()函数设置深度测试函数,第一个参数设置为D3DRS_ZFUNC:

D3DRS_ZFUNC
One member of the D3DCMPFUNC enumerated type. Thedefault value is D3DCMP_LESSEQUAL. This member enables an application toaccept or reject a pixel, based on its distance from the camera.

The depth value of the pixel is compared with thedepth-buffer value. If the depth value of the pixel passes the comparisonfunction, the pixel is written.

The depth value is written to the depth buffer onlyif the render state is TRUE.

Software rasterizers and many hardware acceleratorswork faster if the depth test fails, because there is no need to filter andmodulate the texture if the pixel is not going to be rendered.

 

第二个参数设置为想要设置的深度测试函数,它属于D3DCMPFUNC枚举类型,定义如下:

Defines the supported compare functions.

typedef enum D3DCMPFUNC
{
D3DCMP_NEVER = 1,
D3DCMP_LESS = 2,
D3DCMP_EQUAL = 3,
D3DCMP_LESSEQUAL = 4,
D3DCMP_GREATER = 5,
D3DCMP_NOTEQUAL = 6,
D3DCMP_GREATEREQUAL = 7,
D3DCMP_ALWAYS = 8,
D3DCMP_FORCE_DWORD = 0x7fffffff,
} D3DCMPFUNC, *LPD3DCMPFUNC;Constants
D3DCMP_NEVER
Always fail the test.
D3DCMP_LESS
Accept the new pixel if its value is less than thevalue of the current pixel.
D3DCMP_EQUAL
Accept the new pixel if its value equals the valueof the current pixel.
D3DCMP_LESSEQUAL
Accept the new pixel if its value is less than orequal to the value of the current pixel.
D3DCMP_GREATER
Accept the new pixel if its value is greater thanthe value of the current pixel.
D3DCMP_NOTEQUAL
Accept the new pixel if its value does not equalthe value of the current pixel.
D3DCMP_GREATEREQUAL
Accept the new pixel if its value is greater thanor equal to the value of the current pixel.
D3DCMP_ALWAYS
Always pass the test.
D3DCMP_FORCE_DWORD
Forces this enumeration to compile to 32 bits insize. Without this value, some compilers would allow this enumeration tocompile to a size other than 32 bits. This value is not used.
Remarks

The values in this enumerated type define the supportedcompare functions for the D3DRS_ZFUNC, D3DRS_ALPHAFUNC, and D3DRS_STENCILFUNCrender states.

通常情况下,深度测试函数设置为D3DCMP_LESS,表示当测试点深度值小于深度缓冲区中相应值时,通过深度测试并绘制相关像素,这样没有被遮挡的物体才显示,而被遮挡的物体就不显示。示例代码如下:

g_device->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);

 

更新深度缓冲区

设置了深度测试函数后,还需要设置深度测试成功时对深度缓冲区如何操作,是保持原来的深度值,还是用当前像素的深度值更新对应的数值。

D3DRS_ZWRITEENABLE
TRUE to enable the application to write to thedepth buffer. The default value is TRUE. This member enables an applicationto prevent the system from updating the depth buffer with new depth values.If FALSE, depth comparisons are still made according to the render stateD3DRS_ZFUNC, assuming that depth buffering is taking place, but depth valuesare not written to the buffer.

示例代码如下:

g_device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);

表示如果通过测试,则用当前像素的深度值更新深度缓冲区中对应的数值,这是最常用的设置,也是默认设置。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值