Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: glfrustum near and far values

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2014
    Posts
    3

    glfrustum near and far values

    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?

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,476
    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:

    1. The near distance should be less than the distance to the closest object you need to draw
    2. The far distance should be greater than the distance to the farthest object you need to draw
    3. The near distance should be as large as possible
    4. 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:

    1. The near distance should be as low as it needs to be, but no lower
    2. 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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •