Theorie:
varying vec3 L; // interpolated surface local coordinate light direction varying vec3 E; // interpolated surface local coordinate view direction varying vec3 N; void main(void) { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; gl_TexCoord[0] = gl_MultiTexCoord0; // uv mat3 TBN; TBN[0] = gl_NormalMatrix * vec3 (gl_MultiTexCoord1); // Tangente TBN[1] = gl_NormalMatrix * vec3 (gl_MultiTexCoord2); // Binormale TBN[2] = gl_NormalMatrix * gl_Normal; // Normale vec4 P = gl_ModelViewMatrix * gl_Vertex; E = normalize(TBN * vec3(-P)); L = normalize(TBN * vec3(gl_LightSource[0].position – P)); } |
uniform sampler2D myTexture1; uniform sampler2D myTexture2; varying vec3 L; // interpolated surface local coordinate light direction varying vec3 E; // interpolated surface local coordinate view direction varying vec3 N; void main (void) { vec3 N2; // Fetch normal from normal map vec4 textur = texture2D(myTexture1, vec2 (gl_TexCoord[0])); N2 = vec3(texture2D(myTexture2, vec2 (gl_TexCoord[0]))); N2 = (N2 – 0.5) * 2.0; N2.y = -N2.y; N2 = normalize(N2); vec3 Ln = normalize(L); vec3 En = normalize(E); float e = 8.0 * clamp(L[2],0.0,0.125); // N is (0 0 1) => N dot L is L[2] vec3 R = normalize(-reflect(Ln,N2)); vec4 Iamb = gl_FrontLightProduct[0].ambient; vec4 Idiff = gl_FrontLightProduct[0].diffuse * max(dot(N2,Ln), 0.0); vec4 Ispec = gl_FrontLightProduct[0].specular * pow(max(dot(R,En),0.0), gl_FrontMaterial.shininess); gl_FragColor = textur * ( Iamb + e* (Idiff + Ispec)); } |