Depth of field... how's it done?

Miscellaneous Forums/General Discussion/Depth of field... how's it done?

I have noticed a few modern first person games have depth of field, i.e: More distant objects appear more blurry. I can understand how you might perform pixel averaging on an image representing a fixed distance to blur it, but how do you handle the situation when there are various object at different distances? You can't assume you can average any given pixel with the one next to it. Any idea how it's handled, and do DX, GL have any inbuilt functions to help out?

this may help

http://www.blitzbasic.com/Community/posts.php?topic=82090

I dont know if dx or gl have built in functions or not but this is how I do it

You need to use the depth buffer. So whatever you're using either need to be able to let you access the scene's depth buffer ( I think DX10 does this? Not sure about anything else! ) or you need something where you can generate your own depth map. So you at least need to be able to do basic pixel shaders.

With the depth map, you can use the relative depth compared to your "focal depth" to blur things more the further away they are from the focal depth.

Yeah I would say you need to render the scene and keep the depth buffer - in OpenGL you can use it as a depth texture. Then use that texture to draw a full-screen quad while using a shader to modify how much blurring occurs based on distance from the camera/depth values. It's a bit of a fake but it does approximate the concept.

Personally I think that doing depth of field is a bit pointless. The human eye focusses on a particular distance away in a scene, and while you are focussing your attention there, objects nearer/further away will be blurred - BUT you don't see the blurred objects because you only see what you're focussing on. What you focus on is always IN FOCUS. This connection is broken when you try to add depth of field to an flat image because now there is no connection between what object you're looking at and how focussed it is. Thus you now look at the background and see blurry images, whereas in real life if you looked at the background images they would come into focus. So it's kind of dumb, it's just a novel artistic effect at best.

So it's kind of dumb, it's just a novel artistic effect at best.

so is lense flare, ever seen one of those with the naked eye?
hense the word lense in the name :p

its better than having a naff LoD have sky scrapers jump out at you in race games, but having said that you wouldnt normally be looking out the windows for the scenery, but ive always found scenery pop up at you more disjointing than a perpetually blurred PoV, but being short sighted thats kinda like what i would see any time im without my glasses regardless of my focus off attention

In Myst 4 they used depth of field, on 2D images. :)
Most likely they cut the scene into 2d layers and was bluring everything in front of and behind the layer the player had in the middle of the screen. It worked rather nice I must say.

In a 3D scene you could add in some checks to see what is closest to the center of the screen (not what's in front of the player, what is close to a ray shot stright ahead)
Then you sort them according to some score system where an enemy is very important and a barrel is not as important, and that tree is not important at all. When this is done, use the object you have at the top as the focus point. That is at least something I will try to do next time I get some 3d programming motivation.

Okay, I think I'm getting it. So if you draw your screen in depth order from furthest away to closest, and draw a kind of 'averaged bump' over a patch of pixels corresponding to each image point, where the dimensions of the bump correspond to the distance of the pixel from the viewer, and average it all together... bingo.

It amazes me that there was a 3D game on the PC before the days of 3D gfx cards that managed this feat. The name escapes me.

I think DOF adds massively to graphical realism. I understand what you're saying, IH, but generally you are focussing on the player and their locale, which is of course in focus. It works for films, and it works in games, IMO.

I guess you do have a point if you are mostly focussing on the player, who always in focus.

I think pre-rendered static backgrounds was the usual workaround for this.

Thus you now look at the background and see blurry images, whereas in real life if you looked at the background images they would come into focus. So it's kind of dumb, it's just a novel artistic effect at best.

The effect is more about fotography, cameras and lens than your eye. It's dumb when done improperly.
You can't take cinematography out of the equation just because you are talking about 3D: it's all about reproduction of a reality and the eye is only one type of lens out of the available ones.
In that respect you should throw out of the window all the movies as well.

I still think it's only relevant if the focal point is the same as where the player is looking, otherwise it doesn't make sense, and if the player should never see the out of focus areas at all, then why bother. It's really just a reproduction of a photographic effect trying to cover up another problem such as geometry suddenly appearing.

You're thinking is very abstract. Try looking at some movies to see if the use of lenses and photography enhances the experience or not, or what the scene would look like with different choices. From this standpoint direction and photography would be totally useless as well. Deps made a good point with Myst 4, since as you were asking the focal point is actually where the player is looking, but even in a different case your point is feeble to the point that I would wonder why directors bother otherwise. Not necessarily the author wants to replicate the human eye but more to convey an emotion.

After all we could extremize this line of thinking as to go back to filled polygons, as everything else is beyond need in order to understand what you see on screen.

I for one don't like improper use of effects, but you can't condemn the need to have advanced real life effects, expecially because even though we are faking it in real time, focal point is the base of photography (and since lenses are already mathematically replicated with lifelike detail in rendered graphics we can expect to get there in a few years). Anyway, lens-flares as well would be great if recreated realistically with lenses parameters instead of being emitters attached to a body... the possibilities are far different from the effect you saw in the games of the 90's.