title: Fast dynamic vertices in OpenGL ES 2.0 on Android
body: <p>I am trying to batch draw a bunch of lines on Android using OpenGL ES 2.0 and I need to know the best way to do this.</p>
  
  <p>Right now I made a class called LineEngine which builds up a FloatBuffer of all the vertices to draw and then draws all the lines at once. The problem is that apparently FloatBuffer.put() is very slow and is gobbling up CPU time like crazy.</p>
  
  <p>Here is my class</p>
  
  <pre><code>public class LineEngine {
      private static final float[] IDENTIY = new float[16];
      private FloatBuffer mLinePoints;
      private FloatBuffer mLineColors;
      private int mCount;
  
      public LineEngine(int maxLines) {
          Matrix.setIdentityM(IDENTIY, 0);
  
          ByteBuffer byteBuf = ByteBuffer.allocateDirect(maxLines * 2 * 4 * 4);
          byteBuf.order(ByteOrder.nativeOrder());
          mLinePoints = byteBuf.asFloatBuffer();
  
          byteBuf = ByteBuffer.allocateDirect(maxLines * 2 * 4 * 4);
          byteBuf.order(ByteOrder.nativeOrder());
          mLineColors = byteBuf.asFloatBuffer();
  
          reset();
      }
  
      public void addLine(float[] position, float[] color){
          mLinePoints.put(position, 0, 8); //These lines
          mLineColors.put(color, 0, 4); // are taking
          mLineColors.put(color, 0, 4); // the longest!
          mCount++;
      }
  
      public void reset(){
          mLinePoints.position(0);
          mLineColors.position(0);
          mCount = 0;
      }
  
      public void draw(){
          mLinePoints.position(0);
          mLineColors.position(0);
          GraphicsEngine.setMMatrix(IDENTIY);
          GraphicsEngine.setColors(mLineColors);
          GraphicsEngine.setVertices4d(mLinePoints);
          GraphicsEngine.disableTexture();
          GLES20.glDrawArrays(GLES20.GL_LINES, 0, mCount * 2);
          GraphicsEngine.disableColors();
          reset();
      }
  }
  </code></pre>
  
  <p>Is there a better way to batch all these lines together?</p>