I took a program about lighting from the opengl red book(i altered it slighty to include more primitives and include a shader). The OGL fixed-function lighting works fine with at all distance(distance as in from light sources to vertex), but the shader copes out completely and stops working when in range of 0-5 units of distance.
This is without shader (works at all distances away from lightsource). Blue cube is a positional lightsource.
This is the problem, custom shader isn't working at short range (~5 units).
I would say the problem is with this line:
intensity = dot(vec3(gl_LightSource[0].position),n);
Here you are taking the dot product between the normal and the light source position vector, instead of the vector to the light source. This should have the form:
intensity = dot(vec3(gl_LightSource[0].position) - position,n);
where position is the position of the fragment (pixel) you are shading. You should pass this position as a second varying vec3.
From far away, this difference will be neglible, since the light source position vector is so much bigger, but the result is still slightly incorrect.
if (intensity > 0.90)
color = vec4(1.0,0.5,0.5,1.0);
else if (intensity > 0.5)
color = vec4(0.6,0.3,0.3,1.0);
else if (intensity > 0.2)
color = vec4(0.4,0.2,0.2,1.0);
else
color = vec4(0.2,0.1,0.1,1.0);
gl_FragColor = color;
}
if (intensity > 0.90)
color = vec4(1.0,0.5,0.5,1.0);
else if (intensity > 0.5)
color = vec4(0.6,0.3,0.3,1.0);
else if (intensity > 0.2)
color = vec4(0.4,0.2,0.2,1.0);
else
color = vec4(0.2,0.1,0.1,1.0);
gl_FragColor = color;
}