Transforming directional light to eye space in GLSL

Posts: 35
Joined: 2009.10
Post: #1
I implement basic diffuse lighting in a shader that uses a directional light which I pass as a uniform. The problem is I can't figure out how to transform it into eye space so that the lighting doesn't follow the camera. Mad

This is my vertex shader:
uniform vec3 lightDir;
uniform float ambient;
uniform vec3 lightColour;

void main()
    gl_Position = ftransform();

    vec3 eyeNormal = normalize(gl_NormalMatrix * gl_Normal);
    // Multiplying by gl_ModelViewMatrix seems ineffectual.
    // I also tried multiplying by gl_NormalMatrix, which didn't work either.
    vec3 realLightDir = normalize(vec3(gl_ModelViewMatrix * vec4(lightDir, 0)));

    float intensity = max(ambient, dot(eyeNormal, realLightDir));

    vec4 lightColourV4 = vec4(lightColour, 1.0);

    gl_FrontColor = gl_Color * lightColourV4 * intensity;

And these are the steps I take each frame:
  1. Enable the shader
  2. Set the shader's uniform variables
  3. Set the projection matrix (glMatrixMode(GL_PROJECTION); glLoadIdentity(); Set my projection)
    • I do this each frame since I switch between projections a lot, e.g. perspective for the scene, orthographic for the HUD
  4. Clear the modelview matrix (glMatrixMode(GL_MODELVIEW); glLoadIdentity();)
  5. Position the camera with gluLookAt
  6. Draw objects, rotating and translating as needed
Quote this message in a reply
Posts: 177
Joined: 2002.08
Post: #2
At what point in the process do you call glLight? It should be called after the camera transform but before any object transforms.

OpenGL will automatically transform the light into eye space for you, so have you tried just not multiplying it by anything at all?
Quote this message in a reply
Posts: 35
Joined: 2009.10
Post: #3
I'm not using glLight at all. I'm using my own uniform vector.

I suppose I'll try glLight eventually if I can't figure out how to do the transformation on my own. Wacko
Quote this message in a reply
Posts: 869
Joined: 2003.01
Post: #4
I you supply your own uniform, and that's supposed to be in "world coordinates", you also need to supply a separate matrix with the camera transform's inverse, which you can then use to transform your global coords into eye space.

Or, alternatively, you can do what OpenGL does with glLight, namely transform before submitting to the GPU, thus the shader will see only stuff in eye space to begin with
Quote this message in a reply
Posts: 35
Joined: 2009.10
Post: #5
So, the light vector has to be multiplied by the inverse of the camera's transform matrix I take it then.

gluLookAt is supposed to be depreciated anyway, right? If it is, I guess I might as well come up with my own matrix-using camera class.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL Blending for 2D Light Effects metacollin 10 16,899 Jun 6, 2009 12:51 AM
Last Post: NelsonMandella
  light attenuation NelsonMandella 2 4,779 Jun 4, 2009 03:28 PM
Last Post: reubert
  Help converting normals from global space to tangent space Muthaias 6 7,971 Apr 1, 2008 06:28 AM
Last Post: Muthaias
  2d light shadows KiroNeem 2 4,123 Sep 9, 2005 01:12 PM
Last Post: KiroNeem
  Vissible Light (Volumetric?) hangt5 6 5,239 Mar 28, 2005 02:25 PM
Last Post: OneSadCookie