GLSL将gl_FragCoord.z转换为眼睛空间z [英] GLSL convert gl_FragCoord.z into eye-space z

查看:99
本文介绍了GLSL将gl_FragCoord.z转换为眼睛空间z的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的问题,我讨厌在网上搜索正确的方程式.

This is a simple question, and I'm sick of searching the web for the right equation.

主要问题是每个人都建议做这样的VS:

The main problem is that everyone suggests doing something like this VS:

varying float depth;

depth = ( gl_ModelViewMatrix * gl_Vertex );

但是我不能,因为深度存储在纹理中.

But I can't, because the depth is stored in a texture.

因此,无论如何,我现在是深度值,以及用于从眼睛空间坐标创建深度值的投影矩阵.

So anyway, I now the depth value, and the Projection matrix used to create it from the eye-space coords.

如果您不太了解,请告诉我,我会尝试用更好的措词来表达.

If you don't quite understand, tell me and I'll try to word it better.

先谢谢了. :)

推荐答案

如果从纹理中提取深度值-则其范围为[0,1].首先,您需要将其缩放到[-1,1]范围,然后应用反投影来获得模型视图的深度:

If you extract a depth value from the texture - it's in range [0,1]. First you'll need to scale it into [-1,1] range and then apply the inverse projection to get the model-view depth:

vec2 xy = vec2(x_coord,y_coord); //in [0,1] range
vec4 v_screen = vec4(xy, texture(samplerDepth,xy), 1.0 );
vec4 v_view = inverse(gl_ProjectionMatrix) * (2.0*(read_depth-vec3(0.5)));
float view_depth = v_view.z / v_view.w; //transfer from homogeneous coordinates

这篇关于GLSL将gl_FragCoord.z转换为眼睛空间z的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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