• Popular Now

• 11
• 9
• 10
• 9
• 10

This topic is 403 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey all,

I sorted out my matrices (previous topic), but I am having problems with applying the shadows on the scene...

So I have a shadow map computed as:

float4 shadowpos = mul(float4(inputpos,1.0), modelMatrix);
return shadowpos;

And it renders a plausible shadow map.

Now in my lighting pass, I am doing:

float shadow = 1.0;
{
}
The matrix shadowVPMatrix is exactly the same as the shadow map pass acording to Nsight, worldpos is reconstructed and it is correct (I validated it trying with a world position value stored in gbuffer). I also tried dividing the lightspacePos,xyz by its w, but still wrong...

What can is wrong? How can i go about and investigate this?

Again i am very sorry if this is a stupid question, this are really my first steps in this field :(

Thank you!!

Jacques

Share on other sites

You can simply set a vertex shader with the pixel shader nullptr to output on the shadow map.
WorldViewProjection matrix = ObjectWorldMatrix * SunLightViewProjection.

cbuffer WVP_CBUFFER : register( b0 )
{
float4x4 WorldViewProjection;
};

VS_OUTPUT main( in VS_INPUT Input )
{
VS_OUTPUT Output;
Output.Position = mul( float4( Input.Position, 1.0f ), WorldViewProjection );
Output.TexCoord = Input.TexCoord;
return Output;
}

Then in the pixel shader of the mesh rendering :

// Transform the view position to shadow space.
float4 PositionCS = mul( float4( PositionVS, 1.0f ), SunViewProjection );

// Transform from NDC space to texture space.
float2 ShadowTexCoord = 0.5f * float2( PositionCS.x, -PositionCS.y ) + 0.5f;

float ShadowFactor = ShadowMap.SampleCmpLevelZero( ShadowSampler, ShadowTexCoord, PositionCS.z - 0.006f );

0.006f is an arbitrary bias but it has to be set by scene.
When the projection is orthographic which is the case for the sun shadow map, you don't have to divide by W because it's like dividing by 1.0f.

Edited by Alundra

Share on other sites

The Matrix for rendering the shadow map and then sampling from it are different due to the viewport vs texture coordinates.

When rendering the shadow map the map goes from -1,+1 to +1,-1. When sampling you go from 0,0 to 1,1

[ 0.5    0    0   0 ]

[ 0  -0.5    0    0 ]

[ 0     0     0    0  ]

[0.5 0.5    0    1]