Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting near plane in OpenGL

Tags:

opengl

i have implemented first person camera in OpenGL, but when i get closer to an object it starts to disappear, so i want to set near plane close to zero so i could get closer to the objects. So if anybody can tell me what is the best way to do that. Thank you.

like image 374
Skippy Avatar asked Feb 26 '23 12:02

Skippy


1 Answers

Other responses focus on using glut. Glut is not recommended for professional, or even modern, development, and the OP says nothing about using glut - just OpenGL. So, I'll chime in:

A little background

zNear, the distance from the origin to the near clip plane, is one of the parameters used to build the projection matrix:

Projection Matrix

Projection Matrix

Where

SYMBOL  MEANING                   TYPICAL VALUE
------  --------------           -------------
fov     vertical field of view   45 – 90 degrees
aspect  Aspect ratio             around 1.8 (frame buffer Width / Height)
znear   near clip plane          +1
zfar    far clip plane.          10

SYMBOL      MEANING                          FORMULA
----------  -------------------------------  --------------
halfHeight  half of frustum height at znear  znear∗tan(fov/2)
halfWidth   half of frustum width at znear   halfHeight×aspect
depth       depth of view frustum            zfar−znear

(More nicely-formatted version on http://davidlively.com/programming/graphics/opengl-matrices/perspective-projection/)

When the perspective divide takes place - between the vertex and fragment shaders - the vertices are converted to normalized device coordinates (NDC), in "clip space." In this space, anything that fits in a 2x2x1 (x,y,z) box will be rendered. Any fragments that don't fit in a box with corners (-1, -1, 0) - (+1, +1, +1) will be clipped.

Practical Upshot Being

It doesn't matter what your zNear and zFar values are, as long as they offer sufficient resolution & precision, and zFar > zNear > 0.

Your collision detection & response system is responsible for keeping the camera from getting too close to the geometry. "Too close" is a function of your zNear and geometry bounds. Even if you have a zNear of 1E-9, geometry will get clipped when it gets too close to the clip space origin.

So: fix your collision detection and stop worrying about your zNear.

like image 136
3Dave Avatar answered Mar 08 '23 13:03

3Dave