Hello everybody.
As I said in the topic, I have a problem with my shader for point lights. I followed a ton of tutorials about phong lighting and per fragment point lighting. Still, after all these tutorials, I still have this problem:
The light only lits up objects in the positive X direction.
Here is my shader code:
VERTEX SHADER
#version 120
varying vec2 texCoord;
varying vec3 position;
varying vec3 normal;
varying vec3 tangentSurf2light;
varying vec3 tangentSurf2view;
uniform vec3 lightPos;
void main()
{
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
position = vec3(gl_ModelViewMatrix * gl_Vertex);
normal = normalize(gl_NormalMatrix * gl_Normal);
texCoord=gl_MultiTexCoord0.xy;
vec3 tangent;
vec3 v1=cross(gl_Normal,vec3(0.0,0.0,-1.0));
vec3 v2=cross(gl_Normal,vec3(0.0,-1.0,0.0));
if(length(v1)>length(v2)){
tangent=v1;
}else{
tangent=v2;
}
vec3 t = normalize(gl_NormalMatrix * tangent);
vec3 b = cross(normal,t);
mat3 mat = mat3(t.x,b.x,normal.x,
t.y,b.y,normal.y,
t.z,b.z,normal.z);
vec4 var = vec4(lightPos,1.0);
vec3 lightLoc = vec3(gl_ModelViewMatrix * var);
vec3 vector = normalize(lightLoc-position);
tangentSurf2light=mat*vector;
vector=normalize(-position);
tangentSurf2view=mat*vector;
}
FRAGMENT SHADER
#version 120
uniform sampler2D texture;
uniform sampler2D normalmap;
uniform sampler2D specularmap;
uniform vec3 lightPos;
varying vec2 texCoord;
varying vec3 position;
varying vec3 normal;
uniform vec3 matDiffuse;
uniform vec3 matSpecular;
uniform vec3 lightAmbient;
uniform vec3 lightDiffuse;
uniform vec3 lightSpecular;
varying vec3 tangentSurf2light;
varying vec3 tangentSurf2view;
void main(){
vec4 var = vec4(lightPos,1.0);
vec3 lightLoc = vec3(gl_ModelViewMatrix * var);
float dist=length(position-lightLoc);
float att=1.0/(1.0+0.1*dist+0.01*dist*dist);
vec3 ambient = vec3(texture2D(texture,texCoord)) * lightAmbient;
vec3 surf2light = normalize(tangentSurf2light);
vec3 norm = normalize(texture2D(normalmap,texCoord).xyz*2.0-1.0);
float dcont = max(0.0, dot(norm,surf2light));
vec3 diffuse = dcont * vec3(texture2D(texture,texCoord)) * lightDiffuse;
vec3 surf2view = normalize(tangentSurf2view);
vec3 reflection = reflect(-surf2light,norm);
float scont = pow(max(0.0,dot(surf2view,reflection)),texture2D(specularmap,texCoord).x*50);
vec3 specular = scont*lightSpecular*matSpecular*att;
gl_FragColor = vec4(ambient+diffuse+specular,1.0);
}
Thanks in advance!