Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
735 views
in Technique[技术] by (71.8m points)

geometry - Incorrect gl_FragDepth in raytraced sphere

I am raytracing a sphere in GLSL. The geometry is correct but the depth calculation is wrong. In the below image, the larger sphere on the right should be in front of the others, and the RGB axes should be obscured by the sphere at the origin.

enter image description here

Fragment shader:

#version 320 es
precision mediump float;

in vec3  f_position_view; // pixel coordinate in view space
in vec3  f_centre_view;   // centre of sphere in view space
in vec3  f_centre_world;  // centre of sphere in world space
in float f_radius;

layout (std140) uniform F_GLOBALS
{
    mat4  clip_from_view;  // clip_space_position  = clip_from_view  * view_space_position
    mat4  world_from_view; // world_space_position = world_from_view * view_space_position
}
f_globals;

out vec4 out_colour;

bool raytrace_sphere (vec3, vec3, float, out float);

float fragment_depth_from_clip (vec4 position_clip) {

    float ndc_depth = position_clip.z / position_clip.w;

    return (gl_DepthRange.diff * ndc_depth +
        gl_DepthRange.near + gl_DepthRange.far) / 2.0;
}

void main ()
{
    float t;

    if (false == raytrace_sphere (
        -f_centre_view,
        f_position_view,
        f_radius,
        t /* output parameter */))
    {
        discard;
    }

    vec3 p_view = t * f_position_view;

    vec3 position_world
        = (f_globals.world_from_view * vec4(p_view, 1.0)).xyz;

    gl_FragDepth = fragment_depth_from_clip (
        f_globals.clip_from_view * vec4(p_view, 1.0));

What's wrong with the depth calculation?

question from:https://stackoverflow.com/questions/65884386/incorrect-gl-fragdepth-in-raytraced-sphere

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...