-
Notifications
You must be signed in to change notification settings - Fork 2
/
gluvv.cpp
1515 lines (1382 loc) · 51 KB
/
gluvv.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
//------------------------------------------------------------------------
//
// Joe Kniss
// A glut implementation of the Volume Renderer
// gluvv.cpp "GL Utilites for Virtual reality Volume rendering"
// 3-4-01
// ________ ____ ___
// | \ / | / /
// +---+ \/ |/ /
// +--+| |\ /| <
// | || | \ / | |\ \
// | | \/ | | \ \
// \_____| |__| \__\
// Copyright 2001
// Joe Michael Kniss
// <<< [email protected] >>>
// "All Your Base are Belong to Us"
//-------------------------------------------------------------------------
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#ifdef WIN32
#include <windows.h> // must be included before any OpenGL
#endif
//#include <GL/gl.h>
//#include <GL/glu.h>
#include <GL/glut.h> //glut includes gl and glu for you!
#include "Trackball.h" //SGI quaternion trackball
#include "VectorMath.h" //usefull vector/matrix operations
#include "gluvv.h" //global definitions
#include "glUE.h" //gl utilities and extensions
#include "TFWindow.h" //transfer function window
#include "gluvvui.h" //the user interface
//----------------------------------------------------------------
//----------- Renderable includes --------------------------------
#include "VolumeRenderable.h" //Old school renderer
#include "NV20VolRen.h" //2D texture renderer
#include "NV20VolRen3D.h" //standard renderer
#include "NV20VolRen3D2.h" //shadow renderer
#include "TFWidgetRen1.h" //transfer function renderer
#include "CPWidgetRen.h" //clipping plane renderer
#include "DPWidgetRen.h" //data probe renderer
#include "LTWidgetRen.h" //light widget renderer
#include "TestRen.h" //for testing stuff
#include "R8kVolRen3D.h" //Radeon 8000 series volume renderer (1st render to texture)
#include "R8kVolRen3D_sav.h" //Radeon 8000 series volume renderer (old shadow renderer)
#include "R8kVolRen3D_cpy.h" //Radeon 8000 series volume renderer (new copy shadow)
#include "R8kVolRen3D_r2t.h" //Radeon 8000 series volume renderer (new render to texture)
#include "testPBuff.h" //test render to texture
#include "testPert.h" //test texture perterbation
//----------------------------------------------------------------
//---- the global data structures --------------------------------
gluvvGlobal gluvv; //the global gluvv data structure (gluvv.h)
Trackball tball; //global trackball
Trackball lball; //for moving the light (now using light widget)
gluvvPrimitive renderables; //all renderables attatch to this one
gluvvPrimitive widgets; //all renderables attatch to this one
TFWindow tfwin; //separate transfer function window (not used??)
LTWidgetRen ltwr; //need the light widget to be accesable to other things
gluvvui gui; //the user interface
//----------------------------------------------------------------
//---- GLUT Stuff -------------------------------------------------
int initGLUT(int argc, char **argv); //initialize gl window
void glutdisp(); //display callback
void mouse(int button, int state, int x, int y); //mouse callback
void motion(int x, int y); //motion callback
void glutkey(unsigned char key, int x, int y); //key callback
void glutspecial(int key, int x, int y); //special key callback
void glutidle(); //idle callback
void reshape(int w, int h); //reshape callback
//----------------------------------------------------------------
//---- GL Stuff ---------------------------------------------------
void initGL(); //initialize gl state
//----------------------------------------------------------------
//---- Call Backs -------------------------------------------------
void display(int eye);
//----------------------------------------------------------------
//---- Utils ------------------------------------------------------
void initGluvv(); //"Constructor for gluvv data structures"
void initRenderables(); //this gets called after gl context extists
int parse(int argc, char **argv); //returns 0 if no errors
void gluvvQuit(); //essentialy a destructor
int pick(); //pick objects
int initData(); //make sure data is in the right format
//---- Misc -------------------------------------------------------
int checkExts(); //returns 1 if everything is available, 0 else
int blurnorms; //flag set if normals are to be blurred
//=================================================================
//=================================================================
//================================================= the Main func.
//===============================================================
int main(int argc, char **argv)
{
//===== Initialization ========================
//------------------------------------init gluvv
initGluvv();
//------------------------------------parse
if(parse(argc, argv)) return 1;
//------------------------------------init data
if(initData())
{
char c;
cerr << "Data read failed. Make sure you have the correct path specified" << endl
<< "Hit any key to exit" << endl;
cin >> c;
return 1;
}
gluvv.mv->cacheTStep();
//------------------------------------init glut
initGLUT(argc, argv);
if(!checkExts())
{
cerr << "Sorry, this platform is not supported" << endl;
cerr << " Please make sure you have a GeForce 3 or Radeon 8K with the correct driver" << endl;
return 0;
}
//------------------------------------create renderables
//------------------------------- create volume renderer
if(WILDCAT){ //wildcat
VolumeRenderable *volr = new VolumeRenderable;
renderables.setNext((gluvvPrimitive*)volr);
}
else if(gluvv.plat == GPNV202D){ //GF3 with 2D textures only
NV20VolRen *nv20vr = new NV20VolRen;
renderables.setNext((gluvvPrimitive*)nv20vr);
}
else if(gluvv.plat == GPNV20) { //standard renderer
if(!gluvv.light.shadow){
NV20VolRen3D *nv20vr = new NV20VolRen3D;
renderables.setNext((gluvvPrimitive*)nv20vr);
}
else{ //create the shadow renderer
NV20VolRen3D2 *nv20vr = new NV20VolRen3D2;
renderables.setNext((gluvvPrimitive*)nv20vr);
}
}
else if(gluvv.plat == GPATI8K){
cerr << "creating the radeon volume renderer" << endl;
#if 1 //================================================================== vol ren
#if 1 //this is the advanced scatter shader //-------------------- r2t
#if 1 //++++++++++++++++++++++++++++++++++++++++++ old
R8kVolRen3D * r8kvr = new R8kVolRen3D;
renderables.setNext((gluvvPrimitive*)r8kvr);
#else //++++++++++++++++++++++++++++++++++++++++++ new
R8kVolRen3D_r2t * r8kvr = new R8kVolRen3D_r2t;
renderables.setNext((gluvvPrimitive*)r8kvr);
#endif //+++++++++++++++++++++++++++++++++++++++++
#else //this is the old shadow shader //-------------------------- copies
#if 1 //++++++++++++++++++++++++++++++++++++++++++ old
R8kVolRen3D_sav * r8kvr = new R8kVolRen3D_sav;
renderables.setNext((gluvvPrimitive*)r8kvr);
#else //++++++++++++++++++++++++++++++++++++++++++ new
R8kVolRen3D_cpy * r8kvr = new R8kVolRen3D_cpy;
renderables.setNext((gluvvPrimitive*)r8kvr);
#endif //+++++++++++++++++++++++++++++++++++++++++
#endif //---------------------------------------------------------
#else //================================================================== test
#if 1 //---------------------------------------------------------- pbuffers
testPBuff * testpb = new testPBuff();
renderables.setNext((gluvvPrimitive*)testpb);
#else //---------------------------------------------------------- perturbations
testPert * testpt = new testPert();
renderables.setNext((gluvvPrimitive*)testpt);
#endif //---------------------------------------------------------
#endif //=================================================================
}
//------------------------------- create widgets
#if 1
TFWidgetRen *tfwr = new TFWidgetRen;
renderables.setNext((gluvvPrimitive*)tfwr);
//widgets.setNext((gluvvPrimitive*)tfwr);
CPWidgetRen *cpwr = new CPWidgetRen;
renderables.setNext((gluvvPrimitive*)cpwr);
//widgets.setNext((gluvvPrimitive*)cpwr);
DPWidgetRen *dpwr = new DPWidgetRen;
renderables.setNext((gluvvPrimitive*)dpwr);
//widgets.setNext((gluvvPrimitive*)dpwr);
renderables.setNext((gluvvPrimitive*)<wr);
//widgets.setNext((gluvvPrimitive*)<wr);
#endif
#if 0
TestRen *testr = new TestRen();
renderables.setNext((gluvvPrimitive*)testr);
#endif
//------------------------------------init renderables
// - always after init glut!!
initRenderables();
//------------------------------------start
cerr << "Entering Main Loop\n";
glutMainLoop();
cerr << "Buh Bye!" << endl;
return 0;
}
//=================================================================
//=================================================================
//=============================================== init Gluvv
//==========================================================
void initGluvv(){ //"Constructor for gluvv data structures"
//-------------------- initialize global state ---------------
gluvv.mv = 0;
gluvv.mv1 = 0;
gluvv.mv2 = 0;
gluvv.mv3 = 0;
gluvv.debug = 0;
gluvv.dmode = GDM_V1;
renderables.setName("Dummy Node");
gluvv.picking = 0;
gluvv.mprobe = 1;
gluvv.shade = gluvvShadeFaux;
gluvv.reblend = GB_NONE;
//------------------------------------window
gluvv.win.width = 512; //size
gluvv.win.height = 512;
gluvv.win.xPos = 100; //position
gluvv.win.yPos = 100;
//------------------------------------environment
gluvv.env.eye[0] = 0; //eye
gluvv.env.eye[1] = 0;
gluvv.env.eye[2] = -7;
gluvv.env.at[0] = 0; //at
gluvv.env.at[1] = 0;
gluvv.env.at[2] = 0;
gluvv.env.up[0] = 0; //up
gluvv.env.up[1] = 1;
gluvv.env.up[2] = 0;
gluvv.env.frustum[0] = -.2; //left
gluvv.env.frustum[1] = .2; //right
gluvv.env.frustum[2] = -.2; //bottom
gluvv.env.frustum[3] = .2; //top
gluvv.env.clip[0] = 1; //front
gluvv.env.clip[1] = 20; //back
gluvv.env.bgColor = 0; //white background
//------------------------------------mouse
gluvv.mouse.alt = 0;
gluvv.mouse.ctrl = 0;
gluvv.mouse.shift = 0;
//------------------------------------lights & lighting
gluvv.light.pos[0] = gluvv.light.startpos[0] = 0;
gluvv.light.pos[1] = gluvv.light.startpos[1] = 0;
gluvv.light.pos[2] = gluvv.light.startpos[2] = -5;
gluvv.light.buffsz[0] = 1024; //shadow buffer size
gluvv.light.buffsz[1] = 1024;
gluvv.light.shadow = 0; //shadows off
gluvv.light.softShadow= 1; //soft shadows
gluvv.light.showView = 0; //show the view from the light
gluvv.light.sill = 0; //show silhouette edges
gluvv.light.amb = .05; //shadow strenght
gluvv.light.intens = .75;
gluvv.light.latt = 0;
gluvv.light.lattLimits[0] = .5; //attinuation limits (start)
gluvv.light.lattLimits[1] = -.5; //attinuation limits (finish)
gluvv.light.lattThresh = 1; //amount of attinuation
gluvv.light.gShadowQual = .5; //good shadow quality
gluvv.light.iShadowQual = .20; //interactive shadow quality
gluvv.light.shadowTF = 0; //not using a second, shadow tf
gluvv.light.showShadowTF = 0; //don't show us the shadow tf
gluvv.light.csz = 32; //size of the cube map
gluvv.light.cubeKey[0] = 0; //x+ cube key not created yet
gluvv.light.cubeKey[1] = 0; //x- cube key not created yet
gluvv.light.cubeKey[2] = 0; //y+ cube key not created yet
gluvv.light.cubeKey[3] = 0; //y- cube key not created yet
gluvv.light.cubeKey[4] = 0; //z+ cube key not created yet
gluvv.light.cubeKey[5] = 0; //z- cube key not created yet
gluvv.light.load = 1; //load lighting info
gluvv.light.gload = 1; //load the good lighting
gluvv.light.fog = 0; //fog is off
gluvv.light.fogThresh = .5; //fog blend threshold
gluvv.light.fogLimits[0] = .5; //near fog ramp (start)
gluvv.light.fogLimits[1] = -.5; //far fog ramp (finish)
//------------------------------------render info
identityMatrix(gluvv.rinfo.xform); //init the rotation
gluvv.rinfo.scale = 1; //identity for scale
gluvv.rinfo.trans[0] = 0; //no translation
gluvv.rinfo.trans[1] = 0;
gluvv.rinfo.trans[2] = 0;
//------------------------------------volume rendering stuff
gluvv.volren.interactSamp= .6; //Interactive sample rate
gluvv.volren.goodSamp = 2.5; //High quality sample ..
gluvv.volren.sampleRate = gluvv.volren.goodSamp; //samples per voxel
gluvv.volren.shade = 0; //shade off (this flag no longer used)
gluvv.volren.tlut = 0; //not used?? (1D tf, old school)
gluvv.volren.deptex = 0; //1st & 2nd axies transfer function
gluvv.volren.deptex2 = 0; //3rd axis transfer function
gluvv.volren.deptex3 = 0; //scattering (auxilary 2D) transfer
gluvv.volren.loadTLUT = 0; //no need to load the tf
gluvv.volren.scaleAlphas = 1; //scale alphas to the sample rate
gluvv.volren.gamma = 1; //gamma identitiy
gluvv.volren.timestep = 0; //current timestep
gluvv.volren.usePostCNorm= 0; //use pre-computed Normals to start with
gluvv.volren.loadNorms = 0; //no need to reload normals
//------------------------------------transfer function
gluvv.tf.tfWindow = 0; //not in separate window
gluvv.tf.tfwin = &tfwin; //if it was this would handle the window
gluvv.tf.loadme = 0; //no need to load the tf
gluvv.tf.paintme = 0; //nothing to paint into it
gluvv.tf.dropme = 0; //not widgets to drop
gluvv.tf.clearpaint = 0; //already cleared
gluvv.tf.brushon = 0; //brush is off
gluvv.tf.slider1 = 1; //3rd axis slider high
gluvv.tf.slider1hi = 1; //these are sliders for special cases
gluvv.tf.slider1lo = 0;
gluvv.tf.slider2 = 1;
gluvv.tf.histOn = 1; //histogram off
gluvv.tf.ptexsz[0] = 256; //size of the transfer function axies
gluvv.tf.ptexsz[1] = 256;
gluvv.tf.ptexsz[2] = 1; //3rd axis handled sparately
gluvv.tf.numelts = 4; // RGBA colors
//------------------------------------clip plane
gluvv.clip.mousepnt[0] = 0;
gluvv.clip.mousepnt[1] = 0;
gluvv.clip.mousepnt[2] = 0;
gluvv.clip.ortho = 1;
gluvv.clip.oaxis = VolRenAxisXPos;
//------------------------------------data probe
gluvv.probe.brush = NoBrush;
//------------------------------------trackball
tball.clear();
lball.clear();
//------------------------------------misc
blurnorms = 0;
}
//================================================ init GLUT
//==========================================================
int initGLUT(int argc, char **argv)
{
char displaymode[256];
//char displaymode[] = "1";
if(gluvv.plat == GPOctane)
{
strcpy(displaymode, "rgba=8 depth");
}
else if(gluvv.plat == GPOctane2)
{
strcpy(displaymode, "rgba=8 double depth");
}
else if(gluvv.plat == GPInfinite)
{
strcpy(displaymode, "double rgba depth");
}
else
{
strcpy(displaymode, "double rgba depth");
}
glutInit(&argc, argv);
glutInitDisplayString(displaymode);
//glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(gluvv.win.width, gluvv.win.height);
glutInitWindowPosition(gluvv.win.xPos, gluvv.win.yPos);
gluvv.mainWindow =
glutCreateWindow("GL Utilites for Virtual Reality Volume Rendering");
//print out all of the extensions for the card!
cerr << endl
<< ">>>>>>>>>>>>> GL Extensions <<<<<<<<<<<<<<" << endl
<< "------------------------------------------" << endl;
QueryExtension("Print");
cerr << "------------------------------------------" << endl << endl;
#ifdef WIN32
LoadAllExtensions();
#endif
// print out some window attributes for our (ribbed) pleasure.
{
int r, g, b, a, ar, ag, ab, aa, d, s;
GLboolean db,st, aux;
glGetIntegerv(GL_RED_BITS, &r);
glGetIntegerv(GL_GREEN_BITS, &g);
glGetIntegerv(GL_BLUE_BITS, &b);
glGetIntegerv(GL_ALPHA_BITS, &a);
glGetIntegerv(GL_ACCUM_RED_BITS, &ar);
glGetIntegerv(GL_ACCUM_GREEN_BITS, &ag);
glGetIntegerv(GL_ACCUM_BLUE_BITS, &ab);
glGetIntegerv(GL_ACCUM_ALPHA_BITS, &aa);
glGetIntegerv(GL_DEPTH_BITS, &d);
glGetIntegerv(GL_STENCIL_BITS, &s);
glGetBooleanv(GL_DOUBLEBUFFER, &db);
glGetBooleanv(GL_STEREO, &st);
glGetBooleanv(GL_AUX_BUFFERS, &aux);
cerr << "Window Attributes" << endl;
cerr << "-----------------" << endl;
cerr << "color: " << r + g + b + a << " bits";
cerr << " (" << r << "," << g << "," << b << "," << a << ")" << endl;
cerr << "accum: " << ar+ag+ab+aa << " bits";
cerr << " (" << ar << "," << ag << "," << ab << "," << aa << ")" << endl;
cerr << "depth: " << d << " bits" << endl;
cerr << "stencil: " << s << " bits" << endl;
cerr << "double: ";
if(db) cerr << "YES" << endl;
else cerr << "NO" << endl;
cerr << "aux: ";
if(aux) cerr << "YES" << endl;
else cerr << "NO" << endl;
cerr << "stereo : ";
if(st) cerr << "YES" << endl;
else cerr << "NO" << endl;
cerr << "-----------------" << endl;
}
initGL();
glutDisplayFunc(glutdisp);
GLUI_Master.set_glutReshapeFunc(reshape);
GLUI_Master.set_glutMouseFunc(mouse);
glutMotionFunc(motion);
GLUI_Master.set_glutKeyboardFunc(glutkey);
GLUI_Master.set_glutSpecialFunc(glutspecial);
GLUI_Master.set_glutIdleFunc(glutidle);
return 0;
}
//=================================================================
//=================================================================
//=================================================== init GL
//===========================================================
void initGL( void )
{
/* Black background and interpolated shading. */
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
/* Set up lighting. mostly for widgets */
static float ambient[] = {0.4, 0.4, 0.4, 4.0};
static float diffuse[] = {0.5, 1.0, 1.0, 1.0};
static float front_mat_shininess[] = {60.0};
static float front_mat_specular[] = {0.2, 0.2, 0.2, 1.0};
static float front_mat_diffuse[] = {0.5, 0.28, 0.38, 1.0};
static float lmodel_ambient[] = {0.2, 0.2, 0.2, 1.0};
static float lmodel_twoside[] = {GL_FALSE};
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, gluvv.light.startpos);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDisable(GL_CULL_FACE);
//glEnable(GL_LINE_SMOOTH);
glLineWidth(3);
GlErr("gluvv::","initGL()");
gluvv.env.diff[0] = front_mat_diffuse[0];
gluvv.env.diff[1] = front_mat_diffuse[1];
gluvv.env.diff[2] = front_mat_diffuse[2];
gluvv.env.diff[3] = front_mat_diffuse[3];
gluvv.env.spec[0] = front_mat_specular[0];
gluvv.env.spec[1] = front_mat_specular[1];
gluvv.env.spec[2] = front_mat_specular[2];
gluvv.env.spec[3] = front_mat_specular[3];
}
//=================================================================
//=================================================================
//========================================== init Renderables
//===========================================================
void initRenderables()
{
gluvvPrimitive *tmp = renderables.getNext();
while(tmp){
tmp->init();
tmp = tmp->getNext();
}
}
//=================================================================
//=================================================================
//================================================= glut Disp
//===========================================================
void glutdisp()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(gluvv.env.eye[0], //eye
gluvv.env.eye[1],
gluvv.env.eye[2],
gluvv.env.at[0], //at
gluvv.env.at[1],
gluvv.env.at[2],
gluvv.env.up[0],
gluvv.env.up[1],
gluvv.env.up[2]); //up
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(gluvv.env.frustum[0], //left
gluvv.env.frustum[1], //right
gluvv.env.frustum[2], //top
gluvv.env.frustum[3], //bottom
gluvv.env.clip[0], //front
gluvv.env.clip[1]); //back
glMatrixMode(GL_MODELVIEW);
//glDrawBuffer(GL_FRONT);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawBuffer(GL_BACK);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDepthFunc(GL_LESS);
glDisable(GL_BLEND);
display(1); // our local display function
glFlush();
//glutSwapBuffers();
GlErr("gluvv:","glutdisp()");
}
//=================================================================
//=================================================================
//================================================== Display
//==========================================================
void display(int eye)
{
glPushMatrix();{
#if 0
//build current transformation-------------
glTranslatef(gluvv.rinfo.trans[0],
gluvv.rinfo.trans[1],
gluvv.rinfo.trans[2]);
glMultMatrixf(gluvv.rinfo.xform);
float scale[16];
identityMatrix(scale);
scale[0] = gluvv.rinfo.scale;
scale[5] = gluvv.rinfo.scale;
scale[10] = gluvv.rinfo.scale;
glMultMatrixf(scale);
//done with transformation-----------------
#endif
//cycle through scene graph ---------------
gluvvPrimitive *tmp = renderables.getNext();
while(tmp){
tmp->draw();
tmp = tmp->getNext();
}
#if 1
if(gluvv.reblend == GB_UNDER){ //have to do this for front to back volumes
tmp = renderables.getNext();
while(tmp){
tmp->draw();
tmp = tmp->getNext();
}
#if 1
if(gluvv.env.bgColor == 0){
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE);
gluvv.reblend = GB_NONE;
glDisable(GL_DEPTH_TEST);
glColor4f(1,1,1,1);
glBegin(GL_QUADS);{
glVertex3f(-10,-10,-2);
glVertex3f(-10,10,-2);
glVertex3f(10,10,-2);
glVertex3f(10,-10,-2);
} glEnd();
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glDisable(GL_BLEND);
}
#endif
}
#endif
gluvv.reblend = GB_NONE;
} glPopMatrix();
glutSwapBuffers();
}
//=================================================================
//=================================================================
//================================================== Mouse
//========================================================
void mouse(int button, int state, int x, int y)
{
gluvv.mouse.button = button;
gluvv.mouse.state = state;
if(glutGetModifiers() & GLUT_ACTIVE_CTRL) gluvv.mouse.ctrl = 1;
else gluvv.mouse.ctrl = 0;
if(glutGetModifiers() & GLUT_ACTIVE_SHIFT) gluvv.mouse.shift = 1;
else gluvv.mouse.shift = 0;
if(glutGetModifiers() & GLUT_ACTIVE_ALT) gluvv.mouse.alt = 1;
else gluvv.mouse.alt = 0;
if(state == GLUT_DOWN){ //try picking something
gluvv.pick.pos[0] = x;
gluvv.pick.pos[1] = gluvv.win.height - y;
if(pick()){
if(gluvv.pick.prim){
if(gluvv.pick.prim->pick() &&
gluvv.pick.prim->mouse(button, state, x, y))
return;
}
}
}
//cerr << "hi" << endl;
if(state == GLUT_UP){ //send pick events & reset title
if(gluvv.pick.prim)
gluvv.pick.prim->mouse(button, state, x, y);
else{
glutSetWindowTitle("Simian - Space Monkey 1.0");
}
gluvv.mouse.button = -1;
}
//standard button actions...
switch(button){//////////////////////////////
case GLUT_LEFT_BUTTON: //+++++++++++++++++++++++++++++++++
switch(state){//)))))))))))))))))))
case GLUT_UP:
gluvv.volren.sampleRate = gluvv.volren.goodSamp;
glutPostRedisplay();
break;
case GLUT_DOWN:
if(gluvv.mouse.ctrl && gluvv.mouse.shift){
glutSetWindowTitle("Rotate **LIGHT**");
lball.start((2.0 * x - gluvv.win.width) / gluvv.win.width,
(2.0 * y - gluvv.win.height) / gluvv.win.height);
gluvv.mouse.pos[0] = gluvv.mouse.last[0] = x;
gluvv.mouse.pos[1] = gluvv.mouse.last[1] = y;
gluvv.mouse.ctrl = 1;
gluvv.mouse.shift= 1;
} else {
glutSetWindowTitle("Rotate");
tball.start((2.0 * x - gluvv.win.width) / gluvv.win.width,
(2.0 * y - gluvv.win.height) / gluvv.win.height);
gluvv.mouse.pos[0] = gluvv.mouse.last[0] = x;
gluvv.mouse.pos[1] = gluvv.mouse.last[1] = y;
gluvv.volren.sampleRate = gluvv.volren.interactSamp;
}
glutPostRedisplay();
break;
}//)))))))))))))))))))))))))))))))))
break; //++++++++++++++++++++++++++++++++++++++++++++++
case GLUT_RIGHT_BUTTON: //+++++++++++++++++++++++++++++
if(gluvv.mouse.ctrl){ //simulate a middle button
gluvv.mouse.button = GLUT_MIDDLE_BUTTON;
switch(state){//))))))))))))))))))))))
case GLUT_UP:
gluvv.volren.sampleRate = gluvv.volren.goodSamp;
glutPostRedisplay();
break;
case GLUT_DOWN:
glutSetWindowTitle("Translate");
gluvv.mouse.pos[0] = gluvv.mouse.last[0] = x;
gluvv.mouse.pos[1] = gluvv.mouse.last[1] = y;
gluvv.volren.sampleRate = gluvv.volren.interactSamp;
glutPostRedisplay();
break;
}//))))))))))))))))))))))))))))))))))
}
else {
switch(state){//))))))))))))))))))))
case GLUT_UP:
gluvv.volren.sampleRate = gluvv.volren.goodSamp;
glutPostRedisplay();
break;
case GLUT_DOWN:
glutSetWindowTitle("Zoom");
gluvv.mouse.pos[0] = gluvv.mouse.last[0] = x;
gluvv.mouse.pos[1] = gluvv.mouse.last[1] = y;
gluvv.volren.sampleRate = gluvv.volren.interactSamp;
glutPostRedisplay();
break;
}//))))))))))))))))))))))))))))))))
}
break;//++++++++++++++++++++++++++++++++++++++++++++
case GLUT_MIDDLE_BUTTON://++++++++++++++++++++++++++
switch(state){//)))))))))))))))))))))))
case GLUT_UP:
gluvv.volren.sampleRate = gluvv.volren.goodSamp;
glutPostRedisplay();
break;
case GLUT_DOWN:
glutSetWindowTitle("Translate");
gluvv.mouse.pos[0] = gluvv.mouse.last[0] = x;
gluvv.mouse.pos[1] = gluvv.mouse.last[1] = y;
gluvv.volren.sampleRate = gluvv.volren.interactSamp;
glutPostRedisplay();
break;
}//))))))))))))))))))))))))))))))))))))
break;//+++++++++++++++++++++++++++++++++++++++++
default:
break;
}////////////////////////////////////////////
}
//=================================================================
//=================================================================
//================================================== Motion
//=========================================================
void motion(int x, int y)
{
float dx, dy;
if(gluvv.pick.prim){ //send events to a picked object
if(gluvv.pick.prim->move(x, y))
return;
}
switch(gluvv.mouse.button){////////////////////
case GLUT_LEFT_BUTTON: //++++++++++++++++++
switch(gluvv.mouse.state){////////////
case GLUT_DOWN:
if(gluvv.mouse.ctrl && gluvv.mouse.shift){
lball.update((2.0 * x - gluvv.win.width) /gluvv.win.width,
(2.0 * y - gluvv.win.height) /gluvv.win.height);
float lxf[16];
lball.buildRotMatrix((float (*)[4])lxf);
translateV3(gluvv.light.pos, lxf, gluvv.light.startpos);
glLightfv(GL_LIGHT0, GL_POSITION, gluvv.light.pos);
} else {
tball.update((2.0 * x - gluvv.win.width) /gluvv.win.width,
(2.0 * y - gluvv.win.height) /gluvv.win.height);
tball.buildRotMatrix((float (*)[4])gluvv.rinfo.xform);
}
gluvv.mouse.last[0] = x;
gluvv.mouse.last[1] = y;
glutPostRedisplay();
break;
case GLUT_UP:
break;
}/////////////////////////////////////
break; //+++++++++++++++++++++++++++++++
case GLUT_RIGHT_BUTTON://+++++++++++++++
switch(gluvv.mouse.state){////////////
case GLUT_DOWN:
dx = (x - gluvv.mouse.last[0])/(float)gluvv.win.width;
dy = (y - gluvv.mouse.last[1])/(float)gluvv.win.height;
if(abs(dy) > abs(dx)){ //scale object
gluvv.rinfo.scale += 5.0 * dy;
if(gluvv.rinfo.scale < .05)
gluvv.rinfo.scale = .05;
if(gluvv.rinfo.scale > 10.0)
gluvv.rinfo.scale = 10;
} else { //zoom in
gluvv.env.frustum[0] *= 1+dx;
gluvv.env.frustum[1] *= 1+dx;
gluvv.env.frustum[2] *= 1+dx;
gluvv.env.frustum[3] *= 1+dx;
}
gluvv.mouse.last[0] = x;
gluvv.mouse.last[1] = y;
glutPostRedisplay();
break;
case GLUT_UP:
break;
}/////////////////////////////////////
break; //++++++++++++++++++++++++++++++
case GLUT_MIDDLE_BUTTON://+++++++++++++++
switch(gluvv.mouse.state){////////////
case GLUT_DOWN:
dx = (x - gluvv.mouse.last[0])/(float)gluvv.win.width;
dy = (y - gluvv.mouse.last[1])/(float)gluvv.win.height;
gluvv.rinfo.trans[0] += dx *
(gluvv.env.frustum[1]-gluvv.env.frustum[0]) *
gluvv.env.eye[2]/gluvv.env.clip[0];
gluvv.rinfo.trans[1] += dy *
(gluvv.env.frustum[3]-gluvv.env.frustum[2]) *
gluvv.env.eye[2]/gluvv.env.clip[0];
gluvv.mouse.last[0] = x;
gluvv.mouse.last[1] = y;
glutPostRedisplay();
break;
case GLUT_UP:
break;
}/////////////////////////////////////
break;//+++++++++++++++++++++++++++++++
default:
break;
}//////////////////////////////////////////////
}
//=================================================================
//=================================================================
//================================================== GLUT key
//===========================================================
void glutkey(unsigned char key, int x, int y)
{
if(key == 27){ //excape key
gluvvQuit();
}
if(gluvv.pick.prim)
if(gluvv.pick.prim->key(key, x, y))
return;
switch(key){ ///////////////////////////////////
case 'm':
if(gui.visible()) gui.hide();
else gui.show();
break;
case ',': //decrease interactive sample rate
gluvv.volren.interactSamp *= .95;
cerr << "*Interactive Sample Rate = "
<< gluvv.volren.interactSamp << endl;
break;
case '.': //increase interactive sample rate
gluvv.volren.interactSamp *= 1/.95;
cerr << "*Interactive Sample Rate = "
<< gluvv.volren.interactSamp << endl;
break;
case '<': //decrease good sample rate
gluvv.volren.goodSamp *= .95;
cerr << "*High Quality Sample Rate = "
<< gluvv.volren.goodSamp << endl;
break;
case '>': //increase good sample rate
gluvv.volren.goodSamp *= 1/.95;
cerr << "*High Quality Sample Rate = "
<< gluvv.volren.goodSamp << endl;
break;
#if 0
case 'a': //enable/disable alpha scale based on sample rate
if(gluvv.volren.scaleAlphas) gluvv.volren.scaleAlphas = 0;
else gluvv.volren.scaleAlphas = 1;
gluvv.volren.loadTLUT = 1;
glutPostRedisplay();
break;
#endif
case 'c': //enable/disable clipping plane
if(gluvv.clip.on) gluvv.clip.on = 0;
else gluvv.clip.on = 1;
glutPostRedisplay();
break;
case 'k':
gluvv.tf.brushon = 0;
gluvv.tf.loadme = 1;
glutPostRedisplay();
break;
case 'b': //enable/disable transfer function brush
if(gluvv.tf.brushon && (gluvv.probe.brush == EllipseBrush)) gluvv.tf.brushon = 0;
else gluvv.tf.brushon = 1;
gluvv.probe.brush = EllipseBrush;
gluvv.tf.loadme = 1;
glutPostRedisplay();
break;
case 't': //automaticaly sized triangle brush
if(gluvv.tf.brushon && (gluvv.probe.brush == TriangleBrush)) gluvv.tf.brushon = 0;
else gluvv.tf.brushon = 1;
gluvv.probe.slider = .5;
gluvv.probe.brush = TriangleBrush;
gluvv.tf.loadme = 1;
glutPostRedisplay();
break;
case 'a': //automaticly sized ellipse brush
if(gluvv.tf.brushon && (gluvv.probe.brush == AutoEllipseBrush)) gluvv.tf.brushon = 0;
else gluvv.tf.brushon = 1;
gluvv.probe.slider = .5;
gluvv.probe.brush = AutoEllipseBrush;
gluvv.tf.loadme = 1;
glutPostRedisplay();
break;
case 'o': //auto one d brush
if(gluvv.tf.brushon && (gluvv.probe.brush == AutoOneDBrush)) gluvv.tf.brushon = 0;
else gluvv.tf.brushon = 1;
gluvv.probe.slider = .5;
gluvv.probe.brush = AutoOneDBrush;
gluvv.tf.loadme = 1;
glutPostRedisplay();
break;
case 'O': //one d brush
if(gluvv.tf.brushon &&
((gluvv.probe.brush == AutoOneDBrush)||(gluvv.probe.brush = OneDBrush)))
gluvv.tf.brushon = 0;
else gluvv.tf.brushon = 1;
gluvv.probe.slider = .5;
gluvv.probe.brush = OneDBrush;
gluvv.tf.loadme = 1;
glutPostRedisplay();
break;
case 'h': //toggle histogram
if(gluvv.tf.histOn) gluvv.tf.histOn = 0;
else gluvv.tf.histOn = 1;
glutPostRedisplay();
break;
case 's': //toggle shade modes
case 'S':
if(gluvv.shade == gluvvShadeAmb) gluvv.shade = gluvvShadeDiff;
else if(gluvv.shade == gluvvShadeDiff) gluvv.shade = gluvvShadeDSpec;
else if(gluvv.shade == gluvvShadeDSpec) gluvv.shade = gluvvShadeFaux;
else if(gluvv.shade == gluvvShadeFaux) gluvv.shade = gluvvShadeAmb;
gluvv.tf.loadme = 1;
glutPostRedisplay();
break;
case ' ': //paint action
gluvv.tf.paintme = 1;
glutPostRedisplay();
break;
case 'd': //drop action
gluvv.tf.dropme = 1;
glutPostRedisplay();
break;
case 'n': //clear painted tf
gluvv.tf.clearpaint = 1;
glutPostRedisplay();
break;
case 'l': //toggle light widget on and off
ltwr.onoff();
glutPostRedisplay();
break;
case '-':
case '_': //change the current timestep - down
gluvv.volren.timestep -= 1;
if(gluvv.volren.timestep < gluvv.mv->tstart){
gluvv.volren.timestep = gluvv.mv->tstart;
cerr << "We are at the first time step :" << gluvv.volren.timestep << endl;
} else {
cerr << "Current time step: " << gluvv.volren.timestep << endl;
if(!gluvv.mv->swapTStep(gluvv.volren.timestep)){
gluvv.mv->readAll(gluvv.volren.timestep);
if(gluvv.mv1)
gluvv.mv1->readAll(gluvv.volren.timestep);
if(gluvv.mv2)
gluvv.mv2->readAll(gluvv.volren.timestep);
if(gluvv.mv3)
gluvv.mv3->readAll(gluvv.volren.timestep);
initData();
gluvv.mv->cacheTStep();
}
}
glutPostRedisplay();
break;
case '=':
case '+': //change the current timesetp - up
gluvv.volren.timestep += 1;
if(gluvv.volren.timestep > gluvv.mv->tstop){
gluvv.volren.timestep = gluvv.mv->tstop;
cerr << "We are at the last time step :" << gluvv.volren.timestep << endl;