-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiselbox.cpp
More file actions
1931 lines (1712 loc) · 48.2 KB
/
iselbox.cpp
File metadata and controls
1931 lines (1712 loc) · 48.2 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
// ----------------------------------------------------------------------------
// iselbox.cpp : an independent class encapsulating functions needed for a
// selection box in the interactive graphics application.
//
//
// Creation : Feb. 11th 2011
//
// Copyright(C) 2011-2012 Haipeng Cai
//
// ----------------------------------------------------------------------------
#include "iselbox.h"
using namespace std;
//////////////////////////////////////////////////////////////////////////////
// implementation of struct face_t
_face_t::_face_t(const _face_t& other)
{
pvs = NULL;
wps = NULL;
isSelected = false;
}
_face_t& _face_t::operator = (const _face_t& other)
{
// vertex index store is also to be reconstructed
vidxs.clear();
// face must be associated with an object who will give the pointer to the
// real storage of vertices as follows, just copying the pointers is
// detrimental
pvs = NULL;
wps = NULL;
isSelected = false;
return *this;
}
void _face_t::setSelected(bool bSelected )
{
isSelected = bSelected;
}
point_t _face_t::getNormal() const
{
assert (pvs != NULL);
vector_t edge1( (*pvs)[ vidxs[1] ].x - (*pvs)[ vidxs[0] ].x,
(*pvs)[ vidxs[1] ].y - (*pvs)[ vidxs[0] ].y,
(*pvs)[ vidxs[1] ].z - (*pvs)[ vidxs[0] ].z );
vector_t edge2( (*pvs)[ vidxs[2] ].x - (*pvs)[ vidxs[1] ].x,
(*pvs)[ vidxs[2] ].y - (*pvs)[ vidxs[1] ].y,
(*pvs)[ vidxs[2] ].z - (*pvs)[ vidxs[1] ].z );
return edge1.crossproduct(edge2);
}
point_t _face_t::getNormalInWincoord() const
{
assert ( wps != NULL );
return ((*wps)[ vidxs[1] ] - (*wps)[ vidxs[0] ]).crossproduct(
(*wps)[ vidxs[2] ] - (*wps)[ vidxs[1] ]).normalize();
}
bool _face_t::isUserOriented()
{
assert (pvs != NULL);
// for simplicity, we presume that the vector connecting eye to the
// centeroid of the viewport is always perpendicular to the field of view
GLdouble mvmat[16];
glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
GLdouble w = 0;
GLdouble a = 0, b = 0, c = 1;
transpoint(mvmat, a,b,c,w);
GLdouble ang = vector_t(a,b,c).angleTo( getNormal() );
return ang > M_PI/2;
}
bool _face_t::isFrontFacing()
{
assert ( wps != NULL );
// decide if this face is front facing by computing its area and then see
// the sign of the area
int i,j;
GLdouble a = .0;
for (size_t ei = 0; ei < vidxs.size(); ++ei) {
i = vidxs[ei], j = vidxs[ (ei + 1) % vidxs.size() ];
a += (*wps)[i].x * (*wps)[j].y - (*wps)[j].x * (*wps)[i].y;
}
a /= 2.0;
GLint ff;
glGetIntegerv(GL_FRONT_FACE, &ff);
if ( ff == GL_CCW ) {
return a < 0;
}
return a > 0;
}
bool _face_t::isInFace(GLdouble x, GLdouble y)
{
assert ( wps != NULL );
// check (x,y) against each edge of the polygon, boundary of the face
int i,j,k;
GLdouble sign1, sign2, slope;
for (size_t ei = 0; ei < vidxs.size(); ++ei) {
// current edge :
// (*pvs)[ vidxs[ei] ] -> (*pvs)[ vidxs[ (ei + 1) % vidxs.size() ] ]
i = vidxs[ei], j = vidxs[ (ei + 1) % vidxs.size() ];
k = vidxs[ (ei+2) % vidxs.size() ];
// if (x,y) is inside the polygon, it should lead the normal line equation
// established from the two ends, (*pvs)[i] and (*pvs)[j], to a same
// sign as does any of other vertices than these two on the polygon
slope = ( (*wps)[j].y - (*wps)[i].y ) / ( (*wps)[j].x - (*wps)[i].x );
sign1 = (*wps)[k].y - (*wps)[i].y - slope* ( (*wps)[k].x - (*wps)[i].x );
sign2 = y - (*wps)[i].y - slope* ( x - (*wps)[i].x );
if (sign1 * sign2 < 0) {
return false;
}
}
return true;
}
void _face_t::drawFrame()
{
GLenum mode = GL_LINE_STRIP;
/*
if ( isSelected ) {
mode = GL_POLYGON;
glPushAttrib(GL_ALL_ATTRIB_BITS);
glPolygonMode(GL_FRONT, GL_FILL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f( 0.6f, 0.6f, 0.6f, 0.2f );
}
*/
//glBegin (GL_LINE_LOOP);
//glBegin (GL_POLYGON);
if ( !isFrontFacing() ) {
glPushAttrib(GL_ALL_ATTRIB_BITS);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDisable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f( 1.0f, 1.0f, 1.0f, 0.5f );
//glColor4f( 0.2f, 0.2f, 0.2f, 0.2f );
glBegin (GL_POLYGON);
for (size_t i = 0; i < vidxs.size(); ++i) {
glVertex3f( (*pvs)[ vidxs[i] ].x,
(*pvs)[ vidxs[i] ].y,
(*pvs)[ vidxs[i] ].z );
}
glEnd();
glPopAttrib();
}
glBegin (mode);
for (size_t i = 0; i < vidxs.size(); ++i) {
glVertex3f( (*pvs)[ vidxs[i] ].x,
(*pvs)[ vidxs[i] ].y,
(*pvs)[ vidxs[i] ].z );
}
glEnd();
/*
if ( isSelected ) {
glPopAttrib();
}
*/
}
int _face_t::updateVertices(GLdouble dx, GLdouble dy, GLdouble dz)
{
for (size_t i = 0; i < vidxs.size(); ++i) {
(*pvs)[ vidxs[i] ].x += dx;
(*pvs)[ vidxs[i] ].y += dy;
(*pvs)[ vidxs[i] ].z += dz;
}
return 0;
}
GLdouble _face_t::getNearestZ(GLdouble x, GLdouble y)
{
GLdouble minDist = 0xffffffff, curDist;
point_t p(x,y);
size_t ti = 0;
for (size_t i = 0; i < vidxs.size(); ++i) {
curDist = p.distanceTo( (*wps)[ vidxs[i] ] );
if (curDist < minDist) {
minDist = curDist;
ti = i;
}
}
return (*wps) [ vidxs[ti] ].z;
}
int _face_t::getNearestVidx(GLdouble x, GLdouble y)
{
GLdouble minDist = 0xffffffff, curDist;
point_t p(x,y);
int ret = -1;
int ti = (int)vidxs.size();
for (int i = 0; i < (int)vidxs.size(); ++i) {
// retrieve the vertex nearest to (x,y) that is closer than the
// predefined threshold to this mouse point
curDist = p.distanceTo( point_t((*wps)[ vidxs[i] ].x, (*wps)[ vidxs[i] ].y) );
if (curDist > CORNER_DIST_THRESHOLD) {
continue;
}
if (curDist < minDist) {
minDist = curDist;
ti = i;
}
}
if ( ti < (int)vidxs.size() ) {
ret = (int)vidxs[ti];
}
return ret;
}
int _face_t::getNearestEidx(GLdouble x, GLdouble y, edge_idx_t& eIdx)
{
GLdouble minDist = 0xffffffff, curDist;
point_t p(x,y);
int ret = -1;
int ti = (int)vidxs.size(), tj = 0;
for (int i = 0,j = 0; i < (int)vidxs.size(); ++i) {
// retrieve the edge nearest to (x,y) that is closer than the
// predefined threshold to this mouse point
j = i+1;
if ( i == (int)vidxs.size() - 1) {
j = 0;
}
// we are actually deciding if (x,y) is close to a straight line segment
// rather than a mathematical straight line
if ( x < min( (*wps)[ vidxs[i] ].x, (*wps)[ vidxs[j] ].x ) ||
x > max( (*wps)[ vidxs[i] ].x, (*wps)[ vidxs[j] ].x ) ||
y < min( (*wps)[ vidxs[i] ].y, (*wps)[ vidxs[j] ].y ) ||
y > max( (*wps)[ vidxs[i] ].y, (*wps)[ vidxs[j] ].y ) ) {
continue;
}
curDist = p.dist2line( point_t((*wps)[ vidxs[i] ].x, (*wps)[ vidxs[i] ].y),
point_t( (*wps)[ vidxs[j] ].x, (*wps)[ vidxs[j] ].y ) );
if (curDist > EDGE_DIST_THRESHOLD) {
continue;
}
if (curDist < minDist) {
minDist = curDist;
ti = i;
tj = j;
}
}
if ( ti < (int)vidxs.size() ) {
eIdx.update(vidxs[ti], vidxs[tj]);
ret = 0;
}
return ret;
}
bool _face_t::atCenter(GLdouble x, GLdouble y, int proportion)
{
/*
GLdouble cx = ( ( (*wps)[ vidxs[0] ].x + (*wps)[ vidxs[1] ].x )*1.0/2
+ ( (*wps)[ vidxs[2] ].x + (*wps)[ vidxs[3] ].x )*1.0/2 )/2;
GLdouble cy = ( ( (*wps)[ vidxs[0] ].y + (*wps)[ vidxs[1] ].y )*1.0/2
+ ( (*wps)[ vidxs[2] ].y + (*wps)[ vidxs[3] ].y )*1.0/2 )/2;
*/
GLdouble maxx = (*wps)[ vidxs[0] ].x, maxy = (*wps)[ vidxs[0] ].y,
minx = maxx, miny = maxy;
for (int i = 1; i < (int)vidxs.size(); ++i) {
if ( maxx < (*wps)[ vidxs[i] ].x ) {
maxx = (*wps)[ vidxs[i] ].x;
}
if ( minx > (*wps)[ vidxs[i] ].x ) {
minx = (*wps)[ vidxs[i] ].x;
}
if ( maxy < (*wps)[ vidxs[i] ].y ) {
maxy = (*wps)[ vidxs[i] ].y;
}
if ( miny > (*wps)[ vidxs[i] ].y ) {
miny = (*wps)[ vidxs[i] ].y;
}
}
if (proportion < 2) {
proportion = 2;
}
int regW = (maxx - minx)/proportion;
int regH = (maxy - miny)/proportion;
if ( x >= minx + (maxx-minx -regW)/2 &&
x <= minx + maxx-minx - (maxx-minx - regW)/2 &&
y >= miny + (maxy-miny -regH)/2 &&
y <= miny + maxy-miny - (maxy-miny - regH)/2 ) {
return true;
}
return false;
}
point_t _face_t::getCenter(bool bIn2D) const
{
int x = 0, y = 0, z = 0;
vector<point_t>* pstore = bIn2D?wps:pvs;
for (int i = 1; i < (int)vidxs.size(); ++i) {
x += (*pstore)[ vidxs[i] ].x;
y += (*pstore)[ vidxs[i] ].y;
if ( !bIn2D ) {
z += (*pstore)[ vidxs[i] ].z;
}
}
x /= (int)vidxs.size();
y /= (int)vidxs.size();
z /= (int)vidxs.size();
return point_t(x,y,z);
}
//////////////////////////////////////////////////////////////////////////////
// implementation of struct button_t
//
_button_t::_button_t(const _button_t& other)
{
leftbottom = other.leftbottom;
righttop = other.righttop;
winVertices.resize(4);
winVertices = other.winVertices;
text = other.text;
textpos = other.textpos;
isSelected = false;
isShown = false;
isPressed = false;
pIconImgData = other.pIconImgData;
iconW = other.iconW;
iconH = other.iconH;
isPrimary = other.isPrimary;
text2 = other.text2;
pIconImgData2 = other.pIconImgData2;
iconW2 = other.iconW2;
iconH2 = other.iconH2;
}
_button_t& _button_t::operator = (const _button_t& other)
{
leftbottom = other.leftbottom;
righttop = other.righttop;
winVertices.resize(4);
winVertices = other.winVertices;
text = other.text;
textpos = other.textpos;
isSelected = other.isSelected;
isShown = other.isShown;
isPressed = other.isPressed;
pIconImgData = other.pIconImgData;
iconW = other.iconW;
iconH = other.iconH;
isPrimary = other.isPrimary;
text2 = other.text2;
pIconImgData2 = other.pIconImgData2;
iconW2 = other.iconW2;
iconH2 = other.iconH2;
return *this;
}
void _button_t::setSelected(bool bSelected)
{
isSelected = bSelected;
}
void _button_t::setPressed(bool bPressed)
{
isPressed = bPressed;
}
void _button_t::switchStatus()
{
isPrimary = !isPrimary;
}
bool _button_t::isInButton(GLdouble x, GLdouble y)
{
if ( !isShown ) {
return false;
}
/*
// we presume the buttons are always decorated/bound by regular rectangles,
// so the inclusion detection can be much simplified as follows
return ( x >= wlb.x && x <= wrt.x &&
y >= wlb.y && y <= wrt.y);
*/
// check (x,y) against each edge of the polygon, boundary of the face
// similar way as to check if a mouse point is in a box face above, see
// the annotation in _face_t::isInFace.
int i,j,k;
GLdouble sign1, sign2, slope;
for (size_t ei = 0; ei < winVertices.size(); ++ei) {
i = ei, j = (ei + 1) % winVertices.size();
k = (ei+2) % winVertices.size();
slope = ( (winVertices)[j].y - (winVertices)[i].y ) / ( (winVertices)[j].x - (winVertices)[i].x );
sign1 = (winVertices)[k].y - (winVertices)[i].y - slope* ( (winVertices)[k].x - (winVertices)[i].x );
sign2 = y - (winVertices)[i].y - slope* ( x - (winVertices)[i].x );
if (sign1 * sign2 < 0) {
return false;
}
}
return true;
}
void _button_t::draw()
{
// drawing a button is simply consisted of drawing a rectangular frame and
// printing the button text; specifically, then highlighting by different
// coloring and enlarging combined
vector<point_t> frameVertices(4);
if ( leftbottom.x == righttop.x ) {
// on the yz plane
frameVertices[0].update(leftbottom.x, righttop.y, leftbottom.z);
frameVertices[1].update(leftbottom.x, leftbottom.y, leftbottom.z);
frameVertices[2].update(leftbottom.x, leftbottom.y, righttop.z);
frameVertices[3].update(leftbottom.x, righttop.y, righttop.z);
}
else if ( leftbottom.y == righttop.y ) {
// on the xz plane
frameVertices[0].update(leftbottom.x, leftbottom.y, righttop.z);
frameVertices[1].update(leftbottom.x, leftbottom.y, leftbottom.z);
frameVertices[2].update(righttop.x, leftbottom.y, leftbottom.z);
frameVertices[3].update(righttop.x, leftbottom.y, righttop.z);
}
else if ( leftbottom.z == righttop.z ) {
// on the xy plane
frameVertices[0].update(leftbottom.x, righttop.y, righttop.z);
frameVertices[1].update(leftbottom.x, leftbottom.y, righttop.z);
frameVertices[2].update(righttop.x, leftbottom.y, righttop.z);
frameVertices[3].update(righttop.x, righttop.y, righttop.z);
}
_updateBtnWinCoords( frameVertices );
unsigned char* curpIconImg = isPrimary?pIconImgData:pIconImgData2;
string curText = isPrimary?text:text2;
unsigned int curiconH = isPrimary?iconH:iconH2;
unsigned int curiconW = isPrimary?iconW:iconW2;
if ( !curpIconImg ) { // no icon assigned, use text instead
// draw the rectangular border
glPushAttrib(GL_ALL_ATTRIB_BITS);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_BLEND);
glDisable(GL_LIGHTING);
glLineWidth(2);
glColor3f(1, 1, 1);
if ( isSelected ) {
glLineWidth(4);
glColor3f(0, .6, 0.0);
}
if ( isPressed ) {
glLineWidth(6);
glColor3f(0, 0, 0.6);
}
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_CULL_FACE);
glBegin(GL_POLYGON);
for (int i = 0; i < 4; ++i) {
glVertex3f( frameVertices[i].x, frameVertices[i].y, frameVertices[i].z);
}
glEnd();
// button text are never been printed as lucent or translucent since we just
// want to see it clearly
if (isPressed) {
printText(curText.c_str(),
textpos.x, textpos.y, textpos.z,
0, 1, 1, 1.0, GLUT_BITMAP_TIMES_ROMAN_24);
}
else if ( isSelected ) {
printText(curText.c_str(),
textpos.x, textpos.y, textpos.z,
1.0, 1.0, 0.0, 1.0, GLUT_BITMAP_HELVETICA_18);
}
else {
printText(curText.c_str(),
textpos.x, textpos.y, textpos.z,
0, 0, 0, 1.0, GLUT_BITMAP_TIMES_ROMAN_24);
}
glPopAttrib();
isShown = true;
return;
}
// draw icon image using 2D texture mapping if resource,i.e. the image, is available
//
glPushAttrib(GL_ALL_ATTRIB_BITS);
/* CREATE TEXTURE FROM Icon image for this button */
GLuint texture_id;
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
/*
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
*/
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
//cout << "load icon image : " << iconW << " x " << iconH << "\n";
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
(curiconW-2)/2*2+2, (curiconH-2)/2*2+2,
isSelected?1:0, GL_RGB,
GL_UNSIGNED_BYTE, pIconImgData);
/*
for (int i = 0; i < iconW*iconH; i++) {
printf(" 0x%02x ", pIconImgData[i]);
}
*/
// ----------------------------------------------------
// --------- To map the texture as follows successfully,
// --------- setting the polygon mode to GL_FILL is a must, must!!!
// --------- having fiddled with this nasty problem for more than 5 hours at
// --------- least. What a shame!
//
glDisable( GL_CULL_FACE );
glDisable(GL_BLEND);
glDisable(GL_LIGHTING);
glColor3f(1,1,1);
if ( isSelected ) {
glColor3f(1,1,0);
}
if ( isPressed ) {
glColor3f(1,0,0);
}
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3f( frameVertices[0].x, frameVertices[0].y, frameVertices[0].z);
glTexCoord2f(1.0, 0.0);
glVertex3f( frameVertices[1].x, frameVertices[1].y, frameVertices[1].z);
glTexCoord2f(1.0, 1.0);
glVertex3f( frameVertices[2].x, frameVertices[2].y, frameVertices[2].z);
glTexCoord2f(0.0, 1.0);
glVertex3f( frameVertices[3].x, frameVertices[3].y, frameVertices[3].z);
glEnd();
/*
glRasterPos3f( frameVertices[0].x, frameVertices[0].y, frameVertices[0].z);
glDrawPixels( (iconW-2)/2*2+2, (iconH-2)/2*2+2,
GL_RGB, GL_UNSIGNED_BYTE, pIconImgData);
*/
glPopAttrib();
isShown = true;
}
int _button_t::_updateBtnWinCoords(const vector<point_t>& btnFrameVertices)
{
GLint viewport[4];
GLdouble mvmat[16], prjmat[16];
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
glGetDoublev(GL_PROJECTION_MATRIX, prjmat);
GLboolean ret;
for (size_t i = 0; i < btnFrameVertices.size(); ++i) {
ret = gluProject(
btnFrameVertices[i].x, btnFrameVertices[i].y, btnFrameVertices[i].z,
mvmat, prjmat, viewport,
&winVertices[i].x, &winVertices[i].y, &winVertices[i].z
);
winVertices[i].y = viewport[3] - winVertices[i].y;
if (GL_TRUE != ret) {
return -1;
}
}
return 0;
}
//////////////////////////////////////////////////////////////////////////////
// class implementation of CIselbox
//
CIselbox::CIselbox() :
m_minx(0xffffffff),
m_miny(0xffffffff),
m_minz(0xffffffff),
m_maxx(-0xffffffff),
m_maxy(-0xffffffff),
m_maxz(-0xffffffff),
m_fcr(1.0f), m_fcg(0.1f), m_fcb(0.2f),
m_pressedbtn(-1),
m_selectedFid(-1),
m_highlightedFid(-1),
m_selectedVid(-1),
m_highlightedVid(-1),
m_selectedEid(-1,-1),
m_highlightedEid(-1,-1),
m_nearestZ(0),
m_mx(0), m_my(0), m_mz(0),
m_pvertices(NULL),
m_pedgeflags(NULL),
m_pbuttons(NULL),
m_dx(0),m_dy(0),m_dz(0),
m_bmvlimit(true),
m_bhint(true),
m_bMoveAlongZ(false),
m_bEPscaling(false)
{
}
CIselbox::CIselbox(const CIselbox& other)
{
/* it is never reasonable to coarsely copy following member data
* ,which accounts for the initialization as follows just like it was
* treated in the default constructor
*/
m_pressedbtn = -1;
m_selectedFid = -1;
m_highlightedFid = -1;
m_selectedVid = -1;
m_highlightedVid = -1;
m_selectedEid.update(-1,-1);
m_highlightedEid.update(-1,-1);
m_nearestZ = .0;
m_mx = 0, m_my = 0, m_mz = 0;
m_bmvlimit = true;
m_bhint = true;
m_bMoveAlongZ = false;
m_bEPscaling = false;
m_pvertices = NULL;
m_pedgeflags = NULL;
m_pbuttons = other.m_pbuttons;
/* particularly for this specific class, following duplication should be
* rational to make
*/
m_fcr = other.m_fcr;
m_fcg = other.m_fcg;
m_fcb = other.m_fcb;
m_dx = other.m_dx;
m_dy = other.m_dy;
m_dz = other.m_dz;
m_cout = other.m_cout;
/* it is a crux to reconstruct faces and box vertices after being assigned a
* set of new corners
*/
setMinMax( other.m_minx, other.m_miny, other.m_minz,
other.m_maxx, other.m_maxy, other.m_maxz);
}
CIselbox::~CIselbox()
{
}
CIselbox& CIselbox::operator = (const CIselbox& other)
{
this->m_minx = other.m_minx;
this->m_miny = other.m_miny;
this->m_minz = other.m_minz;
this->m_maxx = other.m_maxx;
this->m_maxy = other.m_maxy;
this->m_maxz = other.m_maxz;
// for CIselbox, m_boxvertices, m_winboxvertices and m_faces are all
// reconstructed instantly from the coordinates fo the two opposite
// corners, it is meaningless or even pernicious to simply copy from other
// instance
/*
this->m_boxvertices.resize( other.m_boxvertices.size() );
std::copy(other.m_boxvertices.begin(), other.m_boxvertices.end(),
this->m_boxvertices.begin());
this->m_winboxvertices.resize( other.m_winboxvertices.size() );
std::copy(other.m_winboxvertices.begin(), other.m_winboxvertices.end(),
this->m_winboxvertices.begin());
this->m_faces.resize( other.m_faces.size() );
std::copy(other.m_faces.begin(), other.m_faces.end(),
this->m_faces.begin());
*/
this->m_fcr = other.m_fcr;
this->m_fcg = other.m_fcg;
this->m_fcb = other.m_fcb;
this->m_pvertices = other.m_pvertices;
this->m_pedgeflags = other.m_pedgeflags;
this->m_pbuttons = other.m_pbuttons;
this->m_dx = other.m_dx;
this->m_dy = other.m_dy;
this->m_dz = other.m_dz;
this->m_cout = other.m_cout;
this->m_bmvlimit = other.m_bmvlimit;
this->m_bhint = other.m_bhint;
this->m_bMoveAlongZ = other.m_bMoveAlongZ;
this->m_bEPscaling = other.m_bEPscaling;
m_pressedbtn = -1;
m_selectedFid = -1;
m_highlightedFid = -1;
m_selectedVid = -1;
m_highlightedVid = -1;
m_selectedEid.update(-1,-1);
m_highlightedEid.update(-1,-1);
m_nearestZ = .0;
m_mx = 0, m_my = 0, m_mz = 0;
/* since already armed with new corners, faces and box vertices all need
* updating
*/
m_boxvertices.clear();
m_winboxvertices.clear();
m_faces.clear();
m_boxvertices.resize(8);
m_winboxvertices.resize(8);
_onMinMaxUpdated();
_initFaces();
return *this;
}
/* ---------------------------------------
* original order of vertices on the box
*
|Y
|
4|_____1
/| /|
5/_|__8/ |
| |___|_|______X
| /3 | /2
6|/____|/
/ 7
/
Z
* CCW for front faces adopted, so none
* of the faces will have inwards normal
*
----------------------------------------*/
void CIselbox::setMinMax(GLdouble minx, GLdouble miny, GLdouble minz,
GLdouble maxx, GLdouble maxy, GLdouble maxz)
{
m_minx = minx, m_miny = miny, m_minz = minz;
m_maxx = maxx, m_maxy = maxy, m_maxz = maxz;
m_boxvertices.resize(8);
m_winboxvertices.resize(8);
_onMinMaxUpdated();
_initFaces();
}
void CIselbox::setFrameColor(GLfloat r, GLfloat g, GLfloat b)
{
m_fcr = r, m_fcg = g, m_fcb = b;
}
void CIselbox::drawFrame(bool bBtnEnabled)
{
//glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor3f(m_fcr, m_fcg, m_fcb);
if ( ! m_bhint ) {
for (size_t i = 0; i < m_faces.size(); ++i) {
m_faces[i].drawFrame();
}
return;
}
_objcoord2wincoord();
glLineWidth(2);
glColor4f(m_fcr, m_fcg, m_fcb, 1.0);
glPushAttrib(GL_ALL_ATTRIB_BITS);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0x0001);
for (size_t i = 0; i < m_faces.size(); ++i) {
if ( !m_faces[i].isFrontFacing() ) {
m_faces[i].drawFrame();
}
}
glPopAttrib();
// draw in-situ buttons if enabled & ready
if (bBtnEnabled && m_pbuttons && -1 != m_highlightedFid) {
// initially all buttons are forced to disappear
size_t szBtn = m_pbuttons->size();
for (size_t idx = 0; idx < szBtn; idx++) {
(*m_pbuttons)[idx].isShown = false;
}
if (0 == _calcBtnLayout()) {
for (size_t i = 0; i < szBtn; i++) {
(*m_pbuttons)[i].draw();
}
}
}
//glColor4f(m_fcb, m_fcr, m_fcg, 1.0);
//glColor4f(m_fcr, m_fcg, m_fcb, 1.0);
glLineWidth(2);
glColor4f(m_fcr, m_fcg, m_fcb, 1.0);
for (size_t i = 0; i < m_faces.size(); ++i) {
if ( m_faces[i].isFrontFacing() ) {
m_faces[i].drawFrame();
}
}
if ( m_highlightedVid != -1 ) {
// draw a sphere at the highlighted vertex as a hint/feedback
//
GLdouble mvmat[16];
glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
glPushAttrib(GL_ALL_ATTRIB_BITS);
glColor3f(1.0, 1.0, 0.0);
glPushMatrix();
glTranslatef(m_boxvertices[m_highlightedVid].x,
m_boxvertices[m_highlightedVid].y,
m_boxvertices[m_highlightedVid].z);
glutSolidSphere(1 * fabs(mvmat[14]/(m_dz*3)) , 50, 50);
glPopMatrix();
glPopAttrib();
return;
}
if ( m_highlightedEid != edge_idx_t(-1,-1) ) {
// further increase thickness as the highlighted edge as a hint/feedback
//
glPushAttrib(GL_ALL_ATTRIB_BITS);
glColor3f(1.0, 1.0, 0.0);
glLineWidth(5.0);
glPushMatrix();
glBegin(GL_LINES);
glVertex3f(
m_boxvertices[m_highlightedEid[0]].x,
m_boxvertices[m_highlightedEid[0]].y,
m_boxvertices[m_highlightedEid[0]].z);
glVertex3f(
m_boxvertices[m_highlightedEid[1]].x,
m_boxvertices[m_highlightedEid[1]].y,
m_boxvertices[m_highlightedEid[1]].z);
glEnd();
glPopMatrix();
glPopAttrib();
return;
}
if ( m_highlightedFid != -1 && m_bEPscaling ) {
// draw a sphere at the center of the highlighted face as a hint/feedback
// to tell that congruent scaling is ready to trigger
GLdouble mvmat[16];
glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
glPushAttrib(GL_ALL_ATTRIB_BITS);
GLint maxLW[2];
glGetIntegerv( GL_ALIASED_LINE_WIDTH_RANGE, maxLW);
glLineWidth(maxLW[1]);
glColor3f(1.0, 1.0, 0.0);
point_t fc = m_faces[m_highlightedFid].getCenter();
glPushMatrix();
glTranslated(fc.x, fc.y, fc.z);
//glutSolidSphere(1 * fabs(mvmat[14]/(m_dz*3)) , 50, 50);
glutWireCube(1 * fabs(mvmat[14]/(m_dz*3)));
glPopMatrix();
glPopAttrib();
}
}
int CIselbox::associateObj(vector<GLdouble>* pvertices,
vector<GLboolean>* pedgeflags,
vector<button_t>* pbuttons,
GLdouble dx, GLdouble dy, GLdouble dz)
{
m_dx = dx, m_dy = dy, m_dz = dz;
m_pbuttons = pbuttons;
if ( !pvertices || !pedgeflags ||
pvertices->size()/3 != pedgeflags->size() ) {
return -1;
}
m_pvertices = pvertices;
m_pedgeflags = pedgeflags;
return 0;
}
int CIselbox::refreshEdgeFlags()
{
// check against the mandatory precondition
if ( !m_pvertices || !m_pedgeflags ||
m_pvertices->size()/3 != m_pedgeflags->size() ) {
return -1;
}
// examine every vertex to see if it is inside
size_t szVertex = m_pvertices->size()/3;
int ret = 0;
for (size_t idx = 0; idx < szVertex; idx++) {
(*m_pedgeflags)[idx] =
( isInside( (*m_pvertices)[idx*3 + 0] + m_dx,
(*m_pvertices)[idx*3 + 1] + m_dy,
(*m_pvertices)[idx*3 + 2] + m_dz)
? GL_TRUE:GL_FALSE
);
if ( GL_FALSE == (*m_pedgeflags)[idx] ) {
ret ++;
}
}
m_cout << ret << " points culled due to changes in edge flags\n";
return ret;
}
ostream& CIselbox::reportself(ostream& os, bool bthin)
{
if ( bthin ) {
os << m_minx << " " << m_miny << " " << m_minz << "\n";
os << m_maxx << " " << m_maxy << " " << m_maxz << "\n";
return os;
}
os << "min: (" << m_minx << "," << m_miny << "," << m_minz << ")\n";
os << "max: (" << m_maxx << "," << m_maxy << "," << m_maxz << ")\n";
// color of the box wire frame
os << "frame color: (" << m_fcr << "," << m_fcg << "," << m_fcb << ")\n";
return os;
}
bool CIselbox::isInside(GLdouble x, GLdouble y, GLdouble z)
{
return x <= m_maxx && x >= m_minx &&
y <= m_maxy && y >= m_miny &&
z <= m_maxz && z >= m_minz;
}
int CIselbox::transformBoxVertices()
{
GLint viewport[4];
GLdouble mvmat[16];
GLdouble prjmat[16];
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
glGetDoublev(GL_PROJECTION_MATRIX, prjmat);
// mime the vertex processing pipeline of openGL
for (size_t i = 0; i < m_boxvertices.size(); ++i) {
// viewing and model transformation
transpoint(mvmat,
m_boxvertices[i].x,
m_boxvertices[i].y,
m_boxvertices[i].z,
m_boxvertices[i].w
);
// projection transformation
transpoint(prjmat,
m_boxvertices[i].x,
m_boxvertices[i].y,
m_boxvertices[i].z,
m_boxvertices[i].w
);
// viewport transformation
m_boxvertices[i].x = 0.5*(m_boxvertices[i].x + 1)*viewport[2];
m_boxvertices[i].y = 0.5*(m_boxvertices[i].y + 1)*viewport[3];
m_boxvertices[i].z = 0.5*(m_boxvertices[i].z + 1);
}