title: Strange behaviour of a uniform integer variable using GLSL
body: <p>I'm coding a graphic engine using OpenGL + GLSL shaders. Currently, I manage several lights in my scene (per-pixel lighting mode). So, I have a loop to access to the properties of all lights in the scene for each pixel. If I put the light count value directly in the loop condition there's not problem but if I want to do this job in a dynamic way by sending an integer uniform variable from my C++ code it does not work. But I checked in the fragment shader the value of the variable and this one is correct. In my example just below, my light list has a size of 1.</p>
  
  <p>Here's my c++ code :</p>
  
  <pre><code>ShaderProgram *program = (*Ite).second-&gt;getShaderProgram();
  
  program-&gt;bind();
  
  program-&gt;setUniform("spotCount", (int)this-&gt;m_Scene-&gt;getLightList().size()); //1 in my example
  </code></pre>
  
  <p>My vertex shader code :</p>
  
  <pre><code>#version 400
  
  layout (location = 0) in vec3 VertexPosition;
  layout (location = 1) in vec3 VertexNormal;
  layout (location = 2) in vec2 VertexTexture;
  
  uniform mat4 ProjectionMatrix;
  uniform mat3 NormalMatrix;
  uniform mat4 ModelViewMatrix;
  uniform mat4 MVP;
  
  out vec3 VPosition;
  out vec3 VNormal;
  out vec2 VTexCoords;
  
  void main(void)
  {
      VTexCoords = VertexTexture;
      VNormal = normalize(NormalMatrix * VertexNormal);
      VPosition = vec3(ModelViewMatrix * vec4(VertexPosition, 1.0f));
      gl_Position = MVP * vec4(VertexPosition, 1.0f);
  }
  </code></pre>
  
  <p>And finally, my fragment shader code :</p>
  
  <pre><code>#version 400
  
  in vec3 Position;
  in vec3 Normal;
  in vec2 TexCoords;
  
  layout (location = 0) out vec4 FragColor;
  
  uniform sampler2D Tex1;
  uniform int spotCount;
  
  struct Light
  {
      vec4 Position;
      vec3 La, Ld, Ls;
  };
  
  uniform Light LightInfos[1];
  
  struct Material
  {
      vec3 Ka, Kd, Ks, Ke;
      float Shininess;
  };
  
  uniform Material MaterialInfos;
  
  vec3 ADS_Phong_Shading(int idx, vec3 n, vec3 s)
  {
      vec3 v = normalize(vec3(-Position));
      vec3 h = normalize(v + s);
  
      vec3 Ambient = LightInfos[idx].La * MaterialInfos.Ka;
      vec3 Diffuse = LightInfos[idx].Ld * MaterialInfos.Kd * max(dot(s, n), 0.0f);
      vec3 Specular = LightInfos[idx].Ls * MaterialInfos.Ks * pow(max(dot(h, n), 0.0f), MaterialInfos.Shininess);
      vec3 Emissive = MaterialInfos.Ke;
  
      return (Ambient + Diffuse + Specular + Emissive);
  }
  
  void main(void)
  {
      vec4 TexColor = texture(Tex1, TexCoords);
      vec3 LightIntensity = vec3(0.0f);
      vec3 position = vec3(0.0f);
  
      if (spotCount == 1) { //Here a stupid condition proving that spotCount = 1
  
      for (int idx = 0; idx &lt; spotCount; idx++)
      {
          vec3 tnorm = (gl_FrontFacing ? -normalize(Normal) : normalize(Normal));
          if (LightInfos[idx].Position.w == 0.0f)
              position = normalize(vec3(LightInfos[idx].Position));
          else if (LightInfos[idx].Position.w == 1.0f)
          {
              position = normalize(vec3(LightInfos[idx].Position) - Position);
          }
          LightIntensity += ADS_Phong_Shading(idx, -tnorm, position);
      }
  
      }
  
      if (TexColor != 0)
          FragColor = vec4(LightIntensity, 1.0f) * TexColor;
      else
          FragColor = vec4(LightIntensity, 1.0f);
  }
  </code></pre>
  
  <p>In the fragment shader code if I replace the code :</p>
  
  <pre><code>for (int idx = 0; idx &lt; spotCount; idx++)
  </code></pre>
  
  <p>By the following code :</p>
  
  <pre><code>for (int idx = 0; idx &lt; 1; idx++)
  </code></pre>
  
  <p>my scene is correctly displayed.</p>
  
  <p>Here's a picture if I use the instruction : for (int idx = 0; idx &lt; spotCount; idx++) (NOT CORRECT) -> spotCount == 1 :</p>
  
  <p><img src="https://i.stack.imgur.com/vZ1bF.png" alt="enter image description here"></p>
  
  <p>And with the instruction for (int idx = 0; idx &lt; 1; idx++) (CORRECT) :</p>
  
  <p><img src="https://i.stack.imgur.com/zcCFD.png" alt="enter image description here"></p>
  
  <p>In conclusion I have the same number of iteration for the two cases but not the same result.</p>
  
  <p>Does anyone can help me ? Thanks a lot in advance for your help.</p>