Depth Buffer

Depth Buffer

 

ps. 抱歉,第二篇忘记是从哪转载的了

1、Learning to Love your Z-buffer

Introduction

One of the more common OpenGL programming problems that I see concerns the poor precision of the Z buffer.

Many of the early 3D adaptors for the PC have a 16 bit Z buffer, some others have 24 bits - and the very best have 32 bits. If you are lucky enough to have a 32 bit Z buffer, then Z-precision may not seem to be an issue for you. However, if you expect your program to be portable, you'd better give it some thought.

The precision of Z matters because the Z buffer determines which objects are hidden behind which others - and if you don't 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.

This is commonly called 'flimmering' or 'Z-fighting' and it's very disturbing to the user.

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:
(PS. 在进行pixel写操作时,即写入AGP Register时,数值会进行scale和bias,所以在depthTexture中取值范围为[0, 1])

  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.

How Bad Is It?

Talking about how large the error is for absolute values of zNear (in feet, meters, lightyears or angstroms) is meaningless. Given the math used to convert Z into the number in the Z buffer, it's also fairly meaningless to talk about absolute values in "OpenGL units" either.

Instead we have to think about the ratio of distances to objects in your scene to the value of zNear.

This equation applies:

  delta = z * z / ( zNear * (1<<N) - z )

Where:

N = number of bits of Z precision
zNear = distance from eye to near clip plane
z = distance from the eye to the object
delta = the smallest resolvable Z separation
at this range.

This equation is approximate - it only applies if zNear is much smaller than zFar - which is true for nearly all applications.

For another way to think about this, suppose we choose to think about the range at which there is a n% error in Z due to the precision of the Z buffer. For ease of discussion, I'll call the ratio of that range to the value of zNear 'Zn%'.

Hence, Z5% is the "range at which there is a 5% error in Z" divided by zNear.

For a 16 bit Z buffer, the value of Z5% is about 3500. It varies *slightly* depending on the value of zFar, and for very small values of zFar, it does get a little bigger - but for practical applications, 3500 is a good rule-of-thumb.

What this means in practice, is that if you place zNear at 1 meter (in whatever units your database uses), then when an object is at 3,500 meters, there will be a 5% error in it's Z value.

  For 16 bit Z:

Z10% = ~8000 *
Z5% = 3500
Z1% = 666
Z0.1% = 66
Z0.01% = 6

  • * NB: The larger the range, the more the zFar distance starts to affect the precision - but for most practical applications, it doesn't make enough of a difference to matter.

The table tell us that value for Z1% is 666 and Z10% is ~8000. So with our 1 meter zNear, we can expect better than 1% precision below 666 meters, better than 5% precision below 3500 meters and better than 10% at under 8000 meters.

Now, if your zNear is at 10cm, you'll see a 5% error at just 350 meters (3500*0.1m)- and out at 3.5km meters, the error will be around 33% - over a kilometer in error. An airplane flying behind a huge mountain could suddenly pop into view in front of it when we are are only a couple of miles away!

(结论:zNear越小,发生zFighting的几率就越 大)

You can see that the placement of zNear is really critical in a 16 bit Z system.

For a 24 bit Z buffer, the ratios are 256 times larger so Z1% is ~170,000, and Z5% is about a million.

For a 32 bit Z buffer, even Z1% is about 45 million and we are unlikely to care about Z5% and Z10% metrics!

 

2、gl_FragDepth


gl_FragCoord 和gl_FragDepth分别是片元着色器的输入和输出变量。(PS. gl_FragDepth基本上可假定为设备空间的z,即Zc)

gl_FragCoord是个vec4,四个分量分别对应x, y, z和1/w。其中,x和y是当前片元的窗口相对坐标,不过它们不是整数,小数部分恒为0.5。x - 0.5和y - 0.5分别位于[0, windowWidth - 1]和[0, windowHeight - 1]内。windowWidth和windowHeight都以像素为单位,亦即用glViewPort指定的宽高。w即为乘过了投影矩阵之后点坐标的 w,用于perspective divide的那个值。gl_FragCoord.z / gl_FragCoord.w可以得到当前片元和camera之间的距离。参见Fog in GLSL page 4

gl_FragCoord.z是固定管线计算出的当前片元的深度。它已经考虑了多边形偏移,并经过了投影变换。它位于[0.0, 1.0] 之间。如果用gl_FragColor = vec4(vec3(gl_FragCoord.z), 1.0)将其可视化,多半会看到一片白。这是由于变换的非线性,大多数点的深度都非常接近于1。用gl_FragColor = vec4(vec3(pow(gl_FragColor.z, exp)), 1.0)并将exp取为合适的值,就能看到从黑到白的深度变化了。距离观察者近的颜色深,接近0.0;距离观察者远的颜色浅,接近1.0;这说明一直以来 的右手坐标系在投影变换后变成了左手坐标系。关于深度的变换和精确性参见OpenGL FAQ - 12 The Depth Buffer

根据GLSLangSpec.Full.1.30.08(p61),gl_FragCoord.z是固定功能计算所得的结果。如果片元着色器不写 gl_FragDepth,那么这个值将用在后续处理中。OpenGL Shading Language 提到(p104),即使将gl_FragCoord.z 赋值给gl_FragDepth也不能保证产生和固定功能完全相同的值 。 但是,可以保证相对正确。加之片元着色器一旦写入gl_FragDepth,就必须保证在每个分支都有写入。因此,如果一个着色器需要在某些条件下自己计 算深度,其它条件下的正确做法就是gl_FragDepth = gl_FragCoord.z。

有种自行计算gl_FragDepth的 方法(近似值):参数近裁面n,远裁面f,相机坐标系下物体与相机的距离eyeDepth(大于0

gl_FragDepth = pow ( f / ( f - n ) - f * n / (( f - n ) * eyeDepth), 15.0 ) ; ( 里面的值即 1 中的 a + b / z ,两式中的15是估计值)

 

3、从DepthTexture中反推世界坐标


// 通过DepthTexture的深度反推世界坐标
float getEyeDepth(vec2 uv) {

    float z = texture2D(m_DepthTexture, uv).x;
    float depth = z ;
    vec2 wh = vec2(g_ViewPort.z - g_ViewPort.x, g_ViewPort.w - g_ViewPort.y);
    vec4 screenPos = vec4(gl_FragCoord.x/wh.x, gl_FragCoord.y/wh.y, depth, 1.0) * 2.0 - 1.0;
    vec4 viewPosition = g_ProjectionMatrixInverse * screenPos;

    z = -(viewPosition.z / viewPosition.w);  // 得到相机与顶点的距离,正 值>0
    // 得到相机坐标
    viewPosition = viewPosition /  viewPosition.w;
    // 得到世界坐标
    vec4 worldCoord = g_ViewMatrixInverse * viewPosition;
    return worldCoord;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值