title: Only setting the attribute in wrong shader program affects attribute value
body: <p>I'm running always into problems, as soon as I try to use more than one shader program in OpenGL. For some reason, setting the attribute of my shader2 program in use is not reflecting at all - but setting the attribute in the currently not used shader1, affects rendering in shader2. I'm sure, that my shader2 is used (setting the attribute inside shader to a constant value reflects on objects drawn with shader2), and that I'm setting the attribute value in shader2.</p>
  
  <p>I encapsuled the ShaderPrograms into a class like:</p>
  
  <pre><code>class ShaderProgram{
  public:
  ShaderProgram(const std::string&amp; vertexSource, const std::string&amp; fragmentSource){
    vertexShader = glCreateShader(GL_VERTEX_SHADER);
    const GLchar *source = (const GLchar *)vertexSource.c_str();
    glShaderSource(vertexShader, 1, &amp;source, 0);
    glCompileShader(vertexShader);
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    source = (const GLchar *)fragmentSource.c_str();
    glShaderSource(fragmentShader, 1, &amp;source, 0);
    glCompileShader(fragmentShader);
    program = glCreateProgram();
    glAttachShader(program, vertexShader);
    glAttachShader(program, fragmentShader);
    glLinkProgram(program);
  }
  void use(){
    glUseProgram(program);
  }
  void setAttribPointer(const char* name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void* pointer) {
    GLuint pos = glGetAttribLocation(program, name);
    glEnableVertexAttribArray(pos);
    glVertexAttribPointer(pos, size, type, normalized, stride, pointer);
  }
  void setAttribValue4f(const char* name, float x, float y, float z, float w) {
    GLuint pos = glGetAttribLocation(program, name);
    glDisableVertexAttribArray(pos);
    const GLfloat pointer[] = { x, y, z, w };
    glVertexAttrib4fv(program, pointer);
  }
  ~ShaderProgram() {
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);
    glDeleteProgram(program);
  }
  private:
   GLuint vertexShader, fragmentShader, program;
  };
  </code></pre>
  
  <p>And the renderloop in my application looks like:</p>
  
  <pre><code>program1.use();
  program1.setAttribValue4f("color", 1.0f, 0.0f, 0.0f, 1.0f);
  program1.setAttribPointer("positions", 3, GL_FLOAT, GL_FALSE, 0, vertices.data());
  glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, indices.data());
  program2.use();
  program2.setAttribValue4f("color", 0.0f, 0.0f, 1.0f, 1.0f);
  program2.setAttribPointer("positions", 3, GL_FLOAT, GL_FALSE, 0, vertices.data());
  glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, indices.data());
  </code></pre>
  
  <p>In this case, both objects are drawn in red - while the second should be blue (color attribute is set to blue in second shader program). I can reproduce this problem with any simple GLSL shader. However, if I change the above code to:</p>
  
  <pre><code>program1.use();
  program1.setAttribValue4f("color", 1.0f, 0.0f, 0.0f, 1.0f);
  program1.setAttribPointer("positions", 3, GL_FLOAT, GL_FALSE, 0, vertices.data());
  glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, indices.data());
  program2.use();
  program1.setAttribValue4f("color", 0.0f, 0.0f, 1.0f, 1.0f);
  program2.setAttribPointer("positions", 3, GL_FLOAT, GL_FALSE, 0, vertices.data());
  glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, indices.data());
  </code></pre>
  
  <p>the second object is drawn in blue (which is what I want to accomplish). But whats confussing is, that <code>glVertexAttrib4fv(program, pointer);</code> is called on the wrong program object (which is not used), but color is changed - while it's not changing, if I'm using the correct program object (program2). I checked <code>glGetError()</code> after <code>glUseProgram()</code> and after <code>glGetAttribLocation()</code>/<code>glVertexAttrib4fv()</code> - always returning 0. The location returned by <code>glGetAttribLocation()</code> is also correct.
  GLSL is <code>#version 330</code></p>
  
  <p>Any idea what could cause this problem?</p>