How to read the result of depth render target?

Hello everyone,
In order to implement the mid-dist shadow map with OpenGL & CgFx. I render the closest the depth to the depth target in first pass, then render the same scene again and read the result from 1st pass to discard the closest one in second pass. In my implementation, I simply attach a depth texture to FBO without any RBO attached, and it works fine in traditional shadow-map mechanism. However, when I try to retrieve the result of depth texture by tex2D(sampler, float2), I always get the value “1” in alpha channel(the others are “0”).

I’ve found a related thread, yet I still can’t figure out how to solve this problem…
Has anyone faced similar problem before? Please help me out. Thanks in advance! :slight_smile:

Ryan47

In order to implement the mid-dist shadow map with OpenGL & CgFx.

What’s a “min-dist” shadow map?

Did you set the GL_TEXTURE_COMPARE_MODE to GL_NONE before this pass? This state must be consistent with the sampler and texturing function you are using in the shader. If the sampler2D&texture2D is used, the mode must be set to NONE. If the sampler2DShadow&texture2DShadow is used, it must be set to COMPARE_R_TO_TEXTURE.

When you render the ordinary shadowmap using distances of the closest front facing (as towards the light) polygons, you will have shadow errors on the polygons closest to the light (some fragments will evaluate to shadowed and some to lit).

The standard way to handle this is by adding offset to the depths however that might cause the light to appear where shadow should be (e.g. the shadow starts in some distance from the foot of the character).

The middle distance based techniques attempt to avoid this by selecting depth that is between depth of the closest polygon (as in ordinary shadowmap) and the depth of the second closest polygon. With such depth the shadow comparison on both polygons is likely to result in correct value.

There are more possibilities of to calculate such depth. If your geometry allows it, you might calculate average between depth of closest front facing polygon and depth of closest back facing polygon. In this case you render the scene twice using different culling modes and averaging the resulting values.

Ryan is calculating the depth as average between depth of closest front facing polygon and depth of second closest front facing polygon. He does this by rendering the scene twice. The first render is a ordinary one. During the second, the shader skips fragments which corresponds to closest polygon so the depth buffer will be left with depth of the second closest polygon. After that he will average depths from both cases.

Interesting.

To avoid some of the self-occlusion problems you could render back faces only into the depth map.

Or consider using a VSM (variance shadow map), which trades that problem for an entirely different one (that of light-bleeding).

P.S. Oops, I wrote “min” instead of “mid”… too late to edit.

Dear Komat, I’ve tried this modification you suggested. I found I did something wrong with the texture sampler within CgFx, and my problem is fixed now. I really appreciate your replies!! :wink:

Yeah, I’ll try to implement VSM next time. Thanks for the idea, modus! :slight_smile: