片段着色器眼睛空间未缩放深度坐标
我正在尝试在 GLSL 片段着色器中使用到场景中对象的未缩放(距前裁剪平面的真实距离)距离。 gl_FragCoord.z 值比我预期的要小。在我的顶点着色器中,我只使用 ftransform() 来设置 gl_Position。当我期望值在 15 到 20 之间时,我看到的值在 2 到 3 之间。
如何获得真实的眼空间深度? 谢谢!
I'm trying to use the unscaled (true distance from the front clipping plane) distance to objects in my scene in a GLSL fragment shader. The gl_FragCoord.z value is smaller than I expect. In my vertex shader, I just use ftransform() to set gl_Position. I'm seeing values between 2 and 3 when I expect them to be between 15 and 20.
How can I get the real eye-space depth?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您想要的任何内容作为顶点着色器的变量传递下来。
片段着色器中可用的 Z 值已根据您的 z-near/z-far(来自投影矩阵)和 DepthRange 经过标准化。所以这不是你所追求的直接目标。从技术上讲,您可以尝试通过恢复在顶点着色器之后发生的 Z 上的各种 OpenGL 操作来重建它,但这可能比仅仅传递您想要的内容更麻烦(从恢复投影矩阵是非线性的事实开始) , 确切地。
附带说明一下,您使用
gl_ModelViewMatrix * gl_Vertex
计算的 Z 是从视点来看的 Z,而不是近 Z 平面。Pass whatever you want down as a varying from the vertex shader.
the Z value available in the fragment shader has gone through normalization based on your z-near/z-far (from the projection matrix), and DepthRange. So it is not directly what you're after. Technically, you could try to reconstruct it by reverting the various OpenGL operations on Z that happen after the vertex shader, but it's probably more trouble (starting with the fact that reverting the projection matrix is non-linear) than just passing down what you want, exactly.
As a side note, the Z you would compute with
gl_ModelViewMatrix * gl_Vertex
is the Z from the point of view, not the near-Z plane.