Followers 0

# Problems with Normal Mapping

## 6 posts in this topic

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.

0

##### Share on other sites

vec2 screenPos = gl_FragCoord.xy;
screenPos.x /= resolution.x;
screenPos.y /= resolution.y;
screenPos
= screenPos * 2 - 1; // Convert to NDC

In case gl_FragCoord is not in projection space (NDC) after vertex function, this is just ok, but consider that.

vec4 lightScreenPos = pointLightInfo[2*index]; // Pre-transformed to NDC
vec3 lightVector = vec3(lightScreenPos.xy - screenPos.xy, lightScreenPos.z);
lightVector
.x *= resolution.x / resolution.y;

if the comment is true, you should keep light vector in NDC and not scale it to screen aspect, since the normal direction is in NDC=texture space right?

But those things do not seem as the couse to me though. What seems as possible couse to me is this

normalMap.g = -normalMap.g; // Our normal map program produces an inverted y axis.

normalMap
= normalMap * 2 - 1; // Normalize to [-1, 1]

try switching those two lines - first make normal -1,1 and then switch sign of g component. You could also try to dot the light vector with a constant (0,0,1) vector to see if it is purely the sampled normal problem.

1

##### Share on other sites

That's definitely a bug. If you negate the Y component first, then apply 0-1 to -1-1 scale, then a negative normal of -1 (0 in the texture) will be -3 instead of -1. This is, as you'd expect, incorrect.

Edited by Styves
1

##### Share on other sites

Thank you for the replies.

Inverting the g channel first was definitely an issue. It led me to realising my light positions z-values were also too small, so I increased those and adjusted the falloff.

Now I have something much closer to my goal, but there is one more issue. Now I have a problem where only normals that are facing almost directly ahead seem to get lit:

I tried encircling one of the spots that isn't lighting, and it didn't change:

Then I tried moving the z- value of the light around and got a confusing result. If the z-value is very small (the background is effectively at z = 0), then some of the spots that were dark becomes lit to the up and right of the light while the normals facing the screen become darker in that area.

I'd be grateful for any ideas or suggestions.

0

##### Share on other sites

Almost immediately after posting, I found a solution; however, I don't understand why I works.

I changed the line:

normalMap = normalMap * 2 - 1; // Normalize to [-1, 1]


To

normalMap.xy = normalMap.xy * 2 - 1; // Normalize to [-1, 1]

So that the z value would stay in the range [0,1]. Now it looks like this:

If someone knows why that change worked and if it while cause problems later, I would be grateful for the insight.

0

##### Share on other sites

seeing this, your normal texture seems to contain negative z for that spot (a z <0.5  in texture) though in texture you posted it does not seem so.

Let me aware you that many normal map generating software does not produce z component with relation to -1,1 scale- due to compressing efficiency accuracy and the fact negative z component is realy not desired for a texture space normal, but x and y are. You can also compute z component youself after having -1,1 x and y components- such as z=sqrt(1.0-x*x+y*y) - what is even more effective than normalizing the sampled normal and allowing you to have 2 normal maps in one texture.

0

##### Share on other sites

What means that your solution is not corrupt, as you may have been thinking. Just make an order in your production system in first place!

0

## Create an account

Register a new account