Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

1

07.04.2016, 21:04

DirectX Shadow Map Verhält sich komisch

Guten Abend,

Ich habe ein mehr oder weniger kleines Problem mit Shadow Mapping in DirectX. Ohne viel Umschweife, erstmal ein Bild (alle Bilder sind auch angehängt):


(Link)


Wie man sieht ist lustigerweise nur ein kleines Dreieck hell, der Rest dunkel. Und ich habe keine Ahnung, woran das liegt.
Wenn man mit Visual Studio Debuggt, sieht man, wie die Shadow Map aussieht. In meinen Augen OK:


(Link)


So wie ich das sehe kann es eigentlich nicht mehr an sehr vielen verschiedenen Dingen liegen. Zu sagen ist noch, dass ich als Vorlagecode (bin noch Anfänger) das Kapitel Shadow Mapping von Frank Lunas "Introduction to 3D Game Programming with DirectX 11".

Das hier ist mein Shader, alle Variablen die nicht hier deklariert werden sind in Konstanten Buffern, mit Ausnahme natürlich der Texturen (Shadow Map und Texture) und der zwei SampleStates.

HLSL-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
VertexOut VS(VertexIn input)
{
    VertexOut output;

    output.posW = mul(float4(input.pos, 1.0f), World).xyz;
    output.normalW = mul(input.normal, (float3x3)WorldInvTranspose);

    output.posH = mul(float4(output.posW, 1.0f), ViewProjection);
    
    output.tex = input.tex;

    output.shadowPos = mul(float4(input.pos, 1.0f), ShadowTransform);

    return output;
}

float4 PS(VertexOut input) : SV_TARGET
{
    input.normalW = normalize(input.normalW);

    float3 toEye = gEyePosW - input.posW;

    // Cache the distance to the eye from this surface point.
    float distToEye = length(toEye);

    // Normalize.
    toEye /= distToEye;

    // Default to multiplicative identity.
    float4 texColor = float4(1, 1, 1, 1);
    texColor = Texture.Sample(Sample, input.tex);

    // Start with a sum of zero. 
    float4 ambient = float4(0.0f, 0.0f, 0.0f, 0.0f);
    float4 diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f);
    float4 spec = float4(0.0f, 0.0f, 0.0f, 0.0f);

    // Sum the light contribution from each light source.  
    ComputeDirectionalLight(material, dirLight, input.normalW, toEye,
        ambient, diffuse, spec);

    float shadow = 1.0f;
    shadow = CalcShadowFactor(SampleShadow, ShadowMap, input.shadowPos);

    diffuse *= shadow;
    spec *= shadow;

    float4 A, D, S;
    [unroll]
    for (int i = 0; i < pointLightCount; i++)
    {
        ComputePointLight(material, pointLight[i], input.posW, input.normalW, toEye, A, D, S);
        ambient += A;
        diffuse += D;
        spec += S;
    }

    // Modulate with late add.
    float4 litColor = texColor*(ambient + diffuse) + spec;

    // Common to take alpha from diffuse material and texture.
    litColor.a = material.Diffuse.a * texColor.a;

    return litColor;
}


Und hier noch die CalcShadowFactor von Frank Luna:

HLSL-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
float CalcShadowFactor(SamplerComparisonState samShadow,
    Texture2D shadowMap,
    float4 shadowPosH)
{
    // Complete projection by doing division by w.
    shadowPosH.xyz /= shadowPosH.w;

    // Depth in NDC space.
    float depth = shadowPosH.z;

    // Texel size.
    const float dx = SMAP_DX;

    float percentLit = 0.0f;
    const float2 offsets[9] =
    {
        float2(-dx,  -dx), float2(0.0f,  -dx), float2(dx,  -dx),
        float2(-dx, 0.0f), float2(0.0f, 0.0f), float2(dx, 0.0f),
        float2(-dx,  +dx), float2(0.0f,  +dx), float2(dx,  +dx)
    };

    [unroll]
    for (int i = 0; i < 9; ++i)
    {
        percentLit += shadowMap.SampleCmpLevelZero(samShadow,
            shadowPosH.xy + offsets[i], depth).r;
    }

    return percentLit /= 9.0f;
}


Es würde mich wirklich freuen, wenn mir jemand helfen könnte. Hab jetzt schon etlich Stunden in diese Problem gesteckt und einfach keine Ahnung woran es liegen kann. Falls der Fehler nicht im oben geschriebenen Code steckt kann ich gerne noch mehr posten, allerdings hat vor der Markierung von D3DX als Deprecated der Großteil des anderen Codes funktioniert.

MFG
FERNman
»FERNman« hat folgende Bilder angehängt:
  • DepthMap.PNG
  • Shadow_Boxhead.PNG

TrommlBomml

Community-Fossil

Beiträge: 2 117

Wohnort: Berlin

Beruf: Software-Entwickler

  • Private Nachricht senden

2

07.04.2016, 21:12

Das Problem muss dann definitiv im ShadowReceiver Code sein. Ich hatte damals auch den Code aus dem Buch als Basis genommen und daran gescheitert, dass die Berechnung der LightView-Matrix blöd gemacht war, weil er das gleich in Texture-Space umgerechnet hat. Und es sieht tatsächlich so aus, als ob da ein Fehler liegt, denn der Rest ist ja im Schatten. Das bedeutet, dass deine ShadowPos im VertexShader falsch berechnet sein könnte (Copy Paste Fehler und mit falscher Matrix im C++ Code gesetzt o.Ä.).