Learning to Love your Z-buffer

https://www.sjbaker.org/steve/omniv/love_your_z_buffer.html
深度缓冲有16位、24位、还有32位。if u are luck enough to have a 32 bit z buffer, then z-precision may not seem to be an issue for u. however, if u expect your program to be portable, u would better give it some tought.

the precision of z matters because the z buffer determines which objects are hidden behind which others.
if u do not have enough precision to resolve the distance between two nearby objects, they will randomly show through each other - sometimes in large zig-zags, sometimes in stripes.

The Near Clip Plane
The near clip plane (zNear for short) is typically set using gluPerspective() or glFrustum() - although it’s also possible to set it by setting the GL_PROJECTION matrix directly.
Some graphics programmers call zNear ‘hither’ 这边 and zFar ‘yonder’ 那边.
Beginners frequently place zNear at a very short distance because they don’t want polygons close to the eye to be clipped against the near plane - and because it isn’t obvious why you’d want to do anything else.
Positioning of zNear too close to the eye is the cause of flimmering (in almost every case) - and the remainder of this document explains why that is.

The Resolution of Z.
What people often fail to realise is that in nearly all machines, the Z buffer is non-linear. The actual number stored in the Z buffer memory is related to the Z coordinate of the object in this manner:
z_buffer_value = (1<<N) * ( a + b / z )
Where:

 N = number of bits of Z precision
 a = zFar / ( zFar - zNear )
 b = zFar * zNear / ( zNear - zFar )
 z = distance from the eye to the object

…and z_buffer_value is an integer.

This means that Z (and hence the precision of Z) is proportional to the reciprocal of the z_buffer_value - and hence there is a LOT of precision close to the eye and very little precision off in the distance.
This reciprocal behaviour is somewhat useful because you need objects that are close to the eye to be rendered in great detail - and you need better Z precision for detailed objects.

However, an undesirable consequence of this is that many of your Z buffer’s bits are wasted - storing insanely fine detail close to the near clip plane. If you pull the near clip closer to your eye, then ever more bits are dedicated to the task of rendering things that are that close to you, at considerable cost to the precision a bit further out.

It follows that in most cases, flimmering can be greatly reduced - or even eliminated by moving the near clip plane further from your eye.
这句话的意思就是,如果near太近的话,那么就会浪费精度在近距离处。

Conclusion
Always put zNear as far from the eye as you can tolerate.

发布了646 篇原创文章 · 获赞 107 · 访问量 36万+

猜你喜欢

转载自blog.csdn.net/wodownload2/article/details/104840685
今日推荐