-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMesh.cpp
543 lines (471 loc) · 15.2 KB
/
Mesh.cpp
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
* Mesh.cpp
* Created by Zachary Ferguson
* Source file for the Mesh class that represents a Mesh in 3D space.
*/
#include "Mesh.h"
/* Constructor for creating a new mesh. */
/* Must send a unsigned int for the number of rows and cols of the mesh. */
/* Also requires the width and depth of the mesh in 3D space. Lastly */
/* requires the color of the mesh. */
Mesh::Mesh(const unsigned int rows, const unsigned int cols, const float width,
const float depth, const Color* color, const float snowCapHeight)
{
/* Check that the width and depth are non-negative. */
assert(depth > 0 && width > 0);
/* Create new vector for the rows. */
this->vertices = new std::vector<std::vector<vec4>*>();
/* Fill the rows with columns. */
float range = (width / rows + depth / cols)/2;
for (unsigned int r = 0; r <= rows; r++)
{
std::vector<vec4>* row = new std::vector<vec4>();
float z = depth * (r/(rows*1.0f)) - (depth / 2.0f);
for (unsigned int c = 0; c <= cols; c++)
{
float x = width * (c/(cols*1.0f)) - ((width) / 2.0f);
float y = (((float)rand()/(float)RAND_MAX) * range) - (range/2.0f);
//float y = ((float)rand()/(float)RAND_MAX) * .5f - .25f;
//float y = (x*x+z*z)/5;
//float y = 0;
row->push_back(vec4(x, y, z, 0.0));
}
this->vertices->push_back(row);
}
/* Set the instance variables. */
this->color = color;
this->snowCapHeight = snowCapHeight;
this->width = width;
this->depth = depth;
}
/* Deletes this Face */
Mesh::~Mesh()
{
for (unsigned int r = 0; r < this->vertices->size(); r++)
{
this->vertices->at(r)->clear();
}
this->vertices->clear();
delete this->vertices;
}
/* Returns the number of rows in the mesh. */
const unsigned int Mesh::getRows() const
{
return this->vertices->size();
}
/* Returns the number of rows in the mesh. */
const unsigned int Mesh::getCols() const
{
return this->vertices->at(0)->size();
}
/* Returns the width of this mesh. */
const float Mesh::getWidth() const
{
return this->width;
}
/* Returns the depth of this mesh. */
const float Mesh::getDepth() const
{
return this->depth;
}
/* Deletes the old color and sets it to the given new one. */
void Mesh::setColor(const Color* newColor)
{
this->color = newColor;
}
/* Returns a constant pointer to the color of this face. */
const Color* Mesh::getColor() const
{
return this->color;
}
/* Sets the height of a vertex at the given row and column number. */
void Mesh::setHeight(const unsigned int row, const unsigned int col,
const float height)
{
/* Check the row and col are in bounds. */
assert(row < this->getRows() && col < this->getCols());
/* Set the height. */
this->vertices->at(row)->at(col)[1] = height;
}
/* Sets the vertex at the given row and column number with the new vec4. */
void Mesh::setVertex(const unsigned int row, const unsigned int col,
vec4 newVertex)
{
/* Check the row and col are in bounds. */
assert(row < this->getRows() && col < this->getCols());
this->vertices->at(row)->at(col) = newVertex;
}
/* Returns the vertex at the given row and column number. */
const vec4 Mesh::getVertex(unsigned int row, unsigned int col) const
{
/* Check the row and col are in bounds. */
assert(row < this->getRows() && col < this->getCols());
return this->vertices->at(row)->at(col);
}
/* Set the current snow cap height. */
void Mesh::setSnowCapHeight(const float height)
{
this->snowCapHeight = height;
}
/* Get the current snow cap height. */
const float Mesh::getSnowCapHeight() const
{
return this->snowCapHeight;
}
/* Returns the indecies of the userRay's selected vertex. Returns NULL if no */
/* vertex selected. */
std::vector<unsigned int>* Mesh::selectVertex(ray userRay)
{
/* Step 6: For each vertex, v, in the mesh, compute the distance from v */
/* to the ray, R, computed in step 5. If the distance is less than or */
/* equal to r, then select that vertex. */
vec4 rayDirection = userRay.value(1) - userRay.value(0);
rayDirection = rayDirection / rayDirection.length();
float closestDistance = FLT_MAX;
std::vector<unsigned int>* closestIndecies = NULL;
for (unsigned int r = 0; r < this->getRows(); r++)
{
for (unsigned int c = 0; c < this->getCols(); c++)
{
vec4 v = this->vertices->at(r)->at(c);
float distance = (rayDirection % (v - userRay.origin())).length();
if(distance <= SELECTION_RADIUS && distance < closestDistance)
{
closestIndecies = new std::vector<unsigned int>();
closestIndecies->push_back(r);
closestIndecies->push_back(c);
closestDistance = distance;
}
}
}
return closestIndecies;
}
/* Copies this mesh into a larger mesh and fractalizes it. */
Mesh* Mesh::fractalize() const
{
//////////////////////////
// Calculate the range. //
//////////////////////////
vec4 v1 = this->getVertex(0, 0);
vec4 v2 = this->getVertex(0, 1);
float deltaX = v2[0] - v1[0];
//float deltaY = v2[1] - v1[1];
float deltaZ = v2[2] - v1[2];
float range = sqrt(deltaX*deltaX + deltaZ*deltaZ);
////////////////////////
// Create a new mesh. //
////////////////////////
/* Create a new larger mesh. */
Mesh* newMesh = new Mesh((this->getRows()*2)-2, (this->getCols()*2)-2,
this->getWidth(), this->getDepth(), this->color, this->snowCapHeight);
//////////////////////////////////////
// Copy over the original vertices. //
//////////////////////////////////////
/* Copy over the original values of the Mesh to the new mesh's even */
/* indecies. Run time complexity of O((r*c)/4). */
for (unsigned int newRow = 0, originalRow = 0; newRow < newMesh->getRows();
newRow+=2, originalRow++)
{
for (unsigned int newCol = 0, originalCol = 0; newCol < newMesh->
getCols(); newCol+=2, originalCol++)
{
newMesh->setVertex(newRow, newCol,
this->getVertex(originalRow, originalCol));
}
}
//////////////////////////////////////////////////////////////
// Add new averaged vertices between the original vertices, //
// and change their heights by a random delta. //
//////////////////////////////////////////////////////////////
/* Create new vectors that are averages of the original mesh's vectors. */
/* Run time complexity of O((r*c)/4). */
for (unsigned int r = 0; r < newMesh->getRows(); r+=2)
{
for (unsigned int c = 1; c < newMesh->getCols(); c+=2)
{
vec4 prev = newMesh->getVertex(r, c - 1);
vec4 next = newMesh->getVertex(r, c + 1);
float delta = ((float)rand()/(float)RAND_MAX)*range-(range/2.0f);
newMesh->setVertex(r, c, vec4((prev[0]+next[0])/2.0f,
(prev[1]+next[1])/2.0f+ delta, (prev[2]+next[2])/2.0f, 1.0));
}
}
/* Create new rows that are average of the above and below column */
/* values. Run time complexity of O((r*c)/2). */
for (unsigned int r = 1; r < newMesh->getRows(); r+=2)
{
for (unsigned int c = 0; c < newMesh->getCols(); c++)
{
vec4 prev = newMesh->getVertex(r-1, c);
vec4 next = newMesh->getVertex(r+1, c);
float delta = ((float)rand()/(float)RAND_MAX)*range-(range/2.0f);
newMesh->setVertex(r, c, vec4((prev[0]+next[0])/2.0f,
(prev[1]+next[1])/2.0f+ delta, (prev[2]+next[2])/2.0f, 1.0));
}
}
return newMesh;
}
Mesh* Mesh::smooth() const
{
////////////////////////
// Create a new mesh. //
////////////////////////
/* Create a new larger mesh. */
Mesh* newMesh = new Mesh((this->getRows()*2)-2, (this->getCols()*2)-2,
this->getWidth(), this->getDepth(), this->color, this->snowCapHeight);
//////////////////////////////////////
// Copy over the original vertices. //
//////////////////////////////////////
/* Copy over the original values of the Mesh to the new mesh's even */
/* indecies. Run time complexity of O((r*c)/4). */
for (unsigned int newRow = 0, originalRow = 0; newRow < newMesh->getRows();
newRow+=2, originalRow++)
{
for (unsigned int newCol = 0, originalCol = 0; newCol < newMesh->
getCols(); newCol+=2, originalCol++)
{
newMesh->setVertex(newRow, newCol,
this->getVertex(originalRow, originalCol));
}
}
/////////////////////////////////////////////////////
// Create new vertices at the center of each face. //
/////////////////////////////////////////////////////
/* Average the vertices of the face to make a centered face vertex. */
for (unsigned int r = 1; r < newMesh->getRows(); r+=2)
{
for (unsigned int c = 1; c < newMesh->getCols(); c+=2)
{
/* Determine the face's vertices. */
vec4 avgVec;
/* Sum up the connected vertices. */
avgVec = newMesh->getVertex(r - 1, c - 1);
avgVec = avgVec + newMesh->getVertex(r + 1, c - 1);
avgVec = avgVec + newMesh->getVertex(r + 1, c + 1);
avgVec = avgVec + newMesh->getVertex(r - 1, c + 1);
/* Average the sum. */
avgVec = avgVec / 4.0f;
newMesh->setVertex(r, c, avgVec);
}
}
//////////////////////////////////////////////////////////////
// For each edge in the mesh, create a new edge midpoint //
// vertex that is the average of the adjacent edge vertices //
// and the adjacent face vertices. //
//////////////////////////////////////////////////////////////
/* Iterate through even rows creating edge vertices. */
for (unsigned int r = 0; r < newMesh->getRows(); r+=2)
{
for (unsigned int c = 1; c < newMesh->getCols(); c+=2)
{
float n = 2.0f;
vec4 avgVec;
/* Sum up the connected vertices. */
avgVec = newMesh->getVertex(r, c - 1);
avgVec = avgVec + newMesh->getVertex(r, c + 1);
/* Sum up the connected face vertices. */
if(r > 0)
{
avgVec = avgVec + newMesh->getVertex(r-1, c);
n++;
}
if(r < newMesh->getRows()-1)
{
avgVec = avgVec + newMesh->getVertex(r+1, c);
n++;
}
/* Average the sum. */
avgVec = avgVec / n;
newMesh->setVertex(r, c, avgVec);
}
}
/* Iterate through odd rows creating edge vertices. */
for (unsigned int r = 1; r < newMesh->getRows(); r+=2)
{
for (unsigned int c = 0; c < newMesh->getCols(); c+=2)
{
float n = 2.0f;
vec4 avgVec;
/* Sum up the connected vertices. */
avgVec = newMesh->getVertex(r - 1, c);
avgVec = avgVec + newMesh->getVertex(r + 1, c);
/* Sum up the connected faces. */
if(c > 0)
{
avgVec = avgVec + newMesh->getVertex(r, c - 1);
n++;
}
if(c < newMesh->getCols()-1)
{
avgVec = avgVec + newMesh->getVertex(r, c + 1);
n++;
}
avgVec = avgVec / n;
newMesh->setVertex(r, c, avgVec);
}
}
///////////////////////////////////////////////////////////////////
// Update the original vertices to be an weighted average of the //
// surrounding face centres, edge midpoints, and vertex. //
///////////////////////////////////////////////////////////////////
for (unsigned int r = 0, origR = 0; r < newMesh->getRows(); r += 2,
origR++)
{
for (unsigned int c = 0, origC = 0; c < newMesh->getRows(); c += 2,
origC++)
{
/* Sum up faces the vertex is part of. */
vec4 faceAvg = vec4(0, 0, 0, 0);
float f = 0;
if(r > 0 && c > 0)
{
faceAvg = faceAvg + newMesh->getVertex(r - 1, c - 1);
f++;
}
if(r > 0 && c < newMesh->getCols()-1)
{
faceAvg = faceAvg + newMesh->getVertex(r - 1, c + 1);
f++;
}
if(r < newMesh->getRows()-1 && c > 0)
{
faceAvg = faceAvg + newMesh->getVertex(r + 1, c - 1);
f++;
}
if(r < newMesh->getRows()-1 && c < newMesh->getCols()-1)
{
faceAvg = faceAvg + newMesh->getVertex(r + 1, c + 1);
f++;
}
/* Average */
faceAvg = faceAvg / f;
/* Sum up all of the edge midpoints connected to the vertex. */
vec4 edgeAvg = vec4(0, 0, 0, 0);
float valence = 0.0;
/* Up Edge */
if(r > 0)
{
vec4 edgeMid = (this->getVertex(origR, origC) +
this->getVertex(origR - 1, origC)) / 2.0f;
edgeAvg = edgeAvg + edgeMid;
valence++;
}
/* Down Edge */
if(r < newMesh->getRows()-1)
{
vec4 edgeMid = (this->getVertex(origR, origC) +
this->getVertex(origR + 1, origC)) / 2.0f;
edgeAvg = edgeAvg + edgeMid;
valence++;
}
/* Left Edge */
if(c > 0)
{
vec4 edgeMid = (this->getVertex(origR, origC) +
this->getVertex(origR, origC - 1)) / 2.0f;
edgeAvg = edgeAvg + edgeMid;
valence++;
}
/* Right Edge */
if(c < newMesh->getCols()-1)
{
vec4 edgeMid = (this->getVertex(origR, origC) +
this->getVertex(origR, origC + 1)) / 2.0f;
edgeAvg = edgeAvg + edgeMid;
valence++;
}
/* Average */
edgeAvg = edgeAvg / valence;
/* Move the vertex. */
vec4 origV = newMesh->getVertex(r, c);
/* Update the vertex. */
valence += f;
vec4 newVertex = (faceAvg + (2 * edgeAvg) + (valence-3)*origV) /
valence;
newMesh->setVertex(r, c, newVertex);
}
}
return newMesh;
}
/* Draws this mesh out to 3D space. */
void Mesh::draw(bool displayEdges, bool displayFaces) const
{
/* Draw the vertices */
for (unsigned int r = 0; r < this->vertices->size()-1; r++)
{
for (unsigned int c = 0; c < this->vertices->at(0)->size()-1; c++)
{
std::vector<vec4>* row1 = this->vertices->at(r);
std::vector<vec4>* row2 = this->vertices->at(r + 1);
/* If the edges are choosen to be displayed. */
if(displayEdges)
{
glColor3f(WHITE);
/* Draw triangle one */
glBegin(GL_LINE_LOOP);
glVertex3f(row1->at(c)[0], row1->at(c)[1], row1->at(c)[2]);
glVertex3f(row2->at(c+1)[0], row2->at(c+1)[1],
row2->at(c+1)[2]);
glVertex3f(row2->at(c)[0], row2->at(c)[1], row2->at(c)[2]);
glEnd();
/* Draw triangle two */
glBegin(GL_LINE_LOOP);
glVertex3f(row1->at(c)[0], row1->at(c)[1], row1->at(c)[2]);
glVertex3f(row1->at(c+1)[0], row1->at(c+1)[1],
row1->at(c+1)[2]);
glVertex3f(row2->at(c+1)[0], row2->at(c+1)[1],
row2->at(c+1)[2]);
glEnd();
}
if(displayFaces)
{
//glColor3f(this->color->getRed(), this->color->getGreen(),
// this->color->getBlue());
/* Draw triangle one */
glBegin(GL_POLYGON);
/* Vertex 1 */
this->colorVertices(row1->at(c)[1]);
glVertex3f(row1->at(c)[0], row1->at(c)[1], row1->at(c)[2]);
/* Vertex 2 */
this->colorVertices(row2->at(c+1)[1]);
glVertex3f(row2->at(c+1)[0], row2->at(c+1)[1],
row2->at(c+1)[2]);
/* Vertex 3 */
this->colorVertices(row2->at(c)[1]);
glVertex3f(row2->at(c)[0], row2->at(c)[1], row2->at(c)[2]);
glEnd();
/* Draw triangle two */
glBegin(GL_POLYGON);
/* Vertex 1 */
this->colorVertices(row1->at(c)[1]);
glVertex3f(row1->at(c)[0], row1->at(c)[1], row1->at(c)[2]);
/* Vertex 2 */
this->colorVertices(row1->at(c+1)[1]);
glVertex3f(row1->at(c+1)[0], row1->at(c+1)[1],
row1->at(c+1)[2]);
/* Vertex 3 */
this->colorVertices(row2->at(c+1)[1]);
glVertex3f(row2->at(c+1)[0], row2->at(c+1)[1],
row2->at(c+1)[2]);
glEnd();
}
}
}
}
/* Set the color of for gl based on the given height and the snow */
/* cap height. Color white if above snow height, default color */
/* otherwise. */
void Mesh::colorVertices(const float y) const
{
/* Color white if above snow height. */
if(y >= this->snowCapHeight)
{
glColor3f(WHITE);
}
/* Default color otherwise. */
else
{
glColor3f(this->color->getRed(), this->color->
getGreen(), this->color->getBlue());
}
}