Depth Buffer是什么

What Is a Depth Buffer?

A depth buffer is a buffer that is the same width and height as your render target. This buffer records the depth of each pixel that is rendered.

When a pixel is rendered a second time - such as when one object is rendered behind another - the depth buffer will keep either the previous depth value, or replace it with the depth value for the second pixel. Which depth is preserved and which depth is discarded depends on the depth function you select. For example, ifCompareFunction.LessEqual is the current depth function, depth values that are less than or equal to the current value are preserved. Any value greater than the current depth value is discarded. This is called the depth test. The depth test occurs every time a pixel is rendered. When a pixel passes the depth test, its color is written to the render target and its depth is written to the depth buffer.

The depth of a pixel is determined based on the size of the view and projection matrix selected for rendering. A pixel that touches the near plane of the projection has depth 0. A pixel that touches the far plane of the projection has depth 1. As each object in the scene is rendered, normally the pixels that are closest to the camera are kept, as those objects block the view of the objects behind them.

The depth buffer may also contain stencil bits - for this reason it's often called the depth-stencil buffer. The depth format describes the composition of the depth buffer. The depth buffer is always 32 bits, but those bits can be arranged in different ways, similar to how texture formats can vary. A common depth format is Depth32, where all 32 bits are reserved for depth information. Another common format is DepthFormat.Depth24Stencil8, where 24 bits are used for depth calcuation and 8 bits are used by the stencil buffer. DepthFormat.Depth24Stencil8Single is a more unusual format where the 24 bits for the depth buffer are arranged as a floating point value. Use theAutoDepthStencilFormat property on PresentationParameters to set the default depth format.

Use RenderState.DepthBufferEnable to enable or disable depth buffering. Use RenderState.DepthBufferFunction to change the comparison function used for the depth test. The depth buffer can be cleared separately by passingClearOptions.DepthBuffer to the GraphicsDevice.Clear method. Use the DepthStencilBuffer class to create your own depth buffer. How To: Create a Depth Texture provides an example of how to create a custom DepthStencilBuffer.


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值