title: GLSL specular lighting
body: <p>When I use my shaders I get following results:
  <img src="https://i.stack.imgur.com/065OB.png" alt="enter image description here"></p>
  
  <p><img src="https://i.stack.imgur.com/hRBBT.png" alt="enter image description here"></p>
  
  <p>One problem is that specular light is sort of deformed and you could see sphere triangles, another is, that I can see specular where I shouldn't (second image). One spehere light is done in vertex shader, other in fragment.</p>
  
  <p>Here is how my vertex light shader looks like:
  Vertex:</p>
  
  <pre><code>// Material data.
  uniform vec3 uAmbient;
  uniform vec3 uDiffuse;
  uniform vec3 uSpecular;
  uniform float uSpecIntensity;
  uniform float uTransparency;
  
  uniform mat4 uWVP;
  uniform mat3 uN;
  uniform vec3 uSunPos;
  uniform vec3 uEyePos;
  attribute vec4 attrPos;
  attribute vec3 attrNorm;
  varying vec4 varColor;
  
  void main(void)
  {
      vec3 N = uN * attrNorm;
      vec3 L = normalize(uSunPos);
      vec3 H = normalize(L + uEyePos);
      float df = max(0.0, dot(N, L));
      float sf = max(0.0, dot(N, H));
      sf = pow(sf, uSpecIntensity);
  
      vec3 col = uAmbient + uDiffuse * df + uSpecular * sf;
      varColor = vec4(col, uTransparency);
      gl_Position = uWVP * attrPos;
  }
  </code></pre>
  
  <p>Fragment:</p>
  
  <pre><code>varying vec4 varColor;
  
  void main(void)
  {
  
      //vec4 col = texture2D(texture_0, varTexCoords);
      //col.r += uLightDir.x;
      //col.rgb = vec3(pow(gl_FragCoord.z, 64));
      gl_FragColor = varColor;
  
  }
  </code></pre>
  
  <p>It is possible, that my supplied data from code is wrong.
  uN is world matrix (not inverted and not transposed, even though doing that didn't seem to do anything different).
  UWVP - world view projection matrix.</p>
  
  <p>Any ideas, as to where the problem might be, would be appreciated.</p>
  
  <p>[EDIT] Here is my light calculations done in fragment:
  Vertex shader file:</p>
  
  <pre><code>uniform mat4 uWVP;
  uniform mat3 uN;
  attribute vec4 attrPos;
  attribute vec3 attrNorm;
  varying vec3 varEyeNormal;
  
  void main(void)
  {
      varEyeNormal = uN * attrNorm;
      gl_Position = uWVP * attrPos;
  }
  </code></pre>
  
  <p>Fragment shader file:</p>
  
  <pre><code>// Material data.
  uniform vec3 uAmbient;
  uniform vec3 uDiffuse;
  uniform vec3 uSpecular;
  uniform float uSpecIntensity;
  uniform float uTransparency;
  
  uniform vec3 uSunPos;
  uniform vec3 uEyePos;
  varying vec3 varEyeNormal;
  
  void main(void)
  {
      vec3 N = varEyeNormal;
      vec3 L = normalize(uSunPos);
      vec3 H = normalize(L + uEyePos);
  
      float df = max(0.0, dot(N, L));
      float sf = max(0.0, dot(N, H));
      sf = pow(sf, uSpecIntensity);
  
      vec3 col = uAmbient + uDiffuse * df + uSpecular * sf;
  
      gl_FragColor = vec4(col, uTransparency);
  }
  </code></pre>
  
  <p>[EDIT2] As Joakim pointed out, I wasn't normalizing varEyeNormal in fragment shader. After fixing that, the result is much better in fragment shader. I also used normalize function on uEyePos, so specular no longer goes to dark side. Thanks for all the help.</p>
  
  <p><img src="https://i.stack.imgur.com/yPqKN.png" alt="enter image description here"></p>