diff options
-rw-r--r-- | o3d/samples/o3d-webgl/buffer.js | 55 | ||||
-rw-r--r-- | o3d/samples/o3d-webgl/field.js | 10 |
2 files changed, 38 insertions, 27 deletions
diff --git a/o3d/samples/o3d-webgl/buffer.js b/o3d/samples/o3d-webgl/buffer.js index 8ef19e3..bf8d572 100644 --- a/o3d/samples/o3d-webgl/buffer.js +++ b/o3d/samples/o3d-webgl/buffer.js @@ -71,7 +71,7 @@ o3d.Buffer.prototype.ArrayType = WebGLFloatArray; /** * Allocates memory for the data to be stored in the buffer based on * the types of fields set on the buffer. - * + * * @param {number} numElements Number of elements to allocate.. * @return {boolean} True if operation was successful. */ @@ -100,11 +100,11 @@ o3d.Buffer.prototype.resize = function(numElements) { /** * Defines a field on this buffer. - * + * * Note: Creating a field after having allocated the buffer is an expensive * operation as the data currently in the buffer has to be shuffled around * to make room for the new field. - * + * * @param {string} field_type type of data in the field. Valid types * are "FloatField", "UInt32Field", and "UByteNField". * @param {number} num_components number of components in the field. @@ -123,11 +123,11 @@ o3d.Buffer.prototype.createField = /** * Removes a field from this buffer. - * + * * Note: Removing a field after having allocated the buffer is an expensive * operation as the data currently in the buffer has to be shuffled around * to remove the old field. - * + * * @param {!o3d.Field} field field to remove. */ o3d.Buffer.prototype.removeField = @@ -146,6 +146,26 @@ o3d.Buffer.prototype.removeField = /** + * Helper function for buffer's and field's getAt functions. Gets elements in + * the buffer as an array. + * @param {number} start_index Index of the first element value to get. + * @param {number} num_elements the number of elements to get. + * @return {!Array.<number>} An array of values. + */ +o3d.Buffer.prototype.getAtHelper_ = + function(start_index, num_elements, offset, num_components) { + var values = []; + for (var i = 0; i < num_elements; ++i) { + for (var c = 0; c < num_components; ++c) { + values.push(this.array_[(start_index + i) * + this.totalComponents + offset + c]); + } + } + return values; +}; + + +/** * Prepares the buffer for read/write. */ o3d.Buffer.prototype.lock = function() { @@ -164,14 +184,16 @@ o3d.Buffer.prototype.unlock = function() { /** * Sets the values in the buffer given array. - * TODO(petersont): This should take other kinds of arguments, like RawData. - * + * * @param {!Array.<number>} values contains data to assign to the Buffer * data itself. * @return {boolean} True if operation was successful. */ o3d.Buffer.prototype.set = function(values) { + if (!values.length) { + o3d.notImplemented(); + } if (this.array_ == null) { this.resize(values.length); } @@ -184,13 +206,6 @@ o3d.Buffer.prototype.set = /** - * The total components in all fields in this buffer. - * @type {number} - */ -o3d.Buffer.prototype.total_components = 0; - - -/** * VertexBufferBase is a the base class for both VertexBuffer and SourceBuffer * @constructor */ @@ -205,20 +220,22 @@ o3d.inherit('VertexBufferBase', 'Buffer'); * Modifying this copy has no effect on the buffer. */ o3d.VertexBufferBase.prototype.get = function() { - o3d.notImplemented(); + return this.getAtHelper_(0, this.array_.length / this.totalComponents, + 0, this.totalComponents); }; /** * Gets a copy of a sub range of the values in the data stored in the buffer. * Modifying this copy has no effect on the buffer. - * + * * @param {number} start_index index of the element value to get. - * @param {number} numElements the number of elements to get. + * @param {number} num_elements the number of elements to get. * @return {!Array.<number>} An array of values. */ o3d.VertexBufferBase.prototype.getAt = - function(start_index, numElements) { + function(start_index, num_elements) { + return this.getAtHelper_(start_index, num_elements, 0, this.totalComponents); }; @@ -243,7 +260,7 @@ o3d.VertexBuffer.prototype.className = "o3d.VertexBuffer"; /** * SourceBuffer is a Buffer object used for storing vertex data for * geometry. (e.g. vertex positions, normals, colors, etc). - * + * * A SourceBuffer is the source for operations like skinning and morph * targets. It can not be directly rendered by the GPU. * @constructor diff --git a/o3d/samples/o3d-webgl/field.js b/o3d/samples/o3d-webgl/field.js index 1ae89e4..9e29569 100644 --- a/o3d/samples/o3d-webgl/field.js +++ b/o3d/samples/o3d-webgl/field.js @@ -108,14 +108,8 @@ o3d.Field.prototype.setAt = */ o3d.Field.prototype.getAt = function(start_index, num_elements) { - var values = []; - for (var i = 0; i < num_elements; ++i) { - for (var c = 0; c < this.numComponents; ++c) { - values.push(this.buffer.array_[(start_index + i) * - this.buffer.totalComponents + this.offset_ + c]); - } - } - return values; + return this.buffer.getAtHelper_(start_index, num_elements, this.offset_, + this.numComponents); }; |