Custom Phong shader node - light position appear relative

Hello,

[video of the problem : http://robot.isp.imath.be/phong.flv]

I try to implement a simple Phong shader , it works nicely , but the main light is following the camera
and I cant figure out why, since I converted a code from GeeksLab (line by line), it should do the same

lightPosition comes from a shader node parameter (lightPosition = MatParam.LightPosition)

void main(){
    normal = normalMatrix * normal; 
    //not sure if I should use WorldViewMatrix  
    vec4 v=vec4(worldViewMatrix* vec4(modelPosition,1.0));    // WorldViewMatrix = glModelViewMatrix ???

    lightDir = lightPosition.xyz- v.xyz;

        eyeVec=-v.xyz;

        //----- in the following node :
        projPosition = worldViewProjectionMatrix * vec4(modelPosition, 1.0);        
}
void main(){
    vec3 _normal= normalize(normal);
    vec3 _lightDir=normalize(lightDir);

    // diffuse -----------------------------------------------------------------------------------------------------------------
        vec4 diffuse = vec4(0.7,0.7,0.7,1);
        lambertTerm = max(dot(_normal, _lightDir), 0.0); 
        vec4 Id = lightDiffuse * diffuse * lambertTerm;

        // phong
        vec3 E = normalize(eyeVec).xyz;
        vec3 R = normalize(reflect(-_lightDir,_normal));
        float spec = pow( max(dot(R,E), 0.0), shininess );
        vec4 Is = specular * lightSpecular * spec;
        //
        outColor = Id + Is;

}

can someone have a look ?

thanks

This code assumes the lightPosition is in viewSpace. JME sends the light position in world space.
So your light dir is wrong and depends on the camera orientation.
You have 2 choices : either you transform lightPosition in view space, either you compute lighting in world space.
for 1 you need to multiply the lightPosition by the ViewMatrix (before computing the lightDir).
for 2 you need to mutiply the modelPosition by the WorldMatrix instead of the WorldViewMatrix.

ok,
thanks,seems so obvious once you point it to me :stuck_out_tongue:

Well, I’ve been there :wink: