• Advertisement
Sign in to follow this  

Apply shadow map on scene

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

If you intended to correct an error in the post then please contact us.

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);
shadowpos = mul(shadowpos, shadowVPMatrix);
return shadowpos;

And it renders a plausible shadow map. 


Now in my lighting pass, I am doing:

float shadow = 1.0;
float4 lightspacePos = mul(float4(worldpos, 1.0),shadowVPMatrix);
float shadowSample = shadowmap.Sample(sampler, lightspacePos.xy).r;
if (compareforshadow(lightspacePos.z, shadowSample ))
    shadow = 0.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!!


Share this post

Link to post
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.
Here the vertex shader :

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;

// Compute the shadow factor.
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 this post

Link to post
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

You need to adjust the shadowVPMatrix  by concatenating something like this

[ 0.5    0    0   0 ]

[ 0  -0.5    0    0 ]

[ 0     0     0    0  ]

[0.5 0.5    0    1]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement