forked from Tevemadar/meshview-demo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mesh.js
50 lines (48 loc) · 1.86 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function Mesh(rawdata){
var view=new DataView(rawdata);
var idx=4;
this.minx=view.getFloat32(idx);idx+=4;
this.maxx=view.getFloat32(idx);idx+=4;
this.miny=view.getFloat32(idx);idx+=4;
this.maxy=view.getFloat32(idx);idx+=4;
this.minz=view.getFloat32(idx);idx+=4;
this.maxz=view.getFloat32(idx);idx+=4;
this.chunks=view.getUint8(idx);idx+=1;
this.lists=[];
for(var i=0;i<this.chunks;i++){
var vertices=view.getUint16(idx);idx+=2;
var list=new Float32Array(vertices*6);
for(var j=0;j<vertices*6;j++){
list[j]=view.getFloat32(idx);idx+=4;
}
this.lists.push(list);
var indices=view.getUint32(idx);idx+=4;
list=new Uint16Array(indices);
for(var j=0;j<indices;j++){
list[j]=view.getUint16(idx);idx+=2;
}
this.lists.push(list);
}
}
Mesh.prototype.createBuffers=function(gl){
this.buffers=[];
for(var i=0;i<this.chunks;i++){
var vertexbuff=gl.createBuffer();
var indexbuff=gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexbuff);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexbuff);
gl.bufferData(gl.ARRAY_BUFFER, this.lists[i*2], gl.STATIC_DRAW);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.lists[i*2+1], gl.STATIC_DRAW);
this.buffers.push(vertexbuff);
this.buffers.push(indexbuff);
}
};
Mesh.prototype.drawElements=function(gl,coords,normals){
for(var i=0;i<this.chunks;i++){
gl.bindBuffer(gl.ARRAY_BUFFER, this.buffers[i*2]);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.buffers[i*2+1]);
gl.vertexAttribPointer(coords, 3, gl.FLOAT, false, 6*4, 0);
if(typeof normals!=="undefined")gl.vertexAttribPointer(normals, 3, gl.FLOAT, false, 6*4, 3*4);
gl.drawElements(gl.TRIANGLES, this.lists[i*2+1].length, gl.UNSIGNED_SHORT,0);
}
};