PDA

View Full Version : Shadow maps, map [-1, 1] to [0, 1]


kruseborn
01-11-2009, 05:42 AM
I am looking at example from nvidias homepage: Variance Shadow Mapping.

They use a texture2d for the shadowmapping and saving the depth value in it. What i understand now is that you want to map the texcoord values [-1, 1] to [0, 1] and you do this by scaling and biasing. Tex.xy * float2(0.5, 0.5)+0.5.

But what they do is that they use a negative 0.5 on the y variable. Why?
tex.xy = Tex.xy * float2(0.5, -0.5)+0.5. They are using Directx 10.

Here is the vertex and pixel shader:


PSIn VSDrawScene( VSIn input )
{
PSIn output = (PSIn)0.0f;

output.tex = input.tex;

float4 worldPos = mul( float4( input.pos, 1.0f ), mWorld );
float4 lightViewPos = mul( worldPos, mShadowView );
float3 lightVec = vLightPos.xyz - worldPos.xyz;

output.pos = mul( mul( worldPos, mView ), mProj );

output.lightViewPos = mul( lightViewPos, mShadowProj );
output.wNorm = normalize( mul( float4( input.norm, 0.0f ), mWorld ) );
output.wLight = normalize( lightVec );


return output;
}

float4 PS( uniform bool bVSM, PSIn input ) : SV_Target
{
float4 output = (float4)0.0f;

input.lightViewPos.xyz /= input.lightViewPos.w;
float2 tex = input.lightViewPos.xy *float2(0.5, -0.5) + 0.5f;


float lit = PCF_FILTER( tex, input.lightViewPos.z );


float3 wLight = normalize( input.wLight );
float3 wNormal = normalize( input.wNorm );

float3 diffuse = texDiffuse.Sample( FILT_LINEAR_WRAP, texScaled );
diffuse *= dot( normalize( wNormal ), wLight );
return float4( diffuse, 1.0f ) * lit;
}

JarkkoL
01-11-2009, 06:14 AM
But what they do is that they use a negative 0.5 on the y variable. Why?
Because in texture coordinates y points down while in screen space y points up, so you have to flip it for texture sampling.

kruseborn
01-11-2009, 06:33 AM
k thank you

Reedbeta
01-11-2009, 11:08 AM
Also, just for future reference, please use ... to post code in the forum.