Search code examples
renderingshadershadowogrecg

some questions about cg shader program (Depth Shadow Mapping in OGRE)


I am now doing with depth shadow mapping,and learning from this tutorial (http://www.ogre3d.org/tikiwiki/Depth+Shadow+Mapping)

I have 3 questions as following:

(1)Is it right that when I use custom shadow caster,I can get depth in shadow receiver using "uniform sampler2D shadowMap "?

void casterVP( out float2 outDepth        : TEXCOORD0)
{
    outPos = mul(worldViewProj, position);
    outDepth.x = (outPos.z - depthRange.x) * depthRange.w;
}
void casterFP( float2 depth            : TEXCOORD0,
              out float4 result        : COLOR)
{
    result = float4(finalDepth, finalDepth, finalDepth, 1);
}

//shadow receiver fragment program
void receiverFP(uniform sampler2D   shadowMap       : register( s0 ))
{
}

(2) I am not very sure what this matrix(texture_viewproj_matrix) used for.

I guess,

texture coordinate->camera coordinate->screen coordinate??

and texture coordinates should be 2-D. Am I right?

(3) In shadow receiver fragment shader,I don't know what this line mean. And,do these 3 variable(finalCenterDepth,shadowUV.z and vertexColour) stand for depth?

result = (finalCenterDepth > shadowUV.z) ? vertexColour : float4(0,0,0,1);

Thank you~ any advice is useful for newbie :D


Solution

  • (1) Not sure If I understood the question correctly. If you wrote depth into that render target then you can read it from the associated texture.

    (2) texture_viewproj_matrix transforms from world space into light's screen space and rescales resulting xy from [-1;1] to [0;1]. Basically in xy/w you get shadow map UV coordinates of the receiver and in z - shadow map depth of the receiver.

    (3) finalCenterDepth - depth read from shadow map and adjusted by the depth bias in order to fix acne artifacts. shadowUV.z - depth of receiver also adjusted by the depth bias. vertexColour - lit color, which was calculated in the vertex shader (see outColour in receiverVP).