开发者

Setting near plane in 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 ob开发者_StackOverflow社区jects. So if anybody can tell me what is the best way to do that. Thank you.


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

Setting near plane in OpenGL

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.


So if anybody can tell me

gluPerspective.


The near plane is set when you set the projection matrix, either with glFrustum or glOrtho. One of the parameters is the near plane. Notice that the distance to the near plane must be > 0.


You don't have many options.

  • Cast some rays from the camera (for instance in the 4 corners and in the center), take the shortest minus epsilon, clamped to a decent value > 0. 0.1f will do.
  • Simply forbid the camera to be here in the first place ! For this you can link it to a sphere in your physics engine, check whether it intersects something, and it it does, move it (how and where to move it is your problem since it's for a large part gameplay. Think of Super Mario Galaxy)

Never set a too little nearPlane. You will run in precision issues with your z-buffer. The farPlane can be quite large though. Values like (0.1, 1000) can be all right depending on your application

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