translucency with alpha-blend & depth test

I’ve got a scene with colour and depth values coming in from an external renderer. I’m using DrawPixels to get them into the frame buffer and I store the result in a buffer region for later redrawing as I move some models through the scene.
So far that’s all good.

Now, I’d like to add an effect where everything in my background above a certain target depth is translucent. Models should be visible through the translucent stuff, but not through each other or the background where it’s below the target depth. Also, models above the translucent stuff should appear solid.

I’m having trouble finding an effective solution that satisfies all the criteria.

The big problem is combining alpha-blending and depth test. I tried setting alpha values based on the depths (alpha=0 below target, alpha=1 above)and drawing my models with glBlendFunc(GL_ONE, GL_DST_ALPHA);

But that doesn’t handle the case where the models overlap each other or if they appear completely in front of the translucent areas. It needs to take into account the depth of the model as well as the background…

Any suggestions? If there’s sample code that would be ideal.

It’s not easy to visualize this from the description, but multiple things come to mind:

  • Multi-pass drawing algorithm using stencil buffers where you can exploit the depth comparison result of the glStencilOp.
  • GL_ALPHA_TEST for the hard cut.
  • Or glClipPlane splitting the world into two half-spaces, once drawing behind, once in front.
  • GL_DEPTH_TEST enabled but glDepthMask(0) for the transparent parts.
  • Draw transparent back faces, then transparent front faces to get a cheap per object back to front sorting.
  • Sort all transparent polygons back to front.

Thanks, Relic, but I just realized that the effect I was trying (badly) to describe wouldn’t be right anyway.

The big limitation is that what I’ve got from the external renderer is just a snapshot of a color + depth buffer with no information about anything that’s obscured. 'Til now that’s been ok, because my models get cut off correctly by the depth test, so they look fine.

However, if I’ve got a model behind something translucent, I have no information about anything deeper down, so I don’t know if there’s something solid between the transparent top layer and the position of the model.

Now I think I can do it with two passes in the external renderer. That way my models will get obscured correctly by the solid stuff underneath, and I can draw the top layer with alpha blend and depth test.

Thanks for letting me bounce this off you! I guess I just needed to write it all down.