Tuesday, January 24, 2006

It's depth of field cap'n, but not as we know it

Recently graphics cards have got enough grunt to do nice special effects in real time, like depth of field - this simulates a camera having a focal point; stuff drawn at different depths from the focal point can be blurred out a bit to give us what looks like a camera being in-focus and out of focus.

So far, so good.

This effect is pretty expensive though and if you are doing a 2.5d scene or game (full of billboarded polygons), there is a neat little trick that is super quick and looks ace. This is for DirectX by the way, i'm sure you can do a similar thing in OpenGL but i'm not particularly familiar with the API.

If we have a texture of size 256 x 256 say, then we can get 9 mip map levels. At its simplest level, one can just set D3DSAMP_MIPMAPLODBIAS to be bigger than zero according to the distance of the billboard from the focal point, and voila!

Incidentally if you do do this then this particualar sampler state expects a float value so you have to do *((DWORD*)(&value_f)) to get passed properly, as the argument type is a DWORD.

So, we have our blur. The box filter that D3D uses is a bit bobbins though, so you can make this a bit better by setting D3DSAMP_MIPFILTER to be something other than D3DTEXF_LINEAR (maybe D3DTEXF_GAUSSIAN).

If the objects are going to stay a fixed distance from the camera then we can probably fiddle with these and get nice enough results. Even so, we can make it look better than this.

Here we get a bit shady and pretend like we don't care about how much memory we are going to use. Let's say that instead of having 1 texture we have a whole bunch, maybe 20, maybe 50. Then, offline, we do a gaussian blur on these textures, each one being a little blurrier than the last. The result is a nice list of textures which get blurrier as we got, and we can just set the texture for the object depending on its distance from the focal point.

Once you have generated the textures (which takes a while) this is nice and fast, and looks ace. Hurrah!

0 Comments:

Post a Comment

<< Home