GLSL gl_FragCoord.z Calculation and Setting gl_FragDepth

前端 未结 2 1548
悲&欢浪女
悲&欢浪女 2020-12-07 22:58

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 calc

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-08 00:01

    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;
    

提交回复
热议问题