-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·1215 lines (942 loc) · 31.4 KB
/
main.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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <GL/glut.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
//#include <glm/glm.hpp>
#include <vector>
#include <stb_image.c>
#include <glext.h>
#define PI 3.1415926535897932384626433832795028841971693993751058209
using namespace std;
//globals for looking at I
float xrot, yrot;
float zoom = -6.0;
//forward class declarations
class vec;
class vertex;
class face;
class halfEdge;
bool checkCC;
bool checkFlush;
bool showMesh;
//this class is for a vector data type
class vec{
public:
float x, y, z;
};
//this class is for a vertex data type
class vertex{
public:
vertex::vertex();
double x, y, z;
halfEdge * oneOutEdge;
vec normal;
};
//this class stores a face
class face{
public:
face::face();
vertex *v1;
vertex *v2;
vertex *v3;
vertex *v4;
halfEdge * oneSideEdge;
vertex * facePoint;
};
class halfEdge{
public:
halfEdge::halfEdge();
vertex * start;
vertex * end;
halfEdge * next;
halfEdge * opposite;
face * heFace;
vertex* edgePoint;
halfEdge* firstHalf;
halfEdge* secondHalf;
};
//constructors
halfEdge::halfEdge(){
start = NULL;
end = NULL;
next = NULL;
opposite = NULL;
heFace = NULL;
edgePoint = NULL;
firstHalf = NULL;
secondHalf = NULL;
}
face::face(){
oneSideEdge = NULL;
facePoint = NULL;
v1 = v2 = v3 = v4 = NULL;
}
vertex::vertex(){
oneOutEdge = NULL;
x = y = z = 0;
}
//bezier control points
vertex cntlPt0;
vertex cntlPt1;
vertex cntlPt2;
vertex cntlPt3;
double t;
bool dirswitch;
//for textures
GLuint texNames[2];
//global mesh vectors
std::vector<halfEdge*> glEdgeVect;
std::vector<vertex*> glVertVect;
std::vector<face*> glFaceVect;
//returns the magnitude of the vector input
float magnitude(float x, float y, float z){
return sqrt(x*x + y*y + z*z);
}
/*given a vector, this function will unitize it
*/
void unitize(vec & inVec){
float currx = inVec.x;
float curry = inVec.y;
float currz = inVec.z;
float length = magnitude(currx, curry, currz);
inVec.x = currx/length;
inVec.y = curry/length;
inVec.z = currz/length;
}
/*this will compute the cross product of 2 vectors, and put that resulting vector into
a third vector passed in as an argument via pointers.
*/
void xprod(float ax, float ay, float az, float bx, float by, float bz, vec &inVec){
inVec.x = (ay* bz) - (by*az);
inVec.y = (az*bx) - (bz*ax);
inVec.z = (ax*by) - (bx* ay);
}
/*goes through every face, and creates a face vertex and points that faces facePoint vector to its face point
*/
void makeFacePoints(std::vector<face*> &faceVect, std::vector<vertex*> &vertVect){
std::vector<face*>::iterator it;
for(it = faceVect.begin(); it < faceVect.end(); it++){
//load the verts of this face
vertex vert1 = *((*it)->v1);
vertex vert2 = *((*it)->v2);
vertex vert3 = *((*it)->v3);
vertex vert4 = *((*it)->v4);
//make new vertex that is average of this faces verts
vertex * newFacePoint = new vertex;
(*newFacePoint).x = (vert1.x + vert2.x + vert3.x + vert4.x)/4;
(*newFacePoint).y = (vert1.y + vert2.y + vert3.y + vert4.y)/4;
(*newFacePoint).z = (vert1.z + vert2.z + vert3.z + vert4.z)/4;
vertVect.push_back(newFacePoint);
(*it)->facePoint = newFacePoint;
}
}
//construct all of the edge points, and store in existing edges
void makeEdgePoints(std::vector<halfEdge*> &HEvect, std::vector<vertex*> &vertVect){
std::vector<halfEdge*>::iterator it;
for(it = HEvect.begin(); it < HEvect.end(); it++){
if( (*it)->opposite->edgePoint != NULL){
(*it)->edgePoint = (*it)->opposite->edgePoint;
}
else{
vertex faceVert1 = *((*it)->heFace->facePoint);
vertex faceVert2 = *((*it)->opposite->heFace->facePoint);
vertex edgeVert1 = *((*it)->end);
vertex edgeVert2 = *((*it)->opposite->end);
vertex * newEdgeVertex = new vertex;
//average other stuff here
(*newEdgeVertex).x = (faceVert1.x + faceVert2.x + edgeVert1.x + edgeVert2.x)/4;
(*newEdgeVertex).y = (faceVert1.y + faceVert2.y + edgeVert1.y + edgeVert2.y)/4;
(*newEdgeVertex).z = (faceVert1.z + faceVert2.z + edgeVert1.z + edgeVert2.z)/4;
vertVect.push_back(newEdgeVertex);
(*it)->edgePoint = newEdgeVertex;
}
}
}
//adjust all of the old vertex points based on the new edge and face points that were calculated above.
void makeAdjustedVerts(std::vector<vertex*> & vertVect){
std::vector<vertex*>::iterator it;
halfEdge* edge1;
halfEdge* edge2;
halfEdge* edge3;
halfEdge* edge4;
halfEdge* edge5;
vertex facePoint1;
vertex facePoint2;
vertex facePoint3;
vertex facePoint4;
vertex facePoint5;
vertex facePointAvg;
vertex edgePoint1;
vertex edgePoint2;
vertex edgePoint3;
vertex edgePoint4;
vertex edgePoint5;
vertex edgePointAvg;
vertex * currVert;
for(it = vertVect.begin(); it < vertVect.end(); it ++){
currVert = *it;
//check if vertex has been checked already
edge1 = currVert->oneOutEdge;
edge2 = edge1->next->next->next->opposite;
edge3 = edge2->next->next->next->opposite;
edge4 = edge3->next->next->next->opposite;
edge5 = edge4->next->next->next->opposite;
edgePoint1 = *(edge1->edgePoint);
edgePoint2 = *(edge2->edgePoint);
edgePoint3 = *(edge3->edgePoint);
edgePoint4 = *(edge4->edgePoint);
edgePoint5 = *(edge5->edgePoint);
facePoint1 = *(edge1->heFace->facePoint);
facePoint2 = *(edge2->heFace->facePoint);
facePoint3 = *(edge3->heFace->facePoint);
facePoint4 = *(edge4->heFace->facePoint);
facePoint5 = *(edge5->heFace->facePoint);
int divnum,multnum;
if(edge1 == edge4){
//average the face points and the edge points
edgePointAvg.x = (edgePoint1.x + edgePoint2.x + edgePoint3.x)/3;
edgePointAvg.y = (edgePoint1.y + edgePoint2.y + edgePoint3.y)/3;
edgePointAvg.z = (edgePoint1.z + edgePoint2.z + edgePoint3.z)/3;
facePointAvg.x = (facePoint1.x + facePoint2.x + facePoint3.x)/3;
facePointAvg.y = (facePoint1.y + facePoint2.y + facePoint3.y)/3;
facePointAvg.z = (facePoint1.z + facePoint2.z + facePoint3.z)/3;
divnum =4;
multnum = 1;
}
else if(edge1 == edge5){
//average the face points and the edge points
edgePointAvg.x = (edgePoint1.x + edgePoint2.x + edgePoint3.x + edgePoint4.x)/4;
edgePointAvg.y = (edgePoint1.y + edgePoint2.y + edgePoint3.y + edgePoint4.y)/4;
edgePointAvg.z = (edgePoint1.z + edgePoint2.z + edgePoint3.z + edgePoint4.z)/4;
facePointAvg.x = (facePoint1.x + facePoint2.x + facePoint3.x + facePoint4.x)/4;
facePointAvg.y = (facePoint1.y + facePoint2.y + facePoint3.y + facePoint4.y)/4;
facePointAvg.z = (facePoint1.z + facePoint2.z + facePoint3.z + facePoint4.z)/4;
divnum =4;
multnum = 1;
}
else{
//average the face points and the edge points
edgePointAvg.x = (edgePoint1.x + edgePoint2.x + edgePoint3.x + edgePoint4.x + edgePoint5.x)/5;
edgePointAvg.y = (edgePoint1.y + edgePoint2.y + edgePoint3.y + edgePoint4.y + edgePoint5.y)/5;
edgePointAvg.z = (edgePoint1.z + edgePoint2.z + edgePoint3.z + edgePoint4.z + edgePoint5.z)/5;
facePointAvg.x = (facePoint1.x + facePoint2.x + facePoint3.x + facePoint4.x + facePoint5.x)/5;
facePointAvg.y = (facePoint1.y + facePoint2.y + facePoint3.y + facePoint4.y + facePoint5.y)/5;
facePointAvg.z = (facePoint1.z + facePoint2.z + facePoint3.z + facePoint4.z + facePoint5.z)/5;
divnum =5;
multnum = 2;
}
//update the current vector based off of averages just computed
currVert->x = (multnum*currVert->x + 2*edgePointAvg.x + facePointAvg.x)/divnum;
currVert->y = (multnum*currVert->y + 2*edgePointAvg.y + facePointAvg.y)/divnum;
currVert->z = (multnum*currVert->z + 2*edgePointAvg.z + facePointAvg.z)/divnum;
}
}
//take all of the new edge and face points, as well as the adjusted old vertices, and connect them together with
//a new set of faces and edges
void compileNewMesh(std::vector<face*> &faceVect, std::vector<face*> &newFaceVect, std::vector<halfEdge*> &newEdgeVect){
std::vector<face*>::iterator faceIt;
for(faceIt = faceVect.begin(); faceIt < faceVect.end(); faceIt ++){
face currFace = **faceIt;
halfEdge* edge1 = currFace.oneSideEdge;
halfEdge *edge2 = edge1->next;
halfEdge *edge3 = edge2->next;
halfEdge *edge4 = edge3->next;
halfEdge* edge1a = new halfEdge;
halfEdge* edge1b = new halfEdge;
halfEdge* edge2a = new halfEdge;
halfEdge* edge2b = new halfEdge;
halfEdge* edge3a = new halfEdge;
halfEdge* edge3b = new halfEdge;
halfEdge* edge4a = new halfEdge;
halfEdge* edge4b = new halfEdge;
halfEdge* edge1in = new halfEdge;
halfEdge* edge1out = new halfEdge;
halfEdge* edge2in = new halfEdge;
halfEdge* edge2out = new halfEdge;
halfEdge* edge3in = new halfEdge;
halfEdge* edge3out = new halfEdge;
halfEdge* edge4in = new halfEdge;
halfEdge* edge4out = new halfEdge;
//construct border edges
edge1->firstHalf = edge1a;
edge1->secondHalf = edge1b;
edge2->firstHalf = edge2a;
edge2->secondHalf = edge2b;
edge3->firstHalf = edge3a;
edge3->secondHalf = edge3b;
edge4->firstHalf = edge4a;
edge4->secondHalf = edge4b;
//replace edge1
edge1a->start = edge1->start;
edge1a->end = edge1->edgePoint;
edge1b->start = edge1->edgePoint;
edge1b->end = edge1->end;
if(edge1->opposite->firstHalf != NULL){
edge1b->opposite = edge1->opposite->firstHalf;
edge1a->opposite = edge1->opposite->secondHalf;
edge1->opposite->firstHalf->opposite = edge1b;
edge1->opposite->secondHalf->opposite = edge1a;
}
//adjust edge pointer from start vertex
edge1->start->oneOutEdge = edge1a;
//inner edge from edge 1
edge1in->start = edge1->edgePoint;
edge1in->end = currFace.facePoint;
edge1out->end = edge1->edgePoint;
edge1out->start = currFace.facePoint;
edge1in->opposite = edge1out;
edge1out->opposite = edge1in;
//set edge pointer from edge1 vertex
edge1->edgePoint->oneOutEdge = edge1in;
//replace edge2
edge2a->start = edge2->start;
edge2a->end = edge2->edgePoint;
edge2b->start = edge2->edgePoint;
edge2b->end = edge2->end;
if(edge2->opposite->firstHalf != NULL){
edge2b->opposite = edge2->opposite->firstHalf;
edge2a->opposite = edge2->opposite->secondHalf;
edge2->opposite->firstHalf->opposite = edge2b;
edge2->opposite->secondHalf->opposite = edge2a;
}
//adjust edge pointer from start vertex
edge2->start->oneOutEdge = edge2a;
//inner edge from edge 2
edge2in->start = edge2->edgePoint;
edge2in->end = currFace.facePoint;
edge2out->end = edge2->edgePoint;
edge2out->start = currFace.facePoint;
edge2in->opposite = edge2out;
edge2out->opposite = edge2in;
//set edge pointer from edge2 vertex
edge2->edgePoint->oneOutEdge = edge2in;
//replace edge3
edge3a->start = edge3->start;
edge3a->end = edge3->edgePoint;
edge3b->start = edge3->edgePoint;
edge3b->end = edge3->end;
if(edge3->opposite->firstHalf != NULL){
edge3b->opposite = edge3->opposite->firstHalf;
edge3a->opposite = edge3->opposite->secondHalf;
edge3->opposite->firstHalf->opposite = edge3b;
edge3->opposite->secondHalf->opposite = edge3a;
}
//adjust edge pointer from start vertex
edge2->start->oneOutEdge = edge2a;
//inner edge from edge 3
edge3in->start = edge3->edgePoint;
edge3in->end = currFace.facePoint;
edge3out->end = edge3->edgePoint;
edge3out->start = currFace.facePoint;
edge3in->opposite = edge3out;
edge3out->opposite = edge3in;
//set edge pointer from edge3 vertex
edge3->edgePoint->oneOutEdge = edge3in;
//replace edge4
edge4a->start = edge4->start;
edge4a->end = edge4->edgePoint;
edge4b->start = edge4->edgePoint;
edge4b->end = edge4->end;
if(edge4->opposite->firstHalf != NULL){
edge4b->opposite = edge4->opposite->firstHalf;
edge4a->opposite = edge4->opposite->secondHalf;
//modify so that BOTH sets of half edges have
edge4->opposite->firstHalf->opposite = edge4b;
edge4->opposite->secondHalf->opposite = edge4a;
}
//adjust edge pointer from start vertex
edge4->start->oneOutEdge = edge4a;
//inner edge from edge 4
edge4in->start = edge4->edgePoint;
edge4in->end = currFace.facePoint;
edge4out->end = edge4->edgePoint;
edge4out->start = currFace.facePoint;
edge4in->opposite = edge4out;
edge4out->opposite = edge4in;
//set edge pointer from edge4 vertex
edge4->edgePoint->oneOutEdge = edge4in;
//set face vertex edge pointer
currFace.facePoint->oneOutEdge = edge4out;
//set all next pointers
edge1a->next = edge1in;
edge1in->next = edge4out;
edge4out->next = edge4b;
edge4b->next = edge1a;
edge2a->next = edge2in;
edge2in->next = edge1out;
edge1out->next = edge1b;
edge1b->next = edge2a;
edge3a->next = edge3in;
edge3in->next = edge2out;
edge2out->next = edge2b;
edge2b->next = edge3a;
edge4a->next = edge4in;
edge4in->next = edge3out;
edge3out->next = edge3b;
edge3b->next = edge4a;
//create new faces/connect all face pointers
face *face1 = new face;
face1->v1 = edge1a->start;
edge1a->heFace = face1;
face1->v2 = edge1in->start;
edge1in->heFace = face1;
face1->v3 = edge4out->start;
edge4out->heFace = face1;
face1->v4 = edge4b->start;
edge4b->heFace = face1;
face1->oneSideEdge = edge4b;
face *face2 = new face;
face2->v1 = edge2a->start;
edge2a->heFace = face2;
face2->v2 = edge2in->start;
edge2in->heFace = face2;
face2->v3 = edge1out->start;
edge1out->heFace = face2;
face2->v4 = edge1b->start;
edge1b->heFace = face2;
face2->oneSideEdge = edge1b;
face *face3 = new face;
face3->v1 = edge3a->start;
edge3a->heFace = face3;
face3->v2 = edge3in->start;
edge3in->heFace = face3;
face3->v3 = edge2out->start;
edge2out->heFace = face3;
face3->v4 = edge2b->start;
edge2b->heFace = face3;
face3->oneSideEdge = edge2b;
face *face4 = new face;
face4->v1 = edge4a->start;
edge4a->heFace = face4;
face4->v2 = edge4in->start;
edge4in->heFace = face4;
face4->v3 = edge3out->start;
edge3out->heFace = face4;
face4->v4 = edge3b->start;
edge3b->heFace = face4;
face4->oneSideEdge = edge3b;
//load each face into the new face vector
newFaceVect.push_back(face1);
newFaceVect.push_back(face2);
newFaceVect.push_back(face3);
newFaceVect.push_back(face4);
//push all new edges onto the new edge vector
newEdgeVect.push_back(edge1a);
newEdgeVect.push_back(edge1b);
newEdgeVect.push_back(edge2a);
newEdgeVect.push_back(edge2b);
newEdgeVect.push_back(edge3a);
newEdgeVect.push_back(edge3b);
newEdgeVect.push_back(edge4a);
newEdgeVect.push_back(edge4b);
newEdgeVect.push_back(edge1in);
newEdgeVect.push_back(edge1out);
newEdgeVect.push_back(edge2in);
newEdgeVect.push_back(edge2out);
newEdgeVect.push_back(edge3in);
newEdgeVect.push_back(edge3out);
newEdgeVect.push_back(edge4in);
newEdgeVect.push_back(edge4out);
}
}
//compute the normal vector of the vertex at the start of the input half edge
vec getNormal(halfEdge currEdge){
vertex v1 = *(currEdge.start);
vertex v2 = *(currEdge.end);
vertex v3 = *((currEdge.next)->end);
vec outNorm;
xprod((v2.x-v1.x),(v2.y-v1.y),(v2.z-v1.z),(v3.x-v2.x),(v3.y-v2.y),(v3.z-v2.z), outNorm);
return outNorm;
}
//iterate over every vertex in the mesh and compute its normal
void computeNormals(std::vector<vertex*> &vertVect){
std::vector<vertex*>::iterator vIt;
halfEdge* edge1;
halfEdge* edge2;
halfEdge* edge3;
halfEdge* edge4;
halfEdge* edge5;
vec norm1;
vec norm2;
vec norm3;
vec norm4;
vec norm5;
vec avgNorm;
vertex * currVert;
for(vIt =vertVect.begin(); vIt < vertVect.end(); vIt ++){
currVert = *vIt;
//check if vertex has been checked already
edge1 = currVert->oneOutEdge;
edge2 = edge1->next->next->next->opposite;
edge3 = edge2->next->next->next->opposite;
edge4 = edge3->next->next->next->opposite;
edge5 = edge4->next->next->next->opposite;
norm1 = getNormal(*edge1);
norm2 = getNormal(*edge2);
norm3 = getNormal(*edge3);
norm4 = getNormal(*edge4);
norm5 = getNormal(*edge5);
if(edge1 == edge4){
avgNorm.x = (norm1.x + norm2.x + norm3.x)/3;
avgNorm.y = (norm1.y + norm2.y + norm3.y)/3;
avgNorm.z = (norm1.z + norm2.z + norm3.z)/3;
}
else if(edge1 == edge5){
//average the face points and the edge points
avgNorm.x = (norm1.x + norm2.x + norm3.x + norm4.x)/4;
avgNorm.y = (norm1.y + norm2.y + norm3.y + norm4.y)/4;
avgNorm.z = (norm1.z + norm2.z + norm3.z + norm4.z)/4;
}
else{
avgNorm.x = (norm1.x + norm2.x + norm3.x + norm4.x + norm5.x)/5;
avgNorm.y = (norm1.y + norm2.y + norm3.y + norm4.y + norm5.y)/5;
avgNorm.z = (norm1.z + norm2.z + norm3.z + norm4.z + norm5.z)/5;
}
/*
avgNorm.x *= -1;
avgNorm.y *= -1;
avgNorm.z *= -1;
*/
unitize(avgNorm);
currVert->normal = avgNorm;
}
}
/* this function is the overall catmull-clark function, responsible for calling all of the above functions in sequence
to create the new subdivided mesh*/
void fullCC(){
std::vector<face*> nFaceVect;
std::vector<halfEdge*> nEdgeVect;
std::vector<vertex*> oldVertVect;
halfEdge * tempEdge;
face * tempFace;
oldVertVect = glVertVect;
makeFacePoints(glFaceVect, glVertVect);
makeEdgePoints(glEdgeVect, glVertVect);
makeAdjustedVerts(oldVertVect);
compileNewMesh(glFaceVect, nFaceVect, nEdgeVect);
computeNormals(glVertVect);
//delete all old edges and faces
while(!glFaceVect.empty()){
tempFace = glFaceVect.back();
glFaceVect.pop_back();
delete tempFace;
}
while(!glEdgeVect.empty()){
tempEdge = glEdgeVect.back();
glEdgeVect.pop_back();
delete tempEdge;
}
glFaceVect = nFaceVect;
glEdgeVect = nEdgeVect;
}
//this function is called below as part of the creation of the initial block I
void initMakeFace(vertex * va, vertex* vb, vertex * vc, vertex * vd,
std::vector<face*> &faceVect,
std::vector<halfEdge*> &edgeVect){
face * nextFace = new face;
halfEdge *e1, *e2, *e3, *e4;
e1 = new halfEdge;
e2 = new halfEdge;
e3 = new halfEdge;
e4 = new halfEdge;
//set face's vertex pointers
nextFace->v1 = va;
nextFace->v2 = vb;
nextFace->v3 = vc;
nextFace->v4 = vd;
//set start and end pointers
e1->start = va;
e2->start = vb;
e3->start = vc;
e4->start = vd;
e1->end = vb;
e2->end = vc;
e3->end = vd;
e4->end = va;
//set oneOutEdge pointers
va->oneOutEdge = e1;
vb->oneOutEdge = e2;
vc->oneOutEdge = e3;
vd->oneOutEdge = e4;
//set next pointers
e1->next = e2;
e2->next = e3;
e3->next = e4;
e4->next = e1;
//set face pointers
e1->heFace = nextFace;
e2->heFace = nextFace;
e3->heFace = nextFace;
e4->heFace = nextFace;
//set faces edge pointer
nextFace->oneSideEdge = e4;
//push everything
faceVect.push_back(nextFace);
edgeVect.push_back(e1);
edgeVect.push_back(e2);
edgeVect.push_back(e3);
edgeVect.push_back(e4);
}
//this function deletes the current mesh being displayed(if any) and creates a new, un subdivided block I to display
void flushAndReset(std::vector<face*> &faceVect, std::vector<halfEdge*> &edgeVect, std::vector<vertex*> &vertVect){
std::vector<face*>::iterator faceIt;
std::vector<halfEdge*>::iterator edgeIt;
vertex * tempVert;
halfEdge * tempEdge;
face * tempFace;
//clean out old mesh
while(!faceVect.empty()){
tempFace = faceVect.back();
faceVect.pop_back();
delete tempFace;
}
while(!edgeVect.empty()){
tempEdge = edgeVect.back();
edgeVect.pop_back();
delete tempEdge;
}
while(!vertVect.empty()){
tempVert = vertVect.back();
vertVect.pop_back();
delete tempVert;
}
//make new mesh
vertex * v1 = new vertex;
vertex * v2 = new vertex;
vertex * v3 = new vertex;
vertex * v4 = new vertex;
vertex * v5 = new vertex;
vertex * v6 = new vertex;
vertex * v7 = new vertex;
vertex * v8 = new vertex;
vertex * v9 = new vertex;
vertex * v10 = new vertex;
vertex * v11 = new vertex;
vertex * v12 = new vertex;
vertex * v13 = new vertex;
vertex * v14 = new vertex;
vertex * v15 = new vertex;
vertex * v16 = new vertex;
vertex * v17 = new vertex;
vertex * v18 = new vertex;
vertex * v19 = new vertex;
vertex * v20 = new vertex;
vertex * v21 = new vertex;
vertex * v22 = new vertex;
vertex * v23 = new vertex;
vertex * v24 = new vertex;
vertex * v25 = new vertex;
vertex * v26 = new vertex;
vertex * v27 = new vertex;
vertex * v28 = new vertex;
vertex * v29 = new vertex;
vertex * v30 = new vertex;
vertex * v31 = new vertex;
vertex * v32 = new vertex;
//push on all new verts
vertVect.push_back(v1);
vertVect.push_back(v2);
vertVect.push_back(v3);
vertVect.push_back(v4);
vertVect.push_back(v5);
vertVect.push_back(v6);
vertVect.push_back(v7);
vertVect.push_back(v8);
vertVect.push_back(v9);
vertVect.push_back(v10);
vertVect.push_back(v11);
vertVect.push_back(v12);
vertVect.push_back(v13);
vertVect.push_back(v14);
vertVect.push_back(v15);
vertVect.push_back(v16);
vertVect.push_back(v17);
vertVect.push_back(v18);
vertVect.push_back(v19);
vertVect.push_back(v20);
vertVect.push_back(v21);
vertVect.push_back(v22);
vertVect.push_back(v23);
vertVect.push_back(v24);
vertVect.push_back(v25);
vertVect.push_back(v26);
vertVect.push_back(v27);
vertVect.push_back(v28);
vertVect.push_back(v29);
vertVect.push_back(v30);
vertVect.push_back(v31);
vertVect.push_back(v32);
//set vertex coordinates
//set all x coords
v1->x = v5->x = v9->x = v13->x = v17->x = v21->x = v25->x = v29->x = -1;
v2->x = v6->x = v10->x = v14->x = v18->x = v22->x = v26->x = v30->x = -.2;
v3->x = v7->x = v11->x = v15->x = v19->x = v23->x = v27->x = v31->x = .2;
v4->x = v8->x = v12->x = v16->x = v20->x = v24->x = v28->x = v32->x = 1;
//set all y coords
v1->y = v2->y = v3->y = v4->y = v17->y = v18->y = v19->y = v20->y = 1;
v5->y = v6->y = v7->y = v8->y = v21->y = v22->y = v23->y = v24->y = .6;
v9->y = v10->y = v11->y = v12->y = v25->y = v26->y = v27->y = v28->y = -.6;
v13->y = v14->y = v15->y = v16->y = v29->y = v30->y = v31->y = v32->y = -1;
//set all z coords
v1->z = v2->z = v3->z = v4->z = v5->z = v6->z = v7->z = v8->z = v9->z = v10->z = v11->z = v12->z = v13->z = v14->z = v15->z = v16->z =.2;
v17->z = v18->z = v19->z = v20->z = v21->z = v22->z = v23->z = v24->z = v25->z = v26->z = v27->z = v28->z = v29->z = v30->z = v31->z = v32->z =-.2;
//call face constructor function for each face/quad of verts
//front face of I
initMakeFace(v5, v6, v2, v1, faceVect, edgeVect);
initMakeFace(v6, v7, v3, v2, faceVect, edgeVect);
initMakeFace(v7, v8, v4, v3, faceVect, edgeVect);
initMakeFace(v10, v11, v7, v6, faceVect, edgeVect);
initMakeFace(v13, v14, v10, v9, faceVect, edgeVect);
initMakeFace(v14, v15, v11, v10, faceVect, edgeVect);
initMakeFace(v15, v16, v12, v11, faceVect, edgeVect);
//back face of I
initMakeFace(v17, v18, v22, v21, faceVect, edgeVect);
initMakeFace(v18, v19, v23, v22, faceVect, edgeVect);
initMakeFace(v19, v20, v24, v23, faceVect, edgeVect);
initMakeFace(v22, v23, v27, v26, faceVect, edgeVect);
initMakeFace(v25, v26, v30, v29, faceVect, edgeVect);
initMakeFace(v26, v27, v31, v30, faceVect, edgeVect);
initMakeFace(v27, v28, v32, v31, faceVect, edgeVect);
//top face
initMakeFace(v18, v17, v1, v2, faceVect, edgeVect);
initMakeFace(v19, v18, v2, v3, faceVect, edgeVect);
initMakeFace(v20, v19, v3, v4, faceVect, edgeVect);
//bottom face
initMakeFace(v14, v13, v29, v30, faceVect, edgeVect);
initMakeFace(v15, v14, v30, v31, faceVect, edgeVect);
initMakeFace(v16, v15, v31, v32, faceVect, edgeVect);
//in-top faces
initMakeFace(v6, v5, v21, v22, faceVect, edgeVect);
initMakeFace(v8, v7, v23, v24, faceVect, edgeVect);
//in-bottom faces
initMakeFace(v9, v10, v26, v25, faceVect, edgeVect);
initMakeFace(v11, v12, v28, v27, faceVect, edgeVect);
//right side
initMakeFace(v4, v8, v24, v20, faceVect, edgeVect);
initMakeFace(v12, v16, v32, v28, faceVect, edgeVect);
initMakeFace(v7, v11, v27, v23, faceVect, edgeVect);
//left side
initMakeFace(v17, v21, v5, v1, faceVect, edgeVect);
initMakeFace(v22, v26, v10, v6, faceVect, edgeVect);
initMakeFace(v25, v29, v13, v9, faceVect, edgeVect);
std::vector<halfEdge*>::iterator edgeIt1;
std::vector<halfEdge*>::iterator edgeIt2;
for( edgeIt1 = edgeVect.begin(); edgeIt1 < edgeVect.end(); edgeIt1 ++){
for(edgeIt2 = edgeIt1 +1; edgeIt2 < edgeVect.end(); edgeIt2++){
if(((*edgeIt1)->start == (*edgeIt2)->end) &&((*edgeIt1)->end == (*edgeIt2)->start)){
(*edgeIt1)->opposite = *edgeIt2;
(*edgeIt2)->opposite = *edgeIt1;
}
}
}
}
void init(void)
{
GLfloat white[] = {1.0,1.0,1.0,1.0};
GLfloat purple[] = {0.5, 0.11, 0.87};
//GLfloat lpos[] = {0.0, 0.0, -5.0, 0.0};
GLfloat grey[] = {.3,.3,.3};
GLfloat lpos[] = {6.0, 9.0, 12.0, 0.0};
GLfloat amb[] = {0.0,0.0,0.0,1.0};
GLfloat color[] = {.4, .2, 1.0, 1.0};
GLfloat ambl[] = {0.2,0.2,0.2,1};
//lighting stuff
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
glLightfv(GL_LIGHT0, GL_SPECULAR, white);
glEnable(GL_LIGHT0);
glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, ambl);
glEnable(GL_LIGHTING);
//material stuff (part of lighting stuff)/*
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, white);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(2, texNames);
//these next 5 blocks of code is responsible for loading a different texture image from a file and putting it into video memory
glBindTexture(GL_TEXTURE_2D, texNames[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
int imageWidth, imageHeight, imageComp;
imageWidth = imageHeight = imageComp = 0;
unsigned char * texture = NULL;
texture = stbi_load(/*"C:/Users/Jeff/Documents/Visual Studio 2010/Projects/MP2/Debug/ */"rbflow.jpeg", &imageWidth, &imageHeight, &imageComp, 3);
//load texture into viedo memory
// texture = NULL;
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, texture);
//set up new mesh of the I