2017-10-19 16 views
-1

Je travaille sur un projet Java + LWJGL. Actuellement, j'essaie d'implémenter Variance Shadow Mapping, mais seule la première carte d'ombre que j'échantais dans mon shader apparaît à la bonne position.Pourquoi la première ombre est-elle restituée à la bonne position?

shader Fragment:

#version 330 core 

in vec2 passTexCoords; 
in vec4[4] shadowCoords; 

//Fragment color 
out vec4 out_Color; 

uniform sampler2D modelTexture; 
uniform sampler2D[4] shadowMaps; 

#define SHADOW_BIAS 0.0005 

float linstep(float low, float high, float v) { 
    return clamp((v-low)/(high-low), 0.0, 1.0); 
} 

//compare ... The depth of the fragment in shadow map sapce 
float sampleVarianceShadowMap(in sampler2D shadowMap, in vec2 coords, in float compare) { 

    /* This is the Code that I want to use when I know what the problem was. 
    vec2 moments = texture(shadowMap, coords.xy).rg; 

    float p = step(compare, moments.x); 
    float variance = max(moments.y - moments.x * moments.x, 0.00002); 

    float d = compare - moments.x; 
    float pMax = linstep(0.2, 1.0, variance/(variance + d*d)); 

    return min(max(p, pMax), 1.0); 
    */ 

    //============================================================================HERE=========================================================================HERE==================== 
    //THE ERROR OCCURES HERE: 

    //This doesn't work: 
    float visibility = step(compare-SHADOW_BIAS, texture(shadowMap, coords.xy).r); 
    return visibility; 

    //The shadows on the ground move in a weird way when the camera moves. 

    //But this does: 
    return step(compare-SHADOW_BIAS, texture(shadowMap, coords.xy).r); 

    //With this code the shadows are at the correct place. 

    //===========================================================================HERE==========================================================================HERE===================== 
} 

//To create a smooth darkness falloff at the edge of the shadow map 
float calcShadowMapVisibilityFalloff(in vec2 coords, in float falloffStart, in float gradient) { 
    float distFromTexCenter = length(coords * vec2(2.0) - vec2(1.0)); 
    float falloff = (clamp(pow(distFromTexCenter, gradient), falloffStart, 1.0) - falloffStart) * (1/(1-falloffStart)); 

    if(falloff > 1.0 || falloff < 0.0) { 
     falloff = 0; 
    } 

    return 1-falloff; 
} 

void main(void){ 

    float shadowInvertedBrightness = 1.0; 
    for(int i = 0; i < shadowMaps.length(); i++) 
    { 
     float visibility = 1 - sampleVarianceShadowMap(shadowMaps[i], shadowCoords[i].xy, shadowCoords[i].z); 
     shadowInvertedBrightness -= (visibility/shadowMaps.length()) * calcShadowMapVisibilityFalloff(shadowCoords[i].xy, 0.85, 2.0); 
    } 

    shadowInvertedBrightness = clamp(shadowInvertedBrightness, 0.2, 1.0); 

    //.bgra because I save textures with the BGRA format (I've read its faster) 
    out_Color = texture(modelTexture, passTexCoords).bgra * vec4(shadowInvertedBrightness,shadowInvertedBrightness,shadowInvertedBrightness,1); 
} 

Vertex Shader:

#version 330 core 

//Vertex coords 
in vec3 position; 
//Texture coords 
in vec2 texCoords; 

//The MVP matrix of the entity 
uniform mat4 MVPMat; 
//The "To Shadow Map Space" matrix 
uniform mat4[4] shadowMVPBiasMats; 
//The Transformation matrix of the entity 
uniform mat4 transformMat; 

out vec2 passTexCoords; 
//Shadow map sample coords 
out vec4[4] shadowCoords; 

void main(void) { 
    gl_Position = MVPMat * vec4(position, 1.0); 

    vec4 worldPos = transformMat * vec4(position, 1.0); 

    for(int i = 0; i < shadowMVPBiasMats.length(); i++) { 
     shadowCoords[i] = shadowMVPBiasMats[i] * worldPos; 
    } 

    passTexCoords = texCoords; 
} 

complet Code (Exemple de projet que vous pouvez importer dans Eclipse) et Captures d'écran:

Informations système:

  • OS: Windows Home, Version: 10.0.15063
  • GPU: Intel HD Graphics 520
  • GPU Version du pilote: 20.19.15.4642 (par Medion)
+1

S'il vous plaît fournir plus d'informations.Certaines captures d'écran, etc Vous voyez les gens downvoted vous.C'est parce qu'il n'y a pas moyen d'aider avec les données que vous avez fourni.En outre, GLSL ne suffit pas. Votre problème peut être du côté client avec les entrées de shader. –

+0

Je recommanderais de tester votre shadowMap un par un, enlevez d'abord votre main et ajoutez simplement float visibility = 1; if (shadowCoord [i] .z-SHADOW_BIAS> texture (shadowMap [i], shadowCoord [i] .xy) .r) {visibilité = 0; } out_Color = texture (modelTexture, passTexCoords) .bgra * vec4 (visibilité, visibilité, visibilité, 1); et dites-nous celui que vous voyez. Vous pouvez également essayer d'enregistrer votre carte d'ombre sur le disque et de l'afficher dans un visualiseur. –

+0

Si vous avez besoin de plus de code ou de captures d'écran, consultez le lien Google Drive. Aussi, je vais essayer ce que Draykoon D a dit. – Wendelin

Répondre

0

Cela semble être un bug dans le pilote, car cela ne se produit pas lorsque j'exécute le programme avec mon GPU NVIDIA.

Une solution de contournement pour le problème:

float textureShadowMap(in sampler2D shadowMap, in vec2 coords) { 
    return texture(shadowMap, coords).r; 
} 

Et dans la sampleVarianceShadowMap Méthode:

float visibility = step(compare-SHADOW_BIAS, textureShadowMap(shadowMap, coords)); 
return visibility; 

Depuis que je ne suis pas sûr à 100% qu'il est un bug, ce serait bien si quelqu'un peut confirme cela.