title: opengl particle engine best practice?
body: <p>I'm developing a simple game and I am currently switching from canvas to openGL. So my openGL experience is very limited.
  I'm trying to create a simple particle engine at thee moment and it works fine BUT with my current implementation I have to run this line on every draw() call [ vertexBuffer.put(vertices); ] and that is really slow..  Is there any way to get around this ? </p>
  
  <p>code looks like this: </p>
  
  <pre><code>public class ParticlesTest {
  
      private float[] vertices;
      private short[] indices;
  
  
      private FloatBuffer vertexBuffer;
  
  
      private ShortBuffer indexBuffer;
      private ByteBuffer vbb;
  
      private int MAX_PARTICLES = 100;
  
      private ArrayList&lt;Particle&gt; particles;
      public ParticlesTest() {
  
          particles = new ArrayList&lt;Particle&gt;();
  
          vertices = new float[MAX_PARTICLES*6*3];
          indices = new short[MAX_PARTICLES*2];
  
  
          short cnt = 0;
          for(int i = 0; i &lt; 60; i++){
              createParticle();
  
              indices[cnt] =  cnt;
              cnt++;
              indices[cnt] =  cnt;
              cnt++;
  
          }
  
          vbb = ByteBuffer.allocateDirect(vertices.length * 4);
          vbb.order(ByteOrder.nativeOrder());
          vertexBuffer = vbb.asFloatBuffer();
          vertexBuffer.put(vertices);
          vertexBuffer.position(0);
  
          ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
          ibb.order(ByteOrder.nativeOrder());
          indexBuffer = ibb.asShortBuffer();
          indexBuffer.put(indices);
          indexBuffer.position(0);
  
  
  
      }
  
      private void createParticle() {
          Particle particle = new Particle();
          particle.setSpeedX((float)(Math.random() * 20 - 10));
          particle.setSpeedY((float)(Math.random() * 20 - 10));
          particle.setX(100);
          particle.setY(100);
  
          particles.add(particle);
  
  
      }
      private void update(){
  
          int len = particles.size();
  
          for(int i = 0; i &lt; len; i++){
              Particle particle = particles.get(i);
  
              float oldX = particle.getX();
              float oldY = particle.getY();
              float speedX = particle.getSpeedX();
              float speedY = particle.getSpeedY();
  
              oldX += speedX;
              oldY += speedY;
  
              particle.setX(oldX);
              particle.setY(oldY);
  
              if(oldX &lt; 0 || oldX &gt; 400 || oldY &lt; 0 || oldY &gt; 600){
                  particles.remove(i);
                  len--;
              }
          }
  
          if(len &lt; MAX_PARTICLES){
              createParticle();
          }
      }
  
  
      private void translate(){
  
  
          int vcount = 0;
          short icount = 0;
  
          int clen = vertices.length;
  
          for(int c = 0; c &lt; clen; c++){
              vertices[c] = 0f;
          }
  
  
          int len = particles.size();
  
          for(int i = 0; i &lt; len; i++){
              Particle particle = particles.get(i);
  
              float oldX = particle.getX();
              float oldY = particle.getY();
  
              float speedX = particle.getSpeedX();
              float speedY = particle.getSpeedY();
  
              vertices[vcount] =oldX;
              vcount++;
              vertices[vcount] = oldY;
              vcount++;
              vertices[vcount] = 0f;
              vcount++;
  
              vertices[vcount] =oldX + speedX;
              vcount++;
              vertices[vcount] = oldY + speedY;
              vcount++;
              vertices[vcount] = 0f;
              vcount++;
  
              indices[icount] = icount;
              icount++;
              indices[icount] = icount;
              icount++;
  
          }
      }
  
  
      public void draw(GL10 gl,int w,int h) {
  
          update();
          translate();
  
          vertexBuffer.put(vertices);
  
          vertexBuffer.position(0);
  
          //GLFPS.VERTS = vertices.length;
  
  
          // rendering.
          gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
          gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);      
  //      gl.glDrawElements(GL10.GL_LINE_STRIP, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
          gl.glDrawElements(GL10.GL_LINES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
  
  
  
          // Disable the vertices buffer.
          gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
      }
  }
  </code></pre>