2017-10-19 97 views
-1

我正在研究Java + LWJGL項目。目前我正在嘗試實現方差陰影貼圖,但只有我在着色器中採樣的第一個陰影貼圖出現在正確的位置。爲什麼只有在正確的位置呈現第一個陰影?

片段着色器:

#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); 
} 

頂點着色器:

#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; 
} 

完整的代碼(示例項目,你可以在Eclipse中導入)和截圖:

系統信息:

  • 操作系統:Windows主頁,版本:10.0.15063
  • GPU:英特爾高清顯卡520
  • GPU驅動程序版本:20.19.15.4642(通過Medion公司)
+1

請提供更多信息。一些截圖等。你看到人們低估了你。這是因爲沒有辦法幫助你提供的數據。而且,GLSL是不夠的。你的問題可以在客戶端使用着色器輸入。 –

+0

我會建議一個一個地測試你的shadowMap,首先刪除你的main,然後加上float visibility = 1;如果(shadowCoord [i] .z-SHADOW_BIAS> texture(shadowMap [i],shadowCoord [i] .xy).r){visibility = 0; } out_Color = texture(modelTexture,passTexCoords).bgra * vec4(visibility,visibility,visibility,1);並告訴我們你看到的一個。您也可以嘗試將影子地圖保存在磁盤上並在查看器中顯示它們 –

+0

如果您需要更多代碼或屏幕截圖,請查看Google Drive鏈接。我也會嘗試Draykoon D說的。 – Wendelin

回答

0

這似乎是驅動程序中的一個錯誤,因爲它在我使用NVIDIA GPU運行程序時不會發生。

的規避問題:

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

而在sampleVarianceShadowMap方法:

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

因爲我不是100%肯定,這是一個錯誤,它會如果有人可真好確認這一點。

相關問題