title: Why must I "use" a shader program before I can set its uniforms?
body: <p>I am using OpenGL 4.5 in C++ and GLSL 410.</p>
  
  <p>I came across a problem recently where I was attempting to set a shader program's uniform variable without first using the shader program with <code>glUseProgram</code>. I had a fragment shader with a uniform variable called <code>input_color</code> (a vec4 which I would set the output color to) attached to my <code>shader_program</code>. Please note I <strong>do</strong> call <code>glUseProgram(shader_program)</code> in my main draw loop before drawing anything that should use it.</p>
  
  <pre><code>// set uniforms
  GLint input_color = glGetUniformLocation(shader_program, "input_color");
  glUniform4f(input_color, 0.5f, 0.3f, 0.2f, 1.0f);
  </code></pre>
  
  <p>A triangle drawn with this program was all black, not the result I was expecting. After some experimenting, I realized I needed to use the program before setting its uniforms.</p>
  
  <pre><code>// set uniforms
  glUseProgram(shader_program); // my change
  GLint input_color = glGetUniformLocation(shader_program, "input_color");
  glUniform4f(input_color, 0.5f, 0.3f, 0.2f, 1.0f);
  </code></pre>
  
  <p>Drawing results in a triangle of the correct color.</p>
  
  <p>My question is why did I need to start using the program first? I understand that OpenGL works as a <strong>state machine</strong>, but in this instance why is the current shader program state necessary? After all, I explicitly pass the <code>shader_program</code> index as a parameter to <code>glGetUniformLocation</code> in order to get <code>input_color</code>'s memory index.</p>
  
  <p>I'm hoping the answer to this will help me understand states in OpenGL better, and how blocks of memory representing shader programs on the GPU are utilized or managed.</p>