title: OpenGL 2.0 rendering multiple objects with the same shader
body: <p>I would like to know how to use the same shader for multiple objects but allow them objects to have a different colour</p>
  
  <p>I have many cubes on the screen which all currently load the same shader, the only difference is when it is draw, I change the cubes colour.  If I set the same _program for all of them, they are become all the same colour.</p>
  
  <pre><code>- (void)draw:(float)eyeOffset
  {
      // Calculate the per-eye model view matrix:
      GLKMatrix4 temp = GLKMatrix4MakeTranslation(eyeOffset, 0.0f, 0.0f);
      GLKMatrix4 eyeBaseModelViewMatrix = GLKMatrix4Multiply(temp, self.baseModelViewMatrix);
  
      if (self.isTransparant)
      {
          glEnable (GL_BLEND);
          glDisable(GL_CULL_FACE);
          //glDisable(GL_DEPTH_TEST);
          glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      }
  
      if (self.textureInfo)
      {
          glBindTexture(self.textureInfo.target, self.textureInfo.name);
      }
  
      glBindVertexArrayOES(_vertexArray);
  
  //See if we are sharing a program shader
      if (self.tprogram)
      {
          glUseProgram(self.tprogram);
      }
      else
      {
          glUseProgram(_program);
      }
  
      self.modelViewMatrix = GLKMatrix4MakeTranslation(self.position.x,self.position.y, self.position.z );//(float)x, (float)y, -1.5f)
      self.modelViewMatrix = GLKMatrix4Scale(self.modelViewMatrix, self.scale.x, self.scale.y, self.scale.z);
  
      //rotation +=0.01;
      self.modelViewMatrix = GLKMatrix4Rotate(self.modelViewMatrix,self.spinRotation, 0.0 ,0.0 ,1.0);
  
      self.modelViewMatrix = GLKMatrix4Multiply(eyeBaseModelViewMatrix, self.modelViewMatrix);
  
      GLKMatrix3 normalMatrix = GLKMatrix3InvertAndTranspose(GLKMatrix4GetMatrix3(self.modelViewMatrix), NULL);
      GLKMatrix4 modelViewProjectionMatrix = GLKMatrix4Multiply(self.projectionMatrix, self.modelViewMatrix);
  
      glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, modelViewProjectionMatrix.m);
      glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, normalMatrix.m);
  
      _colorSlot = glGetUniformLocation(_program, "color");
      GLfloat color[] = {
          self.color.x, self.color.y, self.color.z, self.color.a};
      glUniform4fv(_colorSlot, 1, color);
  
      glDrawArrays(GL_TRIANGLES, 0, 36);
  
      if (self.isTransparant)
      {
           glEnable(GL_CULL_FACE);
           //glEnable(GL_DEPTH_TEST);
           glDisable(GL_BLEND);
      }
  }
  </code></pre>
  
  <p>//setup for each cube</p>
  
  <pre><code>- (void)setup;
  {
  
      glGenVertexArraysOES(1, &amp;_vertexArray);
      glBindVertexArrayOES(_vertexArray);
  
      glGenBuffers(1, &amp;_vertexBuffer);
      glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
      glBufferData(GL_ARRAY_BUFFER, sizeof(gCubeVertexData), gCubeVertexData, GL_STATIC_DRAW);
  
      glEnableVertexAttribArray(GLKVertexAttribPosition);
      glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(0));
      glEnableVertexAttribArray(GLKVertexAttribNormal);
      glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(12));
      glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
      glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(24));
  
      glBindVertexArrayOES(0);
  
  }
  </code></pre>
  
  <p>Shader</p>
  
  <pre><code>attribute vec4 position;
  attribute vec3 normal;
  uniform vec4 color;
  
  varying lowp vec4 colorVarying;
  
  uniform mat4 modelViewProjectionMatrix;
  uniform mat3 normalMatrix;
  
  void main()
  {
      //vec4 diffuseColor = color;
  
      vec3 eyeNormal = normalize(normalMatrix * normal);
      vec3 lightPosition = vec3(0.0, 0.0, 1.0);
      //diffuseColor = vec4(0.4, 0.4, 1.0, 1.0);
  
      float nDotVP = max(0.7, dot(eyeNormal, normalize(lightPosition))); // 0.0
  
      colorVarying = color * nDotVP;
  
      gl_Position = modelViewProjectionMatrix * position;
  }
  </code></pre>
  
  <p>I thought uniform vec4 color; allowed me to change the colour at anytime and if every object has a shader, it works fine, I can change object colours on the fly</p>