Different Objects, Same Shadow Map

Hi.

I’m trying to draw multiple objects to the same shadow map, and later use it for 2 different shaders, but the result is a red screen.

I’m trying to do it like this:

This is the shadow map shader

#if OPENGL
    #define SV_POSITION POSITION
    #define VS_SHADERMODEL vs_3_0 
    #define PS_SHADERMODEL ps_3_0
#else
    #define VS_SHADERMODEL vs_4_0 
    #define PS_SHADERMODEL ps_4_0
#endif

float4x4 WVP;

struct VertexShaderInput
{
    float4 Position : POSITION0;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float2 depth : TEXCOORD0;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;
    output.Position = mul(input.Position, WVP);
    output.depth = output.Position.zw;
    
    return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
    return input.depth.x /input.depth.y;
}

// Technique for creating the shadow map
technique CreateShadowMap
{
    pass Pass1
    {
        VertexShader = compile VS_SHADERMODEL VertexShaderFunction();
        PixelShader = compile PS_SHADERMODEL PixelShaderFunction();
    }
}

This is one of the objects rendering shader (with light information)

#if OPENGL
    #define SV_POSITION POSITION
    #define VS_SHADERMODEL vs_3_0 
    #define PS_SHADERMODEL ps_3_0
#else
    #define VS_SHADERMODEL vs_4_0 
    #define PS_SHADERMODEL ps_4_0
#endif

matrix World;
matrix View;
matrix Projection;
matrix WorldInverseTransposeMatrix;
matrix LightsWorldViewProjection;

float3 CameraPosition;
float3 SunLightDirection;
float4 SunLightColor;
float SunLightIntensity;

#define MAXLIGHT 20

float3 PointLightPosition[MAXLIGHT];
float4 PointLightColor[MAXLIGHT];
float PointLightIntensity[MAXLIGHT];
float PointLightRadius[MAXLIGHT];
int MaxPointLightsRendered = 0;
int MaxSpotLightsRendered = 0;

float3 SpotLightDirection[MAXLIGHT];
float3 SpotLightPosition[MAXLIGHT];
float3 SpotLightViewDirection[MAXLIGHT];

//float4 SpotLightColor[MAXLIGHT];
float SpotLightCutOff[MAXLIGHT];
float SpotLightOuterCutOff[MAXLIGHT];

float4 SpotLightDiffuse[MAXLIGHT];
float4 SpotLightSpecular[MAXLIGHT];
float SpotLightRadius[MAXLIGHT];

Texture2D ColorMapTexture;
Texture2D ShadowMapTexture;

sampler2D ColorMap = sampler_state
{
    Texture = <ColorMapTexture>;
    MagFilter = Linear;
    MinFilter = Anisotropic;
    MipFilter = Linear;
    MaxAnisotropy = 16;
};

sampler ShadowMap = sampler_state 
{
    Texture = <ShadowMapTexture>;
    MagFilter = LINEAR;
    MinFilter = LINEAR;
    MipFilter = LINEAR;
    AddressU = clamp;
    AddressV = clamp;
};



struct VertexShaderInput
{
    float4 Position : POSITION0;
    float2 TexCoords : TEXCOORD0;
    float4 Normal : NORMAL0;
};

struct VertexShaderOutput
{
    float4 Position : SV_POSITION;
    float4 Normal : NORMAL0;
    float2 TexCoords : TEXCOORD0;
    float3 WorldPos : TEXCOORD1;
    float4 Pos2DAsSeenByLight    : TEXCOORD2;

};

float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.1;

float4 GlobalAmbient = float4(0.2, 0.2, 0.2, 0.2);

VertexShaderOutput MainVS(VertexShaderInput input)
{
    VertexShaderOutput Output;
    
    // Calculate the position on screen
    float4x4 wvp = mul(World, mul(View, Projection));

    Output.Position = mul(input.Position, wvp); // Transform by WorldViewProjection

    // Transform the normal to world space
    Output.Normal = mul(input.Normal, World);

    // The fake or real color map tex coords
    Output.TexCoords = input.TexCoords;

    // The position of our vertex in world space
    Output.WorldPos = mul(input.Position, World).xyz;

    Output.Pos2DAsSeenByLight = mul(input.Position, LightsWorldViewProjection);

    
    return Output;
}

