Mon, Jun 29, 2009

Papervision ZBuffer and DOF

One of the features that most modern graphics accelerators support is a ZBuffer (also known as a depth buffer). This can be thought of as an image that provides per pixel depth information of a rendered scene. For every pixel drawn by the renderer the corresponding pixel in the ZBuffer is filled with the Z depth of that pixel.

The two largest benefits of a ZBuffer are that it prevents overdraw and allows for triangle intersection at the pixel level. However the ZBuffer can also be used for a variety of post processing effects, a common one is Depth of Field.

By using a distance based shader (one where the brightness of an object is determined by its distance from a light source) a basic emulation of a ZBuffer can be achieved in Papervision.

In the above experiment a scaled down version of the scene is rendered to a seperate viewport using Andy Zupkos DepthShader to emulate a ZBuffer. A blurred and slightly darkened version of the scene is then layered on top using the fake ZBuffer as its alpha channel.

The fake ZBuffer is displayed in the top left corner. Moving the mouse will reposition the focal point allowing you to blur with varying levels far objects, close objects or even just one side.

This experiment is a proof of concept and still has a large amount of room for optimization. Primarily the DepthShader could be greatly improved specific to this use case.