I am trying to create stationary spot light, but so far I am stuck - shaders aren't my best side. I have created desired effect but only on objects which don't have any transformation. What I am doing wrong? I was trying diffrent combinations for transformed objects but in all cases the light was "moving" when I changed camera position. For simplicity I have removed attenuation (it is set to 1.0).
VP:
void main(void)
{
vec4 normal4 = vec4(normal, 0);
vec4 position4 = modelViewMatrix * position;
vec3 position3 = position4.xyz / position4.w;
vec4 eyeNormal4 = modelViewMatrix * normal4;
varNormal = eyeNormal4.xyz;
varLightDir[0] = normalize(lightPositions[0] - position3);
varSpotDir[0] = normalize((modelViewMatrix * vec4(lightSpotDir0,0)).xyz);
varLightDist[0] = length(lightPositions[0] - position3 );
texCoord = texture0;
gl_Position = modelViewProjectionMatrix * position;
}
FP:
void main (void)
{
vec4 finalColor;
float spotAngle;
vec3 n = normalize(varNormal);
vec3 normalLightDir;
float diff;
float att;
vec4 base = texture2D(textureUnit0, texCoord);
finalColor = base * ambientColor;
normalLightDir = normalize(varLightDir[0]);
diff = max(0.0, dot(n, normalLightDir));
if(diff > 0.0)
{
spotAngle = dot(normalize(-varSpotDir[0]), normalLightDir);
if(spotAngle > 0.75) //spot cut off
{
spotAngle = pow(spotAngle, 12.0);
//att = spotAngle / (0.6 + 0.001 * tempDistance + 0.1 * tempDistance * tempDistance );
att= 1.0;
finalColor += (base) * diff * att ;
}
}
finalColor.a = alpha;
gl_FragColor = finalColor;
}