// Our lighting equations

float4 CalcDiffuseLight(float3 normal, float3 lightDirection, float4 lightColor, float lightIntensity)
{
    return saturate(dot(normal, -lightDirection)) * lightIntensity * lightColor;
}


float4 CalcDirLight(float3 cameraPos, 
                    float3 worldPos,
                    float3 lightDirection, 
                    float3 materialNormal, 
                    float3x3 worldInverseTransposeMatrix,
                    float4 materialDiffuse,
                    float4 lightDiffuse,
                    float4 materialSpecular,
                    float4 lightSpecular,
                    float2 texCoords) 
{
    float materialShininess = 32;

    float3 viewDir = cameraPos - worldPos;
        
    float3 lightDir = -lightDirection;
    float3 halfVector = normalize(normalize(lightDir) + normalize(viewDir));
    float3 normal = mul(materialNormal, (float3x3)worldInverseTransposeMatrix);
    float4 diffuse = materialDiffuse * lightDiffuse;
    float4 specular = materialSpecular * lightSpecular;

    float3 n = normalize(normal);
    float3 h = normalize(halfVector);
    float3 l = normalize(lightDir);
    
    float nDotL = saturate(dot(n, l));
    float nDotH = saturate(dot(n, h));
    float power = (nDotL == 0.0f) ? 0.0f : pow(nDotH, materialShininess);   

    float4 materialAmbient = float4(1, 1, 1, 1);
    float4 lightAmbient = float4(1, 1, 1, 1);

    float4 color = (materialAmbient * (GlobalAmbient + lightAmbient)) +
                   (lightDiffuse * nDotL) + (lightSpecular * power);

    return color * tex2D(ColorMap, texCoords);
}


float4 CalcSpotLight(float2 texCoords,
                     float3 cameraPos,
                     float3 worldPos,
                     float3 lightPos,
                     float lightRadius,
                     float3 spotDirection,
                     float3 materialNormal,
                     float3x3 worldInverseTransposeMatrix,
                     float4 materialDiffuse,
                     float4 materialSpecular,
                     float4 lightDiffuse,
                     float4 lightSpecular,
                     float lightSpotOuterCone,
                     float lightSpotInnerCone)
{
        
    float materialShininess = 32;

    float3 viewDir = cameraPos - worldPos;
    float3 lightDir = (lightPos - worldPos) / lightRadius;
    float3 spotDir = spotDirection;
    float3 normal = mul(materialNormal, (float3x3)worldInverseTransposeMatrix);
    float4 diffuse = materialDiffuse * lightDiffuse;
    float4 specular = materialSpecular * lightSpecular;

    float atten = saturate(1.0f - dot(lightDir, lightDir));
    
    float3 l = normalize(lightDir);
    float2 cosAngles = cos(float2(lightSpotOuterCone, lightSpotInnerCone) * 0.5f);
    float spotDot = dot(-l, normalize(spotDir));
    float spotEffect = smoothstep(cosAngles[0], cosAngles[1], spotDot);
    
    atten *= spotEffect;
                                
    float3 n = normalize(normal);
    float3 v = normalize(viewDir);
    float3 h = normalize(l + v);
    
    float nDotL = saturate(dot(n, l));
    float nDotH = saturate(dot(n, h));
    float power = (nDotL == 0.0f) ? 0.0f : pow(nDotH, materialShininess);
    
    float4 materialAmbient = float4(1, 1, 1, 1);
    float4 lightAmbient = float4(1, 1, 1, 1);

    float4 color = (materialAmbient * (GlobalAmbient + (atten * lightAmbient))) +
                   (lightDiffuse * nDotL * atten) + (lightSpecular * power * atten);

    return color * tex2D(ColorMap, texCoords);
}

