-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgluvi.cpp
1114 lines (1002 loc) · 32.4 KB
/
gluvi.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 <cmath>
#include <cstdarg>
#include <cstdlib>
#include <fstream>
#include "gluvi.h"
#include "vec.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846f
#endif
using namespace std;
namespace Gluvi{
Target3D::
Target3D(float target_[3], float dist_, float heading_, float pitch_, float fovy_, float near_clip_factor_, float far_clip_factor_)
: dist(dist_), heading(heading_), pitch(pitch_), fovy(fovy_),
near_clip_factor(near_clip_factor_), far_clip_factor(far_clip_factor_), action_mode(INACTIVE)
{
if(target_){
target[0]=target_[0];
target[1]=target_[1];
target[2]=target_[2];
}else{
target[0]=0;
target[1]=0;
target[2]=0;
}
default_target[0]=target[0];
default_target[1]=target[1];
default_target[2]=target[2];
default_dist=dist;
default_heading=heading;
default_pitch=pitch;
}
void Target3D::
click(int button, int state, int x, int y)
{
if(state==GLUT_UP)
action_mode=INACTIVE;
else if(button==GLUT_LEFT_BUTTON)
action_mode=ROTATE;
else if(button==GLUT_MIDDLE_BUTTON)
action_mode=TRUCK;
else if(button==GLUT_RIGHT_BUTTON)
action_mode=DOLLY;
oldmousex=x;
oldmousey=y;
}
void Target3D::
drag(int x, int y)
{
switch(action_mode){
case INACTIVE:
return; // nothing to do
case ROTATE:
heading+=0.007*(oldmousex-x);
if(heading<-M_PI) heading+=2*M_PI;
else if(heading>M_PI) heading-=2*M_PI;
pitch+=0.007*(oldmousey-y);
if(pitch<-0.5*M_PI) pitch=-0.5*M_PI;
else if(pitch>0.5*M_PI) pitch=0.5*M_PI;
break;
case TRUCK:
target[0]+=(0.002*dist)*cos(heading)*(oldmousex-x);
target[1]-=(0.002*dist)*(oldmousey-y);
target[2]-=(0.002*dist)*sin(heading)*(oldmousex-x);
break;
case DOLLY:
dist*=pow(1.01, oldmousey-y + x-oldmousex);
break;
}
oldmousex=x;
oldmousey=y;
glutPostRedisplay();
}
void Target3D::
return_to_default(void)
{
target[0]=default_target[0];
target[1]=default_target[1];
target[2]=default_target[2];
dist=default_dist;
heading=default_heading;
pitch=default_pitch;
}
void Target3D::
transform_mouse(int x, int y, float ray_origin[3], float ray_direction[3])
{
float ch=cos(heading), sh=sin(heading);
float cp=cos(pitch), sp=sin(pitch);
ray_origin[0]=target[0]+dist*sh*cp;
ray_origin[1]=target[1]-dist*sp;
ray_origin[2]=target[2]+dist*ch*cp;
float scale=0.5*tan(fovy)/winheight;
float camx=(x-0.5*winwidth)*scale, camy=(0.5*winheight-y)*scale, camz=-1.0; // in camera coordinates, this is ray_direction (but not normalized)
// now need to rotate into world space from camera space
float px=camx, py=camy*cp-camz*sp, pz=camy*sp+camz*cp;
ray_direction[0]=px*ch+pz*sh;
ray_direction[1]=py;
ray_direction[2]=-px*sh+pz*ch;
//@@@ is this right to get us to the near clipping plane?
ray_origin[0]+=near_clip_factor*dist*ray_direction[0];
ray_origin[1]+=near_clip_factor*dist*ray_direction[1];
ray_origin[2]+=near_clip_factor*dist*ray_direction[2];
// normalize direction vector
float mag=sqrt(ray_direction[0]*ray_direction[0]
+ ray_direction[1]*ray_direction[1]
+ ray_direction[2]*ray_direction[2]);
ray_direction[0]/=mag;
ray_direction[1]/=mag;
ray_direction[2]/=mag;
}
void Target3D::
get_viewing_direction(float direction[3])
{
float ch=cos(heading), sh=sin(heading);
float cp=cos(pitch), sp=sin(pitch);
direction[0]=-sh*cp;
direction[1]=sp;
direction[2]=-ch*cp;
}
void Target3D::
gl_transform(void)
{
glViewport(0, 0, (GLsizei)winwidth, (GLsizei)winheight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fovy, winwidth/(float)winheight, near_clip_factor*dist, far_clip_factor*dist);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat pos[3];
pos[0]=target[0]-dist*sin(heading)*cos(pitch);
pos[1]=target[1]-dist*sin(pitch);
pos[2]=target[2]-dist*cos(heading)*cos(pitch);
glTranslatef(0, 0, -dist); // translate target dist away in the z direction
glRotatef(-180/M_PI*pitch, 1, 0, 0); // rotate pitch in the yz plane
glRotatef(-180/M_PI*heading, 0, 1, 0); // rotate heading in the xz plane
glTranslatef(-target[0], -target[1], -target[2]); // translate target to origin
}
void Target3D::
export_rib(ostream &output)
{
output<<"Clipping "<<near_clip_factor*dist<<" "<<far_clip_factor*dist<<endl; // could be more generous here!
output<<"Projection \"perspective\" \"fov\" "<<fovy<<endl;
output<<"ReverseOrientation"<<endl; // RenderMan has a different handedness from OpenGL's default
output<<"Scale 1 1 -1"<<endl; // so we need to correct for that here
output<<"Translate 0 0 "<<-dist<<endl;
output<<"Rotate "<<-180/M_PI*pitch<<" 1 0 0"<<endl;
output<<"Rotate "<<-180/M_PI*heading<<" 0 1 0"<<endl;
output<<"Translate "<<-target[0]<<" "<<-target[1]<<" "<<-target[2]<<endl;
}
//=================================================================================
TargetOrtho3D::
TargetOrtho3D(float target_[3], float dist_, float heading_, float pitch_, float height_factor_, float near_clip_factor_, float far_clip_factor_)
: dist(dist_), heading(heading_), pitch(pitch_), height_factor(height_factor_),
near_clip_factor(near_clip_factor_), far_clip_factor(far_clip_factor_), action_mode(INACTIVE)
{
if(target_){
target[0]=target_[0];
target[1]=target_[1];
target[2]=target_[2];
}else{
target[0]=0;
target[1]=0;
target[2]=0;
}
default_target[0]=target[0];
default_target[1]=target[1];
default_target[2]=target[2];
default_dist=dist;
default_heading=heading;
default_pitch=pitch;
}
void TargetOrtho3D::
click(int button, int state, int x, int y)
{
if(state==GLUT_UP)
action_mode=INACTIVE;
else if(button==GLUT_LEFT_BUTTON)
action_mode=ROTATE;
else if(button==GLUT_MIDDLE_BUTTON)
action_mode=TRUCK;
else if(button==GLUT_RIGHT_BUTTON)
action_mode=DOLLY;
oldmousex=x;
oldmousey=y;
}
void TargetOrtho3D::
drag(int x, int y)
{
switch(action_mode){
case INACTIVE:
return; // nothing to do
case ROTATE:
heading+=0.007*(oldmousex-x);
if(heading<-M_PI) heading+=2*M_PI;
else if(heading>M_PI) heading-=2*M_PI;
pitch+=0.007*(oldmousey-y);
if(pitch<-0.5*M_PI) pitch=-0.5*M_PI;
else if(pitch>0.5*M_PI) pitch=0.5*M_PI;
break;
case TRUCK:
target[0]+=(0.002*dist)*cos(heading)*(oldmousex-x);
target[1]-=(0.002*dist)*(oldmousey-y);
target[2]-=(0.002*dist)*sin(heading)*(oldmousex-x);
break;
case DOLLY:
dist*=pow(1.01, oldmousey-y + x-oldmousex);
break;
}
oldmousex=x;
oldmousey=y;
glutPostRedisplay();
}
void TargetOrtho3D::
return_to_default(void)
{
target[0]=default_target[0];
target[1]=default_target[1];
target[2]=default_target[2];
dist=default_dist;
heading=default_heading;
pitch=default_pitch;
}
void TargetOrtho3D::
transform_mouse(int x, int y, float ray_origin[3], float ray_direction[3])
{
// @@@ unimplemented
}
void TargetOrtho3D::
get_viewing_direction(float direction[3])
{
float ch=cos(heading), sh=sin(heading);
float cp=cos(pitch), sp=sin(pitch);
direction[0]=-sh*cp;
direction[1]=sp;
direction[2]=-ch*cp;
}
void TargetOrtho3D::
gl_transform(void)
{
glViewport(0, 0, (GLsizei)winwidth, (GLsizei)winheight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float halfheight=0.5*height_factor*dist, halfwidth=halfheight*winwidth/(float)winheight;
glOrtho(-halfwidth, halfwidth, -halfheight, halfheight, near_clip_factor*dist, far_clip_factor*dist);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat pos[3];
pos[0]=target[0]-dist*sin(heading)*cos(pitch);
pos[1]=target[1]-dist*sin(pitch);
pos[2]=target[2]-dist*cos(heading)*cos(pitch);
glTranslatef(0, 0, -dist); // translate target dist away in the z direction
glRotatef(-180/M_PI*pitch, 1, 0, 0); // rotate pitch in the yz plane
glRotatef(-180/M_PI*heading, 0, 1, 0); // rotate heading in the xz plane
glTranslatef(-target[0], -target[1], -target[2]); // translate target to origin
}
void TargetOrtho3D::
export_rib(ostream &output)
{
output<<"Clipping "<<near_clip_factor*dist<<" "<<far_clip_factor*dist<<endl; // could be more generous here!
output<<"Projection \"orthographic\""<<endl;
//@@@ incomplete: need a scaling according to height_factor*dist somewhere in here
output<<"ReverseOrientation"<<endl; // RenderMan has a different handedness from OpenGL's default
output<<"Scale 1 1 -1"<<endl; // so we need to correct for that here
output<<"Translate 0 0 "<<-dist<<endl;
output<<"Rotate "<<-180/M_PI*pitch<<" 1 0 0"<<endl;
output<<"Rotate "<<-180/M_PI*heading<<" 0 1 0"<<endl;
output<<"Translate "<<-target[0]<<" "<<-target[1]<<" "<<-target[2]<<endl;
}
//=================================================================================
PanZoom2D::
PanZoom2D(float bottom_, float left_, float height_)
: bottom(bottom_), left(left_), height(height_), action_mode(INACTIVE)
{
default_bottom=bottom;
default_left=left;
default_height=height;
}
void PanZoom2D::
click(int button, int state, int x, int y)
{
if(state==GLUT_UP){
float r=height/winheight;
switch(action_mode){
case PAN:
if(!moved_since_mouse_down){
// make mouse click the centre of the window
left+=r*(x-0.5*winwidth);
bottom+=r*(0.5*winheight-y);
glutPostRedisplay();
}
break;
case ZOOM_IN:
if(moved_since_mouse_down){
// zoom in to selection
float desired_width=fabs((x-clickx)*height/winheight);
float desired_height=fabs((y-clicky)*height/winheight);
if(desired_height==0) desired_height=height/winheight;
if(desired_width*winheight > desired_height*winwidth)
desired_height=winheight*desired_width/winwidth;
else
desired_width=winwidth*desired_height/winheight;
left+=0.5*(x+clickx)*height/winheight-0.5*desired_width;
bottom+=(winheight-0.5*(y+clicky))*height/winheight-0.5*desired_height;
height=desired_height;
}else{
// zoom in by some constant factor on the mouse click
float factor=0.70710678118654752440084;
left+=(1-factor)*height*(x/(float)winheight);
bottom+=(1-factor)*height*(1-y/(float)winheight);
height*=factor;
}
glutPostRedisplay();
break;
case ZOOM_OUT:
// zoom out by some constant factor
{
float factor=1.41421356237309504880168;
left-=0.5*(factor-1)*winwidth*height/winheight;
bottom-=0.5*(factor-1)*height;
height*=factor;
}
glutPostRedisplay();
break;
default:
;// nothing to do
}
action_mode=INACTIVE;
}else if(button==GLUT_LEFT_BUTTON)
action_mode=PAN;
else if(button==GLUT_MIDDLE_BUTTON){
clickx=x;
clicky=y;
action_mode=ZOOM_IN;
}else if(button==GLUT_RIGHT_BUTTON)
action_mode=ZOOM_OUT;
moved_since_mouse_down=false;
oldmousex=x;
oldmousey=y;
}
void PanZoom2D::
drag(int x, int y)
{
if(x!=oldmousex || y!=oldmousey){
moved_since_mouse_down=true;
if(action_mode==PAN){
float r=height/winheight;
left-=r*(x-oldmousex);
bottom+=r*(y-oldmousey);
glutPostRedisplay();
}else if(action_mode==ZOOM_IN)
glutPostRedisplay();
oldmousex=x;
oldmousey=y;
}
}
void PanZoom2D::
return_to_default(void)
{
bottom=default_bottom;
left=default_left;
height=default_height;
}
void PanZoom2D::
transform_mouse(int x, int y, float coords[2])
{
float r=height/winheight;
coords[0]=x*r+left;
coords[1]=(winheight-y)*r+bottom;
}
void PanZoom2D::
gl_transform(void)
{
glViewport(0, 0, (GLsizei)winwidth, (GLsizei)winheight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(left, left+(height*winwidth)/winheight, bottom, bottom+height, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void PanZoom2D::
export_rib(ostream &output)
{
// no projection matrix
output<<"Clipping 1 2000"<<endl; // somewhat arbitrary - hopefully this is plenty of space
output<<"ReverseOrientation"<<endl; // RenderMan has a different handedness from OpenGL's default
output<<"Scale 1 1 -1"<<endl; // so we need to correct for that here
// scale so that smaller dimension gets scaled to size 2
float scalefactor;
if(winwidth>winheight) scalefactor=2.0/height;
else scalefactor=2.0/(winwidth*height/winheight);
output<<"Scale "<<scalefactor<<" "<<scalefactor<<" 1"<<endl;
// translate so centre of view gets mapped to (0,0,1000)
output<<"Translate "<<-(left+0.5*winwidth*height/winheight)<<" "<<-(bottom+0.5*height)<< " 1000"<<endl;
}
void PanZoom2D::
display_screen(void)
{
if(action_mode==ZOOM_IN && moved_since_mouse_down){
glColor3f(1,1,1);
glBegin(GL_LINE_STRIP);
glVertex2i(clickx, winheight-clicky);
glVertex2i(oldmousex, winheight-clicky);
glVertex2i(oldmousex, winheight-oldmousey);
glVertex2i(clickx, winheight-oldmousey);
glVertex2i(clickx, winheight-clicky);
glEnd();
}
}
//=================================================================================
StaticText::
StaticText(const char *text_)
: text(text_)
{}
void StaticText::
display(int x, int y)
{
dispx=x;
dispy=y;
width=glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)text)+1;
height=15;
glColor3f(0.3, 0.3, 0.3);
glRasterPos2i(x, y-height+2);
for(int i=0; text[i]!=0; ++i)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, text[i]);
glColor3f(1, 1, 1);
glRasterPos2i(x+1, y-height+3);
for(int i=0; text[i]!=0; ++i)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, text[i]);
}
//=================================================================================
Button::
Button(const char *text_, int minwidth_)
: status(UNINVOLVED), text(text_), minwidth(minwidth_)
{}
void Button::
display(int x, int y)
{
dispx=x;
dispy=y;
int textwidth=glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)text);
if(textwidth<minwidth) width=minwidth+24;
else width=textwidth+24;
height=17;
if(status==UNINVOLVED){
glColor3f(0.7, 0.7, 0.7);
glBegin(GL_QUADS);
glVertex2i(x+1, y-1);
glVertex2i(x+width, y-1);
glVertex2i(x+width, y-height+1);
glVertex2i(x+1, y-height+1);
glEnd();
glColor3f(0.3, 0.3, 0.3);
glLineWidth(1);
glBegin(GL_LINE_STRIP);
glVertex2i(x, y-2);
glVertex2i(x, y-height);
glVertex2i(x+width-1, y-height);
glEnd();
glColor3f(0.3, 0.3, 0.3);
}else{
if(status==SELECTED) glColor3f(0.8, 0.8, 0.8);
else glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex2i(x, y-1);
glVertex2i(x+width, y-1);
glVertex2i(x+width, y-height);
glVertex2i(x, y-height);
glEnd();
glColor3f(0, 0, 0);
}
glRasterPos2i(x+(width-textwidth)/2, y-height+5);
for(int i=0; text[i]!=0; ++i)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, text[i]);
}
bool Button::
click(int state, int x, int y)
{
if(state==GLUT_DOWN && x>dispx && x<=dispx+width && y<dispy-2 && y>=dispy-height){
status=HIGHLIGHTED;
glutPostRedisplay();
return true;
}else if(state==GLUT_UP && status!=UNINVOLVED){
status=UNINVOLVED;
glutPostRedisplay();
if(x>=dispx && x<dispx+width && y<dispy-2 && y>=dispy-height)
action();
return true;
}else
return false;
}
void Button::
drag(int x, int y)
{
// needs to control highlighting (SELECTED vs. HIGHLIGHTED)
if(status==SELECTED && x>=dispx && x<dispx+width && y<dispy-2 && y>=dispy-height){
status=HIGHLIGHTED;
glutPostRedisplay();
}else if(status==HIGHLIGHTED && !(x>=dispx && x<dispx+width && y<dispy-2 && y>=dispy-height)){
status=SELECTED;
glutPostRedisplay();
}
}
//=================================================================================
Slider::
Slider(const char *text_, int length_, int position_, int justify_)
: status(UNINVOLVED), text(text_), length(length_), justify(justify_), position(position_)
{}
void Slider::
display(int x, int y)
{
dispx=x;
dispy=y;
width=glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)text);
if(width<justify) width=justify;
width+=11+6+length+1;
height=15;
glColor3f(0.3, 0.3, 0.3);
glRasterPos2i(x, y-height+2);
for(int i=0; text[i]!=0; ++i)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, text[i]);
glColor3f(1, 1, 1);
glRasterPos2i(x+1, y-height+3);
for(int i=0; text[i]!=0; ++i)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, text[i]);
scrollxmin=x+width-length-12;
scrollxmax=x+width;
scrollymin=y-height+1;
scrollymax=y-2;
glColor3f(0.3, 0.3, 0.3);
glLineWidth(1);
glBegin(GL_LINE_STRIP);
glVertex2i(scrollxmin, scrollymax-1);
glVertex2i(scrollxmin, scrollymin);
glVertex2i(scrollxmax-1, scrollymin);
glVertex2i(scrollxmax-1, scrollymax-1);
glEnd();
glColor3f(0.7, 0.7, 0.7);
glBegin(GL_LINE_STRIP);
glVertex2i(scrollxmin+1, scrollymax);
glVertex2i(scrollxmin+1, scrollymin+1);
glVertex2i(scrollxmax, scrollymin+1);
glVertex2i(scrollxmax, scrollymax);
glEnd();
if(status==UNINVOLVED){
glColor3f(0.3, 0.3, 0.3);
glBegin(GL_LINE_STRIP);
glVertex2i(scrollxmin+position+2, scrollymax-2);
glVertex2i(scrollxmin+position+2, scrollymin+2);
glVertex2i(scrollxmin+position+10, scrollymin+2);
glEnd();
glColor3f(0.7, 0.7, 0.7);
glBegin(GL_QUADS);
glVertex2i(scrollxmin+position+3, scrollymin+3);
glVertex2i(scrollxmin+position+11, scrollymin+3);
glVertex2i(scrollxmin+position+11, scrollymax);
glVertex2i(scrollxmin+position+3, scrollymax);
glEnd();
}else{ // SELECTED
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex2i(scrollxmin+position+2, scrollymin+2);
glVertex2i(scrollxmin+position+11, scrollymin+2);
glVertex2i(scrollxmin+position+11, scrollymax);
glVertex2i(scrollxmin+position+2, scrollymax);
glEnd();
}
}
bool Slider::
click(int state, int x, int y)
{
if(state==GLUT_DOWN && x>scrollxmin+position+2 && x<=scrollxmin+position+11 && y<scrollymax-1 && y>=scrollymin+2){
status=SELECTED;
clickx=x;
glutPostRedisplay();
return true;
}else if(status!=UNINVOLVED && state==GLUT_UP){
status=UNINVOLVED;
glutPostRedisplay();
return true;
}else
return false;
}
void Slider::
drag(int x, int y)
{
if(status==SELECTED){
glutPostRedisplay();
int newposition=position+(x-clickx);
clickx=x;
if(newposition<0){
clickx+=(0-newposition);
newposition=0;
}else if(newposition>length){
clickx+=(length-newposition);
newposition=length;
}
if(newposition!=position){
position=newposition;
action();
glutPostRedisplay();
}
}
}
//=================================================================================
WidgetList::
WidgetList(int indent_, bool hidden_)
: indent(indent_), hidden(hidden_), downclicked_member(-1)
{
}
void WidgetList::
display(int x, int y)
{
dispx=x;
dispy=y;
if(hidden){
width=height=0;
}else{
height=0;
for(unsigned int i=0; i<list.size(); ++i){
list[i]->display(x+indent, y-height);
height+=list[i]->height;
width=(width<indent+list[i]->width) ? indent+list[i]->width : width;
}
}
}
bool WidgetList::
click(int state, int x, int y)
{
//if(hidden || x<dispx || x>=dispx+width || y>=dispy || y<dispy-height) return false; // early exit
if(state==GLUT_DOWN){ // search for correct widget
for(unsigned int i=0; i<list.size(); ++i){
if(list[i]->click(state, x, y)){
downclicked_member=i;
return true;
}
}
}else if(state==GLUT_UP && downclicked_member>=0){
list[downclicked_member]->click(state, x, y);
downclicked_member=-1;
}
return false;
}
void WidgetList::
drag(int x, int y)
{
if(downclicked_member>=0)
list[downclicked_member]->drag(x, y);
}
//=================================================================================
static void gluviReshape(int w, int h)
{
winwidth=w;
winheight=h;
glutPostRedisplay(); // triggers the camera to adjust itself to the new dimensions
}
//=================================================================================
static void gluviDisplay()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// draw the scene
if(camera) camera->gl_transform();
if(userDisplayFunc) userDisplayFunc();
// now draw widgets on top
glPushAttrib(GL_CURRENT_BIT|GL_ENABLE_BIT|GL_LINE_BIT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glLineWidth(1);
// and probably more needs setting before widgets
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, winwidth, 0, winheight);
root.display(0, winheight);
// and allow the camera to draw something on screen (e.g. for zooming extent)
if(camera) camera->display_screen();
glPopAttrib();
glutSwapBuffers();
}
//=================================================================================
static enum {NOBODY, CAMERA, WIDGETS, USER} mouse_owner=NOBODY;
static void gluviMouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN){
int mods=glutGetModifiers();
if(camera && mods==GLUT_ACTIVE_SHIFT){
camera->click(button, state, x, y);
mouse_owner=CAMERA;
}else if(button==GLUT_LEFT_BUTTON && root.click(state, x, winheight-y)){
mouse_owner=WIDGETS;
}else if(userMouseFunc){
userMouseFunc(button, state, x, y);
mouse_owner=USER;
}
}else{ // mouse up - send event to whoever got the mouse down
switch(mouse_owner){
case CAMERA:
camera->click(button, state, x, y);
break;
case WIDGETS:
root.click(state, x, winheight-y);
break;
case USER:
if(userMouseFunc) userMouseFunc(button, state, x, y);
break;
default:
;// nothing to do
}
mouse_owner=NOBODY;
}
}
//=================================================================================
static void gluviDrag(int x, int y)
{
switch(mouse_owner){
case CAMERA:
camera->drag(x, y);
break;
case WIDGETS:
root.drag(x, winheight-y);
break;
case USER:
if(userDragFunc) userDragFunc(x, y);
break;
default:
;// nothing to do
}
}
//=================================================================================
void ppm_screenshot(const char *filename_format, ...)
{
va_list ap;
va_start(ap, filename_format);
#ifdef _MSC_VER
#define FILENAMELENGTH 256
char filename[FILENAMELENGTH];
_vsnprintf(filename, FILENAMELENGTH, filename_format, ap);
ofstream out(filename, ofstream::binary);
#else
char *filename;
vasprintf(&filename, filename_format, ap);
ofstream out(filename, ofstream::binary);
free(filename);
#endif
if(!out) return;
GLubyte *image_buffer=new GLubyte[3*winwidth*winheight];
glReadBuffer(GL_FRONT);
glReadPixels(0, 0, winwidth, winheight, GL_RGB, GL_UNSIGNED_BYTE, image_buffer);
out<<"P6\n"<<winwidth<<' '<<winheight<<" 255\n";
for(int i=1; i<=winheight; ++i)
out.write((const char*)image_buffer+3*winwidth*(winheight-i), 3*winwidth);
delete[] image_buffer;
}
static void write_big_endian_ushort(std::ostream &output, unsigned short v)
{
output.put((v>>8)%256);
output.put(v%256);
}
static void write_big_endian_uint(std::ostream &output, unsigned int v)
{
output.put((v>>24)%256);
output.put((v>>16)%256);
output.put((v>>8)%256);
output.put(v%256);
}
void sgi_screenshot(const char *filename_format, ...)
{
va_list ap;
va_start(ap, filename_format);
#ifdef _MSC_VER
#define FILENAMELENGTH 256
char filename[FILENAMELENGTH];
_vsnprintf(filename, FILENAMELENGTH, filename_format, ap);
ofstream output(filename, ofstream::binary);
#else
char *filename;
vasprintf(&filename, filename_format, ap);
ofstream output(filename, ofstream::binary);
#endif
if(!output) return;
// first write the SGI header
write_big_endian_ushort(output, 474); // magic number to identify this as an SGI image file
output.put(0); // uncompressed
output.put(1); // use 8-bit colour depth
write_big_endian_ushort(output, 3); // number of dimensions
write_big_endian_ushort(output, winwidth); // x size
write_big_endian_ushort(output, winheight); // y size
write_big_endian_ushort(output, 3); // three colour channels (z size)
write_big_endian_uint(output, 0); // minimum pixel value
write_big_endian_uint(output, 255); // maximum pixel value
write_big_endian_uint(output, 0); // dummy spacing
// image name
int i;
for(i=0; i<80 && filename[i]; ++i)
output.put(filename[i]);
for(; i<80; ++i)
output.put(0);
write_big_endian_uint(output, 0); // colormap is normal
for(i=0; i<404; ++i) output.put(0); // filler to complete header
// now write the SGI image data
GLubyte *image_buffer=new GLubyte[winwidth*winheight];
glReadBuffer(GL_FRONT);
glReadPixels(0, 0, winwidth, winheight, GL_RED, GL_UNSIGNED_BYTE, image_buffer);
output.write((const char*)image_buffer, winwidth*winheight);
glReadPixels(0, 0, winwidth, winheight, GL_GREEN, GL_UNSIGNED_BYTE, image_buffer);
output.write((const char*)image_buffer, winwidth*winheight);
glReadPixels(0, 0, winwidth, winheight, GL_BLUE, GL_UNSIGNED_BYTE, image_buffer);
output.write((const char*)image_buffer, winwidth*winheight);
delete[] image_buffer;
#ifndef _MSC_VER
free(filename);
#endif
}
void set_generic_lights(void)
{
glEnable(GL_LIGHTING);
{
GLfloat ambient[4] = {.3, .3, .3, 1};
glLightModelfv (GL_LIGHT_MODEL_AMBIENT,ambient);
}
{
GLfloat color[4] = {.8, .8, .8, 1};
glLightfv (GL_LIGHT0, GL_DIFFUSE, color);
glLightfv (GL_LIGHT0, GL_SPECULAR, color);
glEnable (GL_LIGHT0);
}
{
GLfloat color[4] = {.4, .4, .4, 1};
glLightfv (GL_LIGHT1, GL_DIFFUSE, color);
glLightfv (GL_LIGHT1, GL_SPECULAR, color);
glEnable (GL_LIGHT1);
}
{
GLfloat color[4] = {.2, .2, .2, 1};
glLightfv (GL_LIGHT2, GL_DIFFUSE, color);
glLightfv (GL_LIGHT2, GL_SPECULAR, color);
glEnable (GL_LIGHT2);
}
}
void set_generic_material(float r, float g, float b, GLenum face)
{
GLfloat ambient[4], diffuse[4], specular[4];
ambient[0]=0.1*r+0.03; ambient[1]=0.1*g+0.03; ambient[2]=0.1*b+0.03; ambient[3]=1;
diffuse[0]=0.7*r; diffuse[1]=0.7*g; diffuse[2]=0.7*b; diffuse[3]=1;
specular[0]=0.1*r+0.1; specular[1]=0.1*g+0.1; specular[2]=0.1*b+0.1; specular[3]=1;
glMaterialfv(face, GL_AMBIENT, ambient);
glMaterialfv(face, GL_DIFFUSE, diffuse);
glMaterialfv(face, GL_SPECULAR, specular);
glMaterialf(face, GL_SHININESS, 32);
}
void set_matte_material(float r, float g, float b, GLenum face)
{
GLfloat ambient[4], diffuse[4], specular[4];
ambient[0]=0.1*r+0.03; ambient[1]=0.1*g+0.03; ambient[2]=0.1*b+0.03; ambient[3]=1;
diffuse[0]=0.9*r; diffuse[1]=0.9*g; diffuse[2]=0.9*b; diffuse[3]=1;
specular[0]=0; specular[1]=0; specular[2]=0; specular[3]=1;
glMaterialfv(face, GL_AMBIENT, ambient);
glMaterialfv(face, GL_DIFFUSE, diffuse);
glMaterialfv(face, GL_SPECULAR, specular);
}
/**
* Draw a vector in 3D. If arrow_head_length not specified, set it to 10% of vector length.
* Mar 29, 2006
*/
void draw_3d_arrow(const float base[3], const float point[3], float arrow_head_length)
{
//glPushAttrib(GL_CURRENT_BIT|GL_ENABLE_BIT|GL_LINE_BIT);
//glDisable(GL_LIGHTING);
glLineWidth(1);
glColor3f(0.5,0.5,0.5);
Vec3f w(point[0]-base[0], point[1]-base[1], point[2]-base[2]);
double len = mag(w);
w = w / (float)len; // normalize to build coordinate system
// create a coordinate system from the vector:
// get a vector perp to w and y-axis
Vec3f u = cross(w, Vec3f(0,1,0));
// If vector is parallel to the y-axis, use the x-axis
if (mag(u) == 0)
u = cross(w, Vec3f(1,0,0));
// get a vector perp to w and u
Vec3f v = cross(w, u/mag(u));
v = v/mag(v);
if (!arrow_head_length)
arrow_head_length = 0.1 * len;
// arrow head points
//@@@@@@@ POSSIBILITY: CREATE FOUR ARROW HEAD SEGMENTS INSTEAD OF TWO
Vec3f arrow1, arrow2;
arrow1[0] = point[0] + arrow_head_length * (v[0] - w[0]);
arrow1[1] = point[1] + arrow_head_length * (v[1] - w[1]);
arrow1[2] = point[2] + arrow_head_length * (v[2] - w[2]);
arrow2[0] = point[0] + arrow_head_length * (-v[0] - w[0]);
arrow2[1] = point[1] + arrow_head_length * (-v[1] - w[1]);
arrow2[2] = point[2] + arrow_head_length * (-v[2] - w[2]);
glBegin(GL_LINES);
glVertex3f(base[0], base[1], base[2]);
glVertex3f(point[0], point[1], point[2]);
glVertex3f(point[0], point[1], point[2]);
glVertex3f(arrow1[0], arrow1[1], arrow1[2]);
glVertex3f(point[0], point[1], point[2]);
glVertex3f(arrow2[0], arrow2[1], arrow2[2]);
glEnd();
//glPopAttrib();
}
// draw_2d_arrow assumptions:
// line width, point size, and color are set by the user prior to calling the routine
void draw_2d_arrow(const Vec2f base, const Vec2f point, float arrow_head_length)
{
//glPushAttrib(GL_CURRENT_BIT|GL_ENABLE_BIT|GL_LINE_BIT);
//glDisable(GL_LIGHTING);