PDA

View Full Version : glfrustum near and far values

rmousa
11-01-2014, 10:21 AM
Hello,

I'm trying to use glfrustum function to get projection matrix, but i don't know what should be the near and far values?

GClements
11-01-2014, 06:28 PM
The view frustum is a frustum: a "pyramid" with the top removed, so it has 6 four-sided faces (essentially a deformed cube). The near plane determines the location of the nearest face (i.e. the one created by removing the top of the pyramid), the far plane determines the location of the farthest face (i.e. the base of the pyramid).

Regarding what the near and far distances should be:

The near distance should be less than the distance to the closest object you need to draw
The far distance should be greater than the distance to the farthest object you need to draw
The near distance should be as large as possible
The ratio of the far distance to the near distance should be as small as possible.

If you don't satisfy points 1 and/or 2, parts of the scene will be missing. If you don't satisfy 3 and/or 4, the depth resolution will be too coarse and parts of objects may "poke through" objects which are just in front of them.

As a rule of thumb, half of the depth range will be used for depth (-Z) values between the near distance and twice the near distance, with the other half used for depth values beyond that. Similarly, three quarters of the depth range will be used for depth values between the near distance and four times the near distance, with the other quarter used for depth values beyond that. More generally, (N-1)/N of the depth range will be used for depth values between the near distance and N times the near distance, with the other 1/N used for depth values beyond that.

So, making the near distance too small will result in most of the depth range being used for depth values close to the viewpoint, with the remainder of the scene receiving only a small fraction of the depth range.

In other words:

The near distance should be as low as it needs to be, but no lower
The far distance should be as high as it needs to be, but no higher

If you have trouble satisfying all of the constraints simultaneously, you may need to render the scene in multiple passes, with "scenery" drawn first then very near objects drawn afterwards. Each pass can then have the near and far distances set appropriately for that pass: the scenery can be drawn with a large near distance, close objects can be drawn with a small far distance. The depth buffer needs to be cleared between passes.