对数深度缓冲的OpenGL对数、深度、OpenGL

2023-09-08 00:48:47 作者:請叫我時間~

我已经成功地实现在OpenGL对数深度缓冲,主要是礼节性的,从 Outerra 文章(你可以阅读它们这里,的这里和的这里)。不过,我有一些问题,我不知道,如果这些问题是固有的使用对数深度缓冲,或者有一些解决办法,我不能想到的。

I've managed to implement a logarithmic depth buffer in OpenGL, mainly courtesy of articles from Outerra (You can read them here, here, and here). However, I'm having some issues, and I'm not sure if these issues are inherent to using a logarithmic depth buffer or if there's some workaround I can't think of.

刚入手了,我这是怎么计算的对数深入顶点着色器中的:

Just to start off, this is how I calculate logarithmic depth within the vertex shader:

gl_Position = MVP * vec4(inPosition, 1.0);
gl_Position.z = log2(max(ZNEAR, 1.0 + gl_Position.w)) * FCOEF - 1.0;
flogz = 1.0 + gl_Position.w;

这是我怎么收拾深度值在片段着色器:

And this is how I fix depth values in the fragment shader:

gl_FragDepth = log2(flogz) * HALF_FCOEF;

其中, ZNEAR = 0.0001 ZFAR = 1000000.0 FCOEF = 2.0 / LOG2( ZFAR + 1.0) HALF_FCOEF = 0.5 * FCOEF C ,于我而言,是1.0,以简化我的code和减少计算。

Where ZNEAR = 0.0001, ZFAR = 1000000.0, FCOEF = 2.0 / log2(ZFAR + 1.0), and HALF_FCOEF = 0.5 * FCOEF. C, in my case, is 1.0, to simplify my code and reduce calculations.

对于初学者来说,我非常高兴的是precision我得到的水平。在正常深度缓冲(znear = 0.1,zfar = 1000.0),我得到了不少Z-朝作战观看距离的边缘。现在,我的更远znear:zfar,我已经把第二接地平面0.01台下方的第一,我找不到任何Z-战斗,不管多远我调整摄像机的焦距(我有点儿Z-当它只有0.0001(0.1 MM)客场作战,但MEH)。

For starters, I'm extremely pleased with the level of precision I get. With normal depth buffering (znear = 0.1, zfar = 1000.0), I get quite a bit of z-fighting towards the edge of the view distance. Right now, with my MUCH further znear:zfar, I've put a second ground plane 0.01 units below the first, and I cannot find any z-fighting, no matter how far I zoom the camera out (I get a little z-fighting when it's only 0.0001 (0.1 mm) away, but meh).

然而,我确实有一些问题/关注。

I do have some issues/concerns, however.

1),我得到比我做了我的正常深度缓冲更接近平面裁剪,它看起来丑陋。它发生的情况下,从逻辑上讲,它确实不应该。这里有几个我的意思截图:

1) I get more near-plane clipping than I did with my normal depth buffer, and it looks ugly. It happens in cases where, logically, it really shouldn't. Here are a couple of screenshots of what I mean:

裁剪地面的裁剪的网格。

这两种情况下,有些事情我也不会跟一般的深度缓冲的经历,我宁愿不看(尤其是前者)。编辑:问题1正式通过解决过glEnable(GL_DEPTH_CLAMP)

Both of these cases are things that I did not experience with my normal depth buffer, and I'd rather not see (especially the former). Problem 1 is officially solved by using glEnable(GL_DEPTH_CLAMP).

2)为了得到这个工作,我需要写gl_FragDepth。我试着不这样做,但结果是不可接受的。写gl_FragDepth意味着我的显卡不能做早期Z轴优化。这将不可避免地推动了我在墙上,所以我想尽快解决这个问题,我可以。

2) In order to get this to work, I need to write to gl_FragDepth. I tried not doing so, but the results were unacceptable. Writing to gl_FragDepth means that my graphics card can't do early z optimizations. This will inevitably drive me up the wall and so I want to fix it as soon as I can.

3)我需要能够检索存储在所述深度缓冲器中的数值(我已经有一个帧缓冲和纹理为这个),然后将其转换为线性视图空间坐标。我真的不知道从哪里开始的,我做到了以前所涉及的逆投影矩阵的方式,但我真的不能在这里这样做。有什么建议?

3) I need to be able to retrieve the value stored in the depth buffer (I already have a framebuffer and texture for this), and then convert it to a linear view-space co-ordinate. I don't really know where to start with this, the way I did it before involved the inverse projection matrix but I can't really do that here. Any advice?

推荐答案

近平面裁剪独立发生的深度测试,而是通过裁剪反对的 CLI空间体积的。在现代的OpenGL可以使用的深度夹紧的让事情看起来不错了。请参见 http://www.arcsynthesis.org/gltut/Positioning/Tut05% 20Depth%20Clamping.html

Near plane clipping happens independently from depth testing, but by clipping against the cli space volume. In modern OpenGL one can use depth clamping to make things look nice again. See http://www.arcsynthesis.org/gltut/Positioning/Tut05%20Depth%20Clamping.html

 
精彩推荐
图片推荐