title: Should lighting be done in view space or world space
body: <p>I've been working on a bit of deferred rendering lately for my engine. I pretty much have it working now. However, when I move the camera a bit, I notice some subtle shading on some surfaces, but it is more apparent on other surfaces (I don't have any specular light). I've been doing my lighting calculations as well as my G-buffer rendering in view space. That makes the question arise: Should I being doing my lighting in world space? I'm pretty sure that the variations in light are coming from the normals being in view space. If it makes a difference, I am computing the view space position from a depth map. I've read that doing calculations in view space are fine, but with a bit of tinkering, I can't figure out what's wrong, and might just resort to doing it in view space. If anyone is curious here is my shader code:</p>
  
  <p>Normal pass:</p>
  
  <pre><code>varying vec3 normal;
  void main(void)
  {
      gl_Position =gl_ModelViewProjectionMatrix*gl_Vertex;
      normal = (gl_NormalMatrix*gl_Normal)* 0.5 + 0.5;
  }
  </code></pre>
  
  <p>Lighting pass:</p>
  
  <pre><code>uniform sampler2D positionMap;
  uniform sampler2D normalMap;
  uniform sampler2D albedoMap;
  uniform mat4 iprojMat;
  uniform int light;
  uniform vec3 lightcolor;
  varying vec2 texcoord;
  
  void main()
  {
      //get all the G-buffer information
      vec3 normal = ((texture2D(normalMap,texcoord)).rgb * 2.0 - 1.0);
      vec3 color = (texture2D(albedoMap,texcoord)).rgb;
      if (color == vec3(0,0,0))
          discard;
      float z = (texture2D(positionMap,texcoord)).r;
      float x = texcoord.x * 2.0 - 1.0;
      float y =  (1.0-texcoord.y) * 2.0 - 1.0;
      vec4 proj = vec4(x,y,z,1.0);
      proj = proj*iprojMat;
      vec3 position = proj.xyz/proj.w;
  
      //start making the light happen
      vec3 lightVec = (gl_LightSource[light].position.xyz - position);
      vec3 diffuselight = lightcolor * max(dot(normal,normalize(lightVec)), 0.0);
      diffuselight = clamp(diffuselight, 0.0, 1.0);
      //calculate attinuation
      float distance = length(lightVec);
      float att = 1.0/((distance*distance)+distance);
  
      gl_FragColor = vec4(diffuselight,1.0);
  }
  </code></pre>
  
  <p>Any help or hints on this would be appreciated.</p>
  
  <p>P.S. I have made a direct rendering shader as well that is also done in view space and the same kind of thing happen, but not as noticeably. </p>