Im trying to set up directional lighting in the fragment shader. So the direction of my light moves with the camera position.

      #version 150 core

      uniform sampler2D diffuseTex;     
      uniform vec4 lightColour;
      uniform vec3 lightDirection;

      vec3 LNorm     = normalize(lightDirection);
  vec3 normal    = normalize(IN.normal);
      vec3 calColour = lightColour[i].rgb  * intensity; 
  gl_FragColor   = vec4(diffuse.rbg * calColour, diffuse.a); 

It lights the entire scene.

  • 1
    \$\begingroup\$ It sounds like you want to create a directional light that shares the position and orientation of the camera, is that correct? It's not clear what the problem is -- what exactly do you mean by "it lights the entire scene?" Is there no apparent shading at all? Or what? A screenshot or two would really help clarify your intent here. \$\endgroup\$
    – user1430
    Nov 12, 2013 at 17:06

1 Answer 1


If you want the light direction to move with your camera position then by definition it's not a directional light, because directional lights are meant to have position at infinity and have uniform direction across the whole scene.

Regardless of definitions, in order to make your light direction change with the camera position you need to calculate the light direction in your vertex shader and pass to your fragment shader:

//In your vertex shader
uniform vec3 lightPosition; // you need to pass it to VS, define it to be very far away
out vec3 lightDir;

lightDir = normalize(lightPosition - cameraPos);

This way you get different light direction based on the camera position.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .