GLSL gl_FragCoord.z 计算和设置 gl_FragDepth [英] GLSL gl_FragCoord.z Calculation and Setting gl_FragDepth

查看:31
本文介绍了GLSL gl_FragCoord.z 计算和设置 gl_FragDepth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个冒名顶替者(真正的几何体是一个立方体,可能被剪掉了,冒名顶替者的几何体是一个 Menger 海绵),我需要计算它的深度.

So, I've got an imposter (the real geometry is a cube, possibly clipped, and the imposter geometry is a Menger sponge) and I need to calculate its depth.

我可以很容易地计算在世界空间中的偏移量.不幸的是,我花了好几个小时都没能用它来扰乱深度.

I can calculate the amount to offset in world space fairly easily. Unfortunately, I've spent hours failing to perturb the depth with it.

我唯一能得到的正确结果是我去的时候:

The only correct results I can get are when I go:

gl_FragDepth = gl_FragCoord.z

基本上,我需要知道 gl_FragCoord.z 是如何计算的,以便我可以:

Basically, I need to know how gl_FragCoord.z is calculated so that I can:

  • 进行从 gl_FragCoord.z 到眼空间的逆变换
  • 添加深度扰动
  • 将此扰动的深度转换回与原始 gl_FragCoord.z 相同的空间.

如果这似乎是一个重复的问题,我深表歉意;这里还有许多其他帖子解决了类似的问题.但是,在实施了所有这些之后,没有一个可以正常工作.在这一点上,我不是试图选择一个来获得帮助,而是要求提供完整的代码.它应该只是几行.

I apologize if this seems like a duplicate question; there's a number of other posts here that address similar things. However, after implementing all of them, none work correctly. Rather than trying to pick one to get help with, at this point, I'm asking for complete code that does it. It should just be a few lines.

推荐答案

供以后参考,关键代码为:

For future reference, the key code is:

float far=gl_DepthRange.far; float near=gl_DepthRange.near;

vec4 eye_space_pos = gl_ModelViewMatrix * /*something*/
vec4 clip_space_pos = gl_ProjectionMatrix * eye_space_pos;

float ndc_depth = clip_space_pos.z / clip_space_pos.w;

float depth = (((far-near) * ndc_depth) + near + far) / 2.0;
gl_FragDepth = depth;

这篇关于GLSL gl_FragCoord.z 计算和设置 gl_FragDepth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