import Kernel from './Kernel' ; const maxBufferSize = 200 ; const buffers = [ ] ; export default class VertexBufferObject { buffer : WebGLBuffer ; constructor ( public target ) { if ( buffers . length > 0 ) { this . buffer = buffers . pop ( ) ; } else { this . buffer = Kernel . gl . createBuffer ( ) ; } } bind ( ) { Kernel . gl . bindBuffer ( this . target , this . buffer ) ; } bufferData ( data , usage , hasBinded = false ) { if ( ! hasBinded ) { this . bind ( ) ; } var gl = Kernel . gl ; if ( this . target === Kernel . gl . ARRAY_BUFFER ) { gl . bufferData ( Kernel . gl . ARRAY_BUFFER , new Float32Array ( data ) , usage ) ; } else if ( this . target === Kernel . gl . ELEMENT_ARRAY_BUFFER ) { gl . bufferData ( Kernel . gl . ELEMENT_ARRAY_BUFFER , new Uint16Array ( data ) , usage ) ; } } destroy ( ) { if ( this . buffer ) { if ( buffers . length < maxBufferSize ) { buffers . push ( this . buffer ) ; } else { Kernel . gl . deleteBuffer ( this . buffer ) ; } } this . buffer = null ; } } ;