-
Notifications
You must be signed in to change notification settings - Fork 2
/
R8kVolRen3D-sav02.cpp
3244 lines (2875 loc) · 98.1 KB
/
R8kVolRen3D-sav02.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
// ________ ____ ___
// | \ / | / /
// +---+ \/ |/ /
// +--+| |\ /| <
// | || | \ / | |\ \
// | | \/ | | \ \
// \_____| |__| \__\
// Copyright 2001
// Joe Michael Kniss
// << [email protected] >>
// "All Your Base are Belong to Us"
//-------------------------------------------------------------------------
// R8kVolRen3D.cpp: implementation of the R8kVolRen3D class.
// -Advanced scattering renderer
//////////////////////////////////////////////////////////////////////
#ifdef WIN32
#include <windows.h>
#endif
#include "R8kVolRen3D.h"
#include <iostream.h>
#include <stdlib.h>
#include "VectorMath.h"
#include "glUE.h"
#include "VolRenAux.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
R8kVolRen3D::R8kVolRen3D()
{
go = 0;
fsNames = 0;
pbfsNames = 0;
pb2fsNames = 0;
pb3fsNames = 0;
currentTStep = gluvv.volren.timestep;
}
R8kVolRen3D::~R8kVolRen3D()
{
}
//================================================================= INIT
//======================================================================
void R8kVolRen3D::init()
{
srand(328973892);
cerr << "initing" << endl;
if(gluvv.mv)
{
go = 1;
createVolumes(1);
create2DDepTex();
createShadowTex();
createFragShades(fsNames);
createFragClip();
createCubeTex();
glFinish();
//---------------- create 2 pbuffer for forward scattering -------------
//-------- second pbuffer
if(!(pbuff2 = new PBuffer(gluvv.light.buffsz[0], gluvv.light.buffsz[1],
GLUT_SINGLE | GLUT_RGBA)))
{
cerr << "ERROR: PBuffer2 intialization failed!" << endl;
}
else
{
pbuff2->Initialize(true, true); //create a shared pbuffer
pbuff2->MakeCurrent(); {
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glMatrixMode( GL_PROJECTION );
glOrtho(0,1,0,1,-1,3);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0,0,2,
0,0,0,
0,1,0);
createFragShadow(pb2fsNames);
} MakeCurrent();
cerr << "PBuffer2 intialization succeded" << endl;
}
//----------------end create 2 pbuffer for forward scattering -------------
//----------- first pbuffer
if(!(pbuff = new PBuffer(gluvv.light.buffsz[0], gluvv.light.buffsz[1],
GLUT_SINGLE | GLUT_RGBA)))
{
cerr << "ERROR: PBuffer intialization failed!" << endl;
}
else
{
pbuff->Initialize(true, true); //create a shared pbuffer
pbuff->MakeCurrent(); {
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glMatrixMode( GL_PROJECTION );
glOrtho(0,1,0,1,-1,3);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0,0,2,
0,0,0,
0,1,0);
createFragShadow(pbfsNames);
} MakeCurrent();
cerr << "PBuffer intialization succeded" << endl;
}
//----------------create a pbuffer for backward scattering-----------------
if(!(sliceBuff = new PBuffer(gluvv.light.buffsz[0], gluvv.light.buffsz[1],
GLUT_SINGLE | GLUT_RGBA)))
{
cerr << "ERROR: slice PBuffer intialization failed!" << endl;
}
else
{
sliceBuff->Initialize(true, true); //create a shared pbuffer
sliceBuff->MakeCurrent(); {
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glMatrixMode( GL_PROJECTION );
glOrtho(0,1,0,1,-1,3);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0,0,2,
0,0,0,
0,1,0);
createFragShadow(pb3fsNames);
} MakeCurrent();
cerr << "slice PBuffer intialization succeded" << endl;
}
}//end if gluvv.mv
}
//================================================================= DRAW
//======================================================================
void R8kVolRen3D::draw()
{
if(!go) return;
renderVolume();
//renderSlice();
}
//======================================================== Render Volume
//======================================================================
//========== NOTES ===========================================
// Standard brick vertex ordering:
//
// (011) (111)
// 6 +---------+ 7 Where 1's are the size of the brick
// /| /| allong that axis
// / | / |
// (001)/ |(101) / |
// 4 +---------+ 5 |
// | | | |(110) z axis
// | 2 +-----+---+ 3 ^
// | /(010) | / | y axis
// | / | / | /
// |/ |/ |/
// 0 +---------+ 1 +-------> x axis
// (000) (100)
//
void R8kVolRen3D::renderVolume()
{
if(gluvv.reblend) return;
if(gluvv.picking)
{
return;
}
if(gluvv.volren.timestep != currentTStep)
{
cerr << "Loading new volume : " << gluvv.volren.timestep << endl;
currentTStep = gluvv.volren.timestep;
createVolumes();
}
//-------------- load new shading cube map -----------------------------
if(gluvv.light.load||gluvv.light.gload)
{
loadCubeTex(gluvv.light.gload);
gluvv.light.load = 0;
gluvv.light.gload = 0;
}
//-------------- end load new shading cube map -------------------------
//-------------- Re-Scale Alpha values ---------------------------------
if(gluvv.volren.scaleAlphas)
{
if((lastSamp != gluvv.volren.sampleRate)||(gluvv.volren.loadTLUT))
{ //see if the sample rate changed
if((lastGoodSamp != gluvv.volren.goodSamp) || gluvv.volren.loadTLUT)
{ //good sample rate changed
copyScale(gDeptex, gluvv.volren.goodSamp * 1/gluvv.volren.gamma, deptex);
lastGoodSamp = gluvv.volren.goodSamp;
}
if((lastInteSamp != gluvv.volren.interactSamp) || gluvv.volren.loadTLUT)
{ //interact samp rate changed
copyScale(iDeptex, gluvv.volren.interactSamp * 1/gluvv.volren.gamma, deptex);
lastInteSamp = gluvv.volren.interactSamp;
}
//seems as though we should just have both loaded
if(gluvv.volren.sampleRate == gluvv.volren.goodSamp)
{ //which one do we load (good)
loadDepTex(gDeptex);
lastSamp = gluvv.volren.goodSamp;
}
else if(gluvv.volren.sampleRate == gluvv.volren.interactSamp)
{ //(interactive)
loadDepTex(iDeptex);
lastSamp = gluvv.volren.interactSamp;
}
//we MUST reload if the transfer function changed
if(gluvv.volren.loadTLUT)
{ //now load the transfer function
loadDepTex(gluvv.volren.deptex2, deptex2Name);
}
gluvv.volren.loadTLUT = 0;
}
}
else
{ //just do gamma scale, don't update for the sample rate (for testing purposes)
if(gluvv.volren.loadTLUT)
{
copyScale(gDeptex, 1/gluvv.volren.gamma, deptex);
loadDepTex(gDeptex);
loadDepTex(gluvv.volren.deptex2, deptex2Name);
gluvv.volren.loadTLUT = 0;
}
}
//-------------- end Re-Scale Alpha values ------------------------------
//-------------- Recompute normals --------------------------------------
if(gluvv.volren.usePostCNorm)
{
if(gluvv.volren.loadNorms)
{
unsigned char *gradout = new unsigned char[
gluvv.mv->xiSize*gluvv.mv->yiSize*gluvv.mv->ziSize *3];
VRA_PostCNorms(gradout);
if(gluvv.mv->numSubVols == 1)
{
loadTex3B(gradout, shadeNames[0],
gluvv.mv->xiSize, gluvv.mv->yiSize, gluvv.mv->ziSize);
}
else
{
cerr << "Bricked recompute not implemented yet" << endl;
}
gluvv.volren.loadNorms = 0;
}
}
//-------------- end Recompute normals ----------------------------------
//-------------- do dot product with clip and view dir
float vdir[3];
subV3(vdir, gluvv.env.eye, gluvv.clip.pos);
normalizeV3(vdir);
normalizeV3(gluvv.clip.dir);
float dv = dotV3(vdir,gluvv.clip.dir);
float globalModV[16]; //save original tranform
glGetFloatv(GL_MODELVIEW_MATRIX, globalModV); //save the world model view
//-------------- end do dot product with clip and view dir
glPushMatrix();
{ //move to the volume location
glTranslatef(gluvv.rinfo.trans[0], //translate
gluvv.rinfo.trans[1],
gluvv.rinfo.trans[2]);
glMultMatrixf(gluvv.rinfo.xform); //rotate
glTranslatef(-gluvv.mv->xfSize/2, //center
-gluvv.mv->yfSize/2,
-gluvv.mv->zfSize/2);
//-- first compute the axis for rendering
float axis[4] = {0,0,1,-1}; //axis[0-2] for view direction and [3] for the dot
// modify slice axis for shadowing -------------------------------------
if(gluvv.light.shadow)
{
float ldir[3]; //figure out the light direction
copyV3(ldir,gluvv.light.pos); //this is the position
negateV3(ldir); //this is the direction
normalizeV3(ldir); //normalize both vectors
normalizeV3(axis); // light & axis
float vdl = dotV3(ldir,axis); //the dot product tells us to flip the axis
if(vdl<=0)
{ //need to negate the axis
//cerr << "back to front" << endl;
negateV3(axis);
}
else
{
//cerr << "front to back" << endl;
}
//now compute (v - l)/2 + l = halfway view and light
float spn[3]; //slice plane normal
subV3(spn, axis, ldir);
scaleV3(.5,spn);
float halfv[3];
addV3(halfv, spn, ldir);
copyV3(axis, halfv);
axis[3] = vdl; //this stores the value of the dot product
//its a left handed coordiante system duh
axis[1] = -axis[1];//why? I have no idea, there must be something wrong with my math
negateV3(axis); //the actual direction for slicing
}
// end modify slice axis for shadowing --------------------------------
GLdouble mv[16];
glGetDoublev(GL_MODELVIEW_MATRIX, mv); //save modelview matrix
// --------------------- set up shading transform ---------------------
float *r = gluvv.rinfo.xform;
float cons0[4] = {r[0]/2+.5, r[4]/2+.5, r[8]/2+.5, gluvv.clip.alpha};
float cons1[4] = {r[1]/2+.5, r[5]/2+.5, r[9]/2+.5, 0};
float cons2[4] = {r[2]/2+.5, r[6]/2+.5, r[10]/2+.5, 0};
glSetFragmentShaderConstantATI(GL_CON_0_ATI, cons0);
glSetFragmentShaderConstantATI(GL_CON_1_ATI, cons1);
glSetFragmentShaderConstantATI(GL_CON_2_ATI, cons2);
// --------------------- end set up shading transform -----------------
// --------------------- set up attinuation parameters ----------------
GLfloat tmpmv[16];
getModelView(tmpmv);
GLfloat invmv[16];
inverseMatrix(invmv, tmpmv);
translateV3W(volview, invmv, gluvv.env.eye);
translateV3W(vollt, invmv, gluvv.light.pos);
ltrange[0] = normV3(gluvv.light.pos) - gluvv.light.lattLimits[0];
ltrange[1] = normV3(gluvv.light.pos) - gluvv.light.lattLimits[1];
fogrange[0] = normV3(gluvv.env.eye) - gluvv.light.fogLimits[0];
fogrange[1] = normV3(gluvv.env.eye) - gluvv.light.fogLimits[1];
float cons3[4] = {gluvv.light.fogColor[0],
gluvv.light.fogColor[1],
gluvv.light.fogColor[2],
gluvv.light.amb};
glSetFragmentShaderConstantATI(GL_CON_3_ATI, cons3);
// --------------------- end set up attinuation parameters ------------
//-------------- draw clip slices --------------------------------------
if(axis[3] <= 0)
{ //only do this if front to back
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(GL_FALSE);
for(int i=0; i<gluvv.mv->numSubVols; ++i)
{
drawClip(i, dv, globalModV);
}
glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
}
//-------------- end draw clip slices ----------------------------------
//---------------------- set up shading environment -------------------
setupTexUs();
setupFragShade(axis[3]);
if(gluvv.light.shadow)
setupPBuff();
glDepthMask(GL_FALSE); //no depth wright, only depth test
//---------------------- end set up shading environment ---------------
#if 1
gluvv.reblend = GB_UNDER; //force gluvv.display to composite the background
#endif
//----------------------------------------------------------------------
//-------------- render the volume -------------------------------------
renderBricks(mv, axis); //RENDER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//-------------- end render the volume ---------------------------------
//----------------------------------------------------------------------
//----------------------- clean up shading environment ----------------
glDepthMask(GL_TRUE);
glDisable(GL_BLEND);
if(gluvv.light.shadow)
resetPBuff();
resetTexUs();
resetFragShade();
//----------------------- end clean up shading environment -------------
//-------------- draw clip slices --------------------------------------
if(axis[3] <= 0)
{
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(GL_FALSE);
for(int i=0; i<gluvv.mv->numSubVols; ++i)
{
drawClip(i, -dv, globalModV);
}
glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
}
else
{
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(GL_FALSE);
for(int i=0; i<gluvv.mv->numSubVols; ++i)
{
drawClip(i, -dv, globalModV);
}
glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
}
//-------------- end draw clip slices ----------------------------------
} glPopMatrix();
resetFragShade();
resetTexUs();
resetClips();
//------------- render last thing copied from pbuffer --------------------
if(gluvv.light.showView)
{
glPushMatrix();
{
glTranslatef(gluvv.rinfo.trans[0], gluvv.rinfo.trans[1], gluvv.rinfo.trans[2]);
//first the forward light buffer
glActiveTexture(GL_TEXTURE5_ARB);
{
glDisable(GL_TEXTURE_3D);
glEnable(GL_TEXTURE_2D);
wglBindTexImageARB(pbuff->buffer, WGL_FRONT_LEFT_ARB);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
glBegin(GL_QUADS);
{
glMultiTexCoord2fARB(GL_TEXTURE5_ARB,0,0);
glVertex3f(-1, -1, 0);
glMultiTexCoord2fARB(GL_TEXTURE5_ARB,0,1);
glVertex3f(-1, 0, 0);
glMultiTexCoord2fARB(GL_TEXTURE5_ARB,1,1);
glVertex3f(0, 0, 0);
glMultiTexCoord2fARB(GL_TEXTURE5_ARB,1,0);
glVertex3f(0, -1, 0);
} glEnd();
glActiveTexture(GL_TEXTURE5_ARB);
{
wglReleaseTexImageARB(pbuff->buffer, WGL_FRONT_LEFT_ARB);
glDisable(GL_TEXTURE_2D);
}
//the other forward buffer
glActiveTexture(GL_TEXTURE4_ARB);
{
glDisable(GL_TEXTURE_CUBE_MAP_ARB);
glDisable(GL_TEXTURE_3D);
glEnable(GL_TEXTURE_2D);
wglBindTexImageARB(pbuff2->buffer, WGL_FRONT_LEFT_ARB);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
glBegin(GL_QUADS);
{
glMultiTexCoord2fARB(GL_TEXTURE4_ARB,1,0);
glVertex3f(1, -1, 0);
glMultiTexCoord2fARB(GL_TEXTURE4_ARB,1,1);
glVertex3f(1, 0, 0);
glMultiTexCoord2fARB(GL_TEXTURE4_ARB,0,1);
glVertex3f(0, 0, 0);
glMultiTexCoord2fARB(GL_TEXTURE4_ARB,0,0);
glVertex3f(0, -1, 0);
} glEnd();
glActiveTexture(GL_TEXTURE4_ARB);
{
wglReleaseTexImageARB(pbuff2->buffer, WGL_FRONT_LEFT_ARB);
glDisable(GL_TEXTURE_2D);
}
//next the backward light buffer
glActiveTexture(GL_TEXTURE1_ARB);
{
glDisable(GL_TEXTURE_3D);
glEnable(GL_TEXTURE_2D);
wglBindTexImageARB(sliceBuff->buffer, WGL_FRONT_LEFT_ARB);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
glBegin(GL_QUADS);
{
glMultiTexCoord2fARB(GL_TEXTURE1_ARB,0,0);
glVertex3f(1, 1, 0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB,0,1);
glVertex3f(1, 0, 0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB,1,1);
glVertex3f(0, 0, 0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB,1,0);
glVertex3f(0, 1, 0);
} glEnd();
} glPopMatrix();
glActiveTexture(GL_TEXTURE1_ARB);{
wglReleaseTexImageARB(sliceBuff->buffer, WGL_FRONT_LEFT_ARB);
glDisable(GL_TEXTURE_2D);
}
glActiveTexture(GL_TEXTURE0_ARB);
}
//------------- end render last thing copied from pbuffer ----------------
glEnable(GL_LIGHTING);
GlErr("r8kvolren::draw", "end");
}
//======================================================= Get Model View
//======================================================================
void R8kVolRen3D::getModelView(float mv[16])
{
float m1[16];
identityMatrix(m1);
m1[12] = gluvv.rinfo.trans[0];
m1[13] = gluvv.rinfo.trans[1];
m1[14] = gluvv.rinfo.trans[2];
float m2[16];
matrixMult(m2, m1, gluvv.rinfo.xform);
float scale[16];
identityMatrix(scale);
scale[0] = gluvv.rinfo.scale;
scale[5] = gluvv.rinfo.scale;
scale[10]= gluvv.rinfo.scale;
float m3[16];
matrixMult(m3, m2, scale);
float m4[16];
identityMatrix(m4);
m4[12] = -.5 * gluvv.mv->xfSize;
m4[13] = -.5 * gluvv.mv->yfSize;
m4[14] = -.5 * gluvv.mv->zfSize;
matrixMult(mv, m3, m4);
}
//======================================================== Render Bricks
//======================================================================
void R8kVolRen3D::renderBricks(GLdouble mv[16], GLfloat axis[4])
{
if(gluvv.mv->numSubVols > 1)
{ //render multiple bricked volumes
int *order = new int[gluvv.mv->numSubVols]; //the correct draw order
float *zval = new float[gluvv.mv->numSubVols];//zvalue of each subvolume
float ref[3];
copyV3(ref, axis);
negateV3(ref);
scaleV3(100, ref);
//--------------- compute zvalues ----------------------
for(int i=0; i< gluvv.mv->numSubVols; ++i) //for each sub volume
{
Volume *v = &gluvv.mv->volumes[i];
order[i] = i;
float c[3] = {v->xfPos + v->xfSize/2.0, v->yfPos + v->yfSize/2.0, v->zfPos + v->zfSize/2.0};
float center[3];
translateV3W(center, mv, c);
float sub[3];
subV3(sub, ref, center);
//zval[i] = normV3(sub);//(float)(center[0]*center[0] + center[1]*center[1] + center[2]*center[2]);
zval[i] = (float)(center[0]*center[0] + center[1]*center[1] + center[2]*center[2]);
}
//--------------- end compute zvalues -------------------
//--------------- sort Z values -------------------------
for(i=0; i< gluvv.mv->numSubVols-1; ++i)
{ //now sort
for(int j=i+1; j<gluvv.mv->numSubVols; ++j)
{
if(axis[3] <=0)
{ //back to front rendering sort
if(zval[order[i]] < zval[order[j]])
{ //test for swap
int tmp = order[i];
order[i] = order[j];
order[j] = tmp;
}
}
else
{ //front to back rendering sort
if(zval[order[i]] > zval[order[j]])
{ //test for swap
int tmp = order[i];
order[i] = order[j];
order[j] = tmp;
}
}
}
}
//--------------- end sort Z values ----------------------
//--------------- render bricks --------------------------
for(i=0 ; i< gluvv.mv->numSubVols; ++i)
{ //finaly render each sub volume
cerr << "rendering brick " << order[i] << endl;
Volume *v = &gluvv.mv->volumes[order[i]];
float sxf = v->xfSize;
float syf = v->yfSize;
float szf = v->zfSize;
float hpx = 0;//1.0/(gluvv.mv->xiSize*2.0);
float hpy = 0;//1.0/(gluvv.mv->yiSize*2.0);
float hpz = 0;//1.0/(gluvv.mv->ziSize*2.0);
float vo[8][3] = {{0,0,0},{sxf,0,0},{0,syf,0},{sxf,syf,0},{0,0,szf},{sxf,0,szf},{0,syf,szf},{sxf,syf,szf}};
float tx[8][3] = {{0,0,0},{1,0,0},{0,1,0},{1,1,0},
{0,0,1},{1,0,1},{0,1,1},{1,1,1}};
glPushMatrix();
{
setupClips(order[i],vo,tx);
glTranslatef(v->xfPos, v->yfPos, v->zfPos);
render3DVA(gluvv.volren.sampleRate,mv,order[i],vo,tx,axis);
} glPopMatrix();
//glFinish();
}
//--------------- end render bricks ----------------------
}
else
{ //just render one volume!
float sxf = gluvv.mv->xfSize;
float syf = gluvv.mv->yfSize;
float szf = gluvv.mv->zfSize;
float vo[8][3] = {{0,0,0},{sxf,0,0},{0,syf,0},{sxf,syf,0},{0,0,szf},{sxf,0,szf},{0,syf,szf},{sxf,syf,szf}};
float tx[8][3] = {{0,0,0},{1 ,0,0},{0,1 ,0},{1 ,1 ,0},{0,0,1 },{1 ,0,1 },{0,1 ,1 },{1 ,1 ,1 }};
setupClips(0,vo,tx);
render3DVA(gluvv.volren.sampleRate,mv,0,vo,tx,axis);
}
}
//========================================================== Setup Clips
//======================================================================
void R8kVolRen3D::setupClips(int vol, float vo[8][3], float tx[8][3])
{
Volume *v = &gluvv.mv->volumes[vol];
if(gluvv.clip.on && gluvv.clip.ortho)
{
float cp[3];
cp[0] = gluvv.clip.vpos[0] > v->xfPos ?
(gluvv.clip.vpos[0] < v->xfSize + v->xfPos ? gluvv.clip.vpos[0] - v->xfPos : v->xfSize) : 0;
cp[1] = gluvv.clip.vpos[1] > v->yfPos ?
(gluvv.clip.vpos[1] < v->yfSize + v->yfPos ? gluvv.clip.vpos[1] - v->yfPos : v->yfSize) : 0;
cp[2] = gluvv.clip.vpos[2] > v->zfPos ?
(gluvv.clip.vpos[2] < v->zfSize + v->zfPos ? gluvv.clip.vpos[2] - v->zfPos : v->zfSize) : 0;
switch(gluvv.clip.oaxis)
{
case VolRenAxisXPos:
tx[1][0] = cp[0]/v->xfSize;
tx[3][0] = cp[0]/v->xfSize;
tx[5][0] = cp[0]/v->xfSize;
tx[7][0] = cp[0]/v->xfSize;
vo[1][0] = cp[0];
vo[3][0] = cp[0];
vo[5][0] = cp[0];
vo[7][0] = cp[0];
break;
case VolRenAxisXNeg:
tx[0][0] = cp[0]/v->xfSize;
tx[2][0] = cp[0]/v->xfSize;
tx[4][0] = cp[0]/v->xfSize;
tx[6][0] = cp[0]/v->xfSize;
vo[0][0] = cp[0];
vo[2][0] = cp[0];
vo[4][0] = cp[0];
vo[6][0] = cp[0];
break;
case VolRenAxisYPos:
tx[2][1] = cp[1]/v->yfSize;
tx[3][1] = cp[1]/v->yfSize;
tx[6][1] = cp[1]/v->yfSize;
tx[7][1] = cp[1]/v->yfSize;
vo[2][1] = cp[1];
vo[3][1] = cp[1];
vo[6][1] = cp[1];
vo[7][1] = cp[1];
break;
case VolRenAxisYNeg:
tx[0][1] = cp[1]/v->yfSize;
tx[1][1] = cp[1]/v->yfSize;
tx[4][1] = cp[1]/v->yfSize;
tx[5][1] = cp[1]/v->yfSize;
vo[0][1] = cp[1];
vo[1][1] = cp[1];
vo[4][1] = cp[1];
vo[5][1] = cp[1];
break;
case VolRenAxisZPos:
tx[4][2] = cp[2]/v->zfSize;
tx[5][2] = cp[2]/v->zfSize;
tx[6][2] = cp[2]/v->zfSize;
tx[7][2] = cp[2]/v->zfSize;
vo[4][2] = cp[2];
vo[5][2] = cp[2];
vo[6][2] = cp[2];
vo[7][2] = cp[2];
break;
case VolRenAxisZNeg:
tx[0][2] = cp[2]/v->zfSize;
tx[1][2] = cp[2]/v->zfSize;
tx[2][2] = cp[2]/v->zfSize;
tx[3][2] = cp[2]/v->zfSize;
vo[0][2] = cp[2];
vo[1][2] = cp[2];
vo[2][2] = cp[2];
vo[3][2] = cp[2];
break;
}
}
}
//============================================================ drawClips
//======================================================================
void R8kVolRen3D::drawClip(int vol, float dv, float wmv[16])
{
if((!gluvv.clip.ortho))
{
switch(gluvv.dmode)
{
case GDM_V1:
case GDM_V1G:
case GDM_V2:
case GDM_VGH_VG:
case GDM_VGH_V:
cerr << "clipping" << endl;
glActiveTexture(GL_TEXTURE2_ARB);
{ //3rd & 4th axis, or clipping
glEnable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_3D);
}
glPushMatrix();
{ //this sets the clipping plane
glLoadIdentity(); //clips are set in world space
glMultMatrixf(wmv); //origional world space coords
glTranslatef(gluvv.clip.pos[0], //location of clipping plane
gluvv.clip.pos[1],
gluvv.clip.pos[2]);
glMultMatrixf(gluvv.clip.xform); //rotation of clip plane
GLdouble zup[4] = {0, 0, -1, 0}; // always in z direction
glEnable(GL_CLIP_PLANE5); //enable the gl clip plane
glClipPlane(GL_CLIP_PLANE5, zup);
} glPopMatrix();
break;
}
}
if((!gluvv.clip.on)||(!gluvv.clip.ortho)) return;
Volume *v = &gluvv.mv->volumes[vol];
float c[4][3];
copyV3(c[0],gluvv.clip.corners[0]);
copyV3(c[1],gluvv.clip.corners[1]);
copyV3(c[2],gluvv.clip.corners[2]);
copyV3(c[3],gluvv.clip.corners[3]);
//this moves the clip plane to sub-volume space
c[0][0] = CLAMP_ARB(0,c[0][0]-v->xfPos,v->xfSize);
c[1][0] = CLAMP_ARB(0,c[1][0]-v->xfPos,v->xfSize);
c[2][0] = CLAMP_ARB(0,c[2][0]-v->xfPos,v->xfSize);
c[3][0] = CLAMP_ARB(0,c[3][0]-v->xfPos,v->xfSize);
c[0][1] = CLAMP_ARB(0,c[0][1]-v->yfPos,v->yfSize);
c[1][1] = CLAMP_ARB(0,c[1][1]-v->yfPos,v->yfSize);
c[2][1] = CLAMP_ARB(0,c[2][1]-v->yfPos,v->yfSize);
c[3][1] = CLAMP_ARB(0,c[3][1]-v->yfPos,v->yfSize);
c[0][2] = CLAMP_ARB(0,c[0][2]-v->zfPos,v->zfSize);
c[1][2] = CLAMP_ARB(0,c[1][2]-v->zfPos,v->zfSize);
c[2][2] = CLAMP_ARB(0,c[2][2]-v->zfPos,v->zfSize);
c[3][2] = CLAMP_ARB(0,c[3][2]-v->zfPos,v->zfSize);
float offset = .001; //this is used to avoid z-compete when the slice is on top
glPushMatrix();
{ //make sure we draw this in sub-volume space
glTranslatef(v->xfPos, v->yfPos, v->zfPos);
switch(gluvv.clip.oaxis)
{
case VolRenAxisXPos:
if((dv < 0)&&(c[0][0] > 0)&&(c[0][0]<v->xfSize))
{
float ov[3] = {offset, 0, 0}; //offset vector
for(int i=0; i<4; ++ i)
addV3(c[i],c[i],ov);
renderSlice(c,vol);
}
break;
case VolRenAxisXNeg:
if(dv > 0)
{
float ov[3] = {-offset, 0, 0}; //offset vector
for(int i=0; i<4; ++ i)
addV3(c[i],c[i],ov);
renderSlice(c,vol);
}
break;
case VolRenAxisYPos:
if((dv > 0)&&(c[0][1] > 0)&&(c[0][1]<v->yfSize))
{
float ov[3] = {0, offset, 0}; //offset vector
for(int i=0; i<4; ++ i)
addV3(c[i],c[i],ov);
renderSlice(c,vol);
}
break;
case VolRenAxisYNeg:
if((dv < 0)&&(c[0][1] > 0)&&(c[0][1]<v->yfSize))
{
float ov[3] = {0, -offset, 0}; //offset vector
for(int i=0; i<4; ++ i)
addV3(c[i],c[i],ov);
renderSlice(c,vol);
}
break;
case VolRenAxisZPos:
if((dv < 0)&&(c[0][2] > 0)&&(c[0][2]<v->zfSize))
{
float ov[3] = {0, 0, offset}; //offset vector
for(int i=0; i<4; ++ i)
addV3(c[i],c[i],ov);
renderSlice(c,vol);
}
break;
case VolRenAxisZNeg:
if((dv > 0)&&(c[0][2] > 0)&&(c[0][2]<v->zfSize))
{
float ov[3] = {0, 0, -offset}; //offset vector
for(int i=0; i<4; ++ i)
addV3(c[i],c[i],ov);
renderSlice(c,vol);
}
break;
}
} glPopMatrix();
}
//============================================== Render Slice w/ corners
//======================================================================
void R8kVolRen3D::renderSlice(float c[4][3], int vol)
{ //this just renders a single slice from the volume
glActiveTexture(GL_TEXTURE0_ARB);
{
glEnable(GL_TEXTURE_3D);
glBindTexture(GL_TEXTURE_3D, texNames[vol]);
}
Volume *v = &gluvv.mv->volumes[vol];
glEnable(GL_FRAGMENT_SHADER_ATI);
glBindFragmentShaderATI(cpfsName);
glBegin(GL_QUADS);
{
glMultiTexCoord3fARB(GL_TEXTURE0_ARB,c[0][0]/(v->xfSize),c[0][1]/(v->yfSize),c[0][2]/(v->zfSize));
glVertex3f(c[0][0], c[0][1], c[0][2]);
glMultiTexCoord3fARB(GL_TEXTURE0_ARB,c[1][0]/(v->xfSize),c[1][1]/(v->yfSize),c[1][2]/(v->zfSize));
glVertex3f(c[1][0], c[1][1], c[1][2]);
glMultiTexCoord3fARB(GL_TEXTURE0_ARB,c[2][0]/(v->xfSize),c[2][1]/(v->yfSize),c[2][2]/(v->zfSize));
glVertex3f(c[2][0], c[2][1], c[2][2]);
glMultiTexCoord3fARB(GL_TEXTURE0_ARB,c[3][0]/(v->xfSize),c[3][1]/(v->yfSize),c[3][2]/(v->zfSize));
glVertex3f(c[3][0], c[3][1], c[3][2]);
} glEnd();
glDisable(GL_FRAGMENT_SHADER_ATI);
glActiveTexture(GL_TEXTURE0_ARB);
{
glDisable(GL_TEXTURE_3D);
}
}
//=========================================================== resetClips
//======================================================================
void R8kVolRen3D::resetClips()
{
glDisable(GL_CLIP_PLANE0);
}
//======================================================== Setup Shaders
//======================================================================
void R8kVolRen3D::setupTexUs()
{
GlErr("set up texture units", "before texture0");
//texture 0 = data
glActiveTexture(GL_TEXTURE0_ARB);
{ //this is for the slice
glEnable(GL_TEXTURE_3D);
}
GlErr("set up texture units", "after texture0");
//texture 1 = VG tf
glActiveTexture(GL_TEXTURE1_ARB);
{ //this is for the transfer function
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, deptexName);
}
GlErr("set up texture units", "after texture1");
//texture 2 = shade volume
glActiveTexture(GL_TEXTURE2_ARB);
{ // for shading or clipping (not both)
glEnable(GL_TEXTURE_3D);
}
GlErr("set up texture units", "after texture2");
//texture 3 = H tf
glActiveTexture(GL_TEXTURE3_ARB);
{ //3rd & 4th axis, or clipping, or shadows
switch(gluvv.dmode){ //hey! we use this for 3rd & 4th axies
case GDM_VGH:
case GDM_V1GH:
case GDM_V2G:
case GDM_V2GH:
case GDM_V3:
case GDM_V3G:
case GDM_V4:
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, deptex2Name);
break;
case GDM_V1:
case GDM_V1G:
case GDM_V2:
case GDM_VGH_VG:
case GDM_VGH_V:
if(!gluvv.clip.ortho) break; //we want to take care of this elsewhere
default:
glDisable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_3D);
}
}
GlErr("set up texture units", "after texture3");
#if 0
//texture 4 = cube map dependant texture
glActiveTexture(GL_TEXTURE4_ARB);
{ //cube map
glEnable(GL_TEXTURE_CUBE_MAP_ARB);
glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, gluvv.light.cubeName);
}
GlErr("set up texture units", "after texture4");
#endif
#if 0
//texture 5 = shadow texture
glActiveTexture(GL_TEXTURE5_ARB);
{ //shadow
glEnable(GL_TEXTURE_2D);
//glBindTexture(GL_TEXTURE_2D, pbuffName);
}
#endif
GlErr("set up texture units", "after texture4");