GLSL convert gl_FragCoord.z into eye-space z

点点圈 提交于 2019-12-11 08:14:02

问题


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 );

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. :)


回答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


来源:https://stackoverflow.com/questions/4918599/glsl-convert-gl-fragcoord-z-into-eye-space-z

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!