# View space spot-light move with camera

This topic is 1515 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have spot light calculated in view space. I had it working in World Space, but now If I put camera in the same position as light, it is working. But if I start moving camera around, light start to rotates.

I have so far "hard coded" everything in fragment shader. WorldMatrix is Identity.

vec3 vNormalVS = (vec4(vNormalWS, 0.0) * (en_mViewInvTransposed)).xyz;

vec4 vPosVS = vec4(vPosWS.xyz, 1.0) * en_mView;

vec4 lightPosVS = vec4(en_spotLight.vPointWS.xyz, 1.0) * en_mView;
vec4 lightDirVS = vec4(en_spotLight.vDirWS.xyz, 1.0) * en_mView;

vPhong = CalcSpotLight(
vec3(10.0, 10.0, 10.0),
vPosVS.xyz,
en_spotLight.vColor,
lightPosVS.xyz,
normalize(lightDirVS.xyz),
en_spotLight.vAtt.xyz,
en_spotLight.fFallOffCos,
mat,
normalize(vNormalVS));

Now, I am just doing simple fallOff

vec4 CalcSpotLight(vec3 vIntensity, vec3 vPos,
vec4 vLColor, vec3 vLPos, vec3 vLDir, vec3 vAtt, float fFallOffCos,
Material mat, vec3 vNormal)
{

vec3 vLightToPixel = normalize(vPos - vLPos);
float fSpotFactor = dot(vLightToPixel, vLDir);

if (fSpotFactor > fFallOffCos)
{
return vec4(1,0,0,0);

//vec4 vColor = CalcPointLight(vIntensity, vPos, vLColor, vLPos, vAtt, mat, vNormal);

//return vColor * (1.0 - (1.0 - fSpotFactor) * 1.0 / (1.0 - fFallOffCos));
}

return vec4(0,1,0,0);
}

But even this is not working and red / green color are moving along with camera. If I put camera into light position, all is correct. It is probably something stupid, but after half a day, I am really lost :-(

I have tested vPosVS and result seems correct (4 color quad centered at screen center).

##### Share on other sites

vec4 lightDirVS = vec4(en_spotLight.vDirWS.xyz, 1.0) * en_mView; <- this line looks suspicious

what happens is that you treat your light direction as a position, when you multiply it with the matrix the translation is applied also, which will produce the effect you observe - when camera moves the light direction changes too.

Under hlsl you could write float3 lightDirVS = mul(en_spotLight.vDirWS.xyzen_mView); in order to just rotate the vector without translation.

Cheers!

Edited by kauna

##### Share on other sites

Eh.. thanks. I missed it. Correct version in GLSL

vec4 lightDirVS = vec4(en_spotLight.vDirWS.xyz, 0.0) * en_mView;

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633665
• Total Posts
3013246
×