Index and vertex buffer allocators

This commit is contained in:
Felipe Alfonso 2017-01-16 21:11:47 -03:00
parent a03d1a571c
commit 0df61f3057
2 changed files with 90 additions and 0 deletions

View file

@ -0,0 +1,42 @@
var IndexBuffer = function (byteSize)
{
this.wordLength = 0;
this.wordCapacity = byteSize / 2;
this.buffer = new ArrayBuffer(byteSize);
this.shortView = new Int16Array(this.buffer);
this.wordView = new Uint16Array(this.buffer);
};
IndexBuffer.prototype.clear = function ()
{
this.wordLength = 0;
};
IndexBuffer.prototype.getByteLength = function ()
{
return this.wordLength * 2;
};
IndexBuffer.prototype.getByteCapacity = function ()
{
return this.buffer.byteLength;
};
IndexBuffer.prototype.allocate = function (wordSize)
{
var currentLength = this.wordLength;
this.wordLength += wordSize;
return currentLength;
};
IndexBuffer.prototype.getUsedBufferAsShort = function ()
{
return this.shortView.subarray(0, this.dwordLength);
};
IndexBuffer.prototype.getUsedBufferAsWord = function ()
{
return this.wordView.subarray(0, this.dwordLength);
};
module.exports = IndexBuffer;

View file

@ -0,0 +1,48 @@
var VertexBuffer = function (byteSize)
{
this.dwordLength = 0;
this.dwordCapacity = byteSize / 4;
this.buffer = new ArrayBuffer(byteSize);
this.floatView = new Float32Array(this.buffer);
this.intView = new Int32Array(this.buffer);
this.uintView = new Uint32Array(this.buffer);
};
VertexBuffer.prototype.clear = function ()
{
this.dwordLength = 0;
};
VertexBuffer.prototype.getByteLength = function ()
{
return this.dwordLength * 4;
};
VertexBuffer.prototype.getByteCapacity = function ()
{
return this.buffer.byteLength;
};
VertexBuffer.prototype.allocate = function (dwordSize)
{
var currentLength = this.dwordLength;
this.dwordLength += dwordSize;
return currentLength;
};
VertexBuffer.prototype.getUsedBufferAsFloat = function ()
{
return this.floatView.subarray(0, this.dwordLength);
};
VertexBuffer.prototype.getUsedBufferAsInt = function ()
{
return this.intView.subarray(0, this.dwordLength);
};
VertexBuffer.prototype.getUsedBufferAsUint = function ()
{
return this.uintView.subarray(0, this.dwordLength);
};
module.exports = VertexBuffer;