-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtkOpenGLGPUVolumeRayCastMapper.cxx
6969 lines (6192 loc) · 221 KB
/
vtkOpenGLGPUVolumeRayCastMapper.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkOpenGLGPUVolumeRayCastMapper.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkOpenGLGPUVolumeRayCastMapper.h"
#include "vtkGraphicsFactory.h"
#include "vtkObjectFactory.h"
#include "vtkVolume.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkCamera.h"
#include "vtkMatrix4x4.h"
#include "vtkImageData.h"
#include "vtkTimerLog.h"
#include "vtkVolumeProperty.h"
#include "vtkColorTransferFunction.h"
#include "vtkPiecewiseFunction.h"
#include "vtkExtensionManager.h"
#include "vtkgl.h"
#ifndef VTK_IMPLEMENT_MESA_CXX
# include "vtkOpenGL.h"
#endif
#include <math.h>
#include <vtkstd/string>
#include <vtkstd/map>
#include <vtkstd/vector>
#include <assert.h>
#include "vtkClipDataSet.h"
#include "vtkCellArray.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkGeometryFilter.h"
#include "vtkMath.h"
#include "vtkPlane.h"
#include "vtkPlaneCollection.h"
#include "vtkPlanes.h"
#include "vtkPolyData.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include "vtkPoints.h"
#include "vtkTriangle.h"
#include "vtkUnsignedCharArray.h"
#include "vtkUnsignedShortArray.h"
#include "vtkUnsignedIntArray.h"
#include "vtkUnstructuredGrid.h"
#include "vtkVoxel.h"
#include "vtkClipConvexPolyData.h"
#include "vtkClipPolyData.h"
#include "vtkDensifyPolyData.h"
#include "vtkImageResample.h"
#include <sstream>
#include <stdlib.h> // qsort()
#include "vtkDataSetTriangleFilter.h"
#include "vtkAbstractArray.h" // required if compiled against VTK 5.0
#include "vtkTessellatedBoxSource.h"
#include "vtkCleanPolyData.h"
#include "vtkCommand.h" // for VolumeMapperRender{Start|End|Progress}Event
#include "vtkPerlinNoise.h"
#include <vtksys/ios/sstream>
#include "vtkStdString.h"
#include "vtkShaderProgram2.h"
#include "vtkShader2.h"
#include "vtkUniformVariables.h"
#include "vtkShader2Collection.h"
#include "vtkOpenGLRenderWindow.h"
// Uncomment the following line to debug Snow Leopard
//#define APPLE_SNOW_LEOPARD_BUG
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
class vtkUnsupportedRequiredExtensionsStringStream
{
public:
vtkstd::ostringstream Stream;
vtkUnsupportedRequiredExtensionsStringStream()
{
}
private:
// undefined copy constructor.
vtkUnsupportedRequiredExtensionsStringStream(const vtkUnsupportedRequiredExtensionsStringStream &other);
// undefined assignment operator.
vtkUnsupportedRequiredExtensionsStringStream &operator=(const vtkUnsupportedRequiredExtensionsStringStream &other);
};
class vtkMapDataArrayTextureId
{
public:
vtkstd::map<vtkImageData *,vtkKWScalarField *> Map;
vtkMapDataArrayTextureId()
{
}
private:
// undefined copy constructor.
vtkMapDataArrayTextureId(const vtkMapDataArrayTextureId &other);
// undefined assignment operator.
vtkMapDataArrayTextureId &operator=(const vtkMapDataArrayTextureId &other);
};
class vtkMapMaskTextureId
{
public:
vtkstd::map<vtkImageData *,vtkKWMask *> Map;
vtkMapMaskTextureId()
{
}
private:
// undefined copy constructor.
vtkMapMaskTextureId(const vtkMapMaskTextureId &other);
// undefined assignment operator.
vtkMapMaskTextureId &operator=(const vtkMapMaskTextureId &other);
};
//-----------------------------------------------------------------------------
extern const char *vtkGPUVolumeRayCastMapper_CompositeFS;
extern const char *vtkGPUVolumeRayCastMapper_CompositeCroppingFS;
extern const char *vtkGPUVolumeRayCastMapper_CompositeNoCroppingFS;
extern const char *vtkGPUVolumeRayCastMapper_HeaderFS;
extern const char *vtkGPUVolumeRayCastMapper_MIPFS;
extern const char *vtkGPUVolumeRayCastMapper_MIPBinaryMaskFS;
extern const char *vtkGPUVolumeRayCastMapper_MIPFourDependentFS;
extern const char *vtkGPUVolumeRayCastMapper_MIPFourDependentCroppingFS;
extern const char *vtkGPUVolumeRayCastMapper_MIPFourDependentNoCroppingFS;
extern const char *vtkGPUVolumeRayCastMapper_MIPCroppingFS;
extern const char *vtkGPUVolumeRayCastMapper_MIPNoCroppingFS;
extern const char *vtkGPUVolumeRayCastMapper_ParallelProjectionFS;
extern const char *vtkGPUVolumeRayCastMapper_PerspectiveProjectionFS;
extern const char *vtkGPUVolumeRayCastMapper_ScaleBiasFS;
extern const char *vtkGPUVolumeRayCastMapper_MinIPFS;
extern const char *vtkGPUVolumeRayCastMapper_MinIPBinaryMaskFS;
extern const char *vtkGPUVolumeRayCastMapper_MinIPFourDependentFS;
extern const char *vtkGPUVolumeRayCastMapper_MinIPFourDependentCroppingFS;
extern const char *vtkGPUVolumeRayCastMapper_MinIPFourDependentNoCroppingFS;
extern const char *vtkGPUVolumeRayCastMapper_MinIPCroppingFS;
extern const char *vtkGPUVolumeRayCastMapper_MinIPNoCroppingFS;
extern const char *vtkGPUVolumeRayCastMapper_CompositeMaskFS;
extern const char *vtkGPUVolumeRayCastMapper_CompositeBinaryMaskFS;
extern const char *vtkGPUVolumeRayCastMapper_NoShadeFS;
extern const char *vtkGPUVolumeRayCastMapper_ShadeFS;
extern const char *vtkGPUVolumeRayCastMapper_OneComponentFS;
extern const char *vtkGPUVolumeRayCastMapper_FourComponentsFS;
extern const char *vtkGPUVolumeRayCastMapper_AdditiveFS;
extern const char *vtkGPUVolumeRayCastMapper_AdditiveCroppingFS;
extern const char *vtkGPUVolumeRayCastMapper_AdditiveNoCroppingFS;
enum
{
vtkOpenGLGPUVolumeRayCastMapperProjectionNotInitialized=-1, // not init
vtkOpenGLGPUVolumeRayCastMapperProjectionPerspective=0, // false
vtkOpenGLGPUVolumeRayCastMapperProjectionParallel=1 // true
};
enum
{
vtkOpenGLGPUVolumeRayCastMapperMethodNotInitialized,
vtkOpenGLGPUVolumeRayCastMapperMethodMIP,
vtkOpenGLGPUVolumeRayCastMapperMethodMIPBinaryMask,
vtkOpenGLGPUVolumeRayCastMapperMethodMIPFourDependent,
vtkOpenGLGPUVolumeRayCastMapperMethodComposite,
vtkOpenGLGPUVolumeRayCastMapperMethodMinIP,
vtkOpenGLGPUVolumeRayCastMapperMethodMinIPBinaryMask,
vtkOpenGLGPUVolumeRayCastMapperMethodMinIPFourDependent,
vtkOpenGLGPUVolumeRayCastMapperMethodCompositeMask,
vtkOpenGLGPUVolumeRayCastMapperMethodCompositeBinaryMask,
vtkOpenGLGPUVolumeRayCastMapperMethodAdditive
};
// component implementation
enum
{
vtkOpenGLGPUVolumeRayCastMapperComponentNotInitialized=-1, // not init
vtkOpenGLGPUVolumeRayCastMapperComponentOne=0, // false
vtkOpenGLGPUVolumeRayCastMapperComponentFour=1, // true
vtkOpenGLGPUVolumeRayCastMapperComponentNotUsed=2 // when not composite
};
// Shade implementation
enum
{
vtkOpenGLGPUVolumeRayCastMapperShadeNotInitialized=-1, // not init
vtkOpenGLGPUVolumeRayCastMapperShadeNo=0, // false
vtkOpenGLGPUVolumeRayCastMapperShadeYes=1, // true
vtkOpenGLGPUVolumeRayCastMapperShadeNotUsed=2 // when not composite
};
// Cropping implementation
enum
{
vtkOpenGLGPUVolumeRayCastMapperCroppingNotInitialized,
vtkOpenGLGPUVolumeRayCastMapperCompositeCropping,
vtkOpenGLGPUVolumeRayCastMapperCompositeNoCropping,
vtkOpenGLGPUVolumeRayCastMapperMIPCropping,
vtkOpenGLGPUVolumeRayCastMapperMIPNoCropping,
vtkOpenGLGPUVolumeRayCastMapperMIPFourDependentCropping,
vtkOpenGLGPUVolumeRayCastMapperMIPFourDependentNoCropping,
vtkOpenGLGPUVolumeRayCastMapperMinIPCropping,
vtkOpenGLGPUVolumeRayCastMapperMinIPNoCropping,
vtkOpenGLGPUVolumeRayCastMapperMinIPFourDependentCropping,
vtkOpenGLGPUVolumeRayCastMapperMinIPFourDependentNoCropping,
vtkOpenGLGPUVolumeRayCastMapperAdditiveCropping,
vtkOpenGLGPUVolumeRayCastMapperAdditiveNoCropping,
};
enum
{
vtkOpenGLGPUVolumeRayCastMapperTextureObjectDepthMap=0, // 2d texture
vtkOpenGLGPUVolumeRayCastMapperTextureObjectFrameBufferLeftFront // 2d texture
};
const int vtkOpenGLGPUVolumeRayCastMapperNumberOfTextureObjects=vtkOpenGLGPUVolumeRayCastMapperTextureObjectFrameBufferLeftFront+2;
const int vtkOpenGLGPUVolumeRayCastMapperOpacityTableSize=1024; //power of two
#ifndef VTK_IMPLEMENT_MESA_CXX
vtkStandardNewMacro(vtkOpenGLGPUVolumeRayCastMapper);
#endif
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
class vtkOpacityTable
{
public:
vtkOpacityTable()
{
this->TextureId=0;
this->LastBlendMode=vtkVolumeMapper::MAXIMUM_INTENSITY_BLEND;
this->LastSampleDistance=1.0;
this->Table=0;
this->Loaded=false;
this->LastLinearInterpolation=false;
}
~vtkOpacityTable()
{
if(this->TextureId!=0)
{
glDeleteTextures(1,&this->TextureId);
this->TextureId=0;
}
if(this->Table!=0)
{
delete[] this->Table;
this->Table=0;
}
}
bool IsLoaded()
{
return this->Loaded;
}
void Bind()
{
assert("pre: uptodate" && this->Loaded);
glBindTexture(GL_TEXTURE_1D,this->TextureId);
}
// \pre the active texture is set to TEXTURE2
void Update(vtkPiecewiseFunction *scalarOpacity,
int blendMode,
double sampleDistance,
double range[2],
double unitDistance,
bool linearInterpolation)
{
assert("pre: scalarOpacity_exists" && scalarOpacity!=0);
bool needUpdate=false;
if(this->TextureId==0)
{
glGenTextures(1,&this->TextureId);
needUpdate=true;
}
glBindTexture(GL_TEXTURE_1D,this->TextureId);
if(needUpdate)
{
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S,
vtkgl::CLAMP_TO_EDGE);
}
if(scalarOpacity->GetMTime() > this->BuildTime ||
(this->LastBlendMode!=blendMode)
|| (blendMode==vtkVolumeMapper::COMPOSITE_BLEND &&
this->LastSampleDistance!=sampleDistance)
|| needUpdate || !this->Loaded)
{
this->Loaded=false;
if(this->Table==0)
{
this->Table=
new float[vtkOpenGLGPUVolumeRayCastMapperOpacityTableSize];
}
scalarOpacity->GetTable(range[0],range[1],
vtkOpenGLGPUVolumeRayCastMapperOpacityTableSize,
this->Table);
this->LastBlendMode=blendMode;
// Correct the opacity array for the spacing between the planes if we
// are using a composite blending operation
if(blendMode==vtkVolumeMapper::COMPOSITE_BLEND)
{
float *ptr=this->Table;
double factor=sampleDistance/unitDistance;
int i=0;
while(i<vtkOpenGLGPUVolumeRayCastMapperOpacityTableSize)
{
if(*ptr>0.0001f)
{
*ptr=static_cast<float>(1.0-pow(1.0-static_cast<double>(*ptr),
factor));
}
++ptr;
++i;
}
this->LastSampleDistance=sampleDistance;
}
else if (blendMode==vtkVolumeMapper::ADDITIVE_BLEND)
{
float *ptr=this->Table;
double factor=sampleDistance/unitDistance;
int i=0;
while(i<vtkOpenGLGPUVolumeRayCastMapperOpacityTableSize)
{
if(*ptr>0.0001f)
{
*ptr=static_cast<float>(static_cast<double>(*ptr)*factor);
}
++ptr;
++i;
}
this->LastSampleDistance=sampleDistance;
}
glTexImage1D(GL_TEXTURE_1D,0,GL_ALPHA16,
vtkOpenGLGPUVolumeRayCastMapperOpacityTableSize,0,
GL_ALPHA,GL_FLOAT,this->Table);
vtkOpenGLGPUVolumeRayCastMapper::PrintError("1d opacity texture is too large");
this->Loaded=true;
this->BuildTime.Modified();
}
needUpdate=needUpdate ||
this->LastLinearInterpolation!=linearInterpolation;
if(needUpdate)
{
this->LastLinearInterpolation=linearInterpolation;
GLint value;
if(linearInterpolation)
{
value=GL_LINEAR;
}
else
{
value=GL_NEAREST;
}
glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MIN_FILTER,value);
glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MAG_FILTER,value);
}
}
protected:
GLuint TextureId;
int LastBlendMode;
double LastSampleDistance;
vtkTimeStamp BuildTime;
float *Table;
bool Loaded;
bool LastLinearInterpolation;
};
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
class vtkOpacityTables
{
public:
vtkstd::vector<vtkOpacityTable> Vector;
vtkOpacityTables(size_t numberOfLevels)
: Vector(numberOfLevels)
{
}
private:
// undefined copy constructor.
vtkOpacityTables(const vtkOpacityTables &other);
// undefined assignment operator.
vtkOpacityTables &operator=(const vtkOpacityTables &other);
};
//-----------------------------------------------------------------------------
class vtkRGBTable
{
public:
vtkRGBTable()
{
this->TextureId=0;
this->Table=0;
this->Loaded=false;
this->LastLinearInterpolation=false;
}
~vtkRGBTable()
{
if(this->TextureId!=0)
{
glDeleteTextures(1,&this->TextureId);
this->TextureId=0;
}
if(this->Table!=0)
{
delete[] this->Table;
this->Table=0;
}
}
bool IsLoaded()
{
return this->Loaded;
}
void Bind()
{
assert("pre: uptodate" && this->Loaded);
glBindTexture(GL_TEXTURE_1D,this->TextureId);
}
// \pre the active texture is set properly. (default color,
// mask1, mask2,..)
void Update(vtkColorTransferFunction *scalarRGB,
double range[2],
bool linearInterpolation)
{
assert("pre: scalarRGB_exists" && scalarRGB!=0);
bool needUpdate=false;
if(this->TextureId==0)
{
glGenTextures(1,&this->TextureId);
needUpdate=true;
}
glBindTexture(GL_TEXTURE_1D,this->TextureId);
if(needUpdate)
{
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S,
vtkgl::CLAMP_TO_EDGE);
}
if(scalarRGB->GetMTime() > this->BuildTime
|| needUpdate || !this->Loaded)
{
this->Loaded=false;
if(this->Table==0)
{
this->Table=
new float[vtkOpenGLGPUVolumeRayCastMapperOpacityTableSize*3];
}
scalarRGB->GetTable(range[0],range[1],
vtkOpenGLGPUVolumeRayCastMapperOpacityTableSize,
this->Table);
glTexImage1D(GL_TEXTURE_1D,0,GL_RGB16,
vtkOpenGLGPUVolumeRayCastMapperOpacityTableSize,0,
GL_RGB,GL_FLOAT,this->Table);
vtkOpenGLGPUVolumeRayCastMapper::PrintError("1d RGB texture is too large");
this->Loaded=true;
this->BuildTime.Modified();
}
needUpdate=needUpdate ||
this->LastLinearInterpolation!=linearInterpolation;
if(needUpdate)
{
this->LastLinearInterpolation=linearInterpolation;
GLint value;
if(linearInterpolation)
{
value=GL_LINEAR;
}
else
{
value=GL_NEAREST;
}
glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MIN_FILTER,value);
glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MAG_FILTER,value);
}
}
protected:
GLuint TextureId;
vtkTimeStamp BuildTime;
float *Table;
bool Loaded;
bool LastLinearInterpolation;
};
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
class vtkKWScalarField
{
public:
vtkKWScalarField()
{
this->TextureId=0;
this->Loaded=false;
this->Supports_GL_ARB_texture_float=false;
this->LoadedTableRange[0]=0.0;
this->LoadedTableRange[1]=1.0;
this->LoadedExtent[0]=VTK_INT_MAX;
this->LoadedExtent[1]=VTK_INT_MIN;
this->LoadedExtent[2]=VTK_INT_MAX;
this->LoadedExtent[3]=VTK_INT_MIN;
this->LoadedExtent[4]=VTK_INT_MAX;
this->LoadedExtent[5]=VTK_INT_MIN;
}
~vtkKWScalarField()
{
if(this->TextureId!=0)
{
glDeleteTextures(1,&this->TextureId);
this->TextureId=0;
}
}
vtkTimeStamp GetBuildTime()
{
return this->BuildTime;
}
void Bind()
{
assert("pre: uptodate" && this->Loaded);
glBindTexture(vtkgl::TEXTURE_3D,this->TextureId);
}
void Update(vtkImageData *input,
int cellFlag,
int textureExtent[6],
int scalarMode,
int arrayAccessMode,
int arrayId,
const char *arrayName,
bool linearInterpolation,
double tableRange[2],
int maxMemoryInBytes)
{
bool needUpdate=false;
bool modified=false;
if(this->TextureId==0)
{
glGenTextures(1,&this->TextureId);
needUpdate=true;
}
glBindTexture(vtkgl::TEXTURE_3D,this->TextureId);
int obsolete=needUpdate || !this->Loaded || input->GetMTime()>this->BuildTime;
if(!obsolete)
{
obsolete=cellFlag!=this->LoadedCellFlag;
int i=0;
while(!obsolete && i<6)
{
obsolete=obsolete || this->LoadedExtent[i]>textureExtent[i];
++i;
obsolete=obsolete || this->LoadedExtent[i]<textureExtent[i];
++i;
}
}
if(!obsolete)
{
obsolete=this->LoadedTableRange[0]!=tableRange[0] ||
this->LoadedTableRange[1]!=tableRange[1];
}
if(obsolete)
{
this->Loaded=false;
int dim[3];
input->GetDimensions(dim);
GLint internalFormat=0;
GLenum format=0;
GLenum type=0;
// shift then scale: y:=(x+shift)*scale
double shift=0.0;
double scale=1.0;
int needTypeConversion=0;
vtkDataArray *sliceArray=0;
vtkDataArray *scalars=
vtkAbstractMapper::GetScalars(input,scalarMode,arrayAccessMode,
arrayId,arrayName,
this->LoadedCellFlag);
// DONT USE GetScalarType() or GetNumberOfScalarComponents() on
// ImageData as it deals only with point data...
int scalarType=scalars->GetDataType();
if(scalars->GetNumberOfComponents()==4)
{
// this is RGBA, unsigned char only
internalFormat=GL_RGBA16;
format=GL_RGBA;
type=GL_UNSIGNED_BYTE;
}
else
{
// input->GetNumberOfScalarComponents()==1
switch(scalarType)
{
case VTK_FLOAT:
if(this->Supports_GL_ARB_texture_float)
{
internalFormat=vtkgl::INTENSITY16F_ARB;
}
else
{
internalFormat=GL_INTENSITY16;
}
format=GL_RED;
type=GL_FLOAT;
shift=-tableRange[0];
scale=1/(tableRange[1]-tableRange[0]);
break;
case VTK_UNSIGNED_CHAR:
internalFormat=GL_INTENSITY8;
format=GL_RED;
type=GL_UNSIGNED_BYTE;
shift=-tableRange[0]/VTK_UNSIGNED_CHAR_MAX;
scale=
VTK_UNSIGNED_CHAR_MAX/(tableRange[1]-tableRange[0]);
break;
case VTK_SIGNED_CHAR:
internalFormat=GL_INTENSITY8;
format=GL_RED;
type=GL_BYTE;
shift=-(2*tableRange[0]+1)/VTK_UNSIGNED_CHAR_MAX;
scale=VTK_SIGNED_CHAR_MAX/(tableRange[1]-tableRange[0]);
break;
case VTK_CHAR:
// not supported
assert("check: impossible case" && 0);
break;
case VTK_BIT:
// not supported
assert("check: impossible case" && 0);
break;
case VTK_ID_TYPE:
// not supported
assert("check: impossible case" && 0);
break;
case VTK_INT:
internalFormat=GL_INTENSITY16;
format=GL_RED;
type=GL_INT;
shift=-(2*tableRange[0]+1)/VTK_UNSIGNED_INT_MAX;
scale=VTK_INT_MAX/(tableRange[1]-tableRange[0]);
break;
case VTK_DOUBLE:
case VTK___INT64:
case VTK_LONG:
case VTK_LONG_LONG:
case VTK_UNSIGNED___INT64:
case VTK_UNSIGNED_LONG:
case VTK_UNSIGNED_LONG_LONG:
needTypeConversion=1; // to float
if(this->Supports_GL_ARB_texture_float)
{
internalFormat=vtkgl::INTENSITY16F_ARB;
}
else
{
internalFormat=GL_INTENSITY16;
}
format=GL_RED;
type=GL_FLOAT;
shift=-tableRange[0];
scale=1/(tableRange[1]-tableRange[0]);
sliceArray=vtkFloatArray::New();
break;
case VTK_SHORT:
internalFormat=GL_INTENSITY16;
format=GL_RED;
type=GL_SHORT;
shift=-(2*tableRange[0]+1)/VTK_UNSIGNED_SHORT_MAX;
scale=VTK_SHORT_MAX/(tableRange[1]-tableRange[0]);
break;
case VTK_STRING:
// not supported
assert("check: impossible case" && 0);
break;
case VTK_UNSIGNED_SHORT:
internalFormat=GL_INTENSITY16;
format=GL_RED;
type=GL_UNSIGNED_SHORT;
shift=-tableRange[0]/VTK_UNSIGNED_SHORT_MAX;
scale=
VTK_UNSIGNED_SHORT_MAX/(tableRange[1]-tableRange[0]);
break;
case VTK_UNSIGNED_INT:
internalFormat=GL_INTENSITY16;
format=GL_RED;
type=GL_UNSIGNED_INT;
shift=-tableRange[0]/VTK_UNSIGNED_INT_MAX;
scale=VTK_UNSIGNED_INT_MAX/(tableRange[1]-tableRange[0]);
break;
default:
assert("check: impossible case" && 0);
break;
}
}
// Enough memory?
int textureSize[3];
int i=0;
while(i<3)
{
textureSize[i]=textureExtent[2*i+1]-textureExtent[2*i]+1;
++i;
}
GLint width;
glGetIntegerv(vtkgl::MAX_3D_TEXTURE_SIZE,&width);
this->Loaded=textureSize[0]<=width && textureSize[1]<=width
&& textureSize[2]<=width;
if(this->Loaded)
{
// so far, so good. the texture size is theorically small enough
// for OpenGL
vtkgl::TexImage3D(vtkgl::PROXY_TEXTURE_3D,0,internalFormat,
textureSize[0],textureSize[1],textureSize[2],0,
format,type,0);
glGetTexLevelParameteriv(vtkgl::PROXY_TEXTURE_3D,0,GL_TEXTURE_WIDTH,
&width);
this->Loaded=width!=0;
if(this->Loaded)
{
// so far, so good but some cards always succeed with a proxy texture
// let's try to actually allocate..
vtkgl::TexImage3D(vtkgl::TEXTURE_3D,0,internalFormat,textureSize[0],
textureSize[1],textureSize[2],0,format,type,0);
GLenum errorCode=glGetError();
this->Loaded=errorCode!=GL_OUT_OF_MEMORY;
if(this->Loaded)
{
// so far, so good, actual allocation succeeded.
if(errorCode!=GL_NO_ERROR)
{
cout<<"after try to load the texture";
cout<<" ERROR (x"<<hex<<errorCode<<") "<<dec;
cout<<vtkOpenGLGPUVolumeRayCastMapper::OpenGLErrorMessage(static_cast<unsigned int>(errorCode));
cout<<endl;
}
// so far, so good but some cards don't report allocation error
this->Loaded=textureSize[0]*textureSize[1]*
textureSize[2]*vtkAbstractArray::GetDataTypeSize(scalarType)*
scalars->GetNumberOfComponents()<=maxMemoryInBytes;
if(this->Loaded)
{
// OK, we consider the allocation above succeeded...
// If it actually didn't the only to fix it for the user
// is to decrease the value of this->MaxMemoryInBytes.
// enough memory! We can load the scalars!
double bias=shift*scale;
// we don't clamp to edge because for the computation of the
// gradient on the border we need some external value.
glTexParameterf(vtkgl::TEXTURE_3D,vtkgl::TEXTURE_WRAP_R,vtkgl::CLAMP_TO_EDGE);
glTexParameterf(vtkgl::TEXTURE_3D,GL_TEXTURE_WRAP_S,vtkgl::CLAMP_TO_EDGE);
glTexParameterf(vtkgl::TEXTURE_3D,GL_TEXTURE_WRAP_T,vtkgl::CLAMP_TO_EDGE);
GLfloat borderColor[4]={0.0,0.0,0.0,0.0};
glTexParameterfv(vtkgl::TEXTURE_3D,GL_TEXTURE_BORDER_COLOR, borderColor);
if(needTypeConversion)
{
// Convert and send to the GPU, z-slice by z-slice.
// Allocate memory on the GPU (NULL data pointer with the right
// dimensions)
// Here we are assuming that GL_ARB_texture_non_power_of_two is
// available
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
// memory allocation is already done.
// Send the slices:
// allocate CPU memory for a slice.
sliceArray->SetNumberOfComponents(1); // FB TODO CHECK THAT
sliceArray->SetNumberOfTuples(textureSize[0]*textureSize[1]);
void *slicePtr=sliceArray->GetVoidPointer(0);
int k=0;
int kInc=(dim[0]-cellFlag)*(dim[1]-cellFlag);
int kOffset=(textureExtent[4]*(dim[1]-cellFlag)
+textureExtent[2])*(dim[0]-cellFlag)
+textureExtent[0];
while(k<textureSize[2])
{
int j=0;
int jOffset=0;
int jDestOffset=0;
while(j<textureSize[1])
{
i=0;
while(i<textureSize[0])
{
sliceArray->SetTuple1(jDestOffset+i,
(scalars->GetTuple1(kOffset+jOffset
+i)
+shift)*scale);
++i;
}
++j;
jOffset+=dim[0]-cellFlag;
jDestOffset+=textureSize[0];
}
// Here we are assuming that GL_ARB_texture_non_power_of_two is
// available
vtkgl::TexSubImage3D(vtkgl::TEXTURE_3D, 0,
0,0,k,
textureSize[0],textureSize[1],
1, // depth is 1, not 0!
format,type, slicePtr);
++k;
kOffset+=kInc;
}
sliceArray->Delete();
}
else
{
// One chunk of data to the GPU.
// It works for the whole volume or for a subvolume.
// Here we are assuming that GL_ARB_texture_non_power_of_two is
// available
// make sure any previous OpenGL call is executed and will not
// be disturbed by our PixelTransfer value
glFinish();
glPixelTransferf(GL_RED_SCALE,static_cast<GLfloat>(scale));
glPixelTransferf(GL_RED_BIAS,static_cast<GLfloat>(bias));
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
if(!(textureExtent[1]-textureExtent[0]+cellFlag==dim[0]))
{
glPixelStorei(GL_UNPACK_ROW_LENGTH,dim[0]-cellFlag);
}
if(!(textureExtent[3]-textureExtent[2]+cellFlag==dim[1]))
{
glPixelStorei(vtkgl::UNPACK_IMAGE_HEIGHT_EXT,
dim[1]-cellFlag);
}
void *dataPtr=scalars->GetVoidPointer(
((textureExtent[4]*(dim[1]-cellFlag)+textureExtent[2])
*(dim[0]-cellFlag)+textureExtent[0])
*scalars->GetNumberOfComponents());
if(1) // !this->SupportsPixelBufferObjects)
{
vtkgl::TexImage3D(vtkgl::TEXTURE_3D, 0, internalFormat,
textureSize[0],textureSize[1],textureSize[2],
0,format,type,dataPtr);
}
else
{
GLuint pbo=0;
vtkgl::GenBuffers(1,&pbo);
vtkOpenGLGPUVolumeRayCastMapper::PrintError("genbuffer");
vtkgl::BindBuffer(vtkgl::PIXEL_UNPACK_BUFFER,pbo);
vtkOpenGLGPUVolumeRayCastMapper::PrintError("binbuffer");
vtkgl::GLsizeiptr texSize=
textureSize[0]*textureSize[1]*textureSize[2]*
vtkAbstractArray::GetDataTypeSize(scalarType)*
scalars->GetNumberOfComponents();
vtkgl::BufferData(vtkgl::PIXEL_UNPACK_BUFFER,texSize,dataPtr,
vtkgl::STREAM_DRAW);
vtkOpenGLGPUVolumeRayCastMapper::PrintError("bufferdata");
vtkgl::TexImage3D(vtkgl::TEXTURE_3D, 0, internalFormat,
textureSize[0],textureSize[1],textureSize[2],
0,format,type,0);
vtkOpenGLGPUVolumeRayCastMapper::PrintError("teximage3d");
vtkgl::BindBuffer(vtkgl::PIXEL_UNPACK_BUFFER,0);
vtkOpenGLGPUVolumeRayCastMapper::PrintError("bindbuffer to 0");
vtkgl::DeleteBuffers(1,&pbo);
}
vtkOpenGLGPUVolumeRayCastMapper::PrintError("3d texture is too large2");
// make sure TexImage3D is executed with our PixelTransfer mode
glFinish();
// Restore the default values.
glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
glPixelStorei(vtkgl::UNPACK_IMAGE_HEIGHT_EXT,0);
glPixelTransferf(GL_RED_SCALE,1.0);
glPixelTransferf(GL_RED_BIAS,0.0);
}
this->LoadedCellFlag=cellFlag;
i=0;
while(i<6)
{
this->LoadedExtent[i]=textureExtent[i];
++i;
}
double spacing[3];
double origin[3];
input->GetSpacing(spacing);
input->GetOrigin(origin);
int swapBounds[3];
swapBounds[0]=(spacing[0]<0);
swapBounds[1]=(spacing[1]<0);
swapBounds[2]=(spacing[2]<0);
if(!this->LoadedCellFlag) // loaded extents represent points
{
// slabsPoints[i]=(slabsDataSet[i] - origin[i/2]) / spacing[i/2];
// in general, x=o+i*spacing.
// if spacing is positive min extent match the min of the
// bounding box
// and the max extent match the max of the bounding box
// if spacing is negative min extent match the max of the
// bounding box
// and the max extent match the min of the bounding box
// if spacing is negative, we may have to rethink the equation
// between real point and texture coordinate...
this->LoadedBounds[0]=origin[0]+
static_cast<double>(this->LoadedExtent[0+swapBounds[0]])*spacing[0];
this->LoadedBounds[2]=origin[1]+
static_cast<double>(this->LoadedExtent[2+swapBounds[1]])*spacing[1];
this->LoadedBounds[4]=origin[2]+
static_cast<double>(this->LoadedExtent[4+swapBounds[2]])*spacing[2];
this->LoadedBounds[1]=origin[0]+
static_cast<double>(this->LoadedExtent[1-swapBounds[0]])*spacing[0];
this->LoadedBounds[3]=origin[1]+
static_cast<double>(this->LoadedExtent[3-swapBounds[1]])*spacing[1];
this->LoadedBounds[5]=origin[2]+
static_cast<double>(this->LoadedExtent[5-swapBounds[2]])*spacing[2];
}
else // loaded extents represent cells
{
int wholeTextureExtent[6];
input->GetExtent(wholeTextureExtent);
i=1;
while(i<6)
{
wholeTextureExtent[i]--;
i+=2;
}
i=0;
while(i<3)
{
if(this->LoadedExtent[2*i]==wholeTextureExtent[2*i])
{
this->LoadedBounds[2*i+swapBounds[i]]=origin[i];
}
else
{
this->LoadedBounds[2*i+swapBounds[i]]=origin[i]+
(static_cast<double>(this->LoadedExtent[2*i])+0.5)*spacing[i];
}