Gl_FragCoord.x, y, z - 1 for all pixels, and w - depth

I am using THREE.js with a shader. I am trying to get zbuffer info.

Vertex shader:

// switch on high precision floats #ifdef GL_ES precision highp float; #endif void main() { gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); } 

Fragment Shader:

 #ifdef GL_ES precision highp float; #endif void main() { gl_FragColor = vec4(gl_FragCoord.x, gl_FragCoord.y, gl_FragCoord.z, 1.0); } 

There are objects in the scene with different positions, so the values ​​in zbuffer must change.

It is strange that gl_FragCoord.x , gl_FragCoord.y and gl_FragCoord.z for all 1.0 fragments, and gl_FragCoord.w seems to change for different fragments.

If I use gl_FragCoord.w :

 #ifdef GL_ES precision highp float; #endif void main() { float zbuffer = gl_FragCoord.w * 500.0; gl_FragColor = vec4(zbuffer, zbuffer, zbuffer, 1.0); } 

This seems to be a zbuffer image:

enter image description here

So, why gl_FragCoord.w present depth information, and gl_FragCoord.z always 1.0 for all fragments?

+6
source share
1 answer

gl_FragCoord is in the window. The window is in the pixel coordinates of the window. Thus, almost all of your fragments will have values> 1.0. And since you will almost certainly not end up in a floating point framebuffer, your colors will be clamped in the [0, 1] range.

gl_FragCoord.z is probably not 1.0, but it may be close enough to it, depending on your depth range and how far the objects are from the camera. If you want real depth information, you need to linearize it .

+14
source

All Articles