Skip to content

Commit

Permalink
VertexBuffer now correctly infers count for most argument types.
Browse files Browse the repository at this point in the history
  • Loading branch information
kbirk committed Jan 19, 2016
1 parent d381868 commit b743898
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 76 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esper",
"version": "0.6.7",
"version": "0.6.8",
"homepage": "https://github.com/kbirk/esper",
"authors": [
"kbirk <birk.kevin@gmail.com>"
Expand Down
99 changes: 62 additions & 37 deletions build/esper.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/esper.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esper",
"version": "0.6.7",
"version": "0.6.8",
"description": "A low-level WebGL rendering framework",
"author": "Kevin Birk <birk.kevin@gmail.com>",
"main": "./src/exports.js",
Expand Down
38 changes: 20 additions & 18 deletions src/core/IndexBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
* @class IndexBuffer
* @classdesc An index buffer object.
*/
function IndexBuffer( array, options ) {
function IndexBuffer( arg, options ) {
options = options || {};
this.gl = WebGLContext.get();
this.buffer = 0;
if ( array ) {
if ( array instanceof WebGLBuffer ) {
if ( arg ) {
if ( arg instanceof WebGLBuffer ) {
// if the argument is already a webglbuffer, simply wrap it
this.buffer = array;
this.buffer = arg;
this.type = options.type || "UNSIGNED_SHORT";
this.count = ( options.count !== undefined ) ? options.count : 0;
} else {
// otherwise, buffer it
this.bufferData( array );
this.bufferData( arg );
}
}
this.offset = ( options.offset !== undefined ) ? options.offset : 0;
Expand All @@ -33,48 +33,50 @@
* Upload index data to the GPU.
* @memberof IndexBuffer
*
* @param {Array|Uint16Array|Uint32Array} array - The array of data to buffer.
* @param {Array|Uint16Array|Uint32Array} arg - The array of data to buffer.
*
* @returns {IndexBuffer} The index buffer object for chaining.
*/
IndexBuffer.prototype.bufferData = function( array ) {
IndexBuffer.prototype.bufferData = function( arg ) {
var gl = this.gl;
// check for type support
var uint32support = WebGLContext.checkExtension( "OES_element_index_uint" );
if( !uint32support ) {
// no support for uint32
if ( array instanceof Array ) {
if ( arg instanceof Array ) {
// if array, buffer to uint16
array = new Uint16Array( array );
} else if ( array instanceof Uint32Array ) {
arg = new Uint16Array( arg );
} else if ( arg instanceof Uint32Array ) {
// if uint32, downgrade to uint16
console.warn( "Cannot create IndexBuffer of format " +
"gl.UNSIGNED_INT as OES_element_index_uint is not " +
"supported, defaulting to gl.UNSIGNED_SHORT." );
array = new Uint16Array( array );
arg = new Uint16Array( arg );
}
} else {
// uint32 is supported
if ( array instanceof Array ) {
if ( arg instanceof Array ) {
// if array, buffer to uint32
array = new Uint32Array( array );
arg = new Uint32Array( arg );
}
}
// set data type based on array
if ( array instanceof Uint16Array ) {
if ( arg instanceof Uint16Array ) {
this.type = "UNSIGNED_SHORT";
} else if ( array instanceof Uint32Array ) {
} else if ( arg instanceof Uint32Array ) {
this.type = "UNSIGNED_INT";
} else {
console.error( "IndexBuffer requires an Array or " +
"ArrayBuffer argument, command ignored." );
return;
}
// create buffer, store count
this.buffer = gl.createBuffer();
this.count = array.length;
if ( !this.buffer ) {
this.buffer = gl.createBuffer();
}
this.count = arg.length;
gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, this.buffer );
gl.bufferData( gl.ELEMENT_ARRAY_BUFFER, array, gl.STATIC_DRAW );
gl.bufferData( gl.ELEMENT_ARRAY_BUFFER, arg, gl.STATIC_DRAW );
return this;
};

Expand Down
59 changes: 41 additions & 18 deletions src/core/VertexBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,51 +65,74 @@
return pointers;
}

function VertexBuffer( array, attributePointers, options ) {
function getNumComponents(pointers) {
var size = 0;
var index;
for ( index in pointers ) {
if ( pointers.hasOwnProperty( index ) ) {
size += pointers[ index ].size;
}
}
return size;
}

function VertexBuffer( arg, attributePointers, options ) {
options = options || {};
this.buffer = 0;
this.gl = WebGLContext.get();
if ( array ) {
if ( array instanceof VertexPackage ) {
// first, set the attribute pointers
if ( arg instanceof VertexPackage ) {
// VertexPackage argument, use its attribute pointers
this.pointers = arg.attributePointers();
// shift options arg since there will be no attrib pointers arg
options = attributePointers || {};
} else {
this.pointers = getAttributePointers( attributePointers );
}
// then buffer the data
if ( arg ) {
if ( arg instanceof VertexPackage ) {
// VertexPackage argument
this.bufferData( array.buffer() );
// shift arg since there will be no attrib pointers
options = attributePointers || {};
// get attribute pointers from vertex package
attributePointers = array.attributePointers();
} else if ( array instanceof WebGLBuffer ) {
this.bufferData( arg.buffer() );
} else if ( arg instanceof WebGLBuffer ) {
// WebGLBuffer argument
this.buffer = array;
this.buffer = arg;
this.count = ( options.count !== undefined ) ? options.count : 0;
} else {
// Array or ArrayBuffer or number argument
this.bufferData( array );
this.bufferData( arg );
}
}
// set attribute pointers
this.pointers = getAttributePointers( attributePointers );
// set stride
this.stride = getStride( this.pointers );
// set draw offset and mode
this.offset = ( options.offset !== undefined ) ? options.offset : 0;
this.mode = ( options.mode !== undefined ) ? options.mode : "TRIANGLES";
}

VertexBuffer.prototype.bufferData = function( array ) {
VertexBuffer.prototype.bufferData = function( arg ) {
var gl = this.gl;
if ( array instanceof Array ) {
if ( arg instanceof Array ) {
// cast arrays into bufferview
array = new Float32Array( array );
} else if ( !Util.isTypedArray( array ) && typeof array !== "number" ) {
arg = new Float32Array( arg );
} else if ( !Util.isTypedArray( arg ) && typeof arg !== "number" ) {
console.error( "VertexBuffer requires an Array or ArrayBuffer, " +
"or a size argument, command ignored." );
return;
}
if ( !this.buffer ) {
this.buffer = gl.createBuffer();
}
// get the total number of attribute components from pointers
var numComponents = getNumComponents(this.pointers);
// set count based on size of buffer and number of components
if (typeof arg === "number") {
this.count = arg / numComponents;
} else {
this.count = arg.length / numComponents;
}
gl.bindBuffer( gl.ARRAY_BUFFER, this.buffer );
gl.bufferData( gl.ARRAY_BUFFER, array, gl.STATIC_DRAW );
gl.bufferData( gl.ARRAY_BUFFER, arg, gl.STATIC_DRAW );
};

VertexBuffer.prototype.bufferSubData = function( array, offset ) {
Expand Down

0 comments on commit b743898

Please sign in to comment.