4
\$\begingroup\$

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?

\$\endgroup\$
1
  • \$\begingroup\$ Do you want to draw depth buffer of rendered scene or draw scene objects with coloring based on pixel depth? \$\endgroup\$
    – kravemir
    Commented Jul 3, 2012 at 9:01

3 Answers 3

4
\$\begingroup\$

position_.z shouldn't be larger than 1 or lower than 0. You should try to use linear depth for the display, which after applying the projection matrix is (usually) stored in the w coordinate instead. Take that, divide by an appropriate number (depending on the scale of your game) to bring the numbers between 0 and 1 for your interesting depth and watch.

gl_fragcoord is not relevant to this discussion; gl_fragdepth is an output variable to modify the post-projection depth in the pixel shader. You're not trying to do that, so don't use that either.

\$\endgroup\$
2
\$\begingroup\$

A cheesy way might be to set up some linear fog. Make the fog colour black, draw all your geometry white, and voila.

\$\endgroup\$
1
  • \$\begingroup\$ Nice idea for a fixed-function pipeline, never thought about that. \$\endgroup\$ Commented Aug 22, 2011 at 12:15
-1
\$\begingroup\$

Maybe you can try one of these two variables. gl_fragcoord or gl_fragdepth

I didn't read those manuals, but I think, it could be helpful.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .