0
\$\begingroup\$

I am trying to implement shadow maps in my game following this tutorial. For some reason, the light is not being occluded.

results

In the above screenshot, the big white sprite in the foreground is a rendering of what the occlusion map looks like. In the background, you can see the result does not produce any shadows. It's hard to see, but in the top left it shows the shadow map.

Enlarged version of the shadow map: shadow map

The occlusion map and the shadow map seem to be generating correctly, so it must be an issue with how I'm taking it into account when rendering the light. Here is the fragment shader for the light:

#version 330

uniform sampler2D uDiffuseTexture;
uniform sampler2D uNormalsTexture;
uniform sampler2D uShadowMap;

uniform vec4 uLightColor;
uniform float uConstAtten;
uniform float uLinearAtten;
uniform float uQuadradicAtten;
uniform float uColorIntensity;

uniform vec4 uAmbientColor;

in vec2 TexCoords;
in vec2 GeomSize;

out vec4 FragColor; 

float sample(vec2 coord, float r) {
    return step(r, texture2D(uShadowMap, coord).r);
}

float occluded() {
   float PI = 3.14;

   vec2 normalized = TexCoords.st * 2.0 - 1.0;
   float theta = atan(normalized.y, normalized.x);
   float r = length(normalized);
   float coord = (theta + PI) / (2.0 * PI);

   vec2 tc = vec2(coord, 0.0);
   float center = sample(tc, r);

   float sum = 0.0;

   float blur = (1.0 / GeomSize.x) * smoothstep(0.0, 1.0, r);

   sum += sample(vec2(tc.x - 4.0*blur, tc.y), r) * 0.05;
   sum += sample(vec2(tc.x - 3.0*blur, tc.y), r) * 0.09;
   sum += sample(vec2(tc.x - 2.0*blur, tc.y), r) * 0.12;
   sum += sample(vec2(tc.x - 1.0*blur, tc.y), r) * 0.15;

   sum += center * 0.16;

   sum += sample(vec2(tc.x + 1.0*blur, tc.y), r) * 0.15;
   sum += sample(vec2(tc.x + 2.0*blur, tc.y), r) * 0.12;
   sum += sample(vec2(tc.x + 3.0*blur, tc.y), r) * 0.09;
   sum += sample(vec2(tc.x + 4.0*blur, tc.y), r) * 0.05;

   return sum + smoothstep(1.0, 0.0, r);
}

float calcAttenuation(float distance) {
    float linearAtten = uLinearAtten * distance;
    float quadAtten = uQuadradicAtten * distance * distance;

    float attenuation = 1.0 / (uConstAtten + linearAtten + quadAtten);

    return attenuation;
}

vec3 calcFragPosition(void) {
    return vec3(TexCoords*GeomSize, 0.0);
}

vec3 calcLightPosition(void) {
    return vec3(GeomSize/2.0, 1.0);
}

float calcDistance(vec3 fragPos, vec3 lightPos) {
    return length(fragPos - lightPos);
}

vec3 calcLightDirection(vec3 fragPos, vec3 lightPos) {
    return normalize(lightPos - fragPos);
}

vec4 calcFinalLight(vec2 worldUV, vec3 lightDir, float attenuation) {
    float diffuseFactor = dot(normalize(texture2D(uNormalsTexture, worldUV).rgb), lightDir);

    vec4 diffuse = vec4(0.0);
    vec4 lightColor = uLightColor * uColorIntensity;

    if(diffuseFactor > 0.0) {
        diffuse = vec4(texture2D(uDiffuseTexture, worldUV.xy).rgb, 1.0);
        diffuse *= diffuseFactor;
        lightColor *= diffuseFactor;
    } else {
        discard;
    }

    return (uAmbientColor + diffuse + lightColor) * attenuation;
}

void main(void) {
    vec3 fragPosition = calcFragPosition();
    vec3 lightPosition = calcLightPosition();

    float distance = calcDistance(fragPosition, lightPosition);    
    float attenuation = calcAttenuation(distance);

    vec2 worldPos = gl_FragCoord.xy / vec2(1024, 768);
    vec3 lightDir = calcLightDirection(fragPosition, lightPosition); 

    lightDir = (lightDir*0.5)+0.5;

    float atten = calcAttenuation(distance);
    FragColor = calcFinalLight(worldPos, lightDir, atten) * vec4(vec3(1.0),
    occluded());
}
\$\endgroup\$
0
\$\begingroup\$

I think the problem is this line, at the end of occluded():

return sum + smoothstep(1.0, 0.0, r);

That probably should be just

return sum;

In the tutorial, the line uses the smoothstep call for attenuation (and also multiplies by it, but I'm guessing that's just a transcription error on your part). You have your own attenuation, so it seems like you'd want to just leave that out.

\$\endgroup\$
0
\$\begingroup\$

Okay, I have solved the problem. The problem was that I wasn't binding the shadow map to a correct texture attachment.

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.