I’ve been de-bugging this for hours. For some reason the diffuse factor on this shader isn’t working.
Fragment Shader:
uniform sampler2D sampler0;
uniform sampler2D sampler1;
uniform sampler2D sampler2;
varying vec3 Vd; // View direction (Tangent space)
varying vec3 Ld; // Light direction (Tangent space)
varying vec2 Vt; // Vertex texture UV
const vec3 Lc = vec3(0.5, 0.2, 0.2);
void main(void){
vec3 Pd = texture2D(sampler0, Vt).rgb;
vec3 Pn = texture2D(sampler1, Vt).rgb;
vec4 Ps = texture2D(sampler2, Vt).rgba;
vec3 N = normalize(Pn * 2.0 - 1.0);
vec3 R = reflect(-Ld, N);
vec4 Iambi = vec4(Pd, 1);
vec4 Idiff = vec4(Lc.xyz, 1) * vec4( max(dot(N, Ld), 0.0) );
vec4 Ispec = vec4(Lc.xyz, 1) * Ps * vec4(pow(max(dot(R, Vd), 0.0), 1.0));
gl_FragColor = vec4(vec3(Idiff).xyz, 1);
}
Vertex Shader:
#version 120
varying vec3 Vd; // View direction (Tangent space)
varying vec3 Ld; // Light direction (Tangent space)
varying vec2 Vt; // Vertex texture uv
void main(void){
vec3 N = normalize( gl_NormalMatrix * gl_Normal );
vec3 T = normalize( gl_NormalMatrix * vec3(gl_Color) );
vec3 B = normalize( cross(N, T) );
mat3 TBN = mat3(T, B, N);
vec4 Vp = gl_ModelViewMatrix * gl_Vertex;
vec4 Lp = vec4(0, 1, 0, 1);
Ld = normalize(TBN * vec3(Lp - Vp));
Vd = normalize(TBN * vec3(-Vp));
Vt = gl_MultiTexCoord0.xy;
gl_Position = gl_ProjectionMatrix * Vp;
}
It has something to do with the vertex position being in view/model space, and the light’s position not. But I’ve never seen anyone transform the light position by the view/model matrix in any examples.