I've been working on a way of using multiple pp lights in a shader to render scene objects - but I seem to be having a problem with the light vector being messed up with the projection matrix. As I have a limit on the amount of variables I can pass between the vs and the fs, and therefore how many light vectors in tangent space I can pass over; i'm trying to do the ts stuff per light in the fs. Does anyone know what i'm doing wrong?
vert:
attribute vec3 tangent;
attribute vec3 binormal;
varying vec3 T, B, N;
varying vec3 V;
uniform mat4 projMatrix;
void main()
{
T = gl_NormalMatrix * tangent;
B = gl_NormalMatrix * binormal;
N = gl_NormalMatrix * gl_Normal;
V = -vec3(gl_ModelViewMatrix * gl_Vertex);
gl_TexCoord[1] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
light stuff in frag:
void _computeLighting( int nLight, vec3 addSpecular, vec3 addLight, vec3 map_normal, vec3 map_specular ){
// LIGHTING COMPONENTS
// per light colors
vec3 l_ambient;
vec3 l_diffuse;
vec3 l_specular;
// diffuse contribution
float diffuse;
// specular contribution
float specular;
// distance from light to vertex in TS
float dist;
// complete attenuation
float atten;
// per light
vec3 thisLight;
vec3 thisSpecular;
// for each light
vec3 L = vec3(gl_LightSource[nLight].position);
vec3 LtoV = (L - V);
normalize(LtoV);
// TS vertex, and light
Lts = (gl_NormalMatrix *LtoV) * TBN_Matrix;
Vts = V*TBN_Matrix;
// distance from light to pixel
dist = abs(distance(Lts, Vts));
// attenuation from light to pixel
atten = 1.0 / (gl_LightSource[nLight].constantAttenuation +
gl_LightSource[nLight].linearAttenuation * dist +
gl_LightSource[nLight].quadraticAttenuation * dist * dist);
l_ambient = gl_LightSource[nLight].ambient.rgb;
l_diffuse = gl_LightSource[nLight].diffuse.rgb;
l_specular = gl_LightSource[nLight].specular.rgb;
diffuse = atten*clamp(dot(Lts, map_normal), 0.0, 1.0);
thisLight = l_ambient+(l_diffuse*diffuse*atten);//+
LightingComponent += thisLight;
thisSpecular = pow(clamp(dot(reflect(-Vts, map_normal), Lts), 0.0,1.0),16.0)*map_specular;
SpecularComponent += thisSpecular;
}