Hi all,

I want to do shadow mapping by doing the depth comparisons myself in order to implement PCSS. In order to do this I modified the osgShadow::DebugShadowMap and StandardShadowMap classes to not enable shadow comparison modes (GL_COMPARE_R_TO_TEXTURE_ARB), and when sampling the shadow map I get raw depth values, which is what I want.

However, to get the right main pass depth to compare to the shadow map value, I need to transform the fragment position to light space. To do this, I've added a uniform that stores the shadow camera's view and projection matrices, which I set in StandardShadowMap::aimShadowCastingCamera():

    _stateset->getOrCreateUniform("ShadowCameraTransform",
                                  osg::Uniform::FLOAT_MAT4)->set(
                                      view * projection);

Using that matrix, I would have thought in my shader I could do the following:

uniform mat4 ShadowCameraTransform;
uniform mat4 osg_ViewMatrixInverse;

// ...

varying vec4 lightSpacePos;

void main()
{
    // ...

    mat4 modelMatrix = osg_ViewMatrixInverse * gl_ModelViewMatrix;
    vec4 worldPos = modelMatrix * gl_Vertex;
    lightSpacePos = ShadowCameraTransform * worldPos;

    // ...
}

And then use that to compare a light-space depth value in the fragment shader which I could use to compare to the shadow map:

    float lightSpaceDepth = (lightSpacePos.xyz / lightSpacePos.w).z;
    float shadowMapDepth = texture2D( shadowTexture, uv ).r;

However the lightSpaceDepth value above doesn't seem to be in the right coordinate space or something, as I get values in too small a range. I've tried the classic transforms ( * 0.5 + 0.5 or * 2.0 - 1.0) but the values are still not in the right range.

First of all, am I doing the math right?

Second of all, I left the texture internal format set to GL_DEPTH_COMPONENT and attached to the DEPTH_BUFFER, assuming that it was OK, but could that explain my problems? Should I change it to a general purpose float format (say GL_LUMINANCE32F_ARB), attach it to the COLOR_BUFFER and write to gl_FragData[0] in the shadow pass's fragment shader?

Just trying to sanity check a bit :-)

Thanks in advance,

J-S
--
______________________________________________________
Jean-Sebastien Guay    [email protected]
                               http://www.cm-labs.com/
                        http://whitestar02.webhop.org/
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to