• Advertisement
Sign in to follow this  

View space spot-light move with camera

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

If you intended to correct an error in the post then please contact us.

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),

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 this post

Link to post
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.



Edited by kauna

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement