package org.helioviewer.jhv.opengl;

import java.nio.Buffer;

import com.jogamp.opengl.GL2;

class VBO {

    private final int buffer_type;
    private final int attribRef;
    private final int vec_len;

    private int bufferID = -1;

    private VBO(int _buffer_type, int _attribRef, int _vec_len) {
        attribRef = _attribRef;
        buffer_type = _buffer_type;
        vec_len = _vec_len;
    }

    static VBO gen_index_VBO() {
        return new VBO(GL2.GL_ELEMENT_ARRAY_BUFFER, -1, -1);
    }

    static VBO gen_float_VBO(int _attribRef, int _vec_len) {
        return new VBO(GL2.GL_ARRAY_BUFFER, _attribRef, _vec_len);
    }

    void bindArray(GL2 gl) {
        gl.glBindBuffer(buffer_type, bufferID);
        if (attribRef != -1) {
            gl.glVertexAttribPointer(attribRef, vec_len, GL2.GL_FLOAT, false, 0, 0);
            gl.glEnableVertexAttribArray(attribRef);
        }
    }

    void unbindArray(GL2 gl) {
        if (attribRef != -1)
            gl.glDisableVertexAttribArray(attribRef);
        gl.glBindBuffer(buffer_type, 0);
    }

    void init(GL2 gl) {
        bufferID = generate(gl);
    }

    void dispose(GL2 gl) {
        gl.glDeleteBuffers(1, new int[] { bufferID }, 0);
        bufferID = -1;
    }

    void bindBufferData4(GL2 gl, Buffer buffer) {
        gl.glBindBuffer(buffer_type, bufferID);
        gl.glBufferData(buffer_type, 4 * buffer.limit(), buffer, GL2.GL_STATIC_DRAW);
        gl.glBindBuffer(buffer_type, 0);
    }

    private static int generate(GL2 gl) {
        int[] tmpId = new int[1];
        gl.glGenBuffers(1, tmpId, 0);
        return tmpId[0];
    }

}

@@ -123,26 +123,25 @@
 l int buffer
-_t
+T
 ype;%0A    pri
@@ -188,23 +188,29 @@
 nal int 
-vec_len
+numComponents
 ;%0A%0A    p
@@ -234,16 +234,51 @@
 ID = -1;
+%0A    private int bufferLength = -1;
 %0A%0A    pr
@@ -294,26 +294,25 @@
 (int _buffer
-_t
+T
 ype, int _at
@@ -317,39 +317,45 @@
 attribRef, int _
-vec_len
+numComponents
 ) %7B%0A        attr
@@ -388,18 +388,17 @@
   buffer
-_t
+T
 ype = _b
@@ -402,18 +402,17 @@
  _buffer
-_t
+T
 ype;%0A   
@@ -420,26 +420,38 @@
     
-vec_len = _vec_len
+numComponents = _numComponents
 ;%0A  
@@ -606,23 +606,29 @@
 f, int _
-vec_len
+numComponents
 ) %7B%0A    
@@ -680,23 +680,29 @@
 ibRef, _
-vec_len
+numComponents
 );%0A    %7D
@@ -754,34 +754,33 @@
 indBuffer(buffer
-_t
+T
 ype, bufferID);%0A
@@ -827,16 +827,22 @@
    gl.gl
+Enable
 VertexAt
@@ -849,15 +849,13 @@
 trib
-Pointer
+Array
 (att
@@ -864,44 +864,8 @@
 bRef
-, vec_len, GL2.GL_FLOAT, false, 0, 0
 );%0A 
@@ -872,38 +872,32 @@
            gl.gl
-Enable
 VertexAttribArra
@@ -884,37 +884,39 @@
 l.glVertexAttrib
-Array
+Pointer
 (attribRef);%0A   
@@ -901,32 +901,74 @@
 ointer(attribRef
+, numComponents, GL2.GL_FLOAT, false, 0, 0
 );%0A        %7D%0A   
@@ -1089,49 +1089,8 @@
 f);%0A
-        gl.glBindBuffer(buffer_type, 0);%0A
     
@@ -1260,24 +1260,51 @@
 ferID = -1;%0A
+        bufferLength = -1;%0A
     %7D%0A%0A    v
@@ -1374,26 +1374,25 @@
 uffer(buffer
-_t
+T
 ype, bufferI
@@ -1407,63 +1407,166 @@
     
-gl.glBufferData(buffer_type, 4 * buffer.limit(), buffer
+int length = 4 * buffer.limit();%0A        if (length != bufferLength) %7B%0A            bufferLength = length;%0A            gl.glBufferData(bufferType, length, null
 , GL
@@ -1584,16 +1584,26 @@
 _DRAW);%0A
+        %7D%0A
         
@@ -1612,32 +1612,50 @@
 .glB
-indB
 uffer
+SubData
 (buffer
-_t
+T
 ype, 0
+, length, buffer
 );%0A 

Update buffers