/
index.js
1499 lines (1350 loc) · 45.8 KB
/
index.js
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
import WebworkerPromise from 'webworker-promise';
import Constants from 'vtk.js/Sources/Rendering/OpenGL/Texture/Constants';
import macro from 'vtk.js/Sources/macro';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import vtkMath from 'vtk.js/Sources/Common/Core/Math';
import vtkViewNode from 'vtk.js/Sources/Rendering/SceneGraph/ViewNode';
import ComputeGradientsWorker from './ComputeGradients.worker';
const { Wrap, Filter } = Constants;
const { VtkDataTypes } = vtkDataArray;
const { vtkDebugMacro, vtkErrorMacro, vtkWarningMacro } = macro;
// ----------------------------------------------------------------------------
// vtkOpenGLTexture methods
// ----------------------------------------------------------------------------
function vtkOpenGLTexture(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkOpenGLTexture');
// Renders myself
publicAPI.render = (renWin = null) => {
if (renWin) {
model.openGLRenderWindow = renWin;
} else {
model.openGLRenderer = publicAPI.getFirstAncestorOfType(
'vtkOpenGLRenderer'
);
// sync renderable properties
model.openGLRenderWindow = model.openGLRenderer.getParent();
}
model.context = model.openGLRenderWindow.getContext();
if (model.renderable.getInterpolate()) {
if (model.generateMipmap) {
publicAPI.setMinificationFilter(Filter.LINEAR_MIPMAP_LINEAR);
} else {
publicAPI.setMinificationFilter(Filter.LINEAR);
}
publicAPI.setMagnificationFilter(Filter.LINEAR);
} else {
publicAPI.setMinificationFilter(Filter.NEAREST);
publicAPI.setMagnificationFilter(Filter.NEAREST);
}
if (model.renderable.getRepeat()) {
publicAPI.setWrapR(Wrap.REPEAT);
publicAPI.setWrapS(Wrap.REPEAT);
publicAPI.setWrapT(Wrap.REPEAT);
}
// clear image if input data is set
if (model.renderable.getInputData()) {
model.renderable.setImage(null);
}
// create the texture if it is not done already
if (
!model.handle ||
model.renderable.getMTime() > model.textureBuildTime.getMTime()
) {
// if we have an Image
if (model.renderable.getImage() !== null) {
if (model.renderable.getInterpolate()) {
model.generateMipmap = true;
publicAPI.setMinificationFilter(Filter.LINEAR_MIPMAP_LINEAR);
}
// Have an Image which may not be complete
if (model.renderable.getImage() && model.renderable.getImageLoaded()) {
publicAPI.create2DFromImage(model.renderable.getImage());
publicAPI.activate();
publicAPI.sendParameters();
model.textureBuildTime.modified();
}
}
// if we have Inputdata
const input = model.renderable.getInputData(0);
if (input && input.getPointData().getScalars()) {
const ext = input.getExtent();
const inScalars = input.getPointData().getScalars();
// do we have a cube map? Six inputs
const data = [];
for (let i = 0; i < model.renderable.getNumberOfInputPorts(); ++i) {
const indata = model.renderable.getInputData(i);
const scalars = indata
? indata
.getPointData()
.getScalars()
.getData()
: null;
if (scalars) {
data.push(scalars);
}
}
if (
model.renderable.getInterpolate() &&
inScalars.getNumberOfComponents() === 4
) {
model.generateMipmap = true;
publicAPI.setMinificationFilter(Filter.LINEAR_MIPMAP_LINEAR);
}
if (data.length % 6 === 0) {
publicAPI.createCubeFromRaw(
ext[1] - ext[0] + 1,
ext[3] - ext[2] + 1,
inScalars.getNumberOfComponents(),
inScalars.getDataType(),
data
);
} else {
publicAPI.create2DFromRaw(
ext[1] - ext[0] + 1,
ext[3] - ext[2] + 1,
inScalars.getNumberOfComponents(),
inScalars.getDataType(),
inScalars.getData()
);
}
publicAPI.activate();
publicAPI.sendParameters();
model.textureBuildTime.modified();
}
}
if (model.handle) {
publicAPI.activate();
}
};
//----------------------------------------------------------------------------
publicAPI.destroyTexture = () => {
// deactivate it first
publicAPI.deactivate();
if (model.context && model.handle) {
model.context.deleteTexture(model.handle);
}
model.handle = 0;
model.numberOfDimensions = 0;
model.target = 0;
model.components = 0;
model.width = 0;
model.height = 0;
model.depth = 0;
publicAPI.resetFormatAndType();
};
//----------------------------------------------------------------------------
publicAPI.createTexture = () => {
// reuse the existing handle if we have one
if (!model.handle) {
model.handle = model.context.createTexture();
if (model.target) {
model.context.bindTexture(model.target, model.handle);
// See: http://www.openmodel.context..org/wiki/Common_Mistakes#Creating_a_complete_texture
// turn off mip map filter or set the base and max level correctly. here
// both are done.
model.context.texParameteri(
model.target,
model.context.TEXTURE_MIN_FILTER,
publicAPI.getOpenGLFilterMode(model.minificationFilter)
);
model.context.texParameteri(
model.target,
model.context.TEXTURE_MAG_FILTER,
publicAPI.getOpenGLFilterMode(model.magnificationFilter)
);
model.context.texParameteri(
model.target,
model.context.TEXTURE_WRAP_S,
publicAPI.getOpenGLWrapMode(model.wrapS)
);
model.context.texParameteri(
model.target,
model.context.TEXTURE_WRAP_T,
publicAPI.getOpenGLWrapMode(model.wrapT)
);
model.context.bindTexture(model.target, null);
}
}
};
//---------------------------------------------------------------------------
publicAPI.getTextureUnit = () => {
if (model.openGLRenderWindow) {
return model.openGLRenderWindow.getTextureUnitForTexture(publicAPI);
}
return -1;
};
//---------------------------------------------------------------------------
publicAPI.activate = () => {
// activate a free texture unit for this texture
model.openGLRenderWindow.activateTexture(publicAPI);
publicAPI.bind();
};
//---------------------------------------------------------------------------
publicAPI.deactivate = () => {
if (model.openGLRenderWindow) {
model.openGLRenderWindow.deactivateTexture(publicAPI);
}
};
//---------------------------------------------------------------------------
publicAPI.releaseGraphicsResources = (rwin) => {
if (rwin && model.handle) {
rwin.activateTexture(publicAPI);
rwin.deactivateTexture(publicAPI);
model.context.deleteTexture(model.handle);
model.handle = 0;
model.numberOfDimensions = 0;
model.target = 0;
model.internalFormat = 0;
model.format = 0;
model.openGLDataType = 0;
model.components = 0;
model.width = 0;
model.height = 0;
model.depth = 0;
}
if (model.shaderProgram) {
model.shaderProgram.releaseGraphicsResources(rwin);
model.shaderProgram = null;
}
};
//----------------------------------------------------------------------------
publicAPI.bind = () => {
model.context.bindTexture(model.target, model.handle);
if (
model.autoParameters &&
publicAPI.getMTime() > model.sendParametersTime.getMTime()
) {
publicAPI.sendParameters();
}
};
//----------------------------------------------------------------------------
publicAPI.isBound = () => {
let result = false;
if (model.context && model.handle) {
let target = 0;
switch (model.target) {
case model.context.TEXTURE_2D:
target = model.context.TEXTURE_BINDING_2D;
break;
default:
vtkWarningMacro('impossible case');
break;
}
const oid = model.context.getIntegerv(target);
result = oid === model.handle;
}
return result;
};
//----------------------------------------------------------------------------
publicAPI.sendParameters = () => {
model.context.texParameteri(
model.target,
model.context.TEXTURE_WRAP_S,
publicAPI.getOpenGLWrapMode(model.wrapS)
);
model.context.texParameteri(
model.target,
model.context.TEXTURE_WRAP_T,
publicAPI.getOpenGLWrapMode(model.wrapT)
);
if (model.openGLRenderWindow.getWebgl2()) {
model.context.texParameteri(
model.target,
model.context.TEXTURE_WRAP_R,
publicAPI.getOpenGLWrapMode(model.wrapR)
);
}
model.context.texParameteri(
model.target,
model.context.TEXTURE_MIN_FILTER,
publicAPI.getOpenGLFilterMode(model.minificationFilter)
);
model.context.texParameteri(
model.target,
model.context.TEXTURE_MAG_FILTER,
publicAPI.getOpenGLFilterMode(model.magnificationFilter)
);
if (model.openGLRenderWindow.getWebgl2()) {
model.context.texParameteri(
model.target,
model.context.TEXTURE_BASE_LEVEL,
model.baseLevel
);
model.context.texParameteri(
model.target,
model.context.TEXTURE_MAX_LEVEL,
model.maxLevel
);
}
// model.context.texParameterf(model.target, model.context.TEXTURE_MIN_LOD, model.minLOD);
// model.context.texParameterf(model.target, model.context.TEXTURE_MAX_LOD, model.maxLOD);
model.sendParametersTime.modified();
};
//----------------------------------------------------------------------------
publicAPI.getInternalFormat = (vtktype, numComps) => {
if (model.internalFormat) {
return model.internalFormat;
}
model.internalFormat = publicAPI.getDefaultInternalFormat(
vtktype,
numComps
);
if (!model.internalFormat) {
vtkDebugMacro(
`Unable to find suitable internal format for T=${vtktype} NC= ${numComps}`
);
}
return model.internalFormat;
};
//----------------------------------------------------------------------------
publicAPI.getDefaultInternalFormat = (vtktype, numComps) => {
let result = 0;
// try default next
result = model.openGLRenderWindow.getDefaultTextureInternalFormat(
vtktype,
numComps,
false
);
if (result) {
return result;
}
// try floating point
result = this.openGLRenderWindow.getDefaultTextureInternalFormat(
vtktype,
numComps,
true
);
if (!result) {
vtkDebugMacro('Unsupported internal texture type!');
vtkDebugMacro(
`Unable to find suitable internal format for T=${vtktype} NC= ${numComps}`
);
}
return result;
};
//----------------------------------------------------------------------------
publicAPI.setInternalFormat = (iFormat) => {
if (iFormat !== model.context.InternalFormat) {
model.internalFormat = iFormat;
publicAPI.modified();
}
};
//----------------------------------------------------------------------------
publicAPI.getFormat = (vtktype, numComps) => {
if (!model.format) {
model.format = publicAPI.getDefaultFormat(vtktype, numComps);
}
return model.format;
};
//----------------------------------------------------------------------------
publicAPI.getDefaultFormat = (vtktype, numComps) => {
if (model.openGLRenderWindow.getWebgl2()) {
switch (numComps) {
case 1:
return model.context.RED;
case 2:
return model.context.RG;
case 3:
return model.context.RGB;
case 4:
return model.context.RGBA;
default:
return model.context.RGB;
}
} else {
switch (numComps) {
case 1:
return model.context.LUMINANCE;
case 2:
return model.context.LUMINANCE_ALPHA;
case 3:
return model.context.RGB;
case 4:
return model.context.RGBA;
default:
return model.context.RGB;
}
}
};
//----------------------------------------------------------------------------
publicAPI.resetFormatAndType = () => {
model.format = 0;
model.internalFormat = 0;
model.openGLDataType = 0;
};
//----------------------------------------------------------------------------
publicAPI.getDefaultDataType = (vtkScalarType) => {
// DON'T DEAL with VTK_CHAR as this is platform dependent.
if (model.openGLRenderWindow.getWebgl2()) {
switch (vtkScalarType) {
// case VtkDataTypes.SIGNED_CHAR:
// return model.context.BYTE;
case VtkDataTypes.UNSIGNED_CHAR:
return model.context.UNSIGNED_BYTE;
// case VtkDataTypes.SHORT:
// return model.context.SHORT;
// case VtkDataTypes.UNSIGNED_SHORT:
// return model.context.UNSIGNED_SHORT;
// case VtkDataTypes.INT:
// return model.context.INT;
// case VtkDataTypes.UNSIGNED_INT:
// return model.context.UNSIGNED_INT;
case VtkDataTypes.FLOAT:
case VtkDataTypes.VOID: // used for depth component textures.
default:
return model.context.FLOAT;
}
}
switch (vtkScalarType) {
// case VtkDataTypes.SIGNED_CHAR:
// return model.context.BYTE;
case VtkDataTypes.UNSIGNED_CHAR:
return model.context.UNSIGNED_BYTE;
// case VtkDataTypes.SHORT:
// return model.context.SHORT;
// case VtkDataTypes.UNSIGNED_SHORT:
// return model.context.UNSIGNED_SHORT;
// case VtkDataTypes.INT:
// return model.context.INT;
// case VtkDataTypes.UNSIGNED_INT:
// return model.context.UNSIGNED_INT;
case VtkDataTypes.FLOAT:
case VtkDataTypes.VOID: // used for depth component textures.
default:
if (
model.context.getExtension('OES_texture_float') &&
model.context.getExtension('OES_texture_float_linear')
) {
return model.context.FLOAT;
}
return model.context.UNSIGNED_BYTE;
}
};
//----------------------------------------------------------------------------
publicAPI.getOpenGLDataType = (vtkScalarType) => {
if (!model.openGLDataType) {
model.openGLDataType = publicAPI.getDefaultDataType(vtkScalarType);
}
return model.openGLDataType;
};
publicAPI.getShiftAndScale = () => {
let shift = 0.0;
let scale = 1.0;
// for all float type internal formats
switch (model.openGLDataType) {
case model.context.BYTE:
scale = 127.5;
shift = scale - 128.0;
break;
case model.context.UNSIGNED_BYTE:
scale = 255.0;
shift = 0.0;
break;
case model.context.SHORT:
scale = 32767.5;
shift = scale - 32768.0;
break;
case model.context.UNSIGNED_SHORT:
scale = 65536.0;
shift = 0.0;
break;
case model.context.INT:
scale = 2147483647.5;
shift = scale - 2147483648.0;
break;
case model.context.UNSIGNED_INT:
scale = 4294967295.0;
shift = 0.0;
break;
case model.context.FLOAT:
default:
break;
}
return { shift, scale };
};
//----------------------------------------------------------------------------
publicAPI.getOpenGLFilterMode = (emode) => {
switch (emode) {
case Filter.NEAREST:
return model.context.NEAREST;
case Filter.LINEAR:
return model.context.LINEAR;
case Filter.NEAREST_MIPMAP_NEAREST:
return model.context.NEAREST_MIPMAP_NEAREST;
case Filter.NEAREST_MIPMAP_LINEAR:
return model.context.NEAREST_MIPMAP_LINEAR;
case Filter.LINEAR_MIPMAP_NEAREST:
return model.context.LINEAR_MIPMAP_NEAREST;
case Filter.LINEAR_MIPMAP_LINEAR:
return model.context.LINEAR_MIPMAP_LINEAR;
default:
return model.context.NEAREST;
}
};
//----------------------------------------------------------------------------
publicAPI.getOpenGLWrapMode = (vtktype) => {
switch (vtktype) {
case Wrap.CLAMP_TO_EDGE:
return model.context.CLAMP_TO_EDGE;
case Wrap.REPEAT:
return model.context.REPEAT;
case Wrap.MIRRORED_REPEAT:
return model.context.MIRRORED_REPEAT;
default:
return model.context.CLAMP_TO_EDGE;
}
};
//----------------------------------------------------------------------------
function updateArrayDataType(dataType, data) {
const pixData = [];
// if the opengl data type is float
// then the data array must be float
if (
dataType !== VtkDataTypes.FLOAT &&
model.openGLDataType === model.context.FLOAT
) {
const pixCount = model.width * model.height * model.components;
for (let idx = 0; idx < data.length; idx++) {
const newArray = new Float32Array(pixCount);
for (let i = 0; i < pixCount; i++) {
newArray[i] = data[idx][i];
}
pixData.push(newArray);
}
}
// if the opengl data type is ubyte
// then the data array must be u8, we currently simply truncate the data
if (
dataType !== VtkDataTypes.UNSIGNED_CHAR &&
model.openGLDataType === model.context.UNSIGNED_BYTE
) {
const pixCount = model.width * model.height * model.components;
for (let idx = 0; idx < data.length; idx++) {
const newArray = new Uint8Array(pixCount);
for (let i = 0; i < pixCount; i++) {
newArray[i] = data[idx][i];
}
pixData.push(newArray);
}
}
// The output has to be filled
if (pixData.length === 0) {
for (let i = 0; i < data.length; i++) {
pixData.push(data[i]);
}
}
return pixData;
}
//----------------------------------------------------------------------------
function scaleTextureToHighestPowerOfTwo(data) {
if (model.openGLRenderWindow.getWebgl2()) {
// No need if webGL2
return data;
}
const pixData = [];
const width = model.width;
const height = model.height;
const numComps = model.components;
if (
data &&
(!vtkMath.isPowerOfTwo(width) || !vtkMath.isPowerOfTwo(height))
) {
// Scale up the texture to the next highest power of two dimensions.
const newWidth = vtkMath.nearestPowerOfTwo(width);
const newHeight = vtkMath.nearestPowerOfTwo(height);
const pixCount = newWidth * newHeight * model.components;
for (let idx = 0; idx < data.length; idx++) {
if (data[idx] !== null) {
let newArray = null;
switch (model.openGLDataType) {
case model.context.FLOAT:
newArray = new Float32Array(pixCount);
break;
default:
case model.context.UNSIGNED_BYTE:
newArray = new Uint8Array(pixCount);
break;
}
const jFactor = height / newHeight;
const iFactor = width / newWidth;
for (let j = 0; j < newHeight; j++) {
const joff = j * newWidth * numComps;
const jidx = j * jFactor;
let jlow = Math.floor(jidx);
let jhi = Math.ceil(jidx);
if (jhi >= height) {
jhi = height - 1;
}
const jmix = jidx - jlow;
const jmix1 = 1.0 - jmix;
jlow = jlow * width * numComps;
jhi = jhi * width * numComps;
for (let i = 0; i < newWidth; i++) {
const ioff = i * numComps;
const iidx = i * iFactor;
let ilow = Math.floor(iidx);
let ihi = Math.ceil(iidx);
if (ihi >= width) {
ihi = width - 1;
}
const imix = iidx - ilow;
ilow *= numComps;
ihi *= numComps;
for (let c = 0; c < numComps; c++) {
newArray[joff + ioff + c] =
data[idx][jlow + ilow + c] * jmix1 * (1.0 - imix) +
data[idx][jlow + ihi + c] * jmix1 * imix +
data[idx][jhi + ilow + c] * jmix * (1.0 - imix) +
data[idx][jhi + ihi + c] * jmix * imix;
}
}
}
pixData.push(newArray);
model.width = newWidth;
model.height = newHeight;
} else {
pixData.push(null);
}
}
}
// The output has to be filled
if (pixData.length === 0) {
for (let i = 0; i < data.length; i++) {
pixData.push(data[i]);
}
}
return pixData;
}
//----------------------------------------------------------------------------
publicAPI.create2DFromRaw = (width, height, numComps, dataType, data) => {
// Now determine the texture parameters using the arguments.
publicAPI.getOpenGLDataType(dataType);
publicAPI.getInternalFormat(dataType, numComps);
publicAPI.getFormat(dataType, numComps);
if (!model.internalFormat || !model.format || !model.openGLDataType) {
vtkErrorMacro('Failed to determine texture parameters.');
return false;
}
model.target = model.context.TEXTURE_2D;
model.components = numComps;
model.width = width;
model.height = height;
model.depth = 1;
model.numberOfDimensions = 2;
model.openGLRenderWindow.activateTexture(publicAPI);
publicAPI.createTexture();
publicAPI.bind();
// Create an array of texture with one texture
const dataArray = [data];
const pixData = updateArrayDataType(dataType, dataArray);
const scaledData = scaleTextureToHighestPowerOfTwo(pixData);
// Source texture data from the PBO.
// model.context.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
model.context.pixelStorei(model.context.UNPACK_ALIGNMENT, 1);
model.context.texImage2D(
model.target,
0,
model.internalFormat,
model.width,
model.height,
0,
model.format,
model.openGLDataType,
scaledData[0]
);
if (model.generateMipmap) {
model.context.generateMipmap(model.target);
}
publicAPI.deactivate();
return true;
};
//----------------------------------------------------------------------------
publicAPI.createCubeFromRaw = (width, height, numComps, dataType, data) => {
// Now determine the texture parameters using the arguments.
publicAPI.getOpenGLDataType(dataType);
publicAPI.getInternalFormat(dataType, numComps);
publicAPI.getFormat(dataType, numComps);
if (!model.internalFormat || !model.format || !model.openGLDataType) {
vtkErrorMacro('Failed to determine texture parameters.');
return false;
}
model.target = model.context.TEXTURE_CUBE_MAP;
model.components = numComps;
model.width = width;
model.height = height;
model.depth = 1;
model.numberOfDimensions = 2;
model.openGLRenderWindow.activateTexture(publicAPI);
model.maxLevel = data.length / 6 - 1;
publicAPI.createTexture();
publicAPI.bind();
const pixData = updateArrayDataType(dataType, data);
const scaledData = scaleTextureToHighestPowerOfTwo(pixData);
// invert the data because opengl is messed up with cube maps
// and uses the old renderman standard with Y going down
// even though it is completely at odds with OpenGL standards
const invertedData = [];
let widthLevel = model.width;
let heightLevel = model.height;
for (let i = 0; i < scaledData.length; i++) {
if (i % 6 === 0 && i !== 0) {
widthLevel /= 2;
heightLevel /= 2;
}
invertedData[i] = new window[dataType](
heightLevel * widthLevel * model.components
);
for (let y = 0; y < heightLevel; ++y) {
const row1 = y * widthLevel * model.components;
const row2 = (heightLevel - y - 1) * widthLevel * model.components;
invertedData[i].set(
scaledData[i].slice(row2, row2 + widthLevel * model.components),
row1
);
}
}
// Source texture data from the PBO.
model.context.pixelStorei(model.context.UNPACK_ALIGNMENT, 1);
// We get the 6 images
for (let i = 0; i < 6; i++) {
// For each mipmap level
let j = 0;
let w = model.width;
let h = model.height;
while (w >= 1 && h >= 1) {
// In webgl 1, all levels need to be defined. So if the latest level size is
// 8x8, we have to add 3 more null textures (4x4, 2x2, 1x1)
// In webgl 2, the attribtue maxLevel will be use.
let tempData = null;
if (j <= model.maxLevel) {
tempData = invertedData[6 * j + i];
}
model.context.texImage2D(
model.context.TEXTURE_CUBE_MAP_POSITIVE_X + i,
j,
model.internalFormat,
w,
h,
0,
model.format,
model.openGLDataType,
tempData
);
j++;
w /= 2;
h /= 2;
}
}
// generateMipmap must not be called here because we manually upload all levels
// if it is called, all levels will be overwritten
publicAPI.deactivate();
return true;
};
//----------------------------------------------------------------------------
publicAPI.createDepthFromRaw = (width, height, dataType, data) => {
// Now determine the texture parameters using the arguments.
publicAPI.getOpenGLDataType(dataType);
model.format = model.context.DEPTH_COMPONENT;
model.internalFormat = model.context.DEPTH_COMPONENT;
if (!model.internalFormat || !model.format || !model.openGLDataType) {
vtkErrorMacro('Failed to determine texture parameters.');
return false;
}
model.target = model.context.TEXTURE_2D;
model.components = 1;
model.width = width;
model.height = height;
model.depth = 1;
model.numberOfDimensions = 2;
model.openGLRenderWindow.activateTexture(publicAPI);
publicAPI.createTexture();
publicAPI.bind();
// Source texture data from the PBO.
// model.context.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
model.context.pixelStorei(model.context.UNPACK_ALIGNMENT, 1);
model.context.texImage2D(
model.target,
0,
model.internalFormat,
model.width,
model.height,
0,
model.format,
model.openGLDataType,
data
);
if (model.generateMipmap) {
model.context.generateMipmap(model.target);
}
publicAPI.deactivate();
return true;
};
//----------------------------------------------------------------------------
publicAPI.create2DFromImage = (image) => {
// Now determine the texture parameters using the arguments.
publicAPI.getOpenGLDataType(VtkDataTypes.UNSIGNED_CHAR);
publicAPI.getInternalFormat(VtkDataTypes.UNSIGNED_CHAR, 4);
publicAPI.getFormat(VtkDataTypes.UNSIGNED_CHAR, 4);
if (!model.internalFormat || !model.format || !model.openGLDataType) {
vtkErrorMacro('Failed to determine texture parameters.');
return false;
}
model.target = model.context.TEXTURE_2D;
model.components = 4;
model.width = image.width;
model.height = image.height;
model.depth = 1;
model.numberOfDimensions = 2;
model.openGLRenderWindow.activateTexture(publicAPI);
publicAPI.createTexture();
publicAPI.bind();
// Source texture data from the PBO.
// model.context.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
model.context.pixelStorei(model.context.UNPACK_ALIGNMENT, 1);
// Scale up the texture to the next highest power of two dimensions (if needed) and flip y.
const needNearestPowerOfTwo =
!vtkMath.isPowerOfTwo(image.width) || !vtkMath.isPowerOfTwo(image.height);
const canvas = document.createElement('canvas');
canvas.width = needNearestPowerOfTwo
? vtkMath.nearestPowerOfTwo(image.width)
: image.width;
canvas.height = needNearestPowerOfTwo
? vtkMath.nearestPowerOfTwo(image.height)
: image.height;
const ctx = canvas.getContext('2d');
ctx.translate(0, canvas.height);
ctx.scale(1, -1);
ctx.drawImage(
image,
0,
0,
image.width,
image.height,
0,
0,
canvas.width,
canvas.height
);
// In Chrome 69 on Windows and Ubuntu, there is a bug that prevents some
// canvases from working properly with webGL textures. By getting any
// image data from the canvas, this works around the bug. See
// https://bugs.chromium.org/p/chromium/issues/detail?id=896307
ctx.getImageData(0, 0, 1, 1);
const safeImage = canvas;
model.context.texImage2D(
model.target,
0,
model.internalFormat,
model.format,
model.openGLDataType,
safeImage
);
if (model.generateMipmap) {
model.context.generateMipmap(model.target);
}
publicAPI.deactivate();
return true;
};
//----------------------------------------------------------------------------
publicAPI.create3DFromRaw = (
width,
height,
depth,
numComps,
dataType,
data
) => {
// Now determine the texture parameters using the arguments.
publicAPI.getOpenGLDataType(dataType);
publicAPI.getInternalFormat(dataType, numComps);
publicAPI.getFormat(dataType, numComps);
if (!model.internalFormat || !model.format || !model.openGLDataType) {
vtkErrorMacro('Failed to determine texture parameters.');
return false;
}
model.target = model.context.TEXTURE_3D;
model.components = numComps;
model.width = width;
model.height = height;
model.depth = depth;
model.numberOfDimensions = 3;
model.openGLRenderWindow.activateTexture(publicAPI);
publicAPI.createTexture();
publicAPI.bind();
// Source texture data from the PBO.
// model.context.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
// model.context.pixelStorei(model.context.UNPACK_ALIGNMENT, 1);
model.context.texImage3D(
model.target,
0,
model.internalFormat,
model.width,
model.height,
model.depth,
0,
model.format,
model.openGLDataType,
data
);
if (model.generateMipmap) {
model.context.generateMipmap(model.target);
}
publicAPI.deactivate();
return true;
};
//----------------------------------------------------------------------------
// This method simulates a 3D texture using 2D
publicAPI.create3DOneComponentFromRaw = (
width,
height,
depth,
dataType,
data
) => {
const numPixelsIn = width * height * depth;