Webgl buffer / add utility getSize function

Saves checking whether the buffer was initialized with
an array already
This commit is contained in:
Olivier Guyot
2019-08-30 10:38:02 +02:00
parent e57e660819
commit c48350b666
3 changed files with 28 additions and 2 deletions

View File

@@ -344,7 +344,7 @@ class WebGLPointsLayerRenderer extends WebGLLayerRenderer {
* @inheritDoc
*/
renderFrame(frameState) {
const renderCount = this.indicesBuffer_.getArray() ? this.indicesBuffer_.getArray().length : 0;
const renderCount = this.indicesBuffer_.getSize();
this.helper.drawElements(0, renderCount);
this.helper.finalizeDraw(frameState);
const canvas = this.helper.getCanvas();
@@ -561,7 +561,7 @@ class WebGLPointsLayerRenderer extends WebGLLayerRenderer {
this.helper.enableAttributeArray(DefaultAttrib.ROTATE_WITH_VIEW, 1, FLOAT, bytesPerFloat * stride, bytesPerFloat * 7);
this.helper.enableAttributeArray(DefaultAttrib.COLOR, 4, FLOAT, bytesPerFloat * stride, bytesPerFloat * 8);
const renderCount = this.indicesBuffer_.getArray() ? this.indicesBuffer_.getArray().length : 0;
const renderCount = this.indicesBuffer_.getSize();
this.helper.drawElements(0, renderCount);
}
}

View File

@@ -95,6 +95,7 @@ class WebGLArrayBuffer {
}
/**
* Will return null if the buffer was not initialized
* @return {Float32Array|Uint32Array} Array.
*/
getArray() {
@@ -107,6 +108,14 @@ class WebGLArrayBuffer {
getUsage() {
return this.usage;
}
/**
* Will return 0 if the buffer is not initialized
* @return {number} Array size
*/
getSize() {
return this.array ? this.array.length : 0;
}
}
/**

View File

@@ -75,4 +75,21 @@ describe('ol.webgl.Buffer', function() {
});
describe('#getSize', function() {
let b;
beforeEach(function() {
b = new WebGLArrayBuffer(ARRAY_BUFFER);
});
it('returns 0 when the buffer array is not initialized', function() {
expect(b.getSize()).to.be(0);
});
it('returns the size of the array otherwise', function() {
b.ofSize(12);
expect(b.getSize()).to.be(12);
});
});
});