float4 CalcSpotLightSpecular(float3 cameraPos,
                             float3 worldPos,
                             float3 lightPos,
                             float lightRadius,
                             float3 spotDirection,
                             float3 materialNormal,
                             float3x3 worldInverseTransposeMatrix,
                             float4 materialDiffuse,
                             float4 materialSpecular,
                             float4 lightDiffuse,
                             float4 lightSpecular,
                             float lightSpotOuterCone,
                             float lightSpotInnerCone)
{
        
    float materialShininess = 32;

    float3 viewDir = cameraPos - worldPos;
    float3 lightDir = (lightPos - worldPos) / lightRadius;
    float3 spotDir = spotDirection;
    float3 normal = mul(materialNormal, (float3x3)worldInverseTransposeMatrix);
    float4 diffuse = materialDiffuse * lightDiffuse;
    float4 specular = materialSpecular * lightSpecular;

    float atten = saturate(1.0f - dot(lightDir, lightDir));
    
    float3 l = normalize(lightDir);
    float2 cosAngles = cos(float2(lightSpotOuterCone, lightSpotInnerCone) * 0.5f);
    float spotDot = dot(-l, normalize(spotDir));
    float spotEffect = smoothstep(cosAngles[0], cosAngles[1], spotDot);
    
    atten *= spotEffect;
                                
    float3 n = normalize(normal);
    float3 v = normalize(viewDir);
    float3 h = normalize(l + v);
    
    float nDotL = saturate(dot(n, l));
    float nDotH = saturate(dot(n, h));
    float power = (nDotL == 0.0f) ? 0.0f : pow(nDotH, materialShininess);
    
    float4 materialAmbient = float4(1, 1, 1, 1);
    float4 lightAmbient = float4(1, 1, 1, 1);

    float4 color = (materialAmbient * (GlobalAmbient + (atten * lightAmbient))) +
                   (lightDiffuse * nDotL * atten) + (lightSpecular * power * atten);

    return color;
}

float4 CalcSpotLightDiffuse (float3 cameraPos,
                             float3 worldPos,
                             float3 lightPos,
                             float lightRadius,
                             float3 spotDirection,
                             float3 materialNormal,
                             float3x3 worldInverseTransposeMatrix,
                             float4 materialDiffuse,
                             float4 materialSpecular,
                             float4 lightDiffuse,
                             float4 lightSpecular,
                             float lightSpotOuterCone,
                             float lightSpotInnerCone)
{
        
    float materialShininess = 32;

    float3 viewDir = cameraPos - worldPos;
    float3 lightDir = (lightPos - worldPos) / lightRadius;
    float3 spotDir = spotDirection;
    float3 normal = mul(materialNormal, (float3x3)worldInverseTransposeMatrix);
    float4 diffuse = materialDiffuse * lightDiffuse;
    float4 specular = materialSpecular * lightSpecular;

    float atten = saturate(1.0f - dot(lightDir, lightDir));
    
    float3 l = normalize(lightDir);
    float2 cosAngles = cos(float2(lightSpotOuterCone, lightSpotInnerCone) * 0.5f);
    float spotDot = dot(-l, normalize(spotDir));
    float spotEffect = smoothstep(cosAngles[0], cosAngles[1], spotDot);
    
    atten *= spotEffect;
                                
    float3 n = normalize(normal);
    float3 v = normalize(viewDir);
    float3 h = normalize(l + v);
    
    float nDotL = saturate(dot(n, l));
    float nDotH = saturate(dot(n, h));
    float power = (nDotL == 0.0f) ? 0.0f : pow(nDotH, materialShininess);
    
    float4 color = diffuse * nDotL * atten;
    
    //return color * tex2D(ColorMap, IN.texCoord);

    return color;
}


float4 CalcSpecularLight(float3 normal, float3 lightDirection, float3 cameraDirection, float4 lightColor, float lightIntensity)
{
    float3 halfVector = normalize(lightDirection + cameraDirection);
    float specular = saturate(dot(halfVector, normal));

    //I have all models be the same reflectance
    float specularPower = 2;

    return lightIntensity * lightColor * pow(abs(specular), specularPower);
}

// The squared length of a vector
float lengthSquared(float3 v1)
{
    return v1.x * v1.x + v1.y * v1.y + v1.z * v1.z;
}

//  Our pixel Shader

float4 PS(VertexShaderOutput input) : COLOR0
{
    float4 diffuseLight = float4(0, 0, 0, 0);
    float4 specularLight = float4(0, 0, 0, 0);
    float4 diffuseSpecular = float4(0, 0, 0, 0);
    float4 ambientLight = float4(0, 0, 0, 0);

    float4 baseColor = tex2D(ColorMap, input.TexCoords);

    ambientLight = AmbientColor * AmbientIntensity;

    //calculate our viewDirection
    float3 cameraDirection = normalize(input.WorldPos - CameraPosition);

    //calculate our sunlight
    diffuseLight += CalcDiffuseLight(input.Normal, SunLightDirection, SunLightColor, SunLightIntensity);
    diffuseSpecular += CalcSpecularLight(input.Normal, SunLightDirection, cameraDirection, SunLightColor, SunLightIntensity);

    //calculate our pointLights
    for (int i = 0; i < MaxPointLightsRendered; i++)
    {
        float3 PointLightDirection = input.WorldPos - PointLightPosition[i];
                           
        float DistanceSq = lengthSquared(PointLightDirection);

        float radius = PointLightRadius[i];
             
        if (DistanceSq < abs(radius * radius))
        {
            float Distance = sqrt(DistanceSq);

            PointLightDirection /= Distance;

            float du = Distance / (1 - DistanceSq / (radius * radius - 1));

            float denom = du / abs(radius) + 1;

            //The attenuation is the falloff of the light depending on distance basically
            float attenuation = 1 / (denom * denom);

            diffuseLight += CalcDiffuseLight(input.Normal, PointLightDirection, PointLightColor[i], PointLightIntensity[i]) * attenuation; 
            
            specularLight += CalcSpecularLight(input.Normal, PointLightDirection, cameraDirection, PointLightColor[i], PointLightIntensity[i]) * attenuation;
        }

    }

    // Spot Lights


    float4 spotLight = float4(0, 0, 0, 0);
    for (int j = 0; j < MaxSpotLightsRendered; j++)
    {
                 
         /*dirLight = CalcDirLight(SpotLightViewDirection[j], 
                                 input.WorldPos,
                                 SpotLightDirection[j],
                                 input.Normal, 
                                 WorldInverseTransposeMatrix,
                                 float4(1, 1, 1, 1),
                                 SpotLightDiffuse[j],
                                 float4(1, 1, 1, 1),
                                 SpotLightSpecular[j],
                                 input.TexCoords);*/

         /*float3 viewDir = normalize(input.WorldPos - SpotLightViewDirection[j]);

         diffuseSpecular += CalcSpecularLight(input.Normal, SpotLightDirection[j], viewDir, SpotLightDiffuse[j], SpotLightSpecular[j]);*/


        spotLight = CalcSpotLight (input.TexCoords,
                                   SpotLightViewDirection[j],
                                   input.WorldPos,
                                   SpotLightPosition[j],
                                   SpotLightRadius[j],
                                   SpotLightDirection[j],
                                   input.Normal,
                                   WorldInverseTransposeMatrix,
                                   float4(1, 1, 1, 1),
                                   float4(1, 1, 1, 1),
                                   SpotLightDiffuse[j],
                                   SpotLightSpecular[j],
                                   SpotLightOuterCutOff[j],
                                   SpotLightCutOff[j]);

    }

    // Shadows

    
    float shadowDiffuseLightingFactor = 1.0;
    float2 ProjectedTexCoords;
    ProjectedTexCoords.x = input.Pos2DAsSeenByLight.x / input.Pos2DAsSeenByLight.w / 2.0f + 0.5f;
    ProjectedTexCoords.y = -input.Pos2DAsSeenByLight.y / input.Pos2DAsSeenByLight.w / 2.0f + 0.5f;

    float4 t = tex2D(ShadowMap, ProjectedTexCoords);
    float depthStoredInShadowMap = t.r;
    float realDistance = input.Pos2DAsSeenByLight.z / input.Pos2DAsSeenByLight.w;

    // If the area isn't lit, then affect its lighting.
    if ((realDistance - 1.0f / 100.0f) > depthStoredInShadowMap)
    {
        shadowDiffuseLightingFactor = 0.6;
    }

    diffuseLight = diffuseLight * shadowDiffuseLightingFactor;


    return (((diffuseLight * baseColor + diffuseSpecular) 
                + ambientLight) 
                + baseColor * SunLightColor * SunLightIntensity) + spotLight;
}

