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

查看:415
本文介绍了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天全站免登陆