Advertisement Jump to content
Sign in to follow this  

depth peeling - calculating depth in shader

This topic is 3335 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Over the weekend I tried to implement simple depth peeling for transparency later to be used for baking ambient occlusion. I got the basic implementation working but theres something thats really been bothering me, firstly here is how I calculate depth:
//Vertex Shader
vec4 position = gl_ModelViewMatrix * gl_Vertex;

//Pixel Shader
float depth = position.z / position.w;
depth = (depth * 0.5) + 0.5;

Im storing the depth in 32F fbo in the alpha channel along with the colour info for the depth layer. Now i've used this depth calculation before for shadow maps, depth of field and other things with no issues, but I get problems with depth peeling. When the camera is looking down the z-axis so -z is in front to discard pixels I have to discard the correct pixels if current_depth >= previous_layer_depth. When the camera is rotated 180 to look up the z-axis I get incorrect results, however changing the depth comparison to discard if current_depth <= previous_layer_depth yields correct results (which makes more sense because I should be discarding pixels nearer as the peeling starts at closest depth and works outward). This is really confusing me because my depth should be 0 - 1 in viewspace with 0 at near and 1 at far right? so why the hell am I getting view dependant results. anyone who could shed some light on this would be much appreciated.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!