• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Problems with Normal Mapping

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

Posted 17 April 2014 - 07:31 AM

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.

### #2JohnnyCode  Members

Posted 17 April 2014 - 08:40 AM

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.

### #3Styves  Members

Posted 17 April 2014 - 01:11 PM

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, 17 April 2014 - 01:11 PM.

Posted 17 April 2014 - 06:08 PM

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.

Posted 17 April 2014 - 06:18 PM

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.

### #6JohnnyCode  Members

Posted 18 April 2014 - 09:08 AM

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.

### #7JohnnyCode  Members

Posted 18 April 2014 - 07:24 PM

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!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.