2

As many newbies to OpenGL and Graphics Programming, I'm having some problems to set the zNear and zFar values for gluPerspective(). Given that I know how big my scene is and taking into account the possible Depth Buffer problems I may have if I set the near plane too close. How can I set these parameters correctly?

In few words, if I know the radius of the scene's bounding sphere how can I set my projection the right way?

2 Answers 2

1

You don't need to worry much about losing depth buffer precision so long as you keep objects a reasonable distance from the camera.

The bits lost are the base two logarithm of the ratio zFar/zNear. In other words, for each bit of depth buffer precision you sacrifice, you can either double zFar or halve zNear. Logarithmic growth is something to be thankful for. Additionally, if you double both zNear and zFar, you sacrifice no additional bits of depth buffer precision.

zNear=1 and zFar=1024 costs you only 9 bits of depth buffer precision. That's a very comfortable space.

See this FAQ for details:

While the blue book description is good at pointing out the relationship, it's somewhat inaccurate. As the ratio (zFar/zNear) increases, less precision is available near the back of the depth buffer and more precision is available close to the front of the depth buffer. So primitives are more likely to interact in Z if they are further from the viewer.

Field of view angle can be in the neighborhood of 90-70.

0

maybe it is simple:

  1. find centre (in Z axis) of the bounding sphere
  2. near = centre.z - radius
  3. far = centre.z + radius

Of course to find that centre you would have to iterate over whole visible objects or use some scene hierarchy.

2
  • Then what would it be the field of view angle? Dec 28, 2011 at 15:46
  • since we are dealing with sphere then the field of view can be set to 90 degrees. Or you can use "atan2" function to calculate exact value for non spherical bounding boxes.
    – fen
    Dec 29, 2011 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.