Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

which space pixelDepth in? #2

Open
douysu opened this issue Mar 30, 2021 · 6 comments
Open

which space pixelDepth in? #2

douysu opened this issue Mar 30, 2021 · 6 comments

Comments

@douysu
Copy link

douysu commented Mar 30, 2021

hi, thanks for your code. is good.

First question

I was confused the pixelDepth space in file filter-narrow-range.fs.glsl

void main()
{
    float pixelDepth = texture(u_DepthTex, f_TexCoord).r;

    if(pixelDepth > 0.0 || pixelDepth < -1000.0f) {
        outDepth = pixelDepth;
    } else {
        outDepth = (u_DoFilter1D == 1) ? filter1D(pixelDepth) : filter2D(pixelDepth);
    }
}

Which space is it? I think is clip Space by look the file depth-pass.fs.glsl code

    vec4 clipSpacePos = projectionMatrix * vec4(fragPos, 1.0);
    outDepth = fragPos.z;

and you limit the pixelDepth range on [-1000, 0], NDC space depth is [-1, 1].

Second question

In file composition-pass.fs.glsl, zn initial value is clip space? The purpose of this formula is convert eyespace to NDC space?

vec3 uvToEye(vec2 texCoord, float eyeDepth)
{
    float x  = texCoord.x * 2.0 - 1.0;
    float y  = texCoord.y * 2.0 - 1.0;
    float zn = ((farZ + nearZ) / (farZ - nearZ) * eyeDepth + 2 * farZ * nearZ / (farZ - nearZ)) / eyeDepth;

    vec4 clipPos = vec4(x, y, zn, 1.0f);
    vec4 viewPos = invProjectionMatrix * clipPos;
    return viewPos.xyz / viewPos.w;
}

Best.

@ttnghia
Copy link
Owner

ttnghia commented Mar 30, 2021

Hi douysu,

Thank you for your interest and I'm sorry for the confusion---I didn't have much time to document carefully.

  1. The depth value is in camera space, thus it can have any value from [-inf, inf]. Here we clip to keep the value within the view frustum.
  2. The purpose of the uvToEye function is to convert back the tuple {texCoord, eyeDepth} back to 3D space (in eye coordinates). For the z_n value, you can have a look at its formula here (near the end of that page): http://www.songho.ca/opengl/gl_projectionmatrix.html

Hope that I answered your questions. Just let me know if you still have anything unclear.

@douysu
Copy link
Author

douysu commented Mar 30, 2021

Thanks for your answer.
file depth-pass.fs.glsl will produce the depth map and the value is outDepth, because is in camera space. But why also need the line code gl_FragDepth = clipSpacePos.z / clipSpacePos.w?

    out float outDepth;
    vec4 clipSpacePos = projectionMatrix * vec4(fragPos, 1.0);
    outDepth = fragPos.z;

    gl_FragDepth = clipSpacePos.z / clipSpacePos.w;

@ttnghia
Copy link
Owner

ttnghia commented Mar 31, 2021

We need to output the depth value in eye space (outDepth) for further computation (filtering), and depth value in NDC space (gl_FragDepth) for depth test.

@douysu
Copy link
Author

douysu commented Mar 31, 2021

ok. thanks

@douysu
Copy link
Author

douysu commented Apr 4, 2021

hi, ttnghia. In file depth-pass.fs.glsl, clipSpacePos.z / clipSpacePos.w is NDC space and it's range is [-1, 1]. But gl_FragDepth range is [0, 1]. Why is gl_FragDepth = clipSpacePos.z / clipSpacePos.w; rather than gl_FragDepth = (clipSpacePos.z / clipSpacePos.w) * 0.5f + 0.5f;

Best~

@ttnghia
Copy link
Owner

ttnghia commented Apr 8, 2021

I can't remember why I used that. Maybe it is a bug---I took it from somewhere else, and maybe mistakenly used it without fixing it. You can try your new formula for computing gl_FragDepth to see if it works 😄 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants