-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.js
35 lines (29 loc) · 1.08 KB
/
mesh.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// OpenGL mesh prototype
function Mesh()
{
this.vertex_buffer = gl.createBuffer();
this.index_buffer = gl.createBuffer();
this.numIndices = null;
this.mesh_type = null;
}
Mesh.prototype.bufferDataFromArray = function(vertices, indices, mesh_type)
{
gl.bindBuffer(gl.ARRAY_BUFFER,this.vertex_buffer);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,this.index_buffer);
gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(vertices),gl.STATIC_DRAW);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,new Uint32Array(indices), gl.STATIC_DRAW);
this.numIndices = indices.length;
this.mesh_type = mesh_type;
}
Mesh.prototype.setVertexAttribPointer = function(index, size, type, normalized, stride, pointer)
{
gl.bindBuffer(gl.ARRAY_BUFFER,this.vertex_buffer);
gl.enableVertexAttribArray(index);
gl.vertexAttribPointer(index,size,type,normalized,stride,pointer);
}
Mesh.prototype.draw = function()
{
gl.bindBuffer(gl.ARRAY_BUFFER,this.vertex_buffer); // check for webgl best practises
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,this.index_buffer);
gl.drawElements(this.mesh_type,this.numIndices,gl.UNSIGNED_INT, 0);
}