Skip to content

Commit 765e995

Browse files
committed
move remaining BufferHelper methods to BufferAccessor; rename h3du-bufferhelper.js to h3du-bufferaccessor.js; etc.
1 parent 69e0171 commit 765e995

16 files changed

+328
-336
lines changed

demos/bsp.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,43 @@ BspTree._negatePlane = function(plane) {
2525
"use strict";
2626
return [-plane[0], -plane[1], -plane[2], plane[3], plane[4], plane[5]];
2727
};
28+
29+
/** @constructor
30+
* @ignore */
31+
BspTree._MiniBuilder = function() {
32+
"use strict";
33+
this.vertices = [];
34+
this.normals = [];
35+
this.indices = [];
36+
this.addPoly = function(poly) {
37+
var index = this.vertices.length / 3;
38+
for(var i = 0; i < poly.vertices.length; i++) {
39+
var v = poly.vertices[i];
40+
this.normals.push(poly.plane[0], poly.plane[1], poly.plane[2]);
41+
this.vertices.push(v[0], v[1], v[2]);
42+
}
43+
for(i = 0; i < poly.vertices.length - 2; i++) {
44+
this.indices.push(index);
45+
this.indices.push(index + i + 1);
46+
this.indices.push(index + i + 2);
47+
}
48+
};
49+
this.toMeshBuffer = function() {
50+
return new H3DU.MeshBuffer()
51+
.setAttribute("POSITION", 0, this.vertices, 0, 3)
52+
.setAttribute("NORMAL", 0, this.normals, 0, 3)
53+
.setIndices(this.indices);
54+
};
55+
};
56+
2857
/**
2958
* Generates a mesh buffer containing the polygons
3059
* stored in this BSP tree.
3160
* @returns {H3DU.MeshBuffer} Return value.
3261
*/
3362
BspTree.prototype.toMeshBuffer = function() {
3463
"use strict";
35-
var mesh = new H3DU.Mesh();
64+
var mesh = new BspTree._MiniBuilder();
3665
this._toMeshInternal(mesh);
3766
return mesh.toMeshBuffer();
3867
};
@@ -42,12 +71,7 @@ BspTree.prototype._toMeshInternal = function(mesh) {
4271
if(typeof this.faces !== "undefined" && this.faces !== null) {
4372
for(var polyIndex = 0; polyIndex < this.faces.length; polyIndex++) {
4473
var poly = this.faces[polyIndex];
45-
mesh.mode(H3DU.Mesh.TRIANGLE_FAN);
46-
mesh.normal3(poly.plane);
47-
for(var i = 0; i < poly.vertices.length; i++) {
48-
mesh.vertex3(poly.vertices[i]);
49-
}
50-
mesh.vertex3(poly.vertices[0]);
74+
mesh.addPoly(poly);
5175
}
5276
}
5377
if(typeof this.frontTree !== "undefined" && this.frontTree !== null) {

doc/H3DU.BufferAccessor.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ value to the start of the next.
3030
* [get](#H3DU.BufferAccessor_get)<br>Gets the first element of the attribute value with the given vertex index.
3131
* [getVec](#H3DU.BufferAccessor_getVec)<br>Gets the elements of a vertex attribute value.
3232
* [makeBlank](#H3DU.BufferAccessor.makeBlank)<br>Generates a vertex attribute buffer, with each value set to all zeros.
33+
* [makeIndices](#H3DU.BufferAccessor.makeIndices)<br>Generates an array of increasing vertex indices.
34+
* [merge](#H3DU.BufferAccessor.merge)<br>Merges two vertex attributes, whose vertices can be indexed differently, into one
35+
combined vertex attribute.
3336
* [set](#H3DU.BufferAccessor_set)<br>Sets the first element of the attribute value with the given vertex index.
3437
* [setVec](#H3DU.BufferAccessor_setVec)<br>Sets the elements of a vertex attribute value.
3538

@@ -141,6 +144,38 @@ Generates a vertex attribute buffer, with each value set to all zeros.
141144

142145
A blank vertex attribute buffer. (Type: <a href="H3DU.BufferAccessor.md">H3DU.BufferAccessor</a>)
143146

147+
<a name='H3DU.BufferAccessor.makeIndices'></a>
148+
### (static) H3DU.BufferAccessor.makeIndices(numIndices)
149+
150+
Generates an array of increasing vertex indices.
151+
152+
#### Parameters
153+
154+
* `numIndices` (Type: number)<br>The number of vertex indices to generate. The array will range from 0 to the number of vertex indices minus 1.
155+
156+
#### Return Value
157+
158+
An array of vertex indices. (Type: Uint16Array | Uint32Array)
159+
160+
<a name='H3DU.BufferAccessor.merge'></a>
161+
### (static) H3DU.BufferAccessor.merge(attr1, indices1, attr2, indices2)
162+
163+
Merges two vertex attributes, whose vertices can be indexed differently, into one
164+
combined vertex attribute.
165+
166+
#### Parameters
167+
168+
* `attr1` (Type: <a href="H3DU.BufferAccessor.md">H3DU.BufferAccessor</a>)<br>A vertex buffer accessor for the first vertex attribute. Can be null, in which case it is assumed that the attribute contains as many values as the length of "indices1" and all the values are zeros.
169+
* `indices1` (Type: Array.&lt;number> | Uint16Array | Uint8Array | Uint32Array)<br>An array of vertex indices associated with the first vertex attribute.
170+
* `attr2` (Type: <a href="H3DU.BufferAccessor.md">H3DU.BufferAccessor</a>)<br>A vertex buffer accessor for the second vertex attribute. Can be null, in which case it is assumed that the attribute contains as many values as the length of "indices2" and all the values are zeros.
171+
* `indices2` (Type: Array.&lt;number> | Uint16Array | Uint8Array | Uint32Array)<br>An array of vertex indices associated with the second vertex attribute.
172+
173+
#### Return Value
174+
175+
The merged attribute, where the vertices from the first vertex
176+
attribute come before those from the second. The merged attribute will have as many
177+
values as the sum of the lengths of "indices1" and "indices2". (Type: <a href="H3DU.BufferAccessor.md">H3DU.BufferAccessor</a>)
178+
144179
<a name='H3DU.BufferAccessor_set'></a>
145180
### H3DU.BufferAccessor#set(index, value)
146181

doc/H3DU.BufferHelper.md

Lines changed: 0 additions & 48 deletions
This file was deleted.

doc/H3DU.MeshBuffer.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
### H3DU.MeshBuffer()
77

88
A geometric mesh in the form of buffer objects.
9-
A mesh buffer is made up of one or more <a href="H3DU.BufferHelper.md">vertex attribute objects</a>,
9+
A mesh buffer is made up of one or more <a href="H3DU.BufferAccessor.md">vertex attribute objects</a>,
1010
and an array of vertex indices. Each vertex attribute object contains
1111
the values of one attribute of the mesh, such as positions,
1212
vertex normals, and texture coordinates. A mesh buffer
@@ -299,7 +299,7 @@ semantic is unsupported. (Type: <a href="H3DU.MeshBuffer.md">H3DU.MeshBuffer</a>
299299

300300
Sets all the vertices in this mesh to the given color, by
301301
assigning each value with the attribute semantic <code>COLOR</code>
302-
to the given color. (If the attribute's count per value
302+
to the given color. (If the attribute's <a href="H3DU.BufferAccessor.md#H3DU.BufferAccessor_countPerValue">count per value</a>
303303
is less than 4, each such value will be set to as many elements of the converted 4-element
304304
color as possible.) If an attribute with the semantic <code>COLOR</code>
305305
doesn't exist, an attribute with the semantic <code>COLOR_0</code> and a count per

doc/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ to create a B&eacute;zier curve.</b>
1818
* <a href="H3DU.BezierSurface.md">H3DU.BezierSurface</a><br><b>Deprecated: Instead of this class, use <a href="H3DU.BSplineSurface.md#H3DU.BSplineSurface.fromBezierSurface">H3DU.BSplineSurface.fromBezierSurface</a>
1919
to create a B&eacute;zier surface.</b>
2020
* <a href="H3DU.BufferAccessor.md">H3DU.BufferAccessor</a><br>A <b>vertex attribute object</b>.
21-
* <a href="H3DU.BufferHelper.md">H3DU.BufferHelper</a><br>A helper class for accessing and setting data in vertex attributes.
2221
* <a href="H3DU.BufferedMesh.md">H3DU.BufferedMesh</a><br><b>Deprecated: This class is likely to become a private class.
2322
Use the <a href="H3DU.MeshBuffer.md">H3DU.MeshBuffer</a> class instead, which is not coupled to WebGL
2423
contexts.</b>

dochtml/H3DU.BufferAccessor.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<!DOCTYPE html><html><head><meta charset=utf-8><title>H3DU.BufferAccessor</title></head><body><h1> H3DU.BufferAccessor</h1><p><a href='index.html'>Back to documentation index.</a></p><a name='H3DU.BufferAccessor'></a>
22
<h3> H3DU.BufferAccessor(buffer, offset, countPerValue, [stride])</h3>A <b>vertex attribute object</b>.<h4> Parameters</h4><ul><li><code>buffer</code> (Type: Float32Array)<br>A buffer to store vertex attribute data; see <a href="H3DU.BufferAccessor.html#H3DU.BufferAccessor_buffer">H3DU.BufferAccessor#buffer</a>.<li><code>offset</code> (Type: number)<br>Offset to the first value; see <a href="H3DU.BufferAccessor.html#H3DU.BufferAccessor_offset">H3DU.BufferAccessor#offset</a>. Throws an error if less than 0.<li><code>countPerValue</code> (Type: number)<br>Number of elements per value; see <a href="H3DU.BufferAccessor.html#H3DU.BufferAccessor_countPerValue">H3DU.BufferAccessor#countPerValue</a>. Throws an error if 0 or less.<li><code>stride</code> (Type: number) (optional)<br>Number of elements from the start of one value to the start of the next; see <a href="H3DU.BufferAccessor.html#H3DU.BufferAccessor_stride">H3DU.BufferAccessor#stride</a>. If null, undefined, or omitted, has the same value as "countPerValue". Throws an error if 0 or less.</ul><h3> Members</h3><ul><li><a href='#H3DU.BufferAccessor_buffer'>buffer</a><br>A <i>buffer</i> of arbitrary size.<li><a href='#H3DU.BufferAccessor_countPerValue'>countPerValue</a><br>A count of the number of elements each value has.<li><a href='#H3DU.BufferAccessor_offset'>offset</a><br>An offset, which identifies the index, starting from 0, of the first value
33
of the attribute within the buffer.<li><a href='#H3DU.BufferAccessor_stride'>stride</a><br>A stride, which gives the number of elements from the start of one
4-
value to the start of the next.</ul><h3> Methods</h3><ul><li><a href='#H3DU.BufferAccessor_copy'>copy</a><br>Copies the values of this accessor into a new vertex attribute object.<li><a href='#H3DU.BufferAccessor_count'>count</a><br>Gets the number of <a href="H3DU.BufferAccessor.html#H3DU.BufferAccessor_buffer">values</a> defined for this accessor.<li><a href='#H3DU.BufferAccessor_get'>get</a><br>Gets the first element of the attribute value with the given vertex index.<li><a href='#H3DU.BufferAccessor_getVec'>getVec</a><br>Gets the elements of a vertex attribute value.<li><a href='#H3DU.BufferAccessor.makeBlank'>makeBlank</a><br>Generates a vertex attribute buffer, with each value set to all zeros.<li><a href='#H3DU.BufferAccessor_set'>set</a><br>Sets the first element of the attribute value with the given vertex index.<li><a href='#H3DU.BufferAccessor_setVec'>setVec</a><br>Sets the elements of a vertex attribute value.</ul><a name='H3DU.BufferAccessor_buffer'></a>
4+
value to the start of the next.</ul><h3> Methods</h3><ul><li><a href='#H3DU.BufferAccessor_copy'>copy</a><br>Copies the values of this accessor into a new vertex attribute object.<li><a href='#H3DU.BufferAccessor_count'>count</a><br>Gets the number of <a href="H3DU.BufferAccessor.html#H3DU.BufferAccessor_buffer">values</a> defined for this accessor.<li><a href='#H3DU.BufferAccessor_get'>get</a><br>Gets the first element of the attribute value with the given vertex index.<li><a href='#H3DU.BufferAccessor_getVec'>getVec</a><br>Gets the elements of a vertex attribute value.<li><a href='#H3DU.BufferAccessor.makeBlank'>makeBlank</a><br>Generates a vertex attribute buffer, with each value set to all zeros.<li><a href='#H3DU.BufferAccessor.makeIndices'>makeIndices</a><br>Generates an array of increasing vertex indices.<li><a href='#H3DU.BufferAccessor.merge'>merge</a><br>Merges two vertex attributes, whose vertices can be indexed differently, into one
5+
combined vertex attribute.<li><a href='#H3DU.BufferAccessor_set'>set</a><br>Sets the first element of the attribute value with the given vertex index.<li><a href='#H3DU.BufferAccessor_setVec'>setVec</a><br>Sets the elements of a vertex attribute value.</ul><a name='H3DU.BufferAccessor_buffer'></a>
56
<h3> H3DU.BufferAccessor#buffer
67

78
</h3>A <i>buffer</i> of arbitrary size. This buffer
@@ -45,7 +46,12 @@ <h3> H3DU.BufferAccessor#get(index)</h3>Gets the first element of the attribute
4546
<h3> H3DU.BufferAccessor#getVec(index, vec)</h3>Gets the elements of a vertex attribute value.<p>
4647
Note that currently, this method does no bounds checking beyond the
4748
checking naturally done when accessing the attribute's buffer.<h4> Parameters</h4><ul><li><code>index</code> (Type: number)<br>A numeric index, starting from 0, that identifies a value stored in the attribute's buffer. For example, 0 identifies the first value, 1 identifies the second, and so on.<li><code>vec</code> (Type: Array.&lt;number>)<br>An array whose elements will be set to those of the value at the given index. The number of elements copied to this array is the attribute's count per value (see <a href="H3DU.BufferAccessor.html#H3DU.BufferAccessor_countPerValue">H3DU.BufferAccessor#countPerValue</a>).</ul><h4> Return Value</h4>The parameter "vec". (Type: Array.&lt;number>)<a name='H3DU.BufferAccessor.makeBlank'></a>
48-
<h3> (static) H3DU.BufferAccessor.makeBlank(count, countPerValue)</h3>Generates a vertex attribute buffer, with each value set to all zeros.<h4> Parameters</h4><ul><li><code>count</code> (Type: number)<br>The number of <a href="H3DU.BufferAccessor.html#H3DU.BufferAccessor_buffer">values</a> the buffer will hold. For example, (10, 20, 5) is a 3-element value.<li><code>countPerValue</code> (Type: number)<br>The number of elements each value will take in the buffer.</ul><h4> Return Value</h4>A blank vertex attribute buffer. (Type: <a href="H3DU.BufferAccessor.html">H3DU.BufferAccessor</a>)<a name='H3DU.BufferAccessor_set'></a>
49+
<h3> (static) H3DU.BufferAccessor.makeBlank(count, countPerValue)</h3>Generates a vertex attribute buffer, with each value set to all zeros.<h4> Parameters</h4><ul><li><code>count</code> (Type: number)<br>The number of <a href="H3DU.BufferAccessor.html#H3DU.BufferAccessor_buffer">values</a> the buffer will hold. For example, (10, 20, 5) is a 3-element value.<li><code>countPerValue</code> (Type: number)<br>The number of elements each value will take in the buffer.</ul><h4> Return Value</h4>A blank vertex attribute buffer. (Type: <a href="H3DU.BufferAccessor.html">H3DU.BufferAccessor</a>)<a name='H3DU.BufferAccessor.makeIndices'></a>
50+
<h3> (static) H3DU.BufferAccessor.makeIndices(numIndices)</h3>Generates an array of increasing vertex indices.<h4> Parameters</h4><ul><li><code>numIndices</code> (Type: number)<br>The number of vertex indices to generate. The array will range from 0 to the number of vertex indices minus 1.</ul><h4> Return Value</h4>An array of vertex indices. (Type: Uint16Array | Uint32Array)<a name='H3DU.BufferAccessor.merge'></a>
51+
<h3> (static) H3DU.BufferAccessor.merge(attr1, indices1, attr2, indices2)</h3>Merges two vertex attributes, whose vertices can be indexed differently, into one
52+
combined vertex attribute.<h4> Parameters</h4><ul><li><code>attr1</code> (Type: <a href="H3DU.BufferAccessor.html">H3DU.BufferAccessor</a>)<br>A vertex buffer accessor for the first vertex attribute. Can be null, in which case it is assumed that the attribute contains as many values as the length of "indices1" and all the values are zeros.<li><code>indices1</code> (Type: Array.&lt;number> | Uint16Array | Uint8Array | Uint32Array)<br>An array of vertex indices associated with the first vertex attribute.<li><code>attr2</code> (Type: <a href="H3DU.BufferAccessor.html">H3DU.BufferAccessor</a>)<br>A vertex buffer accessor for the second vertex attribute. Can be null, in which case it is assumed that the attribute contains as many values as the length of "indices2" and all the values are zeros.<li><code>indices2</code> (Type: Array.&lt;number> | Uint16Array | Uint8Array | Uint32Array)<br>An array of vertex indices associated with the second vertex attribute.</ul><h4> Return Value</h4>The merged attribute, where the vertices from the first vertex
53+
attribute come before those from the second. The merged attribute will have as many
54+
values as the sum of the lengths of "indices1" and "indices2". (Type: <a href="H3DU.BufferAccessor.html">H3DU.BufferAccessor</a>)<a name='H3DU.BufferAccessor_set'></a>
4955
<h3> H3DU.BufferAccessor#set(index, value)</h3>Sets the first element of the attribute value with the given vertex index.<p>
5056
Note that currently, this method does no bounds checking beyond the
5157
checking naturally done when writing to the attribute's buffer.<h4> Parameters</h4><ul><li><code>index</code> (Type: number)<br>A numeric index, starting from 0, that identifies a value stored in the attribute's buffer. For example, 0 identifies the first value, 1 identifies the second, and so on.<li><code>value</code> (Type: number)<br>The number to set the first element to.</ul><h4> Return Value</h4>This object. (Type: <a href="H3DU.BufferAccessor.html">H3DU.BufferAccessor</a>)<a name='H3DU.BufferAccessor_setVec'></a>

0 commit comments

Comments
 (0)