technique BasicLightShader
{
    pass Pass1
    {
        VertexShader = compile VS_SHADERMODEL MainVS();
        PixelShader = compile PS_SHADERMODEL PS();
    }
}

And this is the quad shader

#if OPENGL
	#define SV_POSITION POSITION
	#define VS_SHADERMODEL vs_3_0
	#define PS_SHADERMODEL ps_3_0
#else
	#define VS_SHADERMODEL vs_4_0
	#define PS_SHADERMODEL ps_4_0
#endif

matrix World;
matrix View;
matrix Projection;

matrix LightsWorldViewProjection;

Texture2D InputTexture;
Texture2D ShadowMapTexture;

float Ticks;


sampler InputTextureSampler = sampler_state
{
	Texture = <InputTexture>;
};


sampler ShadowMap = sampler_state 
{
    Texture = <ShadowMapTexture>;
    MagFilter = LINEAR;
    MinFilter = LINEAR;
    MipFilter = LINEAR;
    AddressU = clamp;
    AddressV = clamp;
};

struct VertexShaderInput
{
	float4 VertexPosition : POSITION0;
	float2 VertexTextureMapping : TEXCOORD0;
};

struct VertexShaderOutput
{
	float4 Position : SV_POSITION;
	float2 FragmentTextureMapping : TEXCOORD0;
    float4 Pos2DAsSeenByLight    : TEXCOORD1;
};

struct PixelShaderOutput
{
    float4 Color : COLOR0;
};


VertexShaderOutput MainVS(in VertexShaderInput input)
{
	VertexShaderOutput output;

    float4x4 wvp = mul(World, mul(View, Projection));

    float4 finalPosition;

	finalPosition.x = input.VertexPosition.x;
	finalPosition.y = input.VertexPosition.y + (sin(radians(input.VertexPosition.y + Ticks)) * 0.3);
	finalPosition.z = input.VertexPosition.z;
	finalPosition.w = 1;

    output.Position = mul(finalPosition, wvp); // Transform by WorldViewProjection
	output.FragmentTextureMapping = input.VertexTextureMapping;

    output.Pos2DAsSeenByLight = mul(input.VertexPosition, LightsWorldViewProjection);


	return output;
}

float2 random(float2 p)
{
	return -1.0 + 2.0 * frac(sin(float2(dot(p, float2(1527.1, 3711.7)), dot(p, float2(2629.5, 1853.3)))) * 437658.5453);
}

float noise_perlin(float2 p)
{
	float2 i = floor(p);
	float2 f = frac(p);
	float a = dot(random(i), f);
	float b = dot(random(i + float2(1., 0.)), f - float2(1., 0.));
	float c = dot(random(i + float2(0., 1.)), f - float2(0., 1.));
	float d = dot(random(i + float2(1., 1.)), f - float2(1., 1.));
	float2 u = smoothstep(0., 1., f);
	return lerp(lerp(a, b, u.x), lerp(c, d, u.x), u.y);
}

