|
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); |
112 | 47 | } |
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 | + } |
139 | 59 | } |
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; |
0 commit comments