title: GLSL/GL2.1 Lighting: Transforming to Eye Space
body: <p>So, it helps to transform everything to eye space before doing lighting calculations? I'm having trouble with the transforming part. I've got the normals transformed right, but when I apply translations (when the object is not in the center of the world coordinate system), the lighting remains exactly the same.</p>
  
  <p>I have confirmed that there are no problems with any C++ code.
  I will paste my shaders...</p>
  
  <p>QUESTION: I would like to know what I'm not transforming right, and how I'm supposed to transform it.</p>
  
  <p>vertex shader...</p>
  
  <pre><code>const int MAXLIGHTS = 4;
  
  uniform int lightcount;
  uniform vec4 lPositions[MAXLIGHTS];
  
  //V = transformed vertex
  //N = transformed normal
  //E = eye vector
  //L = vector from vertex to light
  varying vec3 V, N, E, L[MAXLIGHTS];
  
  void main()
  {
      int lcount = lightcount &gt; MAXLIGHTS ? MAXLIGHTS : lightcount;
  
      V = vec3(gl_ModelViewMatrix * gl_Vertex);
  
      N = gl_NormalMatrix * gl_Normal;
  
      E = normalize(-V);
  
      for(int i = 0; i &lt; lcount; i++)
      {
          L[i] = gl_NormalMatrix * normalize(vec3(lPositions[i] - gl_Vertex));
      }
  
      gl_FrontColor = gl_Color;
      gl_Position = ftransform();
  }
  </code></pre>
  
  <p>fragment shader...</p>
  
  <pre><code>const int MAXLIGHTS = 4;
  
  uniform int lightcount;
  uniform vec4 lDiffuses[MAXLIGHTS];
  uniform vec4 lAmbients[MAXLIGHTS];
  
  varying vec3 V, N, E, L[MAXLIGHTS];
  uniform bool justcolor;
  
  void main()
  {
      if(justcolor)
      {
          gl_FragColor = gl_Color;
          return;
      }
      int lcount = lightcount &gt; MAXLIGHTS ? MAXLIGHTS : lightcount;
  
      vec4 ambient;
      vec4 diffuse;
      vec4 specular = vec4(0.0, 0.0, 0.0, 0.0);
      vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
  
      vec3 H;
      float NL;
      float NH;
  
      for(int i = 0; i &lt; lcount; i++)
      {
          specular = vec4(0.0, 0.0, 0.0, 0.0);
  
          ambient = lAmbients[i];
  
          NL = dot(N, L[i]);
          diffuse = lDiffuses[i] * max(NL, 0.0);
  
          if(NL &gt; 0.0)
          {
              H = normalize(E + L[i]);
              NH = max(0.0, dot(N, H));
              specular = pow(NH, 40.0) * vec4(0.3, 0.3, 0.3, 1.0);
          }
  
          color += gl_Color * (diffuse + ambient) + specular;
      }
  
      gl_FragColor = color;
  }
  </code></pre>