• Create Account

## 2D Normal Mapping Problem

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.

### #1lemonzap  Members

102
Like
0Likes
Like

Posted 20 July 2014 - 06:39 PM

I've been trying to get some 2d lighting working but I've run into and issue. I'm trying out normal mapping but the normal map doesn't seem to affect the final image at all. however the dot product between the normalized normal map and the normalized light direction seems to cause issues. Positive x and y values in relation to the lights position remain unlit.

This is my fragment shader at the moment.

//attributes from vertex shader
varying vec2 vTexCoord;

//our texture samplers
uniform sampler2D u_texture;   //diffuse map
uniform sampler2D u_normals;   //normal map

uniform vec2 Resolution;      //resolution of screen
uniform vec3 LightPos;        //light position, normalized
uniform vec4 LightColor;      //light RGBA -- alpha is intensity
uniform vec4 AmbientColor;    //ambient RGBA -- alpha is intensity
uniform vec3 Falloff;         //attenuation coefficients

void main() {
//RGBA of our diffuse color
vec4 DiffuseColor = texture2D(u_texture, vTexCoord);

//RGB of our normal map
vec3 NormalMap = texture2D(u_normals, vTexCoord).rgb;

//The delta position of light

vec3 LightDir = vec3(LightPos.xy - (gl_FragCoord.xy / Resolution.xy), LightPos.z);

//Correct for aspect ratio
LightDir.x *= Resolution.x / Resolution.y;

//Determine distance (used for attenuation)
float D = length(LightDir);

float attenuation = 1.0 / ( Falloff.x + (Falloff.y*D) + (Falloff.z*D*D) );

vec3 N = normalize(NormalMap * 2.0 - 1.0);
vec3 L = normalize(LightDir);
vec3 Diffuse = (LightColor.rgb * LightColor.a) *((N.r*L.x)+(N.g*L.y)+(N.b*L.z));

vec3 color = DiffuseColor.rgb * attenuation * (Diffuse+(AmbientColor.rgb*AmbientColor.a));

gl_FragColor = vec4(color, DiffuseColor.a);
}


the line in question when like this

vec3 Diffuse = (LightColor.rgb * LightColor.a) *((N.r*L.x)+(N.g*L.y)+(N.b*L.z));


produces this

but, when I change it to this

vec3 Diffuse = (LightColor.rgb * LightColor.a);


it produces this

The second version no longer takes into account the normal map or light direction. That gets rid of the lighting problem but also ignores the normal map. However the first version seems to ignore the normal map as well. My normal map for the box is currently pretty random just so I can see if it is using it or not. Anyone know what might be my issue? I can post more code if you need it.

### #2lemonzap  Members

102
Like
0Likes
Like

Posted 21 July 2014 - 10:04 PM

I found the issue. I was sending the normal map to the shader incorrectly. I did this

glUniform1i(glGetUniformLocation(shaderProgram, "u_normals"), GL_TEXTURE1);


as opposed to this which is what I should have been doing

glUniform1i(glGetUniformLocation(shaderProgram, "u_normals"), 1);


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.