-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriMesh.js
executable file
·317 lines (267 loc) · 10.3 KB
/
TriMesh.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
* @fileoverview TriMesh - A simple 3D surface mesh for use with WebGL
* @author Eric Shaffer
*/
/** Class implementing triangle surface mesh. */
class TriMesh{
/**
* Initialize members of a TriMesh object
*/
constructor(){
this.isLoaded = false;
this.minXYZ=[0,0,0];
this.maxXYZ=[0,0,0];
this.numFaces=0;
this.numVertices=0;
// Allocate vertex array
this.vBuffer = [];
// Allocate triangle array
this.fBuffer = [];
// Allocate normal array
this.nBuffer = [];
// Allocate array for edges so we can draw wireframe
this.eBuffer = [];
// Allocate array for texture coordinates
this.texcoordBuffer = [];
console.log("TriMesh: Allocated buffers");
// Get extension for 4 byte integer indices for drawElements
var ext = gl.getExtension('OES_element_index_uint');
if (ext ==null){
alert("OES_element_index_uint is unsupported by your browser and terrain generation cannot proceed.");
}
else{
console.log("OES_element_index_uint is supported!");
}
}
/**
* Return if the JS arrays have been populated with mesh data
*/
loaded(){
return this.isLoaded;
}
/**
* Populate the JS arrays by parsing a string containing an OBJ file
* @param {string} text of an OBJ file
*/
loadFromOBJ(fileText)
{
//Your code here
//console.log(fileText);
var line_split = fileText.split('\n');
for(var i = 0; i < line_split.length; i++) {
if(line_split[i] == 0)
continue;
else if(line_split[i][0] == '#')
continue;
else if(line_split[i][0] == 'v') {
// Handle vertex
var v_data = line_split[i].substring(2, line_split[i].length);
var v_floats = v_data.split(' ');
for(var j = 0; j < v_floats.length; j++) {
var float = parseFloat(v_floats[j]);
//console.log(float);
this.vBuffer.push(float);
}
this.numVertices++;
}
else if(line_split[i][0] == 'f') {
// Handle face
var f_data = line_split[i].substring(2, line_split[i].length);
var f_ints = f_data.split(' ');
//console.log(f_ints);
for(var j = 0; j < f_ints.length; j++) {
var int = parseInt(f_ints[j]-1);
//console.log(int);
this.fBuffer.push(int);
}
this.numFaces++;
}
}
//----------------
console.log("TriMesh: Loaded ", this.numFaces, " triangles.");
console.log("TriMesh: Loaded ", this.numVertices, " vertices.");
this.generateNormals();
console.log("TriMesh: Generated normals");
this.generateLines();
console.log("TriMesh: Generated lines");
myMesh.loadBuffers();
this.isLoaded = true;
}
/**
* Send the buffer objects to WebGL for rendering
*/
loadBuffers()
{
// Specify the vertex coordinates
this.VertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.VertexPositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.vBuffer), gl.STATIC_DRAW);
this.VertexPositionBuffer.itemSize = 3;
this.VertexPositionBuffer.numItems = this.numVertices;
console.log("Loaded ", this.VertexPositionBuffer.numItems, " vertices");
// Specify normals to be able to do lighting calculations
this.VertexNormalBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.VertexNormalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.nBuffer),
gl.STATIC_DRAW);
this.VertexNormalBuffer.itemSize = 3;
this.VertexNormalBuffer.numItems = this.numVertices;
console.log("Loaded ", this.VertexNormalBuffer.numItems, " normals");
// Specify faces of the terrain
this.IndexTriBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.IndexTriBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint32Array(this.fBuffer),
gl.STATIC_DRAW);
this.IndexTriBuffer.itemSize = 1;
this.IndexTriBuffer.numItems = this.fBuffer.length;
console.log("Loaded ", this.IndexTriBuffer.numItems/3, " triangles");
//Setup Edges
this.IndexEdgeBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.IndexEdgeBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint32Array(this.eBuffer),
gl.STATIC_DRAW);
this.IndexEdgeBuffer.itemSize = 1;
this.IndexEdgeBuffer.numItems = this.eBuffer.length;
}
/**
* Render the triangles
*/
drawTriangles(){
gl.bindBuffer(gl.ARRAY_BUFFER, this.VertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, this.VertexPositionBuffer.itemSize,
gl.FLOAT, false, 0, 0);
// Bind normal buffer
gl.bindBuffer(gl.ARRAY_BUFFER, this.VertexNormalBuffer);
gl.vertexAttribPointer(shaderProgram.vertexNormalAttribute,
this.VertexNormalBuffer.itemSize,
gl.FLOAT, false, 0, 0);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_CUBE_MAP, cubeMap);
gl.uniform1i(gl.getUniformLocation(shaderProgram, "uCubeSampler"), 0);
//Draw
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.IndexTriBuffer);
gl.drawElements(gl.TRIANGLES, this.IndexTriBuffer.numItems, gl.UNSIGNED_INT,0);
}
/**
* Render the triangle edges wireframe style
*/
drawEdges(){
gl.bindBuffer(gl.ARRAY_BUFFER, this.VertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, this.VertexPositionBuffer.itemSize,
gl.FLOAT, false, 0, 0);
// Bind normal buffer
gl.bindBuffer(gl.ARRAY_BUFFER, this.VertexNormalBuffer);
gl.vertexAttribPointer(shaderProgram.vertexNormalAttribute,
this.VertexNormalBuffer.itemSize,
gl.FLOAT, false, 0, 0);
//Draw
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.IndexEdgeBuffer);
gl.drawElements(gl.LINES, this.IndexEdgeBuffer.numItems, gl.UNSIGNED_INT,0);
}
/**
* Print vertices and triangles to console for debugging
*/
printBuffers()
{
for(var i=0;i<this.numVertices;i++)
{
console.log("v ", this.vBuffer[i*3], " ",
this.vBuffer[i*3 + 1], " ",
this.vBuffer[i*3 + 2], " ");
}
for(var i=0;i<this.numFaces;i++)
{
console.log("f ", this.fBuffer[i*3], " ",
this.fBuffer[i*3 + 1], " ",
this.fBuffer[i*3 + 2], " ");
}
}
/**
* Generates line values from faces in faceArray
* to enable wireframe rendering
*/
generateLines()
{
var numTris=this.fBuffer.length/3;
for(var f=0;f<numTris;f++)
{
var fid=f*3;
this.eBuffer.push(this.fBuffer[fid]);
this.eBuffer.push(this.fBuffer[fid+1]);
this.eBuffer.push(this.fBuffer[fid+1]);
this.eBuffer.push(this.fBuffer[fid+2]);
this.eBuffer.push(this.fBuffer[fid+2]);
this.eBuffer.push(this.fBuffer[fid]);
}
}
/**
* Set the x,y,z coords of a vertex at location id
* @param {number} the index of the vertex to set
* @param {number} x coordinate
* @param {number} y coordinate
* @param {number} z coordinate
*/
setVertex(id,x,y,z){
var vid = 3*id;
this.vBuffer[vid]=x;
this.vBuffer[vid+1]=y;
this.vBuffer[vid+2]=z;
}
/**
* Return the x,y,z coords of a vertex at location id
* @param {number} the index of the vertex to return
* @param {Object} a length 3 array to populate withx,y,z coords
*/
getVertex(id, v){
var vid = 3*id;
v[0] = this.vBuffer[vid];
v[1] = this.vBuffer[vid+1];
v[2] = this.vBuffer[vid+2];
}
/**
* Compute per-vertex normals for a mesh
*/
generateNormals(){
//per vertex normals
this.numNormals = this.numVertices;
this.nBuffer = new Array(this.numNormals*3);
for(var i=0;i<this.nBuffer.length;i++)
{
this.nBuffer[i]=0;
}
for(var i=0;i<this.numFaces;i++)
{
// Get vertex coodinates
var v1 = this.fBuffer[3*i];
var v1Vec = vec3.fromValues(this.vBuffer[3*v1], this.vBuffer[3*v1+1], this.vBuffer[3*v1+2]);
var v2 = this.fBuffer[3*i+1];
var v2Vec = vec3.fromValues(this.vBuffer[3*v2], this.vBuffer[3*v2+1], this.vBuffer[3*v2+2]);
var v3 = this.fBuffer[3*i+2];
var v3Vec = vec3.fromValues(this.vBuffer[3*v3], this.vBuffer[3*v3+1], this.vBuffer[3*v3+2]);
// Create edge vectors
var e1=vec3.create();
vec3.subtract(e1,v2Vec,v1Vec);
var e2=vec3.create();
vec3.subtract(e2,v3Vec,v1Vec);
// Compute normal
var n = vec3.fromValues(0,0,0);
vec3.cross(n,e1,e2);
// Accumulate
for(var j=0;j<3;j++){
this.nBuffer[3*v1+j]+=n[j];
this.nBuffer[3*v2+j]+=n[j];
this.nBuffer[3*v3+j]+=n[j];
}
}
for(var i=0;i<this.numNormals;i++)
{
var n = vec3.fromValues(this.nBuffer[3*i],
this.nBuffer[3*i+1],
this.nBuffer[3*i+2]);
vec3.normalize(n,n);
this.nBuffer[3*i] = n[0];
this.nBuffer[3*i+1]=n[1];
this.nBuffer[3*i+2]=n[2];
}
}
}