GLSL gl_FragCoord.z Calculation and Setting gl_FragDepth

前端 未结 2 1540
悲&欢浪女
悲&欢浪女 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-07 23:44

    For another future reference, this is the same formula as given by imallett, which was working for me in an OpenGL 4.0 application:

    vec4 v_clip_coord = modelview_projection * vec4(v_position, 1.0);
    float f_ndc_depth = v_clip_coord.z / v_clip_coord.w;
    gl_FragDepth = (1.0 - 0.0) * 0.5 * f_ndc_depth + (1.0 + 0.0) * 0.5;
    

    Here, modelview_projection is 4x4 modelview-projection matrix and v_position is object-space position of the pixel being rendered (in my case calculated by a raymarcher).

    The equation comes from the window coordinates section of this manual. Note that in my code, near is 0.0 and far is 1.0, which are the default values of gl_DepthRange. Note that gl_DepthRange is not the same thing as the near/far distance in the formula for perspective projection matrix! The only trick is using the 0.0 and 1.0 (or gl_DepthRange in case you actually need to change it), I've been struggling for an hour with the other depth range - but that is already "baked" in my (perspective) projection matrix.

    Note that this way, the equation really contains just a single multiply by a constant ((far - near) / 2) and a single addition of another constant ((far + near) / 2). Compare that to multiply, add and divide (possibly converted to a multiply by an optimizing compiler) that is required in the code of imallett.

    0 讨论(0)
  • 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;
    
    0 讨论(0)
提交回复
热议问题