0
\$\begingroup\$

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.

\$\endgroup\$
  • 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\$ – Josh Nov 12 '13 at 17:06
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.