title: Multi lights shadow mapping does not work correctly using GLSL
body: <p>I have implemented the basic shadow mapping algorithm but it works correctly with only one light.</p>
  
  <p>I want to render a scene with two following point lights :</p>
  
  <ul>
  <li>Light_1 - position : vec3(-8.0f, 5.0f, 8.0f), direction : vec3(1.3f, -1.0f, -1.0f)</li>
  <li>Light_2 - position : vec3(8.0f, 5.0f, 8.0f), direction : vec3(1.3f, -1.0f, -1.0f)</li>
  </ul>
  
  <p>If I render separately the two lights I have the following results:</p>
  
  <p>Rendering with Light_1 :</p>
  
  <p><img src="https://i.stack.imgur.com/M4pji.png" alt="enter image description here"></p>
  
  <p>Rendering with Light_2 :</p>
  
  <p><img src="https://i.stack.imgur.com/mSk9T.png" alt="enter image description here"></p>
  
  <p>But the two light together it looks like this :</p>
  
  <p><img src="https://i.stack.imgur.com/gpPAh.png" alt="enter image description here"></p>
  
  <p>As you can see the first shadow seems to be rendered correctly, but it is below the shadow of the light_2 which is not correct. To sum up the situation I have the texture of my box which is bound to the texture unit 0. The shadow depth texture is bound from the texture unit 1 and if there are more than one depth texture (so at least two ligths, like in this example), there are bound to the texture unit 1 + 1 (GL_TEXTURE1 + 1). Here's the code that represent what I said :</p>
  
  <pre><code>for (int idy = 0; idy &lt; this-&gt;m_pScene-&gt;getLightList().size(); idy++)
  
  [...]
  
  Light *light = this-&gt;m_pScene-&gt;getLightList()[idy];
  FrameBuffer *frameBuffer = light-&gt;getFrameBuffer();
  
  glActiveTexture(GL_TEXTURE1 + idy);
  glBindTexture(GL_TEXTURE_2D, frameBuffer-&gt;getTexture()-&gt;getTextureId()); //To unbind
  
  shaderProgram-&gt;setUniform(std::string("ShadowMatrix[").append(Convertor::toString&lt;int&gt;       (idy)).append("]").c_str(), this-&gt;m_pScene-&gt;getLightList()[idy]-&gt;getBiasViewPerspectiveMatrix() * modelMatrix);
                      shaderProgram-&gt;setUniform(std::string("ShadowMap[").append(Convertor::toString&lt;int&gt;(idy)).append("]").c_str(), (int)idy + 1);
  </code></pre>
  
  <p>It corresponds in our case to :</p>
  
  <pre><code>shaderProgram-&gt;setUniform("ShadowMatrix[0]", &lt;shadow_matrix_light_1&gt;);
  shaderProgram-&gt;setUniform("ShadowMap[0]", 1); (GL_TEXTURE1)
  shaderProgram-&gt;setUniform("ShadowMatrix[1]", &lt;shadow_matrix_light_2&gt;);
  shaderProgram-&gt;setUniform("ShadowMap[1]", 2); (GL_TEXTURE2)
  </code></pre>
  
  <p>The vertex shader is the following (available for just 2 lights):</p>
  
  <pre><code>#version 400
  
  #define MAX_SHADOW_MATRIX 10
  #define MAX_SHADOW_COORDS 10
  
  layout (location = 0) in vec4 VertexPosition;
  layout (location = 1) in vec3 VertexNormal;
  layout (location = 2) in vec2 VertexTexture;
  
  uniform mat3 NormalMatrix;
  uniform mat4 ModelViewMatrix;
  uniform mat4 ShadowMatrix[MAX_SHADOW_MATRIX];
  uniform mat4 MVP;
  
  uniform int lightCount;
  
  out vec3 Position;
  out vec3 Normal;
  out vec2 TexCoords;
  out vec4 ShadowCoords[MAX_SHADOW_COORDS];
  
  void main(void)
  {
      TexCoords = VertexTexture;
      Normal = normalize(NormalMatrix * VertexNormal);
      Position = vec3(ModelViewMatrix * VertexPosition);
      for (int idx = 0; idx &lt; lightCount; idx++)
          ShadowCoords[idx] = ShadowMatrix[idx] * VertexPosition;
      gl_Position = MVP * VertexPosition;
  }
  </code></pre>
  
  <p>And a piece of code of the fragment shader :</p>
  
  <pre><code>[...]
  
  vec3 evalBasicFragmentShadow(vec3 LightIntensity, int idx)
  {
      vec3 Ambient = LightInfos[idx].La * MaterialInfos.Ka;
  
      if (ShadowCoords[idx].w &gt; 0.0f)
      {
          vec4 tmp_shadow_coords = ShadowCoords[idx];
  
          tmp_shadow_coords.z -= SHADOW_OFFSET;
  
          float shadow = textureProj(ShadowMap[idx], tmp_shadow_coords);
  
          LightIntensity = LightIntensity * shadow + Ambient;
      }
      else
      {
          LightIntensity = LightIntensity + MaterialInfos.Ka;
      }
      return (LightIntensity);
  }
  
  vec3 getLightIntensity(vec3 TexColor)
  {
      vec3 LightIntensity = vec3(0.0f);
  
      for (int idx = 0; idx &lt; lightCount; idx++)
      {
          vec3 tnorm = (gl_FrontFacing ? -normalize(Normal) : normalize(Normal));
          vec3 lightDir = vec3(LightInfos[idx].Position) - Position;
          vec3 lightDirNorm = normalize(lightDir);
          float lightAtt = getLightAttenuation(lightDir, LightInfos[idx]);
  
          LightIntensity += Point_ADS_Shading(lightAtt, -tnorm, lightDirNorm, TexColor, idx);
          LightIntensity = evalBasicFragmentShadow(LightIntensity, idx);
      }
      return (LightIntensity);
  }
  
  [...]
  </code></pre>
  
  <p>It's look like a texture unit problem because separatly the two shadows have been rendered perfectly and I use glActiveTexture correctly (I think so). Plus, I noticed that if I change the loading order of the lights, the bad shadow is caused by 'the other light' (it's the contrary). So it seems to comes from the texture unit 2, but I don't understand why. Does anyone can help me, please ? Thanks a lot in advance for your help.</p>