title: drawing textures in opengl es 2.0
body: <p>I have been having a problem with drawing texture in opengl es 2 on android for about 3 days and finally decided i should help after all my researching lead to dead ends. The problem is the texture is drawn as a black rectangle and not the actually image.</p>
  
  <pre><code>public class Sprite 
  {
      private static final Shader spriteShader = ShaderManager.getInstance().getShader(1);
      private static final short[] drawOrder = { 0, 1, 2, 0, 2, 3 };
      private static ShortBuffer drawListBuffer;
  
      static 
      {
          ByteBuffer bb = ByteBuffer.allocateDirect(drawOrder.length * 2);
          bb.order(ByteOrder.nativeOrder());
  
          drawListBuffer = bb.asShortBuffer();
          drawListBuffer.put(drawOrder);
          drawListBuffer.position(0);     
      }
  
      private Context m_context;
      private int m_textureId = 0;
      private FloatBuffer m_vertexBuffer;
      private float[] m_coords = 
          {
              -0.5f,  0.5f, 0.0f, // Position 0
               0.0f,  0.0f, // TexCoord 0
              -0.5f, -0.5f, 0.0f, // Position 1
               0.0f,  1.0f, // TexCoord 1
               0.5f, -0.5f, 0.0f, // Position 2
               1.0f,  1.0f, // TexCoord 2
               0.5f,  0.5f, 0.0f, // Position 3
               1.0f,  0.0f // TexCoord 3
          };
  
  
      public Sprite(Context context)
      {
          m_context = context;
  
          ByteBuffer bb = ByteBuffer.allocateDirect(m_coords.length * 4);
          bb.order(ByteOrder.nativeOrder());
  
          m_vertexBuffer = bb.asFloatBuffer();
          m_vertexBuffer.put(m_coords);
          m_vertexBuffer.position(0);
      }
  
      public void initialize(String file)
      {
          InputStream inputStream = null;
  
          try 
          {
              inputStream = m_context.getAssets().open(file);
              m_textureId = loadTexture(inputStream);
          } 
          catch (IOException e) 
          {}
          finally
          {
              Util.closeStream(inputStream);
          }
      }
  
      public void draw()
      {
          spriteShader.use();
  
          int position = spriteShader.getAttribLocation("vPosition");
          int textureCoords = spriteShader.getAttribLocation("aTexCoords");
          int texture = spriteShader.getUniformLocation("uTexture");
  
          m_vertexBuffer.position(0);
          GLES20.glVertexAttribPointer(position, 3, GLES20.GL_FLOAT, false, 20, m_vertexBuffer);
          Util.checkGlError("vertex pointer poistion");
          m_vertexBuffer.position(3);
          GLES20.glVertexAttribPointer(textureCoords, 2, GLES20.GL_FLOAT, false, 20, m_vertexBuffer);
          Util.checkGlError("vertex pointer textureCoords");
          GLES20.glEnableVertexAttribArray(position);
          GLES20.glEnableVertexAttribArray(textureCoords);
  
          GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
          Util.checkGlError("active trexture");
          GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, m_textureId);
          Util.checkGlError("bind texture");
  
          GLES20.glUniform1i(texture, 0);
          Util.checkGlError("uniform1i");
          GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
          Util.checkGlError("drawelements");
  
      }
  
      private int loadTexture(InputStream inputStream)
      {
          int[] texture = new int[1];
          GLES20.glGenTextures(1, texture, 0);
  
          GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture[0]); 
  
          GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
          GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
          GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
          GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
  
          Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
          GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
          bitmap.recycle();
  
          return texture[0];
      }
  }
  </code></pre>
  
  <p>vertex shader</p>
  
  <pre><code>attribute vec2 aTexCoords;
  attribute vec4 vPosition;
  
  varying vec2 vTexCoords;
  
  void main()
  {
  vTexCoords = aTexCoords;
  gl_Position = vPosition;
  }
  </code></pre>
  
  <p>fragment shader</p>
  
  <pre><code>precision mediump float;
  
  uniform sampler2D uTexture;
  varying vec2 vTexCoords;
  
  void main()
  {
  gl_FragColor = texture2D(uTexture, vTexCoords);
  }
  </code></pre>