PixelShaderOutput MainPS(VertexShaderOutput input)
{
	PixelShaderOutput output;

	float3 color1 = float3(0., 0., 0.);
	float3 color2 = float3(0., 0.54, 1.0);

	float2 pos = float2(input.FragmentTextureMapping * 0.8 + Ticks / 50.0);

	float n = noise_perlin(pos);
	n *= 3.4;

	float3 finalColor = lerp(color1, color2, n);
	output.Color = tex2D(InputTextureSampler, input.FragmentTextureMapping) * float4(1, 1, 1, clamp(abs(1.0 - finalColor.z), 0.8, 1.0));

    float shadowDiffuseLightingFactor = 1.0;
    float2 ProjectedTexCoords;
    ProjectedTexCoords.x = input.Pos2DAsSeenByLight.x / input.Pos2DAsSeenByLight.w / 2.0f + 0.5f;
    ProjectedTexCoords.y = -input.Pos2DAsSeenByLight.y / input.Pos2DAsSeenByLight.w / 2.0f + 0.5f;

    float4 t = tex2D(ShadowMap, ProjectedTexCoords);
    float depthStoredInShadowMap = t.r;
    float realDistance = input.Pos2DAsSeenByLight.z / input.Pos2DAsSeenByLight.w;

    // If the area isn't lit, then affect its lighting.
    if ((realDistance - 1.0f / 100.0f) > depthStoredInShadowMap)
    {
        shadowDiffuseLightingFactor = 0.6;
    }

    output.Color = output.Color * shadowDiffuseLightingFactor;


	return output;
}

technique BasicColorDrawing
{
	pass P0
	{
		VertexShader = compile VS_SHADERMODEL MainVS();
		PixelShader = compile PS_SHADERMODEL MainPS();
	}
};

To render the models I’m doing it like so:

var graphicsContext = _graphicsContext;
            var graphics = (graphicsContext as MonoGameGraphicsContext);
            var GraphicsDevice = graphics.WindowContext.Graphics.GraphicsDevice;


            for (int index = 0; index < model.Meshes.Count; index++)
            {
                ModelMesh mesh = model.Meshes[index];
                for (int i = 0; i < mesh.MeshParts.Count; i++)
                {
                    ModelMeshPart meshpart = mesh.MeshParts[i];

                    _shadowMapEffect.Parameters["World"].SetValue(world.ToNumerics());
                    //_shadowMapEffect.Parameters["LightViewProj"].SetValue(CreateLightViewProjectionMatrix());
                    //_shadowMapEffect.Parameters["LightViewProj"].SetValue(GetLightViewProjection().ToNumerics());
                    

                    _shadowMapEffect.CurrentTechnique.Passes[0].Apply();

                    GraphicsDevice.SetVertexBuffer(meshpart.VertexBuffer);
                    GraphicsDevice.Indices = (meshpart.IndexBuffer);
                    int primitiveCount = meshpart.PrimitiveCount;
                    int vertexOffset = meshpart.VertexOffset;
                    int vCount = meshpart.NumVertices;
                    int startIndex = meshpart.StartIndex;

                    GraphicsDevice.DrawIndexedPrimitives(Microsoft.Xna.Framework.Graphics.PrimitiveType.TriangleList, vertexOffset, startIndex,
                        primitiveCount);
                }
            }

And to render the screen quads which is dynamically constructed geometry, I’m rendering like this:

var graphicsContext = _graphicsContext;
            var graphics = (graphicsContext as MonoGameGraphicsContext);
            var GraphicsDevice = graphics.WindowContext.Graphics.GraphicsDevice;

            int currentBatchCount = 0;

            Microsoft.Xna.Framework.Graphics.VertexPositionTexture[] vertices =
                new Microsoft.Xna.Framework.Graphics.VertexPositionTexture[texturedQuads.Count * 4];

            int vertexFillIndicer = 0;
            for (; currentBatchCount < texturedQuads.Count; currentBatchCount++)
            {
                TexturedQuad foo = texturedQuads[currentBatchCount];
                for (int i = 0; i < 4; i++)
                {

                    var position = new Microsoft.Xna.Framework.Vector3((float)foo.Position[i].X, (float)foo.Position[i].Y, (float)foo.Position[i].Z);
                    var mapping = new Microsoft.Xna.Framework.Vector2((float)foo.TextureMapping[i].X, (float)foo.TextureMapping[i].Y);
                    vertices[vertexFillIndicer] = new Microsoft.Xna.Framework.Graphics.VertexPositionTexture(position, mapping);

                    vertexFillIndicer++;
                }
            }

            int verticesInaQuad = 4;
            int indicesInaQuad = 6;
            int verticeCount = vertices.Length;
            int quadCount = verticeCount / verticesInaQuad;
            int indicesCount = quadCount * indicesInaQuad;

            int indexIndicer = 0;


            short[] indices = new short[indicesCount];

            for (int verticeIndicer = 0; verticeIndicer < vertices.Length; verticeIndicer += verticesInaQuad)
            {
                if (GraphicsDevice.RasterizerState == Microsoft.Xna.Framework.Graphics.RasterizerState.CullClockwise)
                {
                    indices[indexIndicer + 0] = (short)(verticeIndicer + 0);
                    indices[indexIndicer + 1] = (short)(verticeIndicer + 1);
                    indices[indexIndicer + 2] = (short)(verticeIndicer + 2);

                    indices[indexIndicer + 3] = (short)(verticeIndicer + 2);
                    indices[indexIndicer + 4] = (short)(verticeIndicer + 3);
                    indices[indexIndicer + 5] = (short)(verticeIndicer + 0);
                }
                else
                {
                    indices[indexIndicer + 0] = (short)(verticeIndicer + 0);
                    indices[indexIndicer + 1] = (short)(verticeIndicer + 2);
                    indices[indexIndicer + 2] = (short)(verticeIndicer + 1);

                    indices[indexIndicer + 3] = (short)(verticeIndicer + 2);
                    indices[indexIndicer + 4] = (short)(verticeIndicer + 0);
                    indices[indexIndicer + 5] = (short)(verticeIndicer + 3);

                }

                indexIndicer += indicesInaQuad;
            }

            if (vertices.Length > 0)
            {

                _shadowMapEffect.Parameters["WVP"].SetValue(world.ToNumerics());
                //_shadowMapEffect.Parameters["LightViewProj"].SetValue(CreateLightViewProjectionMatrix());
                //_shadowMapEffect.Parameters["LightViewProj"].SetValue(GetLightViewProjection().ToNumerics());

                _shadowMapEffect.CurrentTechnique.Passes[0].Apply();

                GraphicsDevice.DrawUserIndexedPrimitives(Microsoft.Xna.Framework.Graphics.PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, (indices.Length / 3), Microsoft.Xna.Framework.Graphics.VertexPositionTexture.VertexDeclaration);

            }

If I take a peek at the shadow map by calling spritebatch.draw I can see only a red texture, no depth information, and of course if I try to take depth information from that it won’t render any shadow.

What can I do? How do I accomplish this? Or do I need to put everything into one single shader and batch process?

Is there any problem with the Draw primitives approach? It does not reset the depth buffer for the main 3d rendering, but looks like multiple draws to the same shadow map seem not to take effect on the previous.

You are likely not distributing your depth values properly. Try using something like this, and send in the near and far values of your projection into the shader.

float zNear;
float zFar;

float linZ(float depth, float zNear, float zFar)
{
    // Redistributes the depth for when using a perspective projection
    return (2 * zNear) / (zFar + zNear - depth * (zFar - zNear));
}


float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
    return linZ(input.depth.x / input.depth.y, zNear, zFar);
}

Do I have to pass the world view projection of the 3d models? Because right now I’m calculating only the world of the models and giving a view projection from the LookAt and projection model of the lights multiplied by the world. Or is it a general World View Projection (specific to the objects) just as if rendering and lighting getting calculated based on depth after I create the shadow map?

That doesn’t work, I’m trying to generate shadows from a directional light

what he told you is switching to a linear depth buffer … but it will not solve your problem, which may or may not be that you shadowmap and your light shader do not use the same coordinate space.

I am bit too focused on my own shaders so I can’t tell which space you are in - but you have to mae sure, that your lighing calculations work in the same space as the shadow map was actually created (looks viewspace to me?)

anyway, as your output is just red (which sounds like a depthbuffer normally looks when rendered) - are you sure you’ve set the correct rendertargets everywhere?