开发者

GLSL convert gl_FragCoord.z into eye-space z

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

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

varying float depth;

depth = ( gl_ModelViewMatrix * gl_Vertex );

开发者_StackOverflow中文版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.

Thanks in advance. :)


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