-
Notifications
You must be signed in to change notification settings - Fork 33
/
glvolume2.pas
2921 lines (2828 loc) · 110 KB
/
glvolume2.pas
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
unit glvolume2;
{$mode objfpc}
{$H+}
interface
{$include opts.inc} //for DEFINE FASTGZ
{$DEFINE STRIP} //we can define cube as either a triangle or triangle strip - no implications on performance
{$DEFINE GPUGRADIENTS} //Computing volume gradients on the GPU is much faster than using the CPU
{$DEFINE VIEW2D}
//{$DEFINE LINE3D}
{$DEFINE CUBE}
{$DEFINE MATCAP}
{$DEFINE CLRBAR}
{$DEFINE TIMER} //reports GPU gradient time to stdout (Unix only)
//{$DEFINE DEPTHPICKER_USEFRAMEBUFFER}
{$include ./Metal-Demos/common/glopts.inc}
uses
{$IFDEF MATCAP} GraphType, FPImage, IntfGraphics, LCLType,{$ENDIF}
{$IFDEF TIMER} DateUtils,{$ENDIF}
{$IFDEF CUBE} Forms, glcube, {$ENDIF}
{$IFDEF CLRBAR}glclrbar, {$ENDIF}
{$IFDEF COREGL} glcorearb, {$ELSE} {$IFNDEF Darwin}gl, {macos has macgl }{$ELSE}MacOSAll,{$ENDIF} glext, {$ENDIF}
{$IFDEF VIEW2D} colorEditor, slices2D, glfont, {$ENDIF}
retinahelper, niftis, SimdUtils, gl_core_utils, VectorMath, Classes, SysUtils, Graphics,
math, OpenGLContext, dialogs, nifti, drawvolume ;
const
kDefaultDistance = 2.25;
kMaxDistance = 40;
kGradientModeGPUFastest = 0; //no gradient smooth
kGradientModeGPUFast = 1; //low precision smooth
kGradientModeGPUSlow = 2; //high precision smooth
kGradientModeCPUSlowest = 3; //highest precision
kQualityMedium = 3;
kQualityBest = 5;
type
TGPUVolume = class
private
{$IFDEF VIEW2D}
{$IFDEF COREGL}
vao, vaoTex2D, vboTex2D, vaoLine2D, vboLine2D,
{$ELSE}
textureSzLoc : GLint;
dlTex2D, dlLine2D, dlBox3D, dlColorEditor,
{$ENDIF}
{$IFDEF LINE3D}
programLine3D,
{$ENDIF}
programLine2D, programTex2D: GLuint;
slices2D: TSlices2D;
{$IFDEF LINE3D}{$IFDEF COREGL}vaoLine3D,{$ENDIF} vboLine3D, vboLineIdx3D, mvpLine3DLoc, colorLine3DLoc, {$ENDIF}
uniform_drawTex, uniform_drawLUT, uniform_drawAlpha,
uniform_texVox, uniform_viewportSizeLine, uniform_viewportSizeTex, uniform_backAlpha,
uniform_tex, uniform_overlay, uniform_overlaysLoc: GLint;
colorEditor: TColorEditor;
isSmooth2D, colorEditorVisible: boolean;
txt: TGPUFont;
{$ENDIF}
{$IFDEF COREGL}vboBox3D: GLuint;{$ENDIF}
shaderPrefs: TShaderPrefs;
RayCastQuality1to5, maxDim,fAzimuth,fElevation, fPitch, overlayNum, overlayGradTexWidth: integer;
fDistance: single;
fLightPos: TVec4;
fClipPlane: TVec4;
{$IFDEF MTX}fModelMatrix: TMat4;{$ENDIF}
{$IFDEF CLRBAR}clrbar: TGPUClrbar;{$ENDIF}
glControl: TOpenGLControl;
prefLoc: array [1..kMaxUniform] of GLint;
overlayGradientVolLoc, overlayIntensityVolLoc,
rayDirLoc,intensityVolLoc, gradientVolLoc,mvpLoc,normLoc, lightPositionLoc,clipPlaneLoc, clipThickLoc,
backAlphaLoc, sliceSizeLoc, stepSizeLoc, overlaysLoc : GLint;
overlayGradientTexture3D, overlayIntensityTexture3D,
drawTexture1D, drawTexture3D,
gradientTexture3D, intensityTexture3D, programRaycast, programRaycastBetter: GLuint;
{$IFDEF DEPTHPICKER2}
mvp: TMat4;
viewportXYWH: TVec4;
{$ENDIF}
{$IFDEF MATCAP}
matcap2D1, matcap2D2: GLuint;
{$ENDIF}
{$IFDEF CUBE} gCube :TGPUCube; {$ENDIF}
{$IFDEF GPUGRADIENTS}programSobel, programBlur: GLuint;
procedure CreateGradientVolumeGPU(Xsz,Ysz,Zsz: integer; var inTex, grTex: GLuint);
procedure GenerateGradient(var inTex, grTex: GLuint);
{$ENDIF}
procedure LoadCube();
function LoadTexture(var vol: TNIfTI; deferGradients: boolean): boolean;
procedure CreateDrawColorTable;//1D texture for drawing
procedure CreateDrawTex(Dim: TVec3i; Vals: TUInt8s);
procedure UpdateDraw(Drawing: TDraw);
procedure CreateOverlayTextures(Dim: TVec3i; volRGBA: TRGBAs);
{$IFNDEF COREGL}procedure UpdateColorEditorDisplayList; {$ENDIF}
public
matcap1Loc, matcap2Loc: GLint;
gradientMode: integer;
ClipThick: single;
{$IFDEF VIEW2D}
SelectionRect: TVec4;
property ShowColorEditor: boolean read colorEditorVisible write colorEditorVisible;
property ShowSmooth2D: boolean read isSmooth2D write isSmooth2D;
property CE: TColorEditor read colorEditor;
property Slices: Tslices2D read slices2D;
function Slice2Dmm(var vol: TNIfTI; out vox: TVec3i): TVec3;
procedure SetSlice2DFrac(frac : TVec3);
function GetSlice2DFrac(mouseX, mouseY: integer; var oOrient: integer): TVec3;
function Unproject(mouseX, mouseY, depth: single): TVec3;
function GetSlice2DMaxXY(mouseX, mouseY: integer; var Lo: TPoint): TPoint;
procedure Paint2D(var vol: TNIfTI; Drawing: TDraw; DisplayOrient: integer);
{$IFDEF MOSAIC}
procedure PaintMosaicRender(var vol: TNIfTI; lRender: TMosaicRender);
procedure PaintMosaic2D(var vol: TNIfTI; Drawing: TDraw; MosaicString: string);
{$ENDIF} //MOSAIC
{$ENDIF}
{$IFDEF MATCAP}procedure SetMatCap(lFilename: string; isPrimary: boolean = true);{$ENDIF}
//procedure CreateOverlayTextures();
procedure UpdateOverlays(vols: TNIfTIs);
property Quality1to5: integer read RayCastQuality1to5 write RayCastQuality1to5;
property ShaderSliders: TShaderPrefs read shaderPrefs write shaderPrefs;
procedure SetShaderSlider(idx: integer; newVal: single);
property Azimuth: integer read fAzimuth write fAzimuth;
property Elevation: integer read fElevation write fElevation;
property Pitch: integer read fPitch write fPitch;
{$IFDEF MTX} property ModelMatrix3D: TMat4 read fModelMatrix write fModelMatrix; {$ENDIF}
property Distance: single read fDistance write fDistance;
property LightPosition: TVec4 read fLightPos write fLightPos;
property ClipPlane: TVec4 read fClipPlane write fClipPlane;
procedure Prepare(shaderName: string);
procedure SetGradientMode(newMode: integer);
constructor Create(fromView: TOpenGLControl);
function MakeCrosshair3D(var vol: TNIfTI): integer;
procedure PaintCrosshair3D(rgba: TVec4; nFaces: integer);
procedure PaintCore(var vol: TNIfTI; widthHeightLeft: TVec3i; clearScreen: boolean = true; isDepthShader: boolean = false);
procedure Paint(var vol: TNIfTI);
procedure PaintDepth(var vol: TNIfTI; isAxCorSagOrient4: boolean = false);
procedure SetShader(shaderName: string);
{$IFDEF CLRBAR}procedure SetColorBar(fromColorbar: TGPUClrbar);{$ENDIF}
procedure SetTextContrast(clearclr: TRGBA);
destructor Destroy; override;
end;
implementation
//uses mainunit;
{$IFNDEF COREGL}{$IFDEF LINUX}
{$DEFINE MESA_HACKS}
{$ENDIF}{$ENDIF}
{$IFDEF LINE3D}{$IFDEF COREGL}
//var gLines3D: array of TVec3;
{$ENDIF}{$ENDIF}
procedure printf (lS: AnsiString);
begin
{$IFNDEF WINDOWS} writeln(lS); {$ENDIF}
end;
{$IFDEF DEPTHPICKER2}
function gluUnProject(winXYZ: TVec3; mvp: TMat4; viewportXYWH: TVec4): TVec3;
//viewport[0]=x, viewport[1]=y, viewport[2]=width, viewport[3]=height
//return coordinates in object space
(*vec4 v = vec4(2.0*(gl_FragCoord.x-view.x)/view.z-1.0,
2.0*(gl_FragCoord.y-view.y)/view.w-1.0,
2.0*texture2DRect(DepthTex,gl_FragCoord.xy).z-1.0,
1.0 );
v = gl_ModelViewProjectionMatrixInverse * v;
v /= v.w;*)
//https://community.khronos.org/t/converting-gl-fragcoord-to-model-space/57397
//https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluUnProject.xml
//http://nehe.gamedev.net/article/using_gluunproject/16013/
var
v: TVec4;
mvpInv: TMat4;
begin
v := vec4(2.0*(winXYZ.x-viewportXYWH.x)/viewportXYWH.z-1.0,
2.0*(winXYZ.y-viewportXYWH.y)/viewportXYWH.w-1.0,
2.0*winXYZ.z-1.0,
1.0);
mvpInv := mvp.inverse;
v := mvpInv * v;
v := v / v.w;
result := vec3(v.x, v.y, v.z);
end;
function TGPUVolume.Unproject(mouseX, mouseY, depth: single): TVec3;
var
winXYZ: TVec3;
begin
winXYZ := vec3(mouseX, mouseY, depth);
result := gluUnProject(winXYZ, mvp, viewportXYWH);
//printf(format('windowXYZ %g %g %g', [winXYZ.x, winXYZ.y, winXYZ.z]));
//printf(format('objXYZ %g %g %g', [result.x, result.y, result.z]));
end;
{$ELSE}
function TGPUVolume.Unproject(mouseX, mouseY, depth: single): TVec3;
begin
result := vec3(2.0, 2.0, 2.0);
end;
{$ENDIF}
destructor TGPUVolume.Destroy;
begin
{$IFDEF VIEW2D}
slices2D.free;
colorEditor.free;
txt.free;
{$ENDIF}
{$IFDEF LINE3D}{$IFDEF COREGL}
//gLines3D := nil;
{$ENDIF}{$ENDIF}
{$IFDEF CUBE} gCube.free; {$ENDIF}
{$IFDEF CLRBAR} clrbar.free; {$ENDIF}
inherited;
end;
procedure GetErrorAll(p: integer; str: string = ''); //report OpenGL Error
{$IFDEF UNIX}
var
Error: GLenum;
s: string;
begin
Error := glGetError();
if Error = GL_NO_ERROR then exit;
s := inttostr(p)+'->';
if Error = GL_INVALID_ENUM then
s := s+'GL_INVALID_ENUM'
else if Error = GL_INVALID_VALUE then
s := s+'GL_INVALID_VALUE' //out of range https://www.khronos.org/registry/OpenGL-Refpages/es2.0/xhtml/glGetError.xml
else
s := s + inttostr(Error);
printf('OpenGL Error (reduce MaxVox) '+str+s);
end;
{$ELSE}
begin
//
end;
{$ENDIF}
procedure TGPUVolume.SetTextContrast(clearclr: TRGBA);
begin
{$IFDEF VIEW2D}
if (clearclr.R + clearclr.G + clearclr.B) > 300 then
txt.FontColor := Vec4(0,0,0,1)
else
txt.FontColor := Vec4(1,1,1,1);
slices2D.SetFontColor(txt.FontColor);
{$ENDIF}
end;
{$IFDEF VIEW2D}
const
{$IFDEF COREGL}
kVertTex2D = '#version 330'
+#10'layout(location = 0) in vec2 point;'
+#10'layout(location = 4) in vec4 texCoordIn;'
+#10'uniform vec2 ViewportSize;'
+#10'out vec4 texCoord;'
+#10'void main() {'
+#10' texCoord = texCoordIn;'
+#10' vec2 pixelPosition = point;'
+#10' pixelPosition -= (ViewportSize/2.0);'
+#10' gl_Position = vec4((pixelPosition / (ViewportSize/2.0)), 0.0, 1.0);'
+#10'}';
kFragTex2D = '#version 330'
+#10'in vec4 texCoord;'
+#10'out vec4 color;'
+#10'uniform float backAlpha = 1.0;'
+#10'uniform sampler3D tex, drawTex, overlay;'
+#10'uniform sampler1D drawLUT;'
+#10'uniform float drawAlpha = 0.0;'
+#10'uniform int overlays = 0;'
+#10'void main() {'
+#10' color = texture(tex,texCoord.xyz);'
+#10' color.a = smoothstep(0.0, 0.01, color.a);'
+#10' //color.a = smoothstep(0.0, 0.1, color.a);'
+#10' //color.a = smoothstep(0.0, 0.00001, color.a);'
+#10' color.a *= backAlpha;'
+#10' //if ((overlays == 0) && (drawAlpha == 0.0)) color.r = 1.0; //test case where neither overlay or draw is visible'
+#10' if ((overlays == 0) && (drawAlpha == 0.0)) return;'
+#10' //if (color.a > 0.0) color.a = backAlpha;'
+#10' vec4 ocolor = texture(overlay, texCoord.xyz);'
+#10' //vec4 ocolor = texture(drawLUT, texture(overlay, texCoord.xyz).r).rgba;'
+#10' //vec4 ocolor = texture(drawLUT, texture(drawTex, texCoord.xyz).r).rgba;'
+#10' color.rgb = mix(color.rgb, ocolor.rgb, ocolor.a);'
+#10' color.a = max(color.a, ocolor.a);'
+#10' if (drawAlpha == 0.0) return;'
+#10' ocolor = texture(drawLUT, texture(drawTex, texCoord.xyz).r).rgba;'
+#10' ocolor.a *= drawAlpha;'
+#10' color.rgb = mix(color.rgb, ocolor.rgb, ocolor.a);'
+#10' color.a = max(color.a, ocolor.a);'
+#10'}';
//TODO: pixelPosition -= 0.5;??? offset: line at row 1 with width 1 should span 0..1 not 0.5..1.5
kVertLine2D = '#version 330'
+#10'layout(location = 0) in vec2 point;'
+#10'layout(location = 4) in vec4 texCoordIn;'
+#10'uniform vec2 ViewportSize;'
+#10'out vec4 texCoord;'
+#10'void main() {'
+#10' texCoord = texCoordIn;'
+#10' vec2 pixelPosition = point;'
+#10' pixelPosition -= (ViewportSize/2.0);'
+#10' gl_Position = vec4((pixelPosition / (ViewportSize/2.0)), 0.0, 1.0);'
+#10'}';
kFragLine2D = '#version 330'
+#10'in vec4 texCoord;'
+#10'out vec4 color;'
+#10'void main() {'
+#10' color = texCoord;'
+#10'}';
{$ELSE}
kVertTex2D = '#version 120'
+#10'uniform vec2 ViewportSize;'
+#10'varying vec4 texCoord;'
+#10'void main() {'
+#10' vec2 pixelPosition = gl_Vertex.xy;'
+#10' pixelPosition -= (ViewportSize/2.0);'
+#10' gl_Position = vec4((pixelPosition / (ViewportSize/2.0)), 0.0, 1.0);'
+#10' texCoord = gl_Color;'
+#10'}';
{$IFDEF MESA_HACKS} //required for Mesa running VirtualBox with "Enable 3D Acceleration"
kMesaKludge = #10' if (texCoord.x > 2.0) {'
+#10' gl_FragColor = vec4(1.0, 0.0, 0.0, 0.5);'
+#10' return;'
+#10' }';
kMesaKuldge2 = #10' ocolor = texture1D(drawLUT, texture3D(drawTex, texCoord.xyz).a).rgba;';
{$ELSE}
kMesaKludge = '';
kMesaKuldge2 = #10' ocolor = texture1D(drawLUT, texture3D(drawTex, texCoord.xyz).r).rgba;';
{$ENDIF}
kFragTex2D = '#version 120'
+#10'varying vec4 texCoord;'
+#10'uniform float backAlpha = 1.0;'
+#10'uniform sampler3D tex, drawTex, overlay;'
+#10'uniform sampler1D drawLUT;'
+#10'uniform float drawAlpha = 0.5;'
+#10'uniform int overlays = 0;'
+#10'void main() {'
+kMesaKludge
+#10' vec4 color = texture3D(tex,texCoord.xyz);'
+#10' color.a = smoothstep(0.0, 0.01, color.a);'
+#10' //color.a = smoothstep(0.0, 0.1, color.a);'
+#10' color.a *= backAlpha;'
+#10' //if ((overlays == 0) && (drawAlpha == 0.0)) color.r = 1.0; //test case where neither overlay or draw is visible'
+#10' if ((overlays == 0) && (drawAlpha == 0.0)) {'
+#10' gl_FragColor = color;'
+#10' return;'
+#10' }'
+#10' //if (color.a > 0.0) color.a = backAlpha;'
+#10' vec4 ocolor = texture3D(overlay, texCoord.xyz);'
+#10' color.rgb = mix(color.rgb, ocolor.rgb, ocolor.a);'
+#10' color.a = max(color.a, ocolor.a);'
+#10' if (drawAlpha <= 0.0) {'
+#10' gl_FragColor = color;'
+#10' return;'
+#10' }'
+kMesaKuldge2
+#10' ocolor.a *= drawAlpha;'
+#10' color.rgb = mix(color.rgb, ocolor.rgb, ocolor.a);'
+#10' color.a = max(color.a, ocolor.a);'
+#10' gl_FragColor = color;'
+#10'}';
kVertLine2D = '#version 120'
+#10'//akVertLine2D'
+#10'uniform vec2 ViewportSize;'
+#10'varying vec4 texCoord;'
+#10'void main() {'
+#10' texCoord = gl_Color;'
+#10' vec2 pixelPosition = gl_Vertex.xy;'
+#10' pixelPosition -= (ViewportSize/2.0);'
+#10' gl_Position = vec4((pixelPosition / (ViewportSize/2.0)), 0.0, 1.0);'
+#10'}';
kFragLine2D = '#version 120'
+#10'//akFragLine2D'
+#10'varying vec4 texCoord;'
+#10'void main() {'
+#10' gl_FragColor = texCoord;'
+#10'}';
{$ENDIF} //IF COREGL else legacy
{$ENDIF} //If View2D
//uses vrForm;
procedure TGPUVolume.SetShaderSlider(idx: integer; newVal: single);
begin
if (idx < 1) or (idx > kMaxUniform) then exit;
shaderPrefs.Uniform[idx].DefaultV:=newVal;
end;
{$IFDEF GPUGRADIENTS}
function bindBlankGL(Xsz,Ysz,Zsz, gradientMode: integer): GLuint;
const
k2Gb = 2147483648;
var
width, height, depth: GLint;
isRGBA8: boolean;
rgbaBytes: int64;
begin //creates an empty texture in VRAM without requiring memory copy from RAM
//later run glDeleteTextures(1,&oldHandle);
GetErrorAll(101,'PreBindBlank');
isRGBA8 := (gradientMode = kGradientModeGPUFast) or (gradientMode = kGradientModeGPUFastest);
rgbaBytes := XSz * YSz * ZSz * 8; //GL_RGBA16 = is 8 bytes per voxel
if (rgbaBytes >= k2Gb) then
isRGBA8 := true;
if isRGBA8 then
glTexImage3D(GL_PROXY_TEXTURE_3D, 0, GL_RGBA8, XSz, YSz, ZSz, 0, GL_RGBA, GL_UNSIGNED_BYTE, NIL)
else
glTexImage3D(GL_PROXY_TEXTURE_3D, 0, GL_RGBA16, XSz, YSz, ZSz, 0, GL_RGBA, GL_UNSIGNED_BYTE, NIL);
glGetTexLevelParameteriv(GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_WIDTH, @width);
glGetTexLevelParameteriv(GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_HEIGHT, @height);
glGetTexLevelParameteriv(GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_DEPTH, @depth);
//https://www.opengl.org/archives/resources/faq/technical/texture.htm
printf(format('blur proxy test %dx%dx%d',[width, height, depth]));
if (width < Xsz) then begin
printf('Unable to generate gradient texture. Solution: adjust "MaxVox" or press "Reset" button in preferences.');
exit(0);
end;
glGenTextures(1, @result);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_3D, result);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); //, GL_CLAMP_TO_BORDER) will wrap
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
if isRGBA8 then
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, XSz, YSz, ZSz, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil)
else
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA16, XSz, YSz, ZSz, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
GetErrorAll(101,'BindBlank');
end;
procedure glUniform1ix(prog: GLuint; name: AnsiString; value: integer);
begin
glUniform1i(glGetUniformLocation(prog, pAnsiChar(Name)), value) ;
end;
procedure glUniform1fx(prog: GLuint; name: AnsiString; value: single );
begin
glUniform1f(glGetUniformLocation(prog, pAnsiChar(Name)), value) ;
end;
{$IFDEF COREGL}
const kBlurSobelVert = '#version 330 core'
+#10'layout(location = 0) in vec3 vPos;'
+#10'out vec2 TexCoord;'
+#10'void main() {'
+#10' TexCoord = vPos.xy;'
+#10' gl_Position = vec4( (vPos.xy-vec2(0.5,0.5))* 2.0, 0.0, 1.0);'
+#10'// gl_Position = vec4( (vPos-vec3(0.5,0.5,0.5))* 2.0, 1.0);'
+#10'}';
const kBlurFrag = '#version 330 core'
+#10'in vec2 TexCoord;'
+#10'out vec4 FragColor;'
+#10'uniform float coordZ, dX, dY, dZ;'
+#10'uniform sampler3D intensityVol;'
+#10'void main(void) {'
+#10' vec3 vx = vec3(TexCoord.xy, coordZ);'
+#10' vec4 samp = texture(intensityVol,vx+vec3(+dX,+dY,+dZ));'
+#10' samp += texture(intensityVol,vx+vec3(+dX,+dY,-dZ));'
+#10' samp += texture(intensityVol,vx+vec3(+dX,-dY,+dZ));'
+#10' samp += texture(intensityVol,vx+vec3(+dX,-dY,-dZ));'
+#10' samp += texture(intensityVol,vx+vec3(-dX,+dY,+dZ));'
+#10' samp += texture(intensityVol,vx+vec3(-dX,+dY,-dZ));'
+#10' samp += texture(intensityVol,vx+vec3(-dX,-dY,+dZ));'
+#10' samp += texture(intensityVol,vx+vec3(-dX,-dY,-dZ));'
+#10' FragColor = samp*0.125;'
+#10'}';
const kSobelFrag = '#version 330 core'
+#10'in vec2 TexCoord;'
+#10'out vec4 FragColor;'
+#10'uniform float coordZ, dX, dY, dZ;'
+#10'uniform sampler3D intensityVol;'
+#10'void main(void) {'
+#10' vec3 vx = vec3(TexCoord.xy, coordZ);'
+#10' float TAR = texture(intensityVol,vx+vec3(+dX,+dY,+dZ)).a;'
+#10' float TAL = texture(intensityVol,vx+vec3(+dX,+dY,-dZ)).a;'
+#10' float TPR = texture(intensityVol,vx+vec3(+dX,-dY,+dZ)).a;'
+#10' float TPL = texture(intensityVol,vx+vec3(+dX,-dY,-dZ)).a;'
+#10' float BAR = texture(intensityVol,vx+vec3(-dX,+dY,+dZ)).a;'
+#10' float BAL = texture(intensityVol,vx+vec3(-dX,+dY,-dZ)).a;'
+#10' float BPR = texture(intensityVol,vx+vec3(-dX,-dY,+dZ)).a;'
+#10' float BPL = texture(intensityVol,vx+vec3(-dX,-dY,-dZ)).a;'
+#10' vec4 gradientSample = vec4 (0.0, 0.0, 0.0, 0.0);'
+#10' gradientSample.r = BAR+BAL+BPR+BPL -TAR-TAL-TPR-TPL;'
+#10' gradientSample.g = TPR+TPL+BPR+BPL -TAR-TAL-BAR-BAL;'
+#10' gradientSample.b = TAL+TPL+BAL+BPL -TAR-TPR-BAR-BPR;'
+#10' gradientSample.a = (abs(gradientSample.r)+abs(gradientSample.g)+abs(gradientSample.b))*0.29;'
+#10' gradientSample.rgb = normalize(gradientSample.rgb);'
+#10' gradientSample.rgb = (gradientSample.rgb * 0.5)+0.5;'
+#10' FragColor = gradientSample;'
+#10'}';
{$ELSE}
const kBlurSobelVert = '';
kBlurFrag = '#version 120'
+#10'uniform float coordZ, dX, dY, dZ;'
+#10'uniform sampler3D intensityVol;'
+#10'void main(void) {'
+#10' vec3 vx = vec3(gl_TexCoord[0].xy, coordZ);'
+#10' vec4 samp = texture3D(intensityVol,vx+vec3(+dX,+dY,+dZ));'
+#10' samp += texture3D(intensityVol,vx+vec3(+dX,+dY,-dZ));'
+#10' samp += texture3D(intensityVol,vx+vec3(+dX,-dY,+dZ));'
+#10' samp += texture3D(intensityVol,vx+vec3(+dX,-dY,-dZ));'
+#10' samp += texture3D(intensityVol,vx+vec3(-dX,+dY,+dZ));'
+#10' samp += texture3D(intensityVol,vx+vec3(-dX,+dY,-dZ));'
+#10' samp += texture3D(intensityVol,vx+vec3(-dX,-dY,+dZ));'
+#10' samp += texture3D(intensityVol,vx+vec3(-dX,-dY,-dZ));'
+#10' gl_FragColor = samp*0.125;'
+#10'}';
//this will estimate a Sobel smooth
const kSobelFrag = '#version 120'
+#10'uniform float coordZ, dX, dY, dZ;'
+#10'uniform sampler3D intensityVol;'
+#10'void main(void) {'
+#10' vec3 vx = vec3(gl_TexCoord[0].xy, coordZ);'
+#10' float TAR = texture3D(intensityVol,vx+vec3(+dX,+dY,+dZ)).a;'
+#10' float TAL = texture3D(intensityVol,vx+vec3(+dX,+dY,-dZ)).a;'
+#10' float TPR = texture3D(intensityVol,vx+vec3(+dX,-dY,+dZ)).a;'
+#10' float TPL = texture3D(intensityVol,vx+vec3(+dX,-dY,-dZ)).a;'
+#10' float BAR = texture3D(intensityVol,vx+vec3(-dX,+dY,+dZ)).a;'
+#10' float BAL = texture3D(intensityVol,vx+vec3(-dX,+dY,-dZ)).a;'
+#10' float BPR = texture3D(intensityVol,vx+vec3(-dX,-dY,+dZ)).a;'
+#10' float BPL = texture3D(intensityVol,vx+vec3(-dX,-dY,-dZ)).a;'
+#10' vec4 gradientSample = vec4 (0.0, 0.0, 0.0, 0.0);'
+#10' gradientSample.r = BAR+BAL+BPR+BPL -TAR-TAL-TPR-TPL;'
+#10' gradientSample.g = TPR+TPL+BPR+BPL -TAR-TAL-BAR-BAL;'
+#10' gradientSample.b = TAL+TPL+BAL+BPL -TAR-TPR-BAR-BPR;'
+#10' gradientSample.a = (abs(gradientSample.r)+abs(gradientSample.g)+abs(gradientSample.b))*0.29;'
+#10' gradientSample.rgb = normalize(gradientSample.rgb);'
+#10' gradientSample.rgb = (gradientSample.rgb * 0.5)+0.5;'
+#10' gl_FragColor = gradientSample;'
+#10'}';
{$ENDIF}
procedure TGPUVolume.CreateGradientVolumeGPU(Xsz,Ysz,Zsz: integer; var inTex, grTex: GLuint);
//given 3D input texture inTex (with dimensions Xsz, Ysz, Zsz) generate 3D gradient texture gradTex
//http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-14-render-to-texture/
//http://www.opengl.org/wiki/Framebuffer_Object_Examples
//{$DEFINE SMOOTHGRAD}
var
i: integer;
coordZ: single;
fb , tempTex3D: GLuint;
isSmoothGrad: boolean;
{$IFDEF TIMER}StartTime: TDateTime;{$ENDIF}
begin
glFinish();//force update
{$IFDEF TIMER}startTime := now;{$ENDIF}
{$IFDEF COREGL}
glBindVertexArray(vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboBox3D);
glGenFramebuffers(1, @fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
{$ELSE}
glGenFramebuffersEXT(1, @fb);
glBindFramebufferEXT(GL_FRAMEBUFFER, fb);
{$ENDIF}
glDisable(GL_CULL_FACE);
GetErrorAll(96,'CreateGradient');
//{$IFNDEF COREGL}glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);{$ENDIF}// <- REQUIRED
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
GetErrorAll(97,'CreateGradient');
glViewport(0, 0, XSz, YSz);
{$IFNDEF COREGL}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho (0, 1,0, 1, -1, 1); //gluOrtho2D(0, 1, 0, 1); https://www.opengl.org/sdk/docs/man2/xhtml/gluOrtho2D.xml
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
{$ENDIF}
//glDisable(GL_TEXTURE_2D); //<- generates error!
glDisable(GL_BLEND);
tempTex3D := 0;
isSmoothGrad := gradientMode > kGradientModeGPUFastest;
if (isSmoothGrad) then begin
//STEP 1: run smooth program gradientTexture -> tempTex3D
tempTex3D := bindBlankGL(Xsz,Ysz,Zsz, gradientMode);
glUseProgram(programBlur);
glActiveTexture( GL_TEXTURE1);
//glBindTexture(GL_TEXTURE_3D, gRayCast.gradientTexture3D);//input texture
glBindTexture(GL_TEXTURE_3D, inTex);//input texture is overlay
glUniform1ix(programBlur, 'intensityVol', 1);
glUniform1fx(programBlur, 'dX', 0.7/XSz); //0.5 for smooth - center contributes
glUniform1fx(programBlur, 'dY', 0.7/YSz);
glUniform1fx(programBlur, 'dZ', 0.7/ZSz);
{$IFDEF COREGL}
glBindVertexArray(vao);
{$ENDIF}
for i := 0 to (ZSz-1) do begin
coordZ := 1/ZSz * (i + 0.5);
glUniform1fx(programBlur, 'coordZ', coordZ);
//glFramebufferTexture3D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, tempTex3D, 0, i);//output texture
//Ext required: Delphi compile on Winodws 32-bit XP with NVidia 8400M
{$IFDEF COREGL}
glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, tempTex3D, 0, i);//output texture
{$ELSE}
glFramebufferTexture3DExt(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, tempTex3D, 0, i);//output texture
{$ENDIF}
glClear(GL_DEPTH_BUFFER_BIT); // clear depth bit (before render every layer)
{$IFDEF COREGL}
{$IFDEF STRIP}
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, nil);
{$ELSE}
glDrawElements(GL_TRIANGLES, 2*3, GL_UNSIGNED_INT, nil);
{$ENDIF}
{$ELSE}
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(1.0, 0);
glVertex2f(1.0, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex2f(1.0, 1.0);
glTexCoord2f(0, 1.0);
glVertex2f(0.0, 1.0);
glEnd();
{$ENDIF}
end;
GetErrorAll(101,'CreateGradient');
glUseProgram(0);
end; //isSmoothGrad
//STEP 2: run sobel program gradientTexture -> tempTex3D
//glUseProgramObjectARB(gRayCast.glslprogramSobel);
glUseProgram(programSobel);
glActiveTexture(GL_TEXTURE1);
//x glBindTexture(GL_TEXTURE_3D, gRayCast.intensityTexture3D);//input texture
if isSmoothGrad then
glBindTexture(GL_TEXTURE_3D, tempTex3D)//input texture
else
glBindTexture(GL_TEXTURE_3D, inTex);//input texture is overlay
glUniform1ix(programSobel, 'intensityVol', 1);
glUniform1fx(programSobel, 'dX', 1.2/XSz ); //1.0 for SOBEL - center excluded
glUniform1fx(programSobel, 'dY', 1.2/YSz);
glUniform1fx(programSobel, 'dZ', 1.2/ZSz);
{$IFDEF COREGL}
glBindVertexArray(vao);
{$ENDIF}
for i := 0 to (ZSz-1) do begin
coordZ := 1/ZSz * (i + 0.5);
glUniform1fx(programSobel, 'coordZ', coordZ);
{$IFDEF COREGL}
glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, grTex, 0, i);//output is background
{$ELSE}
glFramebufferTexture3DExt(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, grTex, 0, i);//output is background
{$ENDIF}
glClear(GL_DEPTH_BUFFER_BIT);
{$IFDEF COREGL}
{$IFDEF STRIP}
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, nil);
{$ELSE}
glDrawElements(GL_TRIANGLES, 2*3, GL_UNSIGNED_INT, nil);
{$ENDIF}
{$ELSE}
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(1.0, 0);
glVertex2f(1.0, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex2f(1.0, 1.0);
glTexCoord2f(0, 1.0);
glVertex2f(0.0, 1.0);
glEnd();
{$ENDIF}
end;
glUseProgram(0);
glFinish();//force update
GetErrorAll(102,'CreateGradient');
//clean up:
if isSmoothGrad then
glDeleteTextures(1,@tempTex3D);
{$IFDEF COREGL}
{$IFDEF LCLgtk3}
glBindFramebuffer(GL_FRAMEBUFFER, 1);
{$ELSE}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
{$ENDIF}
glDeleteFramebuffers(1, @fb);
{$ELSE}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glDeleteFramebuffersEXT(1, @fb);
{$ENDIF}
GetErrorAll(103,'CreateGradient');
glActiveTexture( GL_TEXTURE0 ); //required if we will draw 2d slices next
{$IFDEF TIMER}printf(format('GPU Gradient time %d',[MilliSecondsBetween(Now,startTime)]));{$ENDIF}
end;
{$ENDIF}
procedure TGPUVolume.UpdateDraw(Drawing: TDraw);
begin
if not Drawing.NeedsUpdate then exit;
if Drawing.IsOpen then
CreateDrawTex(Drawing.Dim, Drawing.VolRawBytes)
else
CreateDrawTex(pti(4,4,4), nil);
Drawing.NeedsUpdate := false;
end;
procedure TGPUVolume.CreateDrawTex(Dim: TVec3i; Vals: TUInt8s);
//portion of voiCreate that requires OpenGL context
var
vx,i: int64;
v: TUInt8s;
begin
GetErrorAll(96,'CreateDrawTex');
//printf(format('Creating draw texture %dx%dx%d %d', [Dim.X, Dim.Y, Dim.Z]));
if (drawTexture3D <> 0) then glDeleteTextures(1,@drawTexture3D);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glGenTextures(1, @drawTexture3D);
glBindTexture(GL_TEXTURE_3D, drawTexture3D);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
if (Vals = nil) then begin
vx := prod(Dim);//Xsz * Ysz * Zsz;
setlength(v,vx);
for i := 0 to (vx -1) do
v[i] := random(3);
{$IFDEF MESA_HACKS}
glTexImage3D(GL_TEXTURE_3D, 0, GL_INTENSITY, Dim.X, Dim.Y, Dim.Z, 0, GL_RED, GL_UNSIGNED_BYTE,@v[0]);
{$ELSE}
glTexImage3D(GL_TEXTURE_3D, 0, GL_RED, Dim.X, Dim.Y, Dim.Z, 0, GL_RED, GL_UNSIGNED_BYTE,@v[0]);
{$ENDIF}
v := nil;
end else begin
{$IFDEF MESA_HACKS}
glTexImage3D(GL_TEXTURE_3D, 0, GL_INTENSITY, Dim.X, Dim.Y, Dim.Z, 0, GL_RED, GL_UNSIGNED_BYTE,@Vals[0]);
{$ELSE} //Mesa does not support GL_RED!
glTexImage3D(GL_TEXTURE_3D, 0, GL_RED, Dim.X, Dim.Y, Dim.Z, 0, GL_RED, GL_UNSIGNED_BYTE,@Vals[0]);
{$ENDIF}
end;
GetErrorAll(102,'CreateDrawTex');
end;
const
//A common vertex shader for all volume rendering
{$IFDEF COREGL}
kVert = '#version 330 core'
+#10'layout(location = 0) in vec3 vPos;'
+#10'out vec3 TexCoord1;'
+#10'out vec4 vPosition;'
+#10'uniform mat4 ModelViewProjectionMatrix;'
+#10'void main() {'
+#10' TexCoord1 = vPos;'
+#10' gl_Position = ModelViewProjectionMatrix * vec4(vPos, 1.0);'
+#10' vPosition = gl_Position;'
+#10'}';
{$IFDEF LINE3D}
kVertLine3D = '#version 330 core'
+#10'layout(location = 0) in vec3 vPos;'
+#10'uniform mat4 ModelViewProjectionMatrix;'
+#10'void main() {'
+#10' gl_Position = ModelViewProjectionMatrix * vec4(vPos, 1.0);'
+#10'}';
kFragLine3D = '#version 330 core'
+#10'uniform vec4 Color;'
+#10'out vec4 FragColor;'
+#10'void main() {'
+#10' FragColor = Color;'
+#10'}';
{$ENDIF}//line3D
{$ELSE}
kVert = '#version 120'
+#10'varying vec3 TexCoord1;'
+#10'varying vec4 vPosition;'
+#10'uniform mat4 ModelViewProjectionMatrix;'
+#10'void main() {'
+#10' gl_Position = ModelViewProjectionMatrix * vec4(gl_Vertex.xyz, 1.0);'
+#10' TexCoord1 = gl_Vertex.rgb;'
+#10' vPosition = gl_Position;'
+#10'}';
{$IFDEF LINE3D}
kVertLine3D = '#version 120'
+#10'attribute vec3 Vert;'
+#10'uniform mat4 ModelViewProjectionMatrix;'
+#10'void main() {'
+#10' gl_Position = ModelViewProjectionMatrix * vec4(Vert.xyz, 1.0);'
+#10'}';
kFragLine3D = '#version 120'
+#10'uniform vec4 Color;'
+#10'void main() {'
+#10' gl_FragColor = Color;'
+#10'}';
{$ENDIF}//line3D
{$ENDIF}
{$IFDEF MATCAP}
{$IFDEF WINDOWS}
procedure FlipVertical (var px: TPicture);
var
p: array of byte;
i, half, b: integer;
LoPtr, HiPtr: PInteger;
begin
if px.Height < 3 then exit;
half := (px.Height div 2);
b := px.Bitmap.RawImage.Description.BytesPerLine;
LoPtr := PInteger(px.Bitmap.RawImage.Data);
HiPtr := PInteger(px.Bitmap.RawImage.Data+ ((px.Height -1) * b));
setlength(p, b);
for i := 1 to half do begin
System.Move(LoPtr^,p[0],b); //(src, dst,sz)
System.Move(HiPtr^,LoPtr^,b); //(src, dst,sz)
System.Move(p[0],HiPtr^,b); //(src, dst,sz)
Inc(PByte(LoPtr), b );
Dec(PByte(HiPtr), b);
end;
end; //FlipVertical()
{$ENDIF}
function LoadMatCap(fnm: string; var texID: GLuint): boolean;
var
px: TPicture;
ifnm, MatCapDir: string;
{$IFNDEF WINDOWS}
AImage: TLazIntfImage;
lRawImage: TRawImage;
{$ENDIF}
begin
result := false;
if not fileexists(fnm) then begin
ifnm := fnm;
MatCapDir := ExtractFilePath(ShaderDir)+ 'matcap';
fnm := MatCapDir+pathdelim+fnm+'.jpg';
if not fileexists(fnm) then begin
printf(format('LoadTex: unable to find "%s" or "%s"',[ifnm, fnm]));
exit;
end;
end;
px := TPicture.Create;
try
{$IFDEF Windows}
px.LoadFromFile(fnm);
FlipVertical(px);
{$ELSE}
//ensure order is GL_RGBA8 - it is with many PNG files, but not JPEG
lRawImage.Init;
lRawImage.Description.Init_BPP32_R8G8B8A8_BIO_TTB(0,0);
lRawImage.Description.LineOrder := riloBottomToTop; // openGL uses cartesian coordinates
lRawImage.CreateData(false);
AImage := TLazIntfImage.Create(0,0);
try
AImage.SetRawImage(lRawImage);
AImage.LoadFromFile(fnm);
px.Bitmap.LoadFromIntfImage(AImage);
finally
AImage.Free;
end;
{$ENDIF}
except
px.Bitmap.Width:=-1;
end;
if ((px.Bitmap.PixelFormat <> pf24bit ) and (px.Bitmap.PixelFormat <> pf32bit )) or (px.Bitmap.Width < 1) or (px.Bitmap.Height < 1) then begin
printf(format('LoadTex: unsupported pixel format bpp (%d) or size (%dx%d)',[PIXELFORMAT_BPP[px.Bitmap.PixelFormat], px.Bitmap.Width, px.Bitmap.Height]));
exit;
end;
px.Bitmap.Height;
px.Bitmap.Width;
if texID <> 0 then
glDeleteTextures(1,@texID);
glGenTextures(1, @texID);
glBindTexture(GL_TEXTURE_2D, texID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
//glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA8, px.Width, px.Height, 0, GL_BGRA, GL_UNSIGNED_BYTE, PInteger(px.Bitmap.RawImage.Data));
{$IFDEF WINDOWS}
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA8, px.Width, px.Height, 0, GL_BGRA, GL_UNSIGNED_BYTE, PInteger(px.Bitmap.RawImage.Data));
{$ELSE}
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA8, px.Width, px.Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, PInteger(px.Bitmap.RawImage.Data));
{$ENDIF}
px.Free;
result := true;
end;
procedure TGPUVolume.SetMatCap(lFilename: string; isPrimary: boolean = true);
begin
glControl.MakeCurrent();
if (isPrimary) then
LoadMatCap(lFilename, matcap2D1)
else
LoadMatCap(lFilename, matcap2D2);
glControl.ReleaseContext;
end;
{$ENDIF}
const
{$IFDEF COREGL}
kFragBase = '#version 330'
+#10'in vec3 TexCoord1;'
+#10'out vec4 FragColor;'
+#10'in vec4 vPosition;'
+#10'uniform float stepSize, sliceSize;'
+#10'uniform sampler3D intensityVol, gradientVol;'
+#10'uniform sampler3D intensityOverlay, gradientOverlay;'
+#10'uniform vec3 lightPosition, rayDir;'
+#10'uniform vec4 clipPlane;'
+#10'uniform float clipThick = 1.0;'
+#10'uniform int overlays = 0;'
+#10'uniform float backAlpha = 0.5;'
+#10'uniform mat4 ModelViewProjectionMatrix;'
+#10'void setDepthBuffer(vec3 pos) {'
+#10' gl_FragDepth = ((ModelViewProjectionMatrix * vec4(pos, 1.0)).z + 1.0) * 0.5;'
+#10'}'
+#10'vec3 GetBackPosition (vec3 startPosition) {'
+#10' vec3 invR = 1.0 / rayDir;'
+#10' vec3 tbot = invR * (vec3(0.0)-startPosition);'
+#10' vec3 ttop = invR * (vec3(1.0)-startPosition);'
+#10' vec3 tmax = max(ttop, tbot);'
+#10' vec2 t = min(tmax.xx, tmax.yz);'
+#10' return startPosition + (rayDir * min(t.x, t.y));'
+#10'}'
+#10'void fastPass (float len, vec3 dir, sampler3D vol, inout vec4 samplePos){'
+#10' vec4 deltaDir = vec4(dir.xyz * max(stepSize, sliceSize * 1.95), max(stepSize, sliceSize * 1.95));'
+#10' while (texture(vol,samplePos.xyz).a == 0.0) {'
+#10' samplePos += deltaDir;'
+#10' if (samplePos.a > len) return; //no hit'
+#10' }'
+#10' samplePos -= deltaDir;'
+#10'}'
+#10'vec4 applyClip(vec3 dir, inout vec4 samplePos, inout float len) {'
+#10' float cdot = dot(dir,clipPlane.xyz);'
+#10' if ((clipPlane.a > 1.0) || (cdot == 0.0)) return samplePos;'
+#10' bool frontface = (cdot > 0.0);'
+#10' float dis = (-clipPlane.a - dot(clipPlane.xyz, samplePos.xyz-0.5)) / cdot;'
+#10' float disBackFace = (-(clipPlane.a-clipThick) - dot(clipPlane.xyz, samplePos.xyz-0.5)) / cdot;'
+#10' if (((frontface) && (dis >= len)) || ((!frontface) && (dis <= 0.0))) {'
+#10' samplePos.a = len + 1.0;'
+#10' return samplePos;'
+#10' }'
+#10' if (frontface) {'
+#10' dis = max(0.0, dis);'
+#10' samplePos = vec4(samplePos.xyz+dir * dis, dis);'
+#10' len = min(disBackFace, len);'
+#10' }'
+#10' if (!frontface) {'
+#10' len = min(dis, len);'
+#10' disBackFace = max(0.0, disBackFace);'
+#10' samplePos = vec4(samplePos.xyz+dir * disBackFace, disBackFace);'
+#10' }'
+#10' return samplePos;'
+#10'}'
+#10'vec4 texture2D(sampler2D vol, vec2 coord) {'
+#10' return texture(vol, coord); //trilinear interpolation'
+#10'}';
// GetBackPosition -> when does ray exit unit cube http://prideout.net/blog/?p=64
kFragFaster = kFragBase
+#10'vec4 texture3Df(sampler3D vol, vec3 coord) {'
+#10' return texture(vol, coord); //trilinear interpolation'
+#10'}';
kFragBetter = kFragBase
+#10'vec4 texture3Df(sampler3D vol, vec3 coord) {'
+#10' // shift the coordinate from [0,1] to [-0.5, textureSz-0.5]'
+#10' vec3 textureSz = textureSize(vol, 0);'
+#10' vec3 coord_grid = coord * textureSz - 0.5;'
+#10' vec3 index = floor(coord_grid);'
+#10' vec3 fraction = coord_grid - index;'
+#10' vec3 one_frac = 1.0 - fraction;'
+#10' vec3 w0 = 1.0/6.0 * one_frac*one_frac*one_frac;'
+#10' vec3 w1 = 2.0/3.0 - 0.5 * fraction*fraction*(2.0-fraction);'
+#10' vec3 w2 = 2.0/3.0 - 0.5 * one_frac*one_frac*(2.0-one_frac);'
+#10' vec3 w3 = 1.0/6.0 * fraction*fraction*fraction;'
+#10' vec3 g0 = w0 + w1;'
+#10' vec3 g1 = w2 + w3;'
+#10' vec3 mult = 1.0 / textureSz;'
+#10' vec3 h0 = mult * ((w1 / g0) - 0.5 + index); //h0 = w1/g0 - 1, move from [-0.5, textureSz-0.5] to [0,1]'
+#10' vec3 h1 = mult * ((w3 / g1) + 1.5 + index); //h1 = w3/g1 + 1, move from [-0.5, textureSz-0.5] to [0,1]'
+#10' // fetch the eight linear interpolations'
+#10' // weighting and fetching is interleaved for performance and stability reasons'
+#10' vec4 tex000 = texture(vol,h0);'
+#10' vec4 tex100 = texture(vol,vec3(h1.x, h0.y, h0.z));'
+#10' tex000 = mix(tex100, tex000, g0.x); //weigh along the x-direction'
+#10' vec4 tex010 = texture(vol,vec3(h0.x, h1.y, h0.z));'
+#10' vec4 tex110 = texture(vol,vec3(h1.x, h1.y, h0.z));'
+#10' tex010 = mix(tex110, tex010, g0.x); //weigh along the x-direction'
+#10' tex000 = mix(tex010, tex000, g0.y); //weigh along the y-direction'
+#10' vec4 tex001 = texture(vol,vec3(h0.x, h0.y, h1.z));'