-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject1.cpp
More file actions
1200 lines (1052 loc) · 29.6 KB
/
Copy pathproject1.cpp
File metadata and controls
1200 lines (1052 loc) · 29.6 KB
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 <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
using namespace std;
#if 0
#define DRAW_LAST_EDGE_SINCE_START
#else
#undef DRAW_LAST_EDGE_SINCE_START
#endif
/* Global Definitions */
#define WINDOW_WIDTH 600
#define WINDOW_HEIGHT 500
#define DISTANCE(x,y) abs((x)-(y))
#define MIN_VERTEX_NUM 3
#define SHOW_CLIP_POLY_MILLIS 2500
#define NUM_OF_COLORS 16
#define ENTRY(x,offset) #x,x+offset
#define COLOR_TO_RGB(x) color[x].r,color[x].g,color[x].b
#define ADD_COLOR_ENTRIES(offset) { \
glutAddMenuEntry(ENTRY(BLACK,offset)); \
glutAddMenuEntry(ENTRY(WHITE,offset)); \
glutAddMenuEntry(ENTRY(RED,offset)); \
glutAddMenuEntry(ENTRY(GREEN,offset)); \
glutAddMenuEntry(ENTRY(BLUE,offset)); \
glutAddMenuEntry(ENTRY(YELLOW,offset)); \
glutAddMenuEntry(ENTRY(SIENNA,offset)); \
glutAddMenuEntry(ENTRY(ORANGE,offset)); \
glutAddMenuEntry(ENTRY(INDIGO,offset)); \
glutAddMenuEntry(ENTRY(MAGENTA,offset)); \
glutAddMenuEntry(ENTRY(VIOLET,offset)); \
glutAddMenuEntry(ENTRY(SILVER,offset)); \
glutAddMenuEntry(ENTRY(ROYAL_BLUE,offset)); \
glutAddMenuEntry(ENTRY(CYAN,offset)); \
glutAddMenuEntry(ENTRY(CHARTREUSE,offset)); \
glutAddMenuEntry(ENTRY(GOLD,offset)); \
}
enum color_e {BLACK, WHITE, RED, GREEN, BLUE, YELLOW, SIENNA, ORANGE, INDIGO,
MAGENTA, VIOLET, SILVER, ROYAL_BLUE, CYAN, CHARTREUSE, GOLD};
enum option_e {MENU_EXIT = 2*NUM_OF_COLORS, MENU_POLYGON, MENU_MOVE_VERTEX,
MENU_CLIPPING, MENU_EXTRUDE, MENU_EXIT3D};
enum state_e {NORMAL, DRAWING_POLYGON, MOVING_VERTEX, CLIPPING, EXTRUSION};
enum clipstate_e {CLIPPING_START, CLIPPING_END};
typedef struct color_s {
GLubyte r;
GLubyte g;
GLubyte b;
} color_t;
color_t color[NUM_OF_COLORS] = {{0x00, 0x00, 0x00}, // BLACK
{0xff, 0xff, 0xff}, // WHITE
{0xff, 0x00, 0x00}, // RED
{0x00, 0x80, 0x00}, // GREEN
{0x00, 0x00, 0xff}, // BLUE
{0xff, 0xff, 0x00}, // YELLOW
{0xa0, 0x52, 0x2d}, // SIENNA
{0xff, 0xa5, 0x00}, // ORANGE
{0x4b, 0x00, 0x82}, // INDIGO
{0xff, 0x00, 0xff}, // MAGENTA
{0xee, 0x82, 0xee}, // VIOLET
{0xc0, 0xc0, 0xc0}, // SILVER
{0x41, 0x69, 0xe1}, // ROYAL_BLUE
{0x00, 0xff, 0xff}, // CYAN
{0x7f, 0xff, 0x00}, // CHARTREUSE
{0xff, 0xd7, 0x00}}; // GOLD
/* Class definitions */
class Vertex
{
private:
GLint x;
GLint y;
public:
Vertex(int, int);
Vertex(int, int, int);
Vertex(const Vertex&);
const GLint& get_x() const;
const GLint& get_y() const;
void update(int, int);
Vertex operator-(Vertex v1);
Vertex operator+(Vertex v1);
Vertex operator*(float c);
bool operator==(const Vertex rhs);
bool operator!=(const Vertex rhs);
bool operator<(const Vertex rhs);
friend ostream& operator<<(ostream &strm, const Vertex &v);
bool in_range(int, int, int);
bool in_x_range(int, int, int);
};
Vertex::Vertex(int x, int y)
{
update(x, y);
}
Vertex::Vertex(const Vertex &v)
{
update(v.x, v.y);
}
void Vertex::update(int x, int y)
{
this->x = x;
this->y = y;
}
const GLint& Vertex::get_x() const
{
return x;
}
const GLint& Vertex::get_y() const
{
return y;
}
Vertex Vertex::operator-(Vertex v)
{
Vertex retv = Vertex(x - v.x, y - v.y);
return retv;
}
Vertex Vertex::operator+(Vertex v)
{
Vertex retv = Vertex(x + v.x, y + v.y);
return retv;
}
Vertex Vertex::operator*(float c)
{
Vertex retv = Vertex(x*c, y*c);
return retv;
}
bool Vertex::operator==(const Vertex rhs)
{
return x == rhs.x && y == rhs.y;
}
bool Vertex::operator!=(const Vertex rhs)
{
return x != rhs.x && y != rhs.y;
}
bool Vertex::operator<(const Vertex rhs)
{
return x < rhs.x && y < rhs.y;
}
ostream& operator<<(ostream &strm, const Vertex &v)
{
return strm << "Vertex(" << v.x << ", " << v.y << ")" << endl;
}
bool Vertex::in_range(int x, int y, int radial)
{
return (DISTANCE(this->x, x) <= radial && DISTANCE(this->y, y) <= radial);
}
bool Vertex::in_x_range(int x, int y, int threshold)
{
return (DISTANCE(this->x, x) <= threshold && this->y == y);
}
class Triangle
{
private:
Vertex v0;
Vertex v1;
Vertex v2;
public:
Triangle(Vertex v0, Vertex v1, Vertex v2);
const Vertex& get_v0() const;
const Vertex& get_v1() const;
const Vertex& get_v2() const;
};
Triangle::Triangle(Vertex v0, Vertex v1, Vertex v2)
: v0(v0), v1(v1), v2(v2)
{
// nothing else to do here!
};
const Vertex& Triangle::get_v0() const
{
return v0;
}
const Vertex& Triangle::get_v1() const
{
return v1;
}
const Vertex& Triangle::get_v2() const
{
return v2;
}
class Polygon
{
private:
vector<Vertex> vertices;
vector<Triangle> triangles;
color_e line_clr;
color_e fill_clr;
int extrusion_length;
public:
Polygon(color_e, color_e);
vector<Vertex>& get_vertices();
vector<Triangle>& get_triangles();
const color_e& get_line_clr() const;
const color_e& get_fill_clr() const;
const int& get_extrusion_length() const;
void set_extrusion_length(int extrusion_length);
bool operator==(const Polygon rhs);
Vertex *contains(Vertex v);
};
Polygon::Polygon(color_e line, color_e fill)
{
line_clr = line;
fill_clr = fill;
}
vector<Vertex>& Polygon::get_vertices()
{
return vertices;
}
vector<Triangle>& Polygon::get_triangles()
{
return triangles;
}
const color_e& Polygon::get_line_clr() const
{
return line_clr;
}
const color_e& Polygon::get_fill_clr() const
{
return fill_clr;
}
const int& Polygon::get_extrusion_length() const
{
return extrusion_length;
}
void Polygon::set_extrusion_length(int extrusion_length)
{
this->extrusion_length = extrusion_length;
}
bool Polygon::operator==(const Polygon rhs)
{
if (this->vertices.size() != rhs.vertices.size())
return false;
for (unsigned int i = 0; i < this->vertices.size(); i++)
if (this->vertices[i] != rhs.vertices[i])
return false;
return this->fill_clr == rhs.fill_clr &&
this->line_clr == rhs.line_clr &&
this->extrusion_length == rhs.extrusion_length;
}
Vertex *Polygon::contains(Vertex v)
{
unsigned int i;
for (i = 0; i < vertices.size(); i++)
if (vertices[i] == v)
return &(vertices[i]);
for (i = 0; i < vertices.size(); i++)
if (vertices[i].in_x_range(v.get_x(), v.get_y(), 3))
return &(vertices[i]);
return NULL;
}
/* Function Prototypes */
void window_display(void);
void resize_window(int width, int height);
void menu_handler(int value);
void keyboard_event_handler(unsigned char key, int x, int y);
void mouse_event_handler(int button, int state, int x, int y);
inline void leave_current_state(void);
void finalize(void);
inline void glLine3i(Vertex v0, Vertex v1, int z);
inline void glTriangle3i(Vertex v0, Vertex v1, Vertex v2, int z);
void draw_polygons(void);
void draw_polygon_bounds(void);
void draw_polygon_quads(void);
void draw_polygon_area(void);
void draw_polygon_triangles(void);
void draw_clipping_polygon(void);
void draw_grid(void);
inline float crossproduct(Vertex v1, Vertex v2);
#ifdef DRAW_LAST_EDGE_SINCE_START
bool intersecting_polygon(Polygon *p);
#else
bool intersecting_polygon(Polygon *p, bool ignore_last_edge);
#endif
Vertex *intersection(Vertex *v1, Vertex *v2, Vertex *v3, Vertex *v4, bool ignore_edge_points);
void sh_clip(Polygon *p);
bool inside_clip_edge(Vertex p, Vertex cp1, Vertex cp2);
void triangulate(Polygon *p);
// Given Triangulation Code function prototype
bool Process(const vector<Vertex> &contour, vector<Vertex> &result);
/* Global Data */
int window_id, state = NORMAL;
vector<Polygon> polygons;
color_e line_clr = BLACK, fill_clr = WHITE;
Vertex *cmin, *cmax;
bool show_triangles = false, show_clipping_polygon = false;
// Extrusion-related data
double posx = WINDOW_WIDTH + 150, posy = WINDOW_HEIGHT + 100,
posz = -150, lookx = 0, looky = 1, lookz = 0,
upx = 0, upy = 0, upz = -1;
int main(int argc, char **argv)
{
int action_smenuid, lineclr_smenuid, fillclr_smenuid;
// Initialize GLUT lib and negotiate a session with the window system
glutInit(&argc, argv);
// Set initial display mode to RGBA
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
// Set the initial window size and position respectively
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitWindowPosition(-1, -1); // negative values result in the window system determining actual window position
// Create a top-level window
window_id = glutCreateWindow("Project #1");
// Set 'window_display' as the display callback for the current window
glutDisplayFunc(&window_display);
// Set 'keyboard_event_handler' as the keyboard callback for the current window
glutKeyboardFunc(&keyboard_event_handler);
// Set 'mouse_event_handler' as the mouse callback for the current window
glutMouseFunc(&mouse_event_handler);
// Disable window resizing
glutReshapeFunc(resize_window);
// Create Menu and Sub-menus to be displayed on right click
action_smenuid = glutCreateMenu(&menu_handler);
glutAddMenuEntry("Exit", MENU_EXIT);
glutAddMenuEntry("Polygon", MENU_POLYGON);
glutAddMenuEntry("Move Vertex", MENU_MOVE_VERTEX);
glutAddMenuEntry("Clipping", MENU_CLIPPING);
glutAddMenuEntry("Extrude", MENU_EXTRUDE);
glutAddMenuEntry("Exit 3D mode", MENU_EXIT3D);
lineclr_smenuid = glutCreateMenu(&menu_handler);
ADD_COLOR_ENTRIES(0);
fillclr_smenuid = glutCreateMenu(&menu_handler);
ADD_COLOR_ENTRIES(NUM_OF_COLORS);
glutCreateMenu(&menu_handler);
glutAddSubMenu("ACTION", action_smenuid);
glutAddSubMenu("LINE_COLOR", lineclr_smenuid);
glutAddSubMenu("FILL_COLOR", fillclr_smenuid);
glutAttachMenu(GLUT_RIGHT_BUTTON);
// Enter the GLUT event processing loop.
// This function should never return.
glutMainLoop();
return (EXIT_FAILURE);
}
// Used to display the clipping polygon for SHOW_CLIP_POLY_MILLIS milliseconds
// after selection.
void timer_func(int value)
{
show_clipping_polygon = (value == 0);
glutPostRedisplay();
if (value == 0)
glutTimerFunc(SHOW_CLIP_POLY_MILLIS, timer_func, CLIPPING_END);
else
cmin = cmax = NULL;
}
void window_display()
{
/* Clear colors and depth */
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
/* projection matrix (camera) */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, WINDOW_WIDTH, WINDOW_HEIGHT, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if (state == EXTRUSION)
{
/* Enable depth */
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
GLfloat aspect = (GLfloat) WINDOW_WIDTH / (GLfloat) WINDOW_HEIGHT;
glViewport(0.0, 0.0, WINDOW_WIDTH, WINDOW_HEIGHT);
/* Camera perspective */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, aspect, 1.0, 2000.0);
/* Model view */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(posx, posy, posz, lookx, looky, lookz, upx, upy, upz);
}
draw_polygons();
if (show_clipping_polygon)
draw_clipping_polygon();
glutSwapBuffers();
}
void draw_grid(void)
{
int i;
for (i = 0; i < 40; i++)
{
glPushMatrix();
if (i < 20)
{
glTranslatef(0, 0, i);
glBegin(GL_LINES);
glColor3f(0,0,0);
glLineWidth(1);
glVertex3f(0, -0.1, 0);
glVertex3f(WINDOW_HEIGHT, -0.1, 0);
glEnd();
glPopMatrix();
}
else
{
glTranslatef(i-20, 0, 0);
glRotatef(-90, 0, 1, 0);
glBegin(GL_LINES);
glColor3f(0,0,0);
glLineWidth(1);
glVertex3f(0, -0.1, 0);
glVertex3f(WINDOW_WIDTH, -0.1, 0);
glEnd();
glPopMatrix();
}
}
}
void draw_clipping_polygon(void)
{
if (cmin != NULL && cmax != NULL)
{
glLineWidth(2.0f);
glBegin(GL_LINES);
glColor3ub(COLOR_TO_RGB(RED));
glVertex3i(cmin->get_x(), cmin->get_y(), 0);
glVertex3i(cmax->get_x(), cmin->get_y(), 0);
glVertex3i(cmax->get_x(), cmin->get_y(), 0);
glVertex3i(cmax->get_x(), cmax->get_y(), 0);
glVertex3i(cmax->get_x(), cmax->get_y(), 0);
glVertex3i(cmin->get_x(), cmax->get_y(), 0);
glVertex3i(cmin->get_x(), cmax->get_y(), 0);
glVertex3i(cmin->get_x(), cmin->get_y(), 0);
glEnd();
}
}
void resize_window(int width, int height)
{
glutReshapeWindow(WINDOW_WIDTH, WINDOW_HEIGHT);
}
void menu_handler(int value)
{
switch (value)
{
case MENU_EXIT:
finalize();
break;
case MENU_POLYGON:
if (state == EXTRUSION)
{
cerr << "Cannot create a new polygon when on 3D mode." << endl;
break;
}
state = DRAWING_POLYGON;
polygons.push_back(Polygon(line_clr, fill_clr));
#ifdef VARIABLE_EXTRUSION_LENGTH
cout << "Please provide an extrusion length for this polygon." << endl <<
"A good value is in the range [50, 200] depending" <<
" on the size of your polygons." << endl << "Extrusion length: ";
cin >> polygons.back().extrusion_length;
cout << "You selected " << polygons.back().extrusion_length <<
" as the extrusion length." << endl;
#endif
break;
case MENU_MOVE_VERTEX:
if (state == EXTRUSION)
{
cerr << "Cannot move a polygon vertex when on 3D mode." << endl;
break;
}
state = MOVING_VERTEX;
break;
case MENU_CLIPPING:
if (state == EXTRUSION)
{
cerr << "Cannot clip polygon(s) when on 3D mode." << endl;
break;
}
state = CLIPPING;
break;
case MENU_EXTRUDE:
state = EXTRUSION;
#ifndef VARIABLE_EXTRUSION_LENGTH
cout << "Please provide an extrusion length." << endl <<
"A good value is in the range [50, 200] depending" <<
" on the size of your polygons." << endl << "Extrusion length: ";
int extrusion_length;
cin >> extrusion_length; // Check for positive or something else?
cout << "You selected " << extrusion_length << " as the extrusion length." << endl;
for (vector<Polygon>::iterator p = polygons.begin(); p != polygons.end(); p++)
p->set_extrusion_length(extrusion_length);
#endif
break;
case MENU_EXIT3D:
state = NORMAL;
break;
default:
if (value >= BLACK && value < BLACK + NUM_OF_COLORS)
line_clr = (color_e) value;
else if (value >= BLACK + NUM_OF_COLORS && value < (NUM_OF_COLORS<<1))
fill_clr = (color_e) (value - NUM_OF_COLORS);
}
}
void keyboard_event_handler(unsigned char key, int x, int y)
{
switch(key) {
case 'T':
case 't':
show_triangles = !show_triangles;
break;
case 'W':
case 'w': // move camera up
posz -= 2.0;
break;
case 'S':
case 's': // move camera down
posz += 2.0;
break;
case 'A':
case 'a': // move camera left
posx -= 2.0;
break;
case 'D':
case 'd': // move camera right
posx += 2.0;
break;
case 'I':
case 'i': // move camera in
posy -= 2.0;
break;
case 'O':
case 'o': // move camera out
posy += 2.0;
break;
}
}
void mouse_event_handler(int button, int state, int x, int y)
{
static int mouse_event_count = 0,
editing_polygon_index = -1;
static Vertex *moving_vertex = NULL,
*v0 = NULL, *v1 = NULL;
static bool created_polygon = false;
// A polygon is currently being drawn.
if (::state == DRAWING_POLYGON)
{
glutDetachMenu(GLUT_RIGHT_BUTTON);
// Add another polygon vertex and in case a self-intersecting
// polygon occured, the polygon it is removed.
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
polygons.back().get_vertices().push_back(Vertex(x, y));
triangulate(&(polygons.back())); // To allow area coloring (on the fly)
#ifdef DRAW_LAST_EDGE_SINCE_START
if (intersecting_polygon(&polygons.back()) == true)
#else
if (intersecting_polygon(&polygons.back(), true) == true)
#endif
{
cerr << "You created an intersecting polygon." <<
" Nothing to be saved!" << endl;
polygons.pop_back();
leave_current_state();
}
created_polygon = true;
}
// Stop polygon drawing, check for a minumum number of vertices and
// for self-intersection. Remove polygon if requirements are not met.
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
leave_current_state();
#ifndef DRAW_LAST_EDGE_SINCE_START
bool removed_polygon = false;
if (intersecting_polygon(&polygons.back(), false) == true)
{
cerr << "You created an intersecting polygon." <<
" Nothing to be saved!" << endl;
polygons.pop_back();
removed_polygon = true;
}
#endif
if (created_polygon == true && polygons.back().get_vertices().size() < MIN_VERTEX_NUM)
{
cerr << "At least " << MIN_VERTEX_NUM << " vertices are required to" <<
" create a polygon. Nothing to be saved!" << endl;
#ifndef DRAW_LAST_EDGE_SINCE_START
if (removed_polygon == false)
#endif
{
polygons.pop_back();
}
}
created_polygon = false;
}
}
// Move a vertex to a new position.
else if (::state == MOVING_VERTEX)
{
glutDetachMenu(GLUT_RIGHT_BUTTON);
if (mouse_event_count++ == 0 && state == GLUT_UP)
return; // ignore GLUT_UP generated by menu option selection
if (state == GLUT_UP)
{
if (moving_vertex != NULL) // A vertex has been selected
{
Vertex *old_vertex = new Vertex(*moving_vertex);
moving_vertex->update(x, y);
#ifdef DRAW_LAST_EDGE_SINCE_START
if (intersecting_polygon(&(polygons[editing_polygon_index])) == true)
#else
if (intersecting_polygon(&(polygons[editing_polygon_index]), false) == true)
#endif
{
moving_vertex->update(old_vertex->get_x(), old_vertex->get_y());
cerr << "Cannot move vertex. Resulted in an intersecting polygon!" << endl;
}
else // In case a vertex has been updated
triangulate(&(polygons[editing_polygon_index]));
}
leave_current_state();
editing_polygon_index = -1;
moving_vertex = NULL;
mouse_event_count = 0;
return;
}
// Find the vertex, if it exists, that lies near to the window point the
// user has left (down) clicked.
for (vector<Polygon>::iterator p = polygons.begin(); p != polygons.end(); p++)
for (unsigned int i = 0; i < p->get_vertices().size(); i++)
{
if (p->get_vertices()[i].in_range(x, y, 10))
{
editing_polygon_index = p - polygons.begin();
moving_vertex = &(p->get_vertices()[i]);
}
}
}
// Polygon clipping using the Sutherland–Hodgman algorithm.
else if (::state == CLIPPING)
{
glutDetachMenu(GLUT_RIGHT_BUTTON);
if (mouse_event_count++ == 0 && state == GLUT_UP)
return; // ignore GLUT_UP generated by menu option selection
if (state == GLUT_DOWN)
{
v0 = new Vertex(x,y);
}
else if (state == GLUT_UP)
{
v1 = new Vertex(x,y);
if (*v0 == *v1)
{
cerr << "The clipping reactangular cannot be a single point!" << endl;
cmin = cmax = NULL; // TODO remove if cmin/cmax are set to NULL after clipping
}
else
{
cmin = new Vertex(min(v0->get_x(),v1->get_x()), min(v0->get_y(), v1->get_y()));
cmax = new Vertex(max(v0->get_x(),v1->get_x()), max(v0->get_y(), v1->get_y()));
glutTimerFunc(0, timer_func, CLIPPING_START); // Display clipping polygon
// Clip and triangulate each polygon.
for (vector<Polygon>::iterator p = polygons.begin(); p != polygons.end(); p++)
{
sh_clip(&(*p));
triangulate(&(*p));
}
}
leave_current_state();
mouse_event_count = 0;
}
}
}
inline void leave_current_state(void)
{
glutAttachMenu(GLUT_RIGHT_BUTTON);
::state = NORMAL;
}
void finalize(void)
{
glutDestroyWindow(window_id);
exit(EXIT_SUCCESS);
}
void draw_polygons(void)
{
draw_polygon_area();
if (show_triangles == true)
draw_polygon_triangles();
if (::state == EXTRUSION)
draw_polygon_quads();
draw_polygon_bounds();
}
void draw_polygon_bounds(void)
{
glLineWidth(2.0f);
glBegin(GL_LINES);
for (vector<Polygon>::iterator p = polygons.begin(); p != polygons.end(); p++)
{
glColor3ub(COLOR_TO_RGB(p->get_line_clr()));
for (unsigned int i = 0, j; i < p->get_vertices().size(); i++)
{
#ifndef DRAW_LAST_EDGE_SINCE_START
if (::state == DRAWING_POLYGON && *p == polygons.back() && i == p->get_vertices().size()-1)
continue;
#endif
j = (i + 1) % p->get_vertices().size();
glLine3i(p->get_vertices()[i], p->get_vertices()[j], 0);
if (::state == EXTRUSION)
glLine3i(p->get_vertices()[i], p->get_vertices()[j], -(p->get_extrusion_length()));
}
}
glEnd();
}
void draw_polygon_quads(void)
{
glLineWidth(2.0f);
glDepthMask(true);
for (vector<Polygon>::iterator p = polygons.begin(); p != polygons.end(); p++)
{
glBegin(GL_QUADS);
glColor3ub(COLOR_TO_RGB(p->get_line_clr()));
for (unsigned int i = 0, j; i < p->get_vertices().size(); i++)
{
Vertex *v0 = &(p->get_vertices()[i]);
j = (i + 1) % p->get_vertices().size();
Vertex *v1 = &(p->get_vertices()[j]);
glVertex3i(v0->get_x(), v0->get_y(), -(p->get_extrusion_length()));
glVertex3i(v1->get_x(), v1->get_y(), -(p->get_extrusion_length()));
glVertex3i(v1->get_x(), v1->get_y(), 0);
glVertex3i(v0->get_x(), v0->get_y(), 0);
}
glEnd();
}
}
inline void glTriangle3i(Vertex v0, Vertex v1, Vertex v2, int z)
{
glVertex3i(v0.get_x(), v0.get_y(), z);
glVertex3i(v1.get_x(), v1.get_y(), z);
glVertex3i(v2.get_x(), v2.get_y(), z);
}
inline void glLine3i(Vertex v0, Vertex v1, int z)
{
glVertex3i(v0.get_x(), v0.get_y(), z);
glVertex3i(v1.get_x(), v1.get_y(), z);
}
void draw_polygon_area()
{
glLineWidth(1.0f);
glBegin(GL_TRIANGLES);
for (vector<Polygon>::iterator p = polygons.begin(); p != polygons.end(); p++)
{
#ifndef DRAW_LAST_EDGE_SINCE_START
if (::state == DRAWING_POLYGON && *p == polygons.back())
continue;
#endif
glColor3ub(COLOR_TO_RGB(p->get_fill_clr()));
for (unsigned int i = 0; i < p->get_triangles().size(); i++)
{
Triangle *t = &(p->get_triangles()[i]);
glTriangle3i(t->get_v0(), t->get_v1(), t->get_v2(), 0);
if (::state == EXTRUSION)
glTriangle3i(t->get_v0(), t->get_v1(), t->get_v2(), -(p->get_extrusion_length()));
}
}
glEnd();
}
void draw_polygon_triangles()
{
glLineWidth(2.0f);
glColor3ub(COLOR_TO_RGB(GREEN));
glBegin(GL_LINES);
for (vector<Polygon>::iterator p = polygons.begin(); p != polygons.end(); p++)
{
int z1 = 1, z2 = -1;
if (p->get_extrusion_length() < 0)
{
z1 = -1;
z2 = 1;
}
for (unsigned int i = 0; i < p->get_triangles().size(); i++)
{
Triangle *t = &(p->get_triangles()[i]);
glLine3i(t->get_v0(), t->get_v1(), z1);
glLine3i(t->get_v1(), t->get_v2(), z1);
glLine3i(t->get_v2(), t->get_v0(), z1);
if (::state == EXTRUSION)
{
glLine3i(t->get_v0(), t->get_v1(), -(p->get_extrusion_length()) + z2);
glLine3i(t->get_v1(), t->get_v2(), -(p->get_extrusion_length()) + z2);
glLine3i(t->get_v2(), t->get_v0(), -(p->get_extrusion_length()) + z2);
}
}
}
glEnd();
}
/*
* Checks if two vectors (AB and CD) intersect.
* A = v1, B = v2, C = v3, D = v4
*/
Vertex *intersection(Vertex *v1, Vertex *v2, Vertex *v3, Vertex *v4, bool ignore_edge_points)
{
float denom, l, k;
if (!v1 || !v2 || !v3 || !v4)
return NULL;
Vertex p1 = *v1,
p2 = *v2,
p3 = *v3,
p4 = *v4;
denom = crossproduct(p2-p1, p4-p3);
l = crossproduct(p3-p1, p4-p3) / denom;
k = crossproduct(p1-p3, p2-p1) / -denom;
if (l >= 0 && l <= 1 && k >= 0 && k <= 1 && denom != 0)
{
if (ignore_edge_points && (l == 0 || l == 1 || k == 0 || k == 1))
return NULL;
return new Vertex(p1 + (p2-p1)*l);
}
return NULL;
}
inline float crossproduct(Vertex v1, Vertex v2)
{
return ((v2.get_y() * v1.get_x()) - (v2.get_x() * v1.get_y()));
}
// Checks for a self-intersecting polygon.
#ifdef DRAW_LAST_EDGE_SINCE_START
bool intersecting_polygon(Polygon *p)
#else
bool intersecting_polygon(Polygon *p, bool ignore_last_edge)
#endif
{
unsigned int i, j;
unsigned int vnum = p->get_vertices().size();
if (vnum < 4)
return false;
for (i = 0; i < vnum-1; i++)
{
for (j = i+1; j < vnum; j++)
{
#ifndef DRAW_LAST_EDGE_SINCE_START
if (ignore_last_edge && (i == vnum-1 || j == vnum-1))
continue;
#endif
if (intersection(&(p->get_vertices()[i]),
&(p->get_vertices()[i+1]),
&(p->get_vertices()[j]),
&(p->get_vertices()[(j+1) % vnum]), true) != NULL)
{
return true;
}
}
}
return false;
}
// Implementation of the Sutherland–Hodgman clipping algorithm.
void sh_clip(Polygon *p)
{
if (cmin == NULL || cmax == NULL || p == NULL)
return;
// The clipping polygon
Vertex cp[8] = {
Vertex(0, cmin->get_y()),
Vertex(WINDOW_WIDTH, cmin->get_y()),
Vertex(cmax->get_x(), 0),
Vertex(cmax->get_x(), WINDOW_HEIGHT),
Vertex(WINDOW_WIDTH, cmax->get_y()),
Vertex(0, cmax->get_y()),
Vertex(cmin->get_x(), WINDOW_HEIGHT),
Vertex(cmin->get_x(), 0)
};
vector<Vertex> output_list = p->get_vertices();
Vertex *cp0, *cp1, *ip;
for (int j = 0; j < 8; j += 2)
{
cp0 = &(cp[j]);
cp1 = &(cp[j+1]);
vector<Vertex> input_list = output_list;
output_list.clear();
Vertex *s;
if (input_list.empty() == false)
s = new Vertex(input_list.back());
for (unsigned int i = 0; i < input_list.size(); i++)
{
Vertex *e = &(input_list[i]);
if (inside_clip_edge(*e, *cp0, *cp1))
{
if (!inside_clip_edge(*s, *cp0, *cp1))
{
// Case 4: incoming
ip = intersection(s, e, cp0, cp1, false);
if (ip != NULL)
output_list.push_back(*ip);
else
cerr << "A: no intersection point" << endl;
}
// else: Case 1
output_list.push_back(input_list[i]);
}
else if (inside_clip_edge(*s, *cp0, *cp1))
{
// Case 2: outgoing
ip = intersection(s, e, cp0, cp1, false);
if (ip != NULL)
output_list.push_back(*ip);
else
cerr << "B: no intersection point" << endl;
}
// else: Case 3