Skip to content

Commit

Permalink
IndexBuffer's integer type can be change on the fly - FOR REAL
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Lepers authored and Pierre Lepers committed Apr 16, 2016
1 parent 10a2391 commit daee8ac
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions indexbuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ var TGT = 0x8893;
function IndexBuffer( gl, type, data, usage ){
this.gl = gl;
this.buffer = gl.createBuffer();
this.type = type || gl.UNSIGNED_SHORT;
this.usage = usage || gl.STATIC_DRAW;
this.typeSize = BufferUtils.getComponentSize( this.type );
this.length = 0;
this.type = 0;
this.typeSize = 0;
this.size = 0;

this.setType( type || gl.UNSIGNED_SHORT );

if( data ){
this.data( data );
Expand All @@ -36,6 +38,15 @@ IndexBuffer.prototype = {
this.gl.bindBuffer( TGT, this.buffer );
},

/**
* Change the type of internal type of the IndexBuffer
* @param {GLenum} type the integer type of the indices (GL_UNSIGNED_BYTE, GL_UNSIGNED_INT etc)
*/
setType: function( type ){
this.type = type;
this.typeSize = BufferUtils.getComponentSize( type );
},

/**
* Fill webgl buffer with the given data. You can also pass a uint to allocate the buffer to the given size.
* @param {TypedArray|uint} array the data to send to the buffer, or a size.
Expand All @@ -45,8 +56,7 @@ IndexBuffer.prototype = {
gl.bindBuffer( TGT, this.buffer );
gl.bufferData( TGT, array, this.usage );
gl.bindBuffer( TGT, null );
var bl = ( array.byteLength === undefined ) ? array : array.byteLength;
this.length = bl / this.typeSize;
this.size = ( array.byteLength === undefined ) ? array : array.byteLength;
},

/**
Expand Down Expand Up @@ -78,7 +88,7 @@ IndexBuffer.prototype = {
* @param {uint} [offset=0] the position of the first index to draw
*/
draw: function( mode, count, offset ){
count = ( count === undefined ) ? this.length : count;
count = ( count === undefined ) ? this.size/this.typeSize : count;
this.gl.drawElements( mode, count, this.type, 0|offset );
}

Expand Down

0 comments on commit daee8ac

Please sign in to comment.