Greetings,
I have two shaders below, which I interchange to either render my object + texture in color or in grey levels. Only the last three lines differ in the two shaders.
What I don’t understand is why the color shader renders the diffuse light properly (on back and front faces) but not the grey level shader, which does render the diffuse light on the front faces but not on the back faces. Any clue?
//****************************
//*** color shader **********
uniform sampler2D map0;
uniform bool reversed;
varying vec3 esVertex, esNormal;
varying vec4 vPos;
float snoise(vec2 v) {
return fract(sin(dot(v.xy, vec2(12.9898, 78.233)))*43758.5453);
}
void main()
{
vec4 texColor = texture2D(map0, gl_TexCoord[0].st);
if ( snoise(floor(vPos.xy*2000.)/2000.) < 1.-gl_FrontMaterial.diffuse.a*texColor.a) {
discard;
}
vec3 normal = normalize(esNormal);
vec3 light;
if(gl_LightSource[0].position.w == 0.0)
{
light = normalize(gl_LightSource[0].position.xyz);
}
else
{
light = normalize(gl_LightSource[0].position.xyz - esVertex);
}
if (reversed){
normal = -normal;
light.y = -light.y;
}
vec3 reflectVec = reflect(-light, normal);
vec3 viewVec = normalize(-esVertex);
vec4 color = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
float dotNL = max(dot(normal, light), 0.0);
color += gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse * dotNL;
color *= texColor;
vec3 halfVec = normalize(viewVec+light);
float dotVR = max(dot(normal, halfVec), 0.0);
color += gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(dotVR, gl_FrontMaterial.shininess*4.f);
gl_FragColor = color;
}
//****************************
//*** grey level shader *****
uniform sampler2D map0;
uniform bool reversed;
varying vec3 esVertex, esNormal;
varying vec4 vPos;
float snoise(vec2 v) {
return fract(sin(dot(v.xy, vec2(12.9898, 78.233)))*43758.5453);
}
//Grel level shader
void main()
{
vec4 texColor = texture2D(map0, gl_TexCoord[0].st);
if ( snoise(floor(vPos.xy*2000.)/2000.) < 1.-gl_FrontMaterial.diffuse.a*texColor.a) {
discard;
}
vec3 normal = normalize(esNormal);
vec3 light;
if(gl_LightSource[0].position.w == 0.0)
{
light = normalize(gl_LightSource[0].position.xyz);
}
else
{
light = normalize(gl_LightSource[0].position.xyz - esVertex);
}
if (reversed){
normal = -normal;
light.y = -light.y;
}
vec3 reflectVec = reflect(-light, normal);
vec3 viewVec = normalize(-esVertex);
vec4 color = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
float dotNL = max(dot(normal, light), 0.0);
color += gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse * dotNL;
color *= texColor;
vec3 halfVec = normalize(viewVec+light);
float dotVR = max(dot(normal, halfVec), 0.0);
color += gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(dotVR, gl_FrontMaterial.shininess*4.f);
vec4 colorG = vec4(color.r*0.299+color.g*0.587+color.b*0.114);
colorG.a = 1.0;
gl_FragColor = colorG;
}