|
|
The tex coords you use when doing the shadow map lookup should be the world space vertex transformed by a matrix that is the combination of light view matrix, light projection matrix and a scale/bias matrix to convert the range from [-1,1] to [0,1]. If you are not going to use a proj version of the texture lookup function then you will have to do the divide by w yourself to the tex coords before using them in the shadow map lookup. You should use texRECTproj so it does the divide by w for you.
Here is a snippet of a cg vertex program that I use for shadow mapping:
vertexOut vout;
mv = glstate.matrix.modelview[0];
mvp = glstate.matrix.mvp;
float4 epos = mul ( mv , vert .pos ); // Eye-space vertex position
float4 wpos = mul( cvi, epos ); // Transform back to world space
float4 proj = mul( tex, wpos ); // Transform from world space to projected texture space
vout .hpos = mul ( mvp , vert .pos );
vout.projCoords = proj;
vout.tex0 = vert.tex0;
vout.normal = vert.normal;
vout .v2light = float4 ( lightPos .xyz - vert .pos.xyz , 1 );
vout .view2v = float4 ( vert .pos.xyz - eyePos .xyz , 1 );
return vout;
|
BTW, cvi is the inverse camera view matrix and tex is the matrix that has the matrix products: [Scale/Bias]*[Light Proj]*[Light View].
And here is the fragment program (note I use 2D texures):
float4 finalColor;
float shadow = ( vert.projCoords.w < 0 ) ? 0 : tex2Dproj( shadowMap, vert.projCoords ).r;
float4 lightTex = ( vert.projCoords.w < 0 ) ? 0 : tex2Dproj( projLightTexture, vert.projCoords );
float2 tex0 = vert.tex0.xy;
float4 Kd = tex2D( rgbTexture, tex0 );
float4 Ks = tex2D( specTexture, tex0 );
float3 normal = normalize( vert.normal.xyz );
float3 v2light = normalize( vert.v2light.xyz );
float3 view2v = normalize( vert.view2v.xyz );
float3 l2vert = -v2light;
float diffuse, specular;
float3 R = reflect( -v2light, normal );
float4 lighting = float4( 0, 0, 0, 0 );
lighting = lit( dot( normal, v2light ), dot( R, -view2v ), specExp );
diffuse = lighting.y;
specular = lighting.z;
float3 finalShadow = shadow * lightTex.xyz;
finalColor = float4( ( Kd.xyz * ambient ) + ( Kd.xyz * diffuse + Ks.xyz * specular ) * finalShadow, 1 );
return finalColor;
|
-SirKnight
|