title: OpenGL ES 2.0 1 only using last bound texture
body: <p>Please see Edit at end for progress.</p>
  
  <p>I'm in the process of trying to learn OpenGL ES 2.0 (I'm going to be developing on Android devices)</p>
  
  <p>I'm a little confused about the Vertex and Fragment shaders.  I understand their purpose, but if I'm building a shape from a custom built class (say a 'point') and setting it's size and colour or applying a texture and assuming that both shaders are declared and defined initially in the object class's constructor, would this mean that each instance of that class would have it's very own pair of shaders?</p>
  
  <p>That is my first question. My second is that if this is the case (shader pairs for each object).........is this the way to go?  I've heard that having one shader pair and switching it's parameters isn't a good idea because of performance, but if I have 100 sprites all of the same size and colour (or texture) does it make sense for them all to have a different pair of shaders with exactly the same parameters?</p>
  
  <p>I hope I'm asking the correct question, I've not been studying ES 2.0 for long so find it a little confusing. I currently only have a limited understanding of OpenGL!</p>
  
  <p>Edit</p>
  
  <p>Adding code as requested.</p>
  
  <pre><code>public class Dot {
  
      int iProgId;
      int iPosition;
      float size = 10;
      FloatBuffer vertexBuf;
      float r = 1f;
      float g = 1f;
      float b = 1f;
      float a = 1f;
      int iBaseMap;
      int texID; 
      Bitmap imgTexture;
  
  
      //Constructor
      public Dot() {
  
          float[] vertices = {
                          0,0,0f              
                          };
  
      //Create vertex shader
      String strVShader =  
                "attribute vec4 a_position;\n"+
                "void main()\n" +
                "{\n" +
                "gl_PointSize = " +size+ ";\n" +
                "gl_Position = a_position;\n"+                 
                "}";
  
      //Create fragment shader
      String strFShader = 
                "precision mediump float;" +
                "void main() " +
                "{" +
                "gl_FragColor = vec4(0,0,0,1);" +
                "}";
                iProgId = Utils.LoadProgram(strVShader, strFShader);
                iPosition = GLES20.glGetAttribLocation(iProgId, "a_position");
  
                    vertexBuf = ByteBuffer.allocateDirect(vertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
  
                    vertexBuf.put(vertices).position(0);
  
  
          }
  </code></pre>
  
  <p>My setTexture method</p>
  
  <pre><code>public void setTexture(GLSurfaceView view, Bitmap imgTexture){
      this.imgTexture=imgTexture;
  
      //Create vertex shader
      String strVShader =  
              "attribute vec4 a_position;\n"+
              "void main()\n" +
              "{\n" +
              "gl_PointSize = " +size+ ";\n" +
              "gl_Position = a_position;\n"+                 
              "}";
  
      //Fragment shader
      String strFShader =
              "precision mediump float;" +
              "uniform sampler2D u_baseMap;" +
              "void main()" +
              "{" +
              "vec4 color;" +
              "color = texture2D(u_baseMap, gl_PointCoord);" +
              "gl_FragColor = color;" +
              "}";
  
      iProgId = Utils.LoadProgram(strVShader, strFShader);
      iBaseMap = GLES20.glGetUniformLocation(iProgId, "u_baseMap");
  
      GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
      GLES20.glUniform1i(iBaseMap, 0);
  
      texID = Utils.LoadTexture(view, imgTexture);  //See code below
  }
  </code></pre>
  
  <p>LoadTexture() method from my <code>Utils</code> class:</p>
  
  <pre><code>public static int LoadTexture(GLSurfaceView view, Bitmap imgTex) {
      int textures[] = new int[1];
      try {
          GLES20.glGenTextures(1, textures, 0);
          GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
          GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_LINEAR);
          GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_LINEAR);
          GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, imgTex, 0);
      } catch
      }
      return textures[0];
  }
  </code></pre>
  
  <p>And finally my Drawing method:</p>
  
  <pre><code>public void drawDot(float x, float y){
  
          float[] vertices = {
                  x,y,0f              
                  };
  
           vertexBuf = ByteBuffer.allocateDirect(vertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
           vertexBuf.put(vertices).position(0);
          GLES20.glUseProgram(iProgId);
          GLES20.glVertexAttribPointer(iPosition, 3, GLES20.GL_FLOAT, false, 0, vertexBuf);
          GLES20.glEnableVertexAttribArray(iPosition);
          GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 1);
      }
  </code></pre>
  
  <p>So I can create stuff like so:</p>
  
  <pre><code>Dot dot1 = new Dot();
  dot1.setSize(40);
  setTexture(myBitmap); //(created earlier with BitmapFactory)
  drawDot(0,0);
  </code></pre>
  
  <p>Thank you!</p>
  
  <p>Edit 1: Thanks for the answer so far. On further research, it seem a few other people have had this exact same problem.  The issue seems to be that I'm not calling glBindTexture in my rendering routine, thus OpenGL is just using the last texture that it loaded, which I guess makes sense.</p>
  
  <p>If I put the following into my Rendering routine:</p>
  
  <pre><code>GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 1);
  </code></pre>
  
  <p>it will apply the first bitmap and display it</p>
  
  <p>if I put:</p>
  
  <pre><code>GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 2);
  </code></pre>
  
  <p>it will apply the second bitmap and display it</p>
  
  <p>So, getting somewhere!  But my question would now be how can I get my rendering method to automatically know which bitmap to use based on which object is calling it (the rendering routine)?</p>
  
  <p>Thanks again</p>