Hi JGO!
I have a little problem with my normal map lighting shader and I can’t figure out what.
I’ll just post both shaders and a picture to explain what’s wrong.
Vertex shader:
#version 330 core
attribute vec3 a_position;
attribute vec2 a_texCoord0;
out vec2 v_texCoord;
out vec3 v_lightPosition;
out vec4 v_vertPos;
uniform mat4 u_projTrans; // camera.combined
uniform mat4 u_view; // camera.view
uniform vec2 u_lightPos; // light's wrold position
uniform float u_lightZ;
void main()
{
v_lightPosition = vec4(u_view * vec4(u_lightPos, u_lightZ, 1.0)).xyz;
v_texCoord = vec4(u_projTrans * vec4(a_position, 1.0) + vec4(1.0)).xy / 2.0;
v_vertPos = u_view * vec4(a_position, 1);
gl_Position = u_projTrans * vec4(a_position, 1.0);
}
Fragment Shader:
#version 330
layout (location = 0) out vec4 color;
in vec2 v_texCoord;
in vec3 v_lightPosition;
in vec4 v_vertPos;
uniform sampler2D u_diff, u_normSpec; // Diffuse and Normal texture (Normal -> rgb, Specular -> a)
uniform float u_lightZ;
uniform float u_normalStrength; // Default is 1.0f
uniform float u_radius; // Currently 300.0f
void main(){
vec4 diffuse = texture2D(u_diff, v_texCoord);
vec3 normal = texture2D(u_normSpec, v_texCoord).rgb;
normal.g = 1.0 - normal.g;
vec3 nBase = vec3(0.5, 0.5, 1.0);
normal = mix(nBase, normal, u_normalStrength);
vec3 N = normalize(normal * 2.0 - 1.0);
float specular = texture2D(u_normSpec, v_texCoord).a;
vec3 light_pos = v_lightPosition;
vec3 light_color = vec3(1, 1, 1);
vec3 vertex = v_vertPos.xyz;
vec3 dirLight = light_pos - vertex;
vec3 L = normalize(dirLight);
float lambert = dot(N, L);
float dst = length(light_pos - vertex);
float dst2 = dst / (1.0 - pow(dst / u_radius, 2.0));
float attenuation = 1.0 / pow((dst2 / u_radius + 1.0), 2.0);
lambert *= attenuation;
specular *= attenuation;
vec3 result = (vec3(lambert) + vec3(specular)) * diffuse.rgb * light_color;
color = vec4(result, 1.0);
}
On this picture, half of the screen is textured the other half is just cleared with glClear and both lights have the same parameters except thier position.
The first picture is the default state and as you can see the light has a wierd angle.
However if I change the u_normalStrength variable to 0.9 from 1.0 it produces the 2nd picture’s light wich is what I’d need.
I got the normalStrength thing from an older deferred lighting thread here on JGO but nontheless it should work without it aswell.
Maybe I’m using the wrong coordinates somewhere? ???
The whole secen is rendered with ambient and then I render the lights as simple quad meshes.