Skip to content

Commit 78214c2

Browse files
Merge pull request #1 from plepers/develop
pull plepers
2 parents 3c2b221 + 6d04e10 commit 78214c2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+9495
-1508
lines changed

.jshintrc

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

.npmignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
test
2-
docs
2+
docs
3+
src
4+
tsconfig.json
5+
.travis.yml

.travis.yml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
language: node_js
22
node_js:
3-
- '0.12'
4-
env:
5-
global:
6-
- secure: "fEoC5yuauEgf0kDqAyHwjwgRE9q8+oO4oL5mt1z4ju7CB5uZOCMhtd9kLsJPY8gN8FRK7jM+qcpedJ7uiFu1go6nmE4ks3/UyQXlUvQtWKwJVqK8qOgWZiBpIxUOOZ2mm27aXsjWLWdi6u8Z/DLAqmNfm5e5iHDUJM6aNqkCUElcjPkitSPu1jaHdZcH5RcviWDbCoWbTbW7+1yn2OLIv+ONCqq2Ywmvc/n+tN8y8NZiesJOgWMPbtpKjOwclJoQifnBAdZrUnlKZ/crSzKODb7/cLhJ5tRsEXKHyUbLZxD/ldZl3HSmdR2v9FtJjYDuMa7KpL1WrCJGPYzs/o0w7BBWi9o4OB0uAiSF8yYQOhazN1fLijrb+4HwV/t1DEzi3LrhwaJIh5QUezKz9i0ZtMkoyIdzvjySQU4JpNGfWnqXKeEVWjL8GPDaLeVqmmuIht0dVlM+Nb//B3KdpOmmTH8/aKrHROdFyWvV+0wp2OvamFjewNBnsU3sEhHwWharas0Em6tpFQ8G1EBglavmmBB1UlB4KRe/DbFWW26LvSSq0AzAsHpNT6fwD2JtzzFabHF8ZSdx2SM5sf4PR7yuROsejBNc6uhoW5lEFrdkHpfUfsMk8DQ4r/rS4pI7vPtrSbzjj41+q6mjhDxiyU0oKsN5R70hGdiDbg/nr572xRY="
7-
- secure: "pj8rEQm5ArtdYOr60O/pmNwJlvBVOlEjzcbRgDcSVJ8tl6ySDS5QPKZoAEbUub1CIxEqTg/XyD8FBxikIeS3VnVGwtBKFolLfYvrj21tn4EKpxxYW2qbCXOVBpn71H3UVMu7JCq1ftKqERSY4zYRGgU9lKUxa4+fMySYOgpfS+AJ5AHe09nHzHZXxWca5Ih8oDqio6+RJwwrBOnUopWBbeUllo7457HV3cqIwnamb8FxUiY6XouRkmv0S++eXAyTrkMtOysTmwe0HnsmodjqUtBOVf31cHQRnDiVmE35mOfJEHvN2M6nJ5t6UaQHvEGcBqClOsOxrkYy88xCMKMmxcyex28O84Ge+iE6XcvrFOW35f08KND7EE2WguV96H3xYjrtGByH3xwImI+u7XwDeo/Tx7/PcrRi0ygYFi5MwQCl8zhPEjtg5D4Qv7NxJfBIkV7bMTHSyEAr/OS8aeD9+un1EVwiIar0rblAZ50H8TQPB2Ez79NbAlHp9etFI93ZWLjYQLdEcsKnM+gwmJeZ+zLhpCAkR1ONgajI8Z6S5An/OHWFotcUpPgUHmINBCUMdSKiyPjpV0IPmPPXruIXrxjTasPgHlpO1BPZ1zNTtax1/luNMj7ChCpb6kQ26tDH1Rn8oQDMY0PNjYkLo0C4+1KyjNQp7ID05C6MoEgHzCk="
3+
- 9.5.0
84
branches:
95
only:
106
- develop
@@ -14,9 +10,8 @@ cache:
1410
- node_modules
1511
deploy:
1612
provider: npm
17-
18-
api_key:
19-
secure: tKdp4/Xvu83IwtTahMHj8To2u9qa45MLco29i6tu9H2mjwyPHMjj4tUJYySdpBnPUSpFT3e+nlUS7H+pWPbLhqIG5ZYmtaM08EdT09mkv/IrT6IRNAyj1IAN6E6wtTQzHLgh0U8e+sjjdndLz8NbfYc4h+oc09RSeufLTjs5FFtOoCZQPieFRGiksZbuchNAkCS6h8Es0jCNFJdrzhUh9Ws1sR816O9lAY4NjoZuv4Gq8Jhv6bFl9h+KPOy0S4DXxlcde2tMZ1u+vuznw4gpP3FhUV9AXOAYnuyPT2XkCtPnZNVj+i/E6EwZeVbnpc6HRcWxA7qmNIfGLaKeCZL8MuWO4hMc/kqLHAhJPSb5/mpj4nwkEZhUnxKCERw2OJcbHQHp1MVLB39Q7L9WiQWBXTePwd92KYzMXb3J0aHRPl6V7ZNSjdk1AKw8Y8J72tb2icDQ+0k4eepZohFduC+hWB9ISPg+67ndXfM/zW18ynIqyyyKJM5zCzJbD/MGVYPgbz+Dl9U6zFuQgcKRO+CBcFZQVfU0uD4MUY7KnCyxTErLKyv5oqIr0Us/fCdtDcYS9RqAZFbB/gU+hICGc4jITEJ+FX4GaAh5AKAypvZ3FBiAWuYKCQeMEpuVtLdvU7Vn4KZ/w4Eifuz53qQjHpvfQm6UJeD9bjKXIhYaVnu6YFA=
13+
email: $NPM_EMAIL
14+
api_key: $NPM_TOKEN
2015
on:
2116
branch: master
2217
repo: plepers/nanogl

arraybuffer.d.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Program from './program';
2+
import BaseBuffer from './basebuffer';
3+
import { GLContext } from './types';
4+
interface AttributeDef {
5+
name: string;
6+
type: GLenum;
7+
size: number;
8+
offset: number;
9+
normalize: boolean;
10+
stride: number;
11+
}
12+
declare class ArrayBuffer extends BaseBuffer {
13+
readonly gl: GLContext;
14+
readonly buffer: WebGLBuffer;
15+
usage: GLenum;
16+
stride: number;
17+
byteLength: number;
18+
length: number;
19+
attribs: AttributeDef[];
20+
constructor(gl: GLContext, data?: GLsizeiptr | BufferSource, usage?: GLenum, glbuffer?: WebGLBuffer);
21+
bind(): void;
22+
attrib(name: string, size: number, type: GLenum, normalize?: boolean): this;
23+
data(array: BufferSource | GLsizeiptr): void;
24+
subData(array: BufferSource, offset: number): void;
25+
attribPointer(program: Program): void;
26+
draw(mode: GLenum, count?: number, offset?: number): void;
27+
dispose(): void;
28+
_computeLength(): void;
29+
}
30+
export default ArrayBuffer;

arraybuffer.js

Lines changed: 70 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,72 @@
1-
var BufferUtils = require( './bufferutils' );
2-
3-
/*
4-
* GL_ARRAY_BUFFER */
5-
var TGT = 0x8892;
6-
7-
8-
9-
/**
10-
* @class
11-
* @implements {Drawable}
12-
* @param {WebGLRenderingContext} gl then webgl context this ArrayBuffer belongs to
13-
* @param {TypedArray|uint} [data] optional data to copy to buffer, or the size (in bytes)
14-
* @param {GLenum} [usage=GL_STATIC_DRAW] the usage hint for this buffer.
15-
*
16-
*/
17-
function ArrayBuffer( gl, data, usage ){
18-
this.gl = gl;
19-
this.usage = usage || gl.STATIC_DRAW;
20-
this.buffer = gl.createBuffer();
21-
this.attribs = [];
22-
this.stride = 0;
23-
this.byteLength = 0;
24-
this.length = 0;
25-
26-
if( data ){
27-
this.data( data );
28-
}
29-
}
30-
31-
32-
ArrayBuffer.prototype = {
33-
34-
/**
35-
* Bind the underlying webgl buffer.
36-
*/
37-
bind: function(){
38-
this.gl.bindBuffer( TGT, this.buffer );
39-
},
40-
41-
/**
42-
* Add attribute declaration for this buffer. Once attributes declared, the buffer can be linked to
43-
* programs attributes using {@link ArrayBuffer#attribPointer}
44-
* @param {string} name the name of the program's attribute
45-
* @param {uint} size the size of the attribute (3 for a vec3)
46-
* @param {GLenum} type the type of data (GL_FLOAT, GL_SHORT etc)
47-
* @param {boolean} [normalize=false] indicate if the data must be normalized
48-
*/
49-
attrib: function( name, size, type, normalize ){
50-
this.attribs.push({
51-
name : name ,
52-
type : 0|type ,
53-
size : 0|size ,
54-
normalize : !!normalize,
55-
offset : this.stride
56-
});
57-
this.stride += BufferUtils.getComponentSize( type ) * size;
58-
this._computeLength();
59-
return this;
60-
},
61-
62-
/**
63-
* Fill webgl buffer with the given data. You can also pass a uint to allocate the buffer to the given size.
64-
* @param {TypedArray|uint} array the data to send to the buffer, or a size.
65-
*/
66-
data: function( array ){
67-
var gl = this.gl;
68-
gl.bindBuffer( TGT, this.buffer );
69-
gl.bufferData( TGT, array, this.usage );
70-
gl.bindBuffer( TGT, null );
71-
72-
this.byteLength = ( array.byteLength === undefined ) ? array : array.byteLength;
73-
this._computeLength();
74-
},
75-
76-
/**
77-
* Set a part of the buffer with the given data, starting a offset (in bytes)
78-
* @param {typedArray} array the data to send to buffer
79-
* @param {uint} offset the offset in byte where the data will be written
80-
*/
81-
subData: function( array, offset ){
82-
var gl = this.gl;
83-
gl.bindBuffer( TGT, this.buffer );
84-
gl.bufferSubData( TGT, offset, array );
85-
gl.bindBuffer( TGT, null );
86-
},
87-
88-
/**
89-
* Link given program attributes to this buffer. You should first declare attributes using {@link ArrayBuffer#attrib}
90-
* before calling this method.
91-
* @param {Program} program the nanogl Program
92-
*/
93-
attribPointer: function( program ){
94-
var gl = this.gl;
95-
gl.bindBuffer( TGT, this.buffer );
96-
97-
for (var i = 0; i < this.attribs.length; i++) {
98-
var attrib = this.attribs[i];
99-
100-
if( program[attrib.name] !== undefined ){
101-
var aLocation = program[attrib.name]();
102-
gl.enableVertexAttribArray( aLocation );
103-
gl.vertexAttribPointer( aLocation,
104-
attrib.size,
105-
attrib.type,
106-
attrib.normalize,
107-
this.stride,
108-
attrib.offset
109-
);
110-
}
111-
1+
import BaseBuffer from './basebuffer';
2+
import { getComponentSize, isBufferSource } from './utils';
3+
const GL_ARRAY_BUFFER = 0x8892;
4+
class ArrayBuffer extends BaseBuffer {
5+
constructor(gl, data, usage = gl.STATIC_DRAW, glbuffer) {
6+
super();
7+
this.gl = gl;
8+
this.usage = usage;
9+
this.buffer = (glbuffer !== undefined) ? glbuffer : gl.createBuffer();
10+
this.attribs = [];
11+
this.stride = 0;
12+
this.byteLength = 0;
13+
this.length = 0;
14+
if (data) {
15+
this.data(data);
16+
}
17+
}
18+
bind() {
19+
this.gl.bindBuffer(GL_ARRAY_BUFFER, this.buffer);
20+
}
21+
attrib(name, size, type, normalize = false) {
22+
this.attribs.push({
23+
name,
24+
type: 0 | type,
25+
size: 0 | size,
26+
normalize,
27+
offset: this.stride,
28+
stride: 0
29+
});
30+
this.stride += getComponentSize(type) * size;
31+
this._computeLength();
32+
return this;
33+
}
34+
data(array) {
35+
const gl = this.gl;
36+
gl.bindBuffer(GL_ARRAY_BUFFER, this.buffer);
37+
gl.bufferData(GL_ARRAY_BUFFER, array, this.usage);
38+
gl.bindBuffer(GL_ARRAY_BUFFER, null);
39+
this.byteLength = isBufferSource(array) ? array.byteLength : array;
40+
this._computeLength();
41+
}
42+
subData(array, offset) {
43+
const gl = this.gl;
44+
gl.bindBuffer(GL_ARRAY_BUFFER, this.buffer);
45+
gl.bufferSubData(GL_ARRAY_BUFFER, offset, array);
46+
gl.bindBuffer(GL_ARRAY_BUFFER, null);
11247
}
113-
},
114-
115-
/**
116-
* Shortcut to gl.drawArrays
117-
* @param {GLenum} mode the type of primitive to draw (GL_TRIANGLE, GL_POINTS etc)
118-
* @param {uint} [count] the number of vertices to draw (full buffer is used if omited)
119-
* @param {uint} [offset=0] the position of the first vertex to draw
120-
*/
121-
draw: function( mode, count, offset ){
122-
count = ( count === undefined ) ? this.length : count;
123-
this.gl.drawArrays( mode, offset, 0|count );
124-
},
125-
126-
/**
127-
* Delete underlying webgl objects
128-
*/
129-
dispose: function(){
130-
this.gl.deleteBuffer( this.buffer );
131-
this.buffer = null;
132-
this.gl = null;
133-
},
134-
135-
136-
_computeLength: function(){
137-
if( this.stride > 0 ) {
138-
this.length = this.byteLength / this.stride;
48+
attribPointer(program) {
49+
const gl = this.gl;
50+
gl.bindBuffer(GL_ARRAY_BUFFER, this.buffer);
51+
for (var i = 0; i < this.attribs.length; i++) {
52+
var attrib = this.attribs[i];
53+
if (program[attrib.name] !== undefined) {
54+
var aLocation = program[attrib.name]();
55+
gl.enableVertexAttribArray(aLocation);
56+
gl.vertexAttribPointer(aLocation, attrib.size, attrib.type, attrib.normalize, attrib.stride || this.stride, attrib.offset);
57+
}
58+
}
13959
}
140-
}
141-
142-
};
143-
144-
/*
145-
* Implement Drawable
146-
*/
147-
BufferUtils.Drawable( ArrayBuffer.prototype );
148-
149-
150-
module.exports = ArrayBuffer;
60+
draw(mode, count = this.length, offset = 0) {
61+
this.gl.drawArrays(mode, offset, 0 | count);
62+
}
63+
dispose() {
64+
this.gl.deleteBuffer(this.buffer);
65+
}
66+
_computeLength() {
67+
if (this.stride > 0) {
68+
this.length = this.byteLength / this.stride;
69+
}
70+
}
71+
}
72+
export default ArrayBuffer;

basebuffer.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
declare abstract class BaseBuffer {
2+
abstract bind(): void;
3+
abstract dispose(): void;
4+
abstract data(array: BufferSource | GLsizeiptr): void;
5+
abstract subData(array: BufferSource, offset: number): void;
6+
abstract draw(mode: GLenum, count?: number, offset?: number): void;
7+
drawPoints(count?: number, offset?: number): void;
8+
drawLines(count?: number, offset?: number): void;
9+
drawLineLoop(count?: number, offset?: number): void;
10+
drawLineStrip(count?: number, offset?: number): void;
11+
drawTriangles(count?: number, offset?: number): void;
12+
drawTriangleStrip(count?: number, offset?: number): void;
13+
drawTriangleFan(count?: number, offset?: number): void;
14+
}
15+
export default BaseBuffer;

basebuffer.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class BaseBuffer {
2+
drawPoints(count, offset) {
3+
this.draw(0, count, offset);
4+
}
5+
drawLines(count, offset) {
6+
this.draw(1, count, offset);
7+
}
8+
drawLineLoop(count, offset) {
9+
this.draw(2, count, offset);
10+
}
11+
drawLineStrip(count, offset) {
12+
this.draw(3, count, offset);
13+
}
14+
drawTriangles(count, offset) {
15+
this.draw(4, count, offset);
16+
}
17+
drawTriangleStrip(count, offset) {
18+
this.draw(5, count, offset);
19+
}
20+
drawTriangleFan(count, offset) {
21+
this.draw(6, count, offset);
22+
}
23+
}
24+
export default BaseBuffer;

0 commit comments

Comments
 (0)