I’m still working on my 3D-shooter, and my actual Problem is, that when I’m too close to an Oject (some Polygons), I can look through it, although it’s actually in front of me.
I’ve got that problem since I changed the “near”-parameter of the gluPerspective(…) function from 0.01f to 1.0f (because I’ve read that this parameter should be >= 1.0f).
What can I do?
What you can do is to move it back again (it does not have to be greater than 1.0). Just make sure you don’t set it to zero or less.
I’ve just read that the z-Buffer doesn’t work fine if the near-value of the gluPerspective-function gets to close to zero. It’s right that it’s not allowed to set it zero or less, but it is recommended to set it at least at 1.0f.
I’ve tried settin the near-value to 0.001f, and the result was very bad. At 0.01f it’s not so very bad, but worse than at 1.0f.
Isn’t there another solution to avoid the clipping-effect?
Prevent the player from moving so close to the object?
It should be an easy addition to what you already have.
Setting zNear less than 1 is ok, as long as you also change zFar so that the zNear to zFar ratio remains reasonable. I usually try to keep the ratio around 1000 for 16 bit depth, and 10000 for 24 bit depth, though that may be a bit conservative.
I’ve just read that the z-Buffer doesn’t work fine if the near-value of the gluPerspective-function gets to close to zero.
Both right and wrong. If the zNear get clsoe to zero, you MIGHT have some strange behaviour in the Z-buffer, but that depends in the zFar value. As DFrey says, the absolute value does not matter, but zNear relative the zFar does. The Z-buffer will only behave strange if the near Z-plane is close to zero AND the far plane is far away (thats relative, as DFrey said, a ratio of about 1-10k is good). Having a zNear or 0.0001 and a zFar of 0.1 is not bad at all.
Ok, thanks for your answers
and for correcting me.
So, did I understand it right, that changing the depth-buffer size (in the PIXELFORMATDESCRIPTOR-setting) from 16 to 24 or more, I’ll get better results with depth-testing?
One the one hand I’d pretty like changing the zNear back to 0.01f, but on the other hand, I need a quite big value for zFar (at least 100.0f), because the scene should be quite big. So what value shall I take for the depth-buffer size?
Increasing the size of the depthuffer helps.
Like DFrey said, you should keep a near:far ratio of about 1000 to 10000, depending on the size of the depthbuffer. So if you have your far plane at 100, then you can safely set the near plane somewhere between 0.1 and 0.01 without worrying too much about precision problems.