Author Topic: Stationary spot light - how to?  (Read 961 times)

Offline zbych

  • int
  • **
  • Posts: 75
    • View Profile
Stationary spot light - how to?
« on: October 29, 2012, 08:40:20 pm »
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:
Code: [Select]
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:
Code: [Select]
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;
}

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Stationary spot light - how to?
« Reply #1 on: October 30, 2012, 07:10:07 am »
What exactly do you mean by "moving"? Do you have some screen shots to show the problem? At fuirst glance, the shaders look ok to me...but then again, it's pretty early in the morning here... ;)

Offline zbych

  • int
  • **
  • Posts: 75
    • View Profile
Re: Stationary spot light - how to?
« Reply #2 on: October 30, 2012, 05:55:28 pm »
When I add objects to scene, and translate/rotate them, the spot for them is calculated completely wrong. The objects with letter "A" are almost in the same position as walls.



I think the problem is with spot direction - IMO it shouldn't be multiplied by modelView matrix, but if I don't do it the spot is changing its direction every time I move/rotate camera.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Stationary spot light - how to?
« Reply #3 on: October 30, 2012, 08:23:18 pm »
I think the problem is with spot direction - IMO it shouldn't be multiplied by modelView matrix, but if I don't do it the spot is changing its direction every time I move/rotate camera.
I agree. It's not totally clear what lightSpotDir0 actually is, because you haven't posted about it, but if it is what i think it is, it shouldn't be transformed into world space that way. I'm not 100%, but i think that removing the translational part from the matrix should do the trick. Something like...

Code: [Select]
normalize(mat3(modelViewMatrix) * lightSpotDir0);

Offline zbych

  • int
  • **
  • Posts: 75
    • View Profile
Re: Stationary spot light - how to?
« Reply #4 on: October 30, 2012, 11:51:24 pm »
Looks like it was quite diffrent problem. I solved it with workaround. I wanted to created programmatically a rectangle and make it appear in diffrent places. To make it short - some of the rectangles have changed order of indexes from CCW to CW, I don't know how but it works  ???