hidayat
hidayat

Reputation: 9763

draw the depth value in opengl using shaders

I want to draw the depth buffer in the fragment shader, I do this:

Vertex shader:

varying vec4 position_;

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
position_ = gl_ModelViewProjectionMatrix * gl_Vertex;

Fragment shader:

float depth = ((position_.z / position_.w) + 1.0) * 0.5;

gl_FragColor = vec4(depth, depth, depth, 1.0);

But all I print is white, what am I doing wrong?

Upvotes: 21

Views: 31592

Answers (2)

bobobobo
bobobobo

Reputation: 67346

Indeed, the "depth" value of a fragment can be read from it's z value in clip space (that is, after all matrix transformations). That much is correct.

However, your problem is in the division by w. Division by w is called perspective divide. Yes, it is necessary for perspective projection to work correctly.

However. Division by w in this case "bunches up" all your values (as you have seen), to being very close to 1.0. There is a good reason for this: in a perspective projection, w= (some multiplier) *z. That is, you are dividing the z value (whatever it was computed out to be) by the (some factor of) original z. No wonder you always get values near 1.0. You're almost dividing z by itself.

As a very simple fix for this, try dividing z just by the farPlane, and send that to the fragment shader as depth.

Vertex shader

varying float DEPTH ;

uniform float FARPLANE ;  // send this in as a uniform to the shader

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
DEPTH = gl_Position.z / FARPLANE ; // do not divide by w

Fragment shader:

varying float DEPTH ;
// far things appear white, near things black
gl_Color.rgb=vec3(DEPTH,DEPTH,DEPTH) ;

The result is a not-bad, very linear-looking fade.

enter image description here

Upvotes: 15

Nicol Bolas
Nicol Bolas

Reputation: 474376

In what space do you want to draw the depth? If you want to draw the window-space depth, you can do this:

gl_FragColor = vec4(gl_FragCoord.z);

However, this will not be particularly useful, since most of the numbers will be very close to 1.0. Only extremely close objects will be visible. This is the nature of the distribution of depth values for a depth buffer using a standard perspective projection.

Or, to put it another way, that's why you're getting white.

If you want these values in a linear space, you will need to do something like the following:

float ndcDepth = ndcPos.z =
    (2.0 * gl_FragCoord.z - gl_DepthRange.near - gl_DepthRange.far) /
    (gl_DepthRange.far - gl_DepthRange.near);
float clipDepth = ndcDepth / gl_FragCoord.w;
gl_FragColor = vec4((clipDepth * 0.5) + 0.5); 

Upvotes: 27

Related Questions