Hello,
I'm trying to implement lighting with normal maps in my 2D prototype. I'm stuck on a strange issue where the lighting fades out below my lights. It looks like the dot product between the light direction and the normal must be negative below the light, but I can't get a grasp on why that's happening.
Here is an image of the problem:
No matter where I place the light(s), the area below each light stays dark.
This is the code for my frag shader. I've simplified it to try and narrow the problem down:
#version 130
in vec2 texCoords;
in vec4 color;
uniform float numPointLights;
uniform sampler2D texture;
uniform sampler2D textureN;
uniform vec4 ambient;
uniform vec4 pointLightInfo[100];
uniform vec2 resolution;
void main()
{
vec4 diffuseColor = texture2D(texture, texCoords) * color;
vec3 normalMap = texture2D(textureN, texCoords).rgb;
normalMap.g = -normalMap.g; // Our normal map program produces an inverted y axis.
normalMap = normalMap * 2 - 1; // Normalize to [-1, 1]
vec2 screenPos = gl_FragCoord.xy;
screenPos.x /= resolution.x;
screenPos.y /= resolution.y;
screenPos = screenPos * 2 - 1; // Convert to NDC
vec3 finalColor = (ambient.rgb * ambient.a) * diffuseColor.rgb;
for(int index = 0; index < numPointLights; index++) {
vec4 lightScreenPos = pointLightInfo[2*index]; // Pre-transformed to NDC
vec3 lightVector = vec3(lightScreenPos.xy - screenPos.xy, lightScreenPos.z);
lightVector.x *= resolution.x / resolution.y;
float lightDist = length(lightVector);
vec3 normal = normalize(normalMap);
vec3 lightDir = lightVector / lightDist;
vec4 lightColor = pointLightInfo[2*index+1];
float normalIntensity = max(dot(normal, lightDir), 0.0);
vec3 diffuse = (lightColor.rgb * lightColor.a) * normalIntensity;
vec3 falloff = vec3(0.009,0.09,0.5);
float attenuation = 1.0 / ( falloff.x + (falloff.y*lightDist) + (falloff.z*lightDist*lightDist) );
vec3 intensity = diffuse * attenuation;
finalColor += diffuseColor.rgb * intensity;
}
gl_FragColor = vec4(finalColor, diffuseColor.a);
}
In case it's relevant, here is the level's normal map:
I'm at a bit of a loss for how to proceed. I'm hoping I made a silly error that will be quickly obvious to someone. Thanks in advance for any help.