forked from neurolabusc/surf-ice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshaderu.pas
executable file
·1357 lines (1291 loc) · 51.9 KB
/
shaderu.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 shaderu;
{$Include opts.inc}
{$IFDEF FPC}{$mode objfpc}{$H+}{$ENDIF}
{$D-,O+,Q-,R-,S-}
interface
uses
{$IFDEF DGL} dglOpenGL, {$ELSE DGL} {$IFDEF COREGL}glcorearb, {$ELSE} gl, glext, {$IFDEF GEOM_GLEXT}glext2, {$ENDIF} {$ENDIF} {$ENDIF DGL}
{$IFDEF COREGL} gl_core_3d, {$ELSE} gl_legacy_3d, {$ENDIF}
sysutils,dialogs, define_types, userdir, StrUtils, math;
const
{$IFDEF DGL}
kGL_FALSE = FALSE;
kGL_TRUE = TRUE;
//kGL_TRUE = 1;
//kGL_FALSE = 0;
{$ELSE}
kGL_FALSE = GL_FALSE;
kGL_TRUE = GL_TRUE;
{$ENDIF}
kMinDistance = 0.1;
kMaxDistance = 10;
kMaxUniform = 10;
kError = 666;
kNote = 777;
kBool = 0;
kInt = 1;
kFloat = 2;
kSet = 3;
type
TUniform = record
Name: string;
Widget: integer;
Min,DefaultV,Max: single;
Bool: boolean;
end;
TFrameBuffer = record
{$IFDEF HEMISSAO} tex1, {$ENDIF} depthBuf,frameBuf, tex: GLUint;
w, h: integer;
end;
TShader = record
vao_point2d, vbo_face2d, program2d, program3dx, programDefault, programTrackID, programAoID: GLuint;
OverlayVolume,nUniform: integer;
f1, f2, fScreenShot: TFrameBuffer;
lightPos : TPoint3f;
{$IFDEF COREGL} TrackAmbient, TrackDiffuse, TrackSpecular : single; {$ENDIF}
isGeometryShaderSupported: boolean;
AOradiusU: integer;
FragmentProgram,VertexProgram, GeometryProgram, Note, Vendor: AnsiString;
Uniform: array [1..kMaxUniform] of TUniform;
end;
var
gShader: TShader;
function initVertFrag(vert, geom, frag: string): GLuint;
function LoadShader(lFilename: string; var Shader: TShader): boolean;
function InitGLSL (isStartUp: boolean): boolean;
procedure RunOverlayGLSL(clipPlane: TPoint4f);
procedure RunMeshGLSL (clipPlane: TPoint4f; UseDefaultShader: boolean);
procedure RunTrackGLSL (lineWidth, ScreenPixelX, ScreenPixelY: integer);
procedure RunAoGLSL (var f1, f2 : TFrameBuffer; zoom : integer; alpha1, blend1, fracAO, distance: single);
function setFrame (wid, ht: integer; var f : TFrameBuffer; isMultiSample: boolean; var isOK: boolean) : boolean; //returns true if multi-sampling
procedure initFrame(var f : TFrameBuffer);
procedure releaseFrame;
procedure freeFrame (var f : TFrameBuffer);
//procedure Set2DDraw (w,h: integer; r,g,b: byte);
implementation
uses mainunit;
{$IFDEF COREGL}
const kVert2D ='#version 330'
+#10'layout(location = 0) in vec3 Vert;'
+#10'layout(location = 3) in vec4 Clr;'
+#10'out vec4 vClr;'
+#10'uniform mat4 ModelViewProjectionMatrix;'
+#10'void main() {'
+#10' gl_Position = ModelViewProjectionMatrix * vec4(Vert, 1.0);'
+#10' //gl_Position.z = gl_Position.z - 0.7;'
+#10' vClr = Clr;'
+#10'}';
const kFrag2D = '#version 330'
+#10'in vec4 vClr;'
+#10'out vec4 color;'
+#10'void main() {'
+#10' color = vClr;'
+#10'}';
const kAoShaderVert = '#version 330'
+#10'layout(location = 0) in vec3 Vert;'
+#10'layout(location = 3) in vec2 Coord;'
+#10'smooth out vec2 texCoord;'
+#10'void main () {'
+#10' gl_Position = vec4 (Vert, 1.0);'
+#10' texCoord = Coord;'
+#10'}';
{$IFDEF HEMISSAO}
//https://gist.github.com/fisch0920/6770363
//http://blog.evoserv.at/index.php/2012/12/hemispherical-screen-space-ambient-occlusion-ssao-for-deferred-renderers-using-openglglsl/
const kAoShaderFrag = '#version 330'
+#10'uniform sampler2D tex1, tex2, depth_texture1, depth_texture2, norm1;'
+#10'uniform float blend1, alpha1, fracAO, aoRadius;'
+#10'uniform vec2 texture_size;'
+#10'#define PI 3.14159265'
+#10'float width = texture_size.x; //texture width'
+#10'float height = texture_size.y; //texture height'
+#10'smooth in vec2 texCoord;'
+#10'out vec4 color;'
+#10'vec3 viewNormal;'
+#10'//------------------------------------------'
+#10'//general stuff'
+#10'//make sure that these two values are the same for your camera, otherwise distances will be wrong.'
+#10'//user variables'
+#10'int samples = 64; //ao sample count'
+#10'float aoclamp = 0.25; //depth clamp - reduces haloing at screen edges'
+#10'bool noise = true; //use noise instead of pattern for sample dithering'
+#10'float noiseamount = 0.0002; //dithering amount'
+#10'float diffarea = 0.4; //self-shadowing reduction'
+#10'float gdisplace = 0.4; //gauss bell center'
+#10'//float near = 1.0; //Z-near;'
+#10'//float far = 3.0; //Z-far'
+#10'//--------------------------------------------------------'
+#10'vec2 rand(vec2 coord) {'
+#10' float noiseX = ((fract(1.0-coord.s*(width/2.0))*0.25)+(fract(coord.t*(height/2.0))*0.75))*2.0-1.0;'
+#10' float noiseY = ((fract(1.0-coord.s*(width/2.0))*0.75)+(fract(coord.t*(height/2.0))*0.25))*2.0-1.0;'
+#10' if (noise) {'
+#10' noiseX = clamp(fract(sin(dot(coord ,vec2(12.9898,78.233))) * 43758.5453),0.0,1.0)*2.0-1.0;'
+#10' noiseY = clamp(fract(sin(dot(coord ,vec2(12.9898,78.233)*2.0)) * 43758.5453),0.0,1.0)*2.0-1.0;'
+#10' }'
+#10' return vec2(noiseX,noiseY)*noiseamount;'
+#10'}'
+#10'float readDepth(in vec2 coord) {'
+#10' return texture(depth_texture1, coord).x;'
+#10'}'
+#10'float calAO(float depth, float dw, float dh, float mx) {'
+#10' //float dd = (1.0-depth)*aoRadius;'
+#10' float dd = aoRadius;'
+#10' //float dd = aoRadius * 0.1;'
+#10' float coordw = texCoord.x + dw*dd;'
+#10' float coordh = texCoord.y + dh*dd;'
+#10' float d = readDepth(vec2(coordw, coordh));'
+#10' //if (d > dd) return 0.0;'
+#10' vec3 sampleDir = vec3(dw*dd, dh*dd, depth-d);'
+#10' sampleDir = normalize(sampleDir);'
+#10' float NdotS = max(dot(viewNormal, sampleDir), 0);'
+#10' //float rangeCheck = 1.0 - step(mx, abs(depth-d));'
+#10' return NdotS;// * rangeCheck;'
+#10'}'
+#10'float getAO(void) {'
+#10' viewNormal = texture(norm1, texCoord).xyz;'
+#10' vec2 noise = rand(texCoord); '
+#10' float depth = readDepth(texCoord);'
+#10' float w = (1.0 / width)+(noise.x*(1.0-noise.x));'
+#10' float h = (1.0 / height)+(noise.y*(1.0-noise.y));'
+#10' float mx = max(w,h) * aoRadius * 4.0;'
+#10' //float w = (1.0 / width)/clamp(depth,aoclamp,1.0)+(noise.x*(1.0-noise.x));'
+#10' //float h = (1.0 / height)/clamp(depth,aoclamp,1.0)+(noise.y*(1.0-noise.y));'
+#10' float pw;'
+#10' float ph;'
+#10' float ao = 0.0;'
+#10' float dl = PI*(3.0-sqrt(5.0));'
+#10' float dz = 1.0/float(samples);'
+#10' float l = 0.0;'
+#10' float z = 1.0 - dz/2.0;'
+#10' for (int i = 0; i <= samples; i ++) {'
+#10' float r = sqrt(1.0-z);'
+#10' pw = cos(l)*r;'
+#10' ph = sin(l)*r;'
+#10' ao += calAO(depth,pw*w,ph*h, mx);'
+#10' z = z - dz;'
+#10' l = l + dl;'
+#10' }'
+#10' ao /= float(samples);'
+#10' //ao = clamp(ao, 0.0, 1.0);'
+#10' ao = (clamp(ao, 0.0, 0.7) ) * 1.428; //threshold then LERP 0..1'
+#10' //ao = pow(ao, 0.2);'
+#10' ao = smoothstep(0.0, 1.0, ao);'
+#10' //ao = pow(ao, 0.2);'
+#10' //ao = (1.0 - ao);// * fracAO;'
+#10' return ao * fracAO;'
+#10'}'
+#10'void main(void) {'
+#10' //color = vec4(1.0, 0.0, 0.0, 1.0); return;'
+#10' vec4 t1 = texture(tex1, texCoord);'
+#10' if (t1.a == 0.0) discard;'
+#10' vec4 t2 = texture(tex2, texCoord);'
+#10' float ao = getAO();'
+#10' //float ao = fracAO * getAO();'
+#10' t1.rgb = clamp(t1.rgb-ao, 0.0, 1.0);'
+#10' //if (fracAO > 0.0)'
+#10' // t1.rgb = clamp(t1.rgb-getAO(), 0.0, 1.0);'
+#10' t1.rgb = mix(t2.rgb,t1.rgb, alpha1);'
+#10' float depth = 1.0 - (3.0 * (texture(depth_texture2, texCoord).x - texture(depth_texture1, texCoord).x));'
+#10' //t2.a *= clamp(depth, 0.0, 1.0);'
+#10' depth = clamp(depth, 0.0, 1.0);'
+#10' color = mix(t1, t2, blend1 * depth);'
+#10' //color.rgb = texture(norm1, texCoord).rgb;'
+#10' //color.rgb = vec3(1.0 - ao);'
+#10'}';
{$ELSE}
(*//this next shader is nice, but AO changes as image is zoomed closer/further
const kAoShaderFragToon = '#version 330'
+#10'uniform sampler2D tex1, tex2, depth_texture1, depth_texture2;'
+#10'uniform float blend1, alpha1, fracAO;'
+#10'uniform vec2 texture_size;'
+#10'smooth in vec2 texCoord;'
+#10'out vec4 color;'
+#10'float getAO(void) {'
+#10' vec2 px = vec2(1.0 / texture_size.x, 1.0 / texture_size.y) * 1.4;'
+#10' vec2 nXY = texCoord;'
+#10' vec2 x1 = vec2(px.x, 0.0);'
+#10' vec2 y1 = vec2(0.0, px.y);'
+#10' float dx = 0.125 * (texture(depth_texture1, nXY-x1-y1).x+texture(depth_texture1, nXY-y1).x+texture(depth_texture1, nXY+x1-y1).x+texture(depth_texture1, nXY-x1).x+texture(depth_texture1, nXY+x1).x+texture(depth_texture1, nXY-x1+y1).x+texture(depth_texture1, nXY+y1).x+texture(depth_texture1, nXY+x1+y1).x);'
+#10' dx = abs(texture(depth_texture1, nXY).x - dx);'
+#10' dx = dx * 5000.0;'
+#10' dx = smoothstep(1.0-fracAO, 1.0, dx);'
+#10' return dx;'
+#10'}'
+#10'void main(void) {'
+#10' vec4 t1 = texture(tex1, texCoord);'
+#10' if (t1.a == 0.0) discard;'
+#10' vec4 t2 = texture(tex2, texCoord);'
+#10' if (fracAO > 0.0)'
+#10' t1.rgb = clamp(t1.rgb-getAO(), 0.0, 1.0);'
+#10' t1.rgb = mix(t2.rgb,t1.rgb, alpha1);'
+#10' float depth = 1.0 - (3.0 * (texture(depth_texture2, texCoord).x - texture(depth_texture1, texCoord).x));'
+#10' depth = clamp(depth, 0.0, 1.0);'
+#10' color = mix(t1, t2, blend1 * depth);'
+#10'}'; *)
const kAoShaderFrag = '#version 330'
+#10'uniform sampler2D tex1, tex2, depth_texture1, depth_texture2;'
+#10'uniform float blend1, alpha1, fracAO, aoRadius;'
+#10'uniform vec2 texture_size;'
+#10'#define PI 3.14159265'
+#10'smooth in vec2 texCoord;'
+#10'out vec4 color;'
+#10'//general stuff'
+#10'int samples = 32; //ao sample count'
+#10'float aoclamp = 0.25; //depth clamp - reduces haloing at screen edges'
+#10'bool noise = true; //use noise instead of pattern for sample dithering'
+#10'float noiseamount = 0.0002; //dithering amount'
+#10'float diffarea = 0.5; //self-shadowing reduction'
+#10'float gdisplace = 0.4; //gauss bell center'
+#10'vec2 rand(vec2 coord) {'
+#10' float noiseX = ((fract(1.0-coord.s*(texture_size.x/2.0))*0.25)+(fract(coord.t*(texture_size.y/2.0))*0.75))*2.0-1.0;'
+#10' float noiseY = ((fract(1.0-coord.s*(texture_size.x/2.0))*0.75)+(fract(coord.t*(texture_size.y/2.0))*0.25))*2.0-1.0;'
+#10' if (noise) {'
+#10' noiseX = clamp(fract(sin(dot(coord ,vec2(12.9898,78.233))) * 43758.5453),0.0,1.0)*2.0-1.0;'
+#10' noiseY = clamp(fract(sin(dot(coord ,vec2(12.9898,78.233)*2.0)) * 43758.5453),0.0,1.0)*2.0-1.0;'
+#10' }'
+#10' return vec2(noiseX,noiseY)*noiseamount;'
+#10'}'
+#10'float readDepth(in vec2 coord) {'
+#10' return 1.0 - ((texture(depth_texture1, coord).x + 0.87) * 0.534);'
+#10'}'
+#10'float compareDepths(in float depth1, in float depth2,inout int far) {'
+#10' float garea = 2.0; //gauss bell width'
+#10' float diff = (depth1 - depth2)*100.0; //depth difference (0-100)'
+#10' if (diff<gdisplace) //reduce left bell width to avoid self-shadowing'
+#10' garea = diffarea;'
+#10' else'
+#10' far = 1;'
+#10' float gauss = pow(2.7182,-2.0*(diff-gdisplace)*(diff-gdisplace)/(garea*garea));'
+#10' return gauss;'
+#10'}'
+#10'float calAO(float depth, vec2 coordwh) {'
+#10' int far = 0;'
+#10' float temp = compareDepths(depth, readDepth(texCoord+coordwh),far);'
+#10' if (far > 0) {'
+#10' float temp2 = compareDepths(readDepth(texCoord-coordwh),depth,far);'
+#10' temp += (1.0-temp)*temp2;'
+#10' }'
+#10' return temp;'
+#10'}'
+#10'float getAO(void) {'
+#10' vec2 noise = rand(texCoord);'
+#10' float depth = readDepth(texCoord);'
+#10' float dd = (1.0-depth)*aoRadius;'
+#10' float w = (1.0 / texture_size.x)/clamp(depth,aoclamp,1.0)+(noise.x*(1.0-noise.x));'
+#10' float h = (1.0 / texture_size.y)/clamp(depth,aoclamp,1.0)+(noise.y*(1.0-noise.y));'
+#10' float ao = 0.0;'
+#10' float dl = PI*(3.0-sqrt(5.0));'
+#10' float dz = 1.0/float(samples);'
+#10' float l = 0.0;'
+#10' float z = 1.0 - dz/2.0;'
+#10' for (int i = 0; i <= samples; i ++) {'
+#10' float r = sqrt(1.0-z);'
+#10' ao += calAO(depth, vec2(cos(l)*r*w*dd,sin(l)*r*h*dd));'
+#10' z = z - dz;'
+#10' l = l + dl;'
+#10' }'
+#10' ao /= float(samples);'
+#10' ao = clamp(ao, 0.0, 0.4) * 2.5; //threshold then LERP 0..1'
+#10' ao = smoothstep(0.0, 1.0, ao);'
+#10' ao = (1.0 - ao) * fracAO;'
+#10' return ao;'
+#10'}'
+#10'void main(void) {'
+#10' vec4 t1 = texture(tex1, texCoord);'
+#10' if (t1.a == 0.0) discard;'
+#10' vec4 t2 = texture(tex2, texCoord);'
+#10' //float ao = 1.0 - getAO(); color = vec4(ao, ao, ao, 1.0); return;'
+#10' if (fracAO > 0.0)'
+#10' t1.rgb = clamp(t1.rgb-getAO(), 0.0, 1.0);'
+#10' t1.rgb = mix(t2.rgb,t1.rgb, alpha1);'
+#10' float depth = 1.0 - (3.0 * (texture(depth_texture2, texCoord).x - texture(depth_texture1, texCoord).x));'
+#10' depth = clamp(depth, 0.0, 1.0);'
+#10' color = mix(t1, t2, blend1 * depth);'
+#10'}';
{$ENDIF}
{$ELSE}
const kVert2D ='varying vec4 vClr;'
+#10'void main() {'
+#10' gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;'
+#10' vClr = gl_Color;'
+#10'}';
const kFrag2D = 'varying vec4 vClr;'
+#10'void main() {'
+#10' gl_FragColor = vClr;'
+#10'}';
const kAoShaderFrag = 'uniform sampler2D tex1, tex2, depth_texture1, depth_texture2;'
+#10'uniform float blend1, alpha1, fracAO, aoRadius;'
+#10'uniform vec2 texture_size;'
+#10'#define PI 3.14159265'
+#10'vec2 texCoord = gl_TexCoord[0].xy;'
+#10'//general stuff'
+#10'int samples = 32; //ao sample count'
+#10'float aoclamp = 0.25; //depth clamp - reduces haloing at screen edges'
+#10'bool noise = true; //use noise instead of pattern for sample dithering'
+#10'float noiseamount = 0.0002; //dithering amount'
+#10'float diffarea = 0.5; //self-shadowing reduction'
+#10'float gdisplace = 0.4; //gauss bell center'
+#10'vec2 rand(vec2 coord) {'
+#10' float noiseX = ((fract(1.0-coord.s*(texture_size.x/2.0))*0.25)+(fract(coord.t*(texture_size.y/2.0))*0.75))*2.0-1.0;'
+#10' float noiseY = ((fract(1.0-coord.s*(texture_size.x/2.0))*0.75)+(fract(coord.t*(texture_size.y/2.0))*0.25))*2.0-1.0;'
+#10' if (noise) {'
+#10' noiseX = clamp(fract(sin(dot(coord ,vec2(12.9898,78.233))) * 43758.5453),0.0,1.0)*2.0-1.0;'
+#10' noiseY = clamp(fract(sin(dot(coord ,vec2(12.9898,78.233)*2.0)) * 43758.5453),0.0,1.0)*2.0-1.0;'
+#10' }'
+#10' return vec2(noiseX,noiseY)*noiseamount;'
+#10'}'
+#10'float readDepth(in vec2 coord) {'
+#10' return 1.0 - ((texture2D(depth_texture1, coord).x + 0.87) * 0.534);'
+#10'}'
+#10'float compareDepths(in float depth1, in float depth2,inout int far) {'
+#10' float garea = 2.0; //gauss bell width'
+#10' float diff = (depth1 - depth2)*100.0; //depth difference (0-100)'
+#10' if (diff<gdisplace) //reduce left bell width to avoid self-shadowing'
+#10' garea = diffarea;'
+#10' else'
+#10' far = 1;'
+#10' float gauss = pow(2.7182,-2.0*(diff-gdisplace)*(diff-gdisplace)/(garea*garea));'
+#10' return gauss;'
+#10'}'
+#10'float calAO(float depth, vec2 coordwh) {'
+#10' int far = 0;'
+#10' float temp = compareDepths(depth, readDepth(texCoord+coordwh),far);'
+#10' if (far > 0) {'
+#10' float temp2 = compareDepths(readDepth(texCoord-coordwh),depth,far);'
+#10' temp += (1.0-temp)*temp2;'
+#10' }'
+#10' return temp;'
+#10'}'
+#10'float getAO(void) {'
+#10' vec2 noise = rand(texCoord);'
+#10' float depth = readDepth(texCoord);'
+#10' float dd = (1.0-depth)*aoRadius;'
+#10' float w = (1.0 / texture_size.x)/clamp(depth,aoclamp,1.0)+(noise.x*(1.0-noise.x));'
+#10' float h = (1.0 / texture_size.y)/clamp(depth,aoclamp,1.0)+(noise.y*(1.0-noise.y));'
+#10' float ao = 0.0;'
+#10' float dl = PI*(3.0-sqrt(5.0));'
+#10' float dz = 1.0/float(samples);'
+#10' float l = 0.0;'
+#10' float z = 1.0 - dz/2.0;'
+#10' for (int i = 0; i <= samples; i ++) {'
+#10' float r = sqrt(1.0-z);'
+#10' ao += calAO(depth, vec2(cos(l)*r*w*dd,sin(l)*r*h*dd));'
+#10' z = z - dz;'
+#10' l = l + dl;'
+#10' }'
+#10' ao /= float(samples);'
+#10' ao = clamp(ao, 0.0, 0.4) * 2.5; //threshold then LERP 0..1'
+#10' ao = smoothstep(0.0, 1.0, ao);'
+#10' ao = (1.0 - ao) * fracAO;'
+#10' return ao;'
+#10'}'
+#10'void main(void) {'
+#10' vec4 t1 = texture2D(tex1, texCoord);'
+#10' if (t1.a == 0.0) discard;'
+#10' vec4 t2 = texture2D(tex2, texCoord);'
+#10' //float ao = 1.0 - getAO(); gl_FragColor = vec4(ao, ao, ao, 1.0); return;'
+#10' if (fracAO > 0.0)'
+#10' t1.rgb = clamp(t1.rgb-getAO(), 0.0, 1.0);'
+#10' t1.rgb = mix(t2.rgb,t1.rgb, alpha1);'
+#10' float depth = 1.0 - (3.0 * (texture2D(depth_texture2, texCoord).x - texture2D(depth_texture1, texCoord).x));'
+#10' depth = clamp(depth, 0.0, 1.0);'
+#10' gl_FragColor = mix(t1, t2, blend1 * depth);'
+#10'}';
{$ENDIF}
(*moved to gl_2d procedure Set2DDraw (w,h: integer; r,g,b: byte);
begin
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho (0, 1, 0, 1, -6, 12);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glDepthMask(kGL_TRUE); // enable writes to Z-buffer
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE); // glEnable(GL_CULL_FACE); //check on pyramid
glEnable(GL_BLEND);
glEnable(GL_NORMALIZE);
glClearColor(r/255, g/255, b/255, 0.0); //Set background
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT or GL_STENCIL_BUFFER_BIT);
glViewport( 0, 0, w, h); //required when bitmap zoom <> 1
end; *)
procedure uniform1ix(prog: GLint; name: AnsiString; value: integer);
begin
glUniform1i(glGetUniformLocation(prog, pAnsiChar(Name)), value) ;
end;
procedure uniform1fx(prog: GLint; name: AnsiString; value: single );
begin
glUniform1f(glGetUniformLocation(prog, pAnsiChar(Name)), value) ;
end;
procedure uniform2fx(prog: GLint; name: AnsiString; v1, v2: single );
begin
glUniform2f(glGetUniformLocation(prog, pAnsiChar(Name)), v1, v2) ;
end;
procedure uniform4fx(prog: GLint; name: AnsiString; v: TPoint4f);
begin
glUniform4f(glGetUniformLocation(prog, pAnsiChar(Name)), v.X,v.Y,v.Z, v.W) ;
end;
procedure uniform3fx(prog: GLint; name: AnsiString; v1,v2,v3: single);
begin
glUniform3f(glGetUniformLocation(prog, pAnsiChar(Name)), v1,v2,v3) ;
end;
procedure uniform1i( name: AnsiString; value: integer);
begin
glUniform1i(glGetUniformLocation(gShader.program3dx, pAnsiChar(Name)), value) ;
end;
procedure uniform1f( name: AnsiString; value: single );
begin
glUniform1f(glGetUniformLocation(gShader.program3dx, pAnsiChar(Name)), value) ;
end;
procedure uniform4f( name: AnsiString; v1,v2,v3, v4: single);
begin
glUniform4f(glGetUniformLocation(gShader.program3dx, pAnsiChar(Name)), v1,v2,v3, v4) ;
end;
procedure uniform3f( name: AnsiString; v1,v2,v3: single);
begin
glUniform3f(glGetUniformLocation(gShader.program3dx, pAnsiChar(Name)), v1,v2,v3) ;
end;
procedure releaseFrame;
begin
{$IFDEF COREGL}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
{$ELSE}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); //only included in 3.0! https://www.opengl.org/sdk/docs/man/html/glBindFramebuffer.xhtml
{$ENDIF}
end;
procedure freeFrame (var f : TFrameBuffer);
begin
//Delete resources
glDeleteTextures(1, @f.tex);
{$IFDEF HEMISSAO} glDeleteTextures(1, @f.tex1); {$ENDIF}
glDeleteTextures(1, @f.depthBuf);
{$IFDEF COREGL}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDeleteFramebuffers(1, @f.frameBuf);
{$ELSE}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glDeleteFramebuffersEXT(1, @f.frameBuf);
{$ENDIF}
//Bind 0, which means render to back buffer, as a result, frameBuf is unbound
end;
//{$DEFINE MULTISAMPLE}
{$IFDEF MULTISAMPLE}
function setFrame (wid, ht: integer; var f : TFrameBuffer; isMultiSample: boolean; var isOK: boolean) : boolean; //returns true if multi-sampling
//http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-14-render-to-texture/
//https://stackoverflow.com/questions/33587682/opengl-how-can-i-attach-a-depth-buffer-to-a-framebuffer-using-a-multisampled-2d
var
w,h: integer;
drawBuf: array[0..1] of GLenum;
begin
{$IFNDEF COREGL}
error requires core
{$ENDIF}
w := wid;
h := ht;
isOK := true;
if isMultiSample then begin
w := w * 2;
h := h * 2;
end;
result := isMultiSample;
if (w = f.w) and (h = f.h) then begin
glBindFramebuffer(GL_FRAMEBUFFER, f.frameBuf);
exit;
end;
freeframe(f);
f.w := w;
f.h := h;
//https://www.opengl.org/wiki/Framebuffer_Object_Examples#Quick_example.2C_render_to_texture_.282D.29
glGenTextures(1, @f.tex);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, f.tex);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA8, f.w, f.h, 0,GL_RGBA, GL_UNSIGNED_BYTE, nil); //RGBA16 for AO
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGBA8, f.w, f.h, GL_TRUE);
glGenFramebuffers(1, @f.frameBuf);
glBindFramebuffer(GL_FRAMEBUFFER, f.frameBuf);
//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, f.tex, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, f.tex, 0);
(*glGenTextures(1, @f.depthBuf);
glBindTexture(GL_RENDERBUFFER, f.depthBuf);
glTexParameterf(GL_RENDERBUFFER, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_RENDERBUFFER, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_RENDERBUFFER, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_RENDERBUFFER, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, f.w, f.h, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nil);
//glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_DEPTH24_STENCIL8, f.w, f.h, GL_TRUE);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH_COMPONENT24, f.w, f.h);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, f.depthBuf); *)
glGenTextures(1, @f.depthBuf);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, f.depthBuf);
glTexParameterf(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_DEPTH_COMPONENT, f.w, f.h, GL_TRUE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, f.depthBuf, 0);
//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, f.depthBuf, 0);
drawBuf[0] := GL_COLOR_ATTACHMENT0;
drawBuf[1] := GL_COLOR_ATTACHMENT1;
glDrawBuffers(1, @drawBuf[0]); // draw colors only
end;
{$ELSE}
function setFrame (wid, ht: integer; var f : TFrameBuffer; isMultiSample: boolean; var isOK: boolean) : boolean; //returns true if multi-sampling
//http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-14-render-to-texture/
var
w,h: integer;
drawBuf: array[0..1] of GLenum;
i: GLint;
begin
w := wid;
h := ht;
isOK := true;
if isMultiSample then begin
w := w * 2;
h := h * 2;
end;
result := isMultiSample;
if (w = f.w) and (h = f.h) then begin
{$IFDEF COREGL}
glBindFramebuffer(GL_FRAMEBUFFER, f.frameBuf);
{$ELSE}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, f.frameBuf); //only included in 3.0! https://www.opengl.org/sdk/docs/man/html/glBindFramebuffer.xhtml
{$ENDIF}
exit;
end;
freeframe(f);
f.w := w;
f.h := h;
//https://www.opengl.org/wiki/Framebuffer_Object_Examples#Quick_example.2C_render_to_texture_.282D.29
glGenTextures(1, @f.tex);
(*if false then begin// not isMultiSample then begin
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, f.tex);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2DMultisample( GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGBA8, f.w, f.h, 0);
//glTexImage2D(GL_TEXTURE_2D_MULTISAMPLE, 0,GL_RGBA8, f.w, f.h, 0,GL_RGBA, GL_UNSIGNED_BYTE, nil); //RGBA16 for AO
end else begin *)
glBindTexture(GL_TEXTURE_2D, f.tex);
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_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
{$IFDEF COREGL}
glTexImage2D(GL_PROXY_TEXTURE_2D, 0,GL_RGBA8, f.w, f.h, 0,GL_RGBA, GL_UNSIGNED_BYTE, nil); //RGBA16 for AO
//glTexImage2DMultisample( GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGBA8, f.w, f.h, 0);
glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, @i);
if i < 1 then begin
GLForm1.ShowmessageError(' setFrame error: reduce bitmap zoom');
isOK := false;
exit;
end;
{$ENDIF}
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA8, f.w, f.h, 0,GL_RGBA, GL_UNSIGNED_BYTE, nil); //RGBA16 for AO
//end;
{$IFDEF HEMISSAO}
glGenTextures(1, @f.tex1);
glBindTexture(GL_TEXTURE_2D, f.tex1);
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_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA8, f.w, f.h, 0,GL_RGBA, GL_UNSIGNED_BYTE, nil); //RGBA16 for AO
{$ENDIF}
{$IFDEF COREGL}
glGenFramebuffers(1, @f.frameBuf);
glBindFramebuffer(GL_FRAMEBUFFER, f.frameBuf);
//Attach 2D texture to this FBO
//if not isMultiSample then
// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, f.tex, 0)
//else
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, f.tex, 0);
{$IFDEF HEMISSAO}glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, f.tex1, 0);{$ENDIF}
{$ELSE}
glGenFramebuffersEXT(1, @f.frameBuf);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, f.frameBuf);
//Attach 2D texture to this FBO
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, f.tex, 0);
{$IFDEF HEMISSAO}glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, f.tex1, 0);{$ENDIF}
{$ENDIF}
//glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, f.tex, 0);
// Create the depth buffer
glGenTextures(1, @f.depthBuf);
glBindTexture(GL_TEXTURE_2D, f.depthBuf);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
{$IFDEF COREGL}
glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, f.w, f.h, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nil);
glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, @i);
if i < 1 then begin
GLForm1.ShowmessageError(' setFrame error: reduce bitmap zoom');
isOK := false;
exit;
end;
{$ENDIF}
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, f.w, f.h, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nil);
{$IFDEF COREGL}
if glGetError() <> GL_NO_ERROR then begin
//GLForm1.ShowmessageError('Frame buffer error 0x'+inttohex(glCheckFramebufferStatus(GL_FRAMEBUFFER),4) );
GLForm1.ShowmessageError(' setFrame error: reduce bitmap zoom');
isOK := false;
exit;
end;
{$ENDIF}
{$IFDEF COREGL}
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, f.depthBuf, 0);
{$ELSE}
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, f.depthBuf, 0);
{$ENDIF}
// Set "renderedTexture" as our colour attachement #0
// Set the list of draw buffers.
//drawBuf := GL_COLOR_ATTACHMENT0;
//glDrawBuffers(1, @drawBuf); // "1" is the size of DrawBuffers
drawBuf[0] := GL_COLOR_ATTACHMENT0;
drawBuf[1] := GL_COLOR_ATTACHMENT1;
{$IFDEF HEMISSAO}
glDrawBuffers(2, @drawBuf[0]); // draw colors and normals
{$ELSE}
glDrawBuffers(1, @drawBuf[0]); // draw colors only
{$ENDIF}
{$IFDEF COREGL}
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) <> GL_FRAMEBUFFER_COMPLETE) then begin
GLForm1.ShowmessageError('Frame buffer error 0x'+inttohex(glCheckFramebufferStatus(GL_FRAMEBUFFER),4) );
isOK := false;
exit;
end;
{$ENDIF}
glEnable( GL_MULTISAMPLE );
end;
{$ENDIF}
procedure initFrame(var f : TFrameBuffer);
begin
f.frameBuf := 0;
f.tex := 0;
{$IFDEF HEMISSAO} f.tex1 := 0; {$ENDIF}
f.depthBuf := 0;
f.w := 0;
f.h := 0;
end;
procedure GetError(p: integer); //report OpenGL Error
var
Error: GLenum;
s: string;
begin
exit;
Error := glGetError();
if Error = GL_NO_ERROR then exit;
{$IFDEF DGL}
s := 'DGL' +inttostr(p)+'->' + inttostr(Error);
{$ELSE}
s := inttostr(p)+'->' + inttostr(Error);
{$ENDIF}
if Error = GL_INVALID_VALUE then
s := s + '(GL_INVALID_VALUE)';
GLForm1.ShowmessageError('OpenGL error : '+s );
end;
procedure ReportCompileShaderError(shaderType: GLEnum; glObjectID: GLuint);
var
s : string;
maxLength, isCompiled : GLint;
begin
//status := 0;
glGetShaderiv(glObjectID, GL_COMPILE_STATUS, @isCompiled);
if (isCompiled <> 0) then exit; //report compiling errors.
glGetError;
glGetShaderiv(glObjectID, GL_INFO_LOG_LENGTH, @maxLength);
if (maxLength < 1) then exit; //no info
setlength(s, maxLength);
{$IFDEF OLDDGL} //older DGL
glGetShaderInfoLog(glObjectID, maxLength, maxLength, @s[1]);
{$ELSE}
glGetShaderInfoLog(glObjectID, maxLength, @maxLength, @s[1]);
{$ENDIF}
s:=trim(s);
if shaderType = GL_VERTEX_SHADER then
s := 'VERT '+ s
else if shaderType = GL_GEOMETRY_SHADER then
s := 'GEOM '+ s
else if shaderType = GL_FRAGMENT_SHADER then
s := 'FRAG '+ s;
GLForm1.ShowmessageError('Shader compile error '+s);
end;
procedure ReportCompileProgramError(glObjectID: GLuint);
var
s : string;
maxLength : GLint;
begin
glGetProgramiv(glObjectID, GL_LINK_STATUS, @maxLength);
//if (maxLength = GL_TRUE) then exit;
if (maxLength = 1) then exit; //DGL GL_TRUE
maxLength := 4096;
setlength(s, maxLength);
{$IFDEF OLDDGL} //older DGL
glGetProgramInfoLog(glObjectID, maxLength, maxLength, @s[1]);
{$ELSE}
glGetProgramInfoLog(glObjectID, maxLength, @maxLength, @s[1]);
{$ENDIF}
if maxLength < 1 then begin
GLForm1.ShowmessageError('Program compile error (unspecified)');
exit
end;
s:=trim(s);
if (length(s) < 2) then exit;
GLForm1.ShowmessageError('Program compile error '+s);
end;
function compileShaderOfType (shaderType: GLEnum; shaderText: string): GLuint;
begin
result := glCreateShader(shaderType);
{$IFDEF DGL}
glShaderSource(result, 1, PPGLChar(@shaderText), nil);
{$ELSE}
glShaderSource(result, 1, PChar(@shaderText), nil);
{$ENDIF}
glCompileShader(result);
//ReportCompileShaderHint(shaderType, result);
ReportCompileShaderError(shaderType, result);
end;
function initVertFrag(vert, geom, frag: string): GLuint;
var
fs, gs, vs: GLuint;
begin
result := 0;
if (not gShader.isGeometryShaderSupported) and (length(geom) > 0) then begin
{$IFDEF GEOM_GLEXT} //requires new version of glext.pp with glProgramParameteriEXT
GLForm1.ShowmessageError('Error: graphics driver does not support geometry shaders');
{$ELSE}
GLForm1.ShowmessageError('Error: software not compiled to support geometry shaders');
{$ENDIF}
exit;
end;
glGetError(); //<- ignore proior errors
//GetError(121); // <- report prior errors
result := glCreateProgram();
if (length(vert) > 0) then begin
vs := compileShaderOfType(GL_VERTEX_SHADER, vert);
if (vs = 0) then exit;
glAttachShader(result, vs);
end;
fs := compileShaderOfType(GL_FRAGMENT_SHADER, frag);
if (fs = 0) then exit;
glAttachShader(result, fs);
if (length(geom) > 0) then begin
gs := compileShaderOfType(GL_GEOMETRY_SHADER, geom);
if (gs = 0) then exit;
glAttachShader(result, gs);
//if length(gShader.GeometryProgram) > 0 then
//glProgramParameteri ( gShader.program3d, GL_GEOMETRY_VERTICES_OUT, 3 );
//glProgramParameteriEXT(result, GL_GEOMETRY_VERTICES_OUT_EXT, 3); //GL_GEOMETRY_VERTICES_OUT_EXT = $8DDA;
{$IFNDEF COREGL}
//The next line requires the updated glext.pp http://mantis.freepascal.org/view.php?id=29051
{$IFDEF GEOM_GLEXT} //requires new version of glext.pp with glProgramParameteriEXT
glProgramParameteriEXT(result, GL_GEOMETRY_VERTICES_OUT_EXT, 3); //GL_GEOMETRY_VERTICES_OUT_EXT = $8DDA;
//glProgramParameteri(result, $8DDA, 3); // <- using this on the old GLEXT does not work
{$ENDIF}
{$ENDIF}
end;
glLinkProgram(result);
//ReportCompileShaderHint(666, result);
ReportCompileProgramError(result);
if (length(vert) > 0) then begin
glDetachShader(result, vs);
glDeleteShader(vs);
end;
glDetachShader(result, fs);
glDeleteShader(fs);
//glUseProgram(result);
GetError(123);
glGetError();
end;
procedure AdjustShaders (lShader: TShader);
//sends the uniform values to the GPU
var
i: integer;
begin
if (lShader.nUniform < 1) or (lShader.nUniform > kMaxUniform) then
exit;
for i := 1 to lShader.nUniform do begin
if i = lShader.AOradiusU then continue;
case lShader.Uniform[i].Widget of
kFloat: uniform1f(lShader.Uniform[i].name,lShader.Uniform[i].defaultV);
kInt: uniform1i(lShader.Uniform[i].name,round(lShader.Uniform[i].defaultV));
kBool: begin
if lShader.Uniform[i].bool then
uniform1i(lShader.Uniform[i].name,1)
else
uniform1i(lShader.Uniform[i].name,0);
end;
end;//case
end; //for each uniform
end; //AdjustShaders()
function HasGeometryShaderSupport: boolean; //in OpenGL 2.1 the geometry shader is optional, e.g. not in Mesa for Intel
var
AllExtStr: string;
begin
AllExtStr := glGetString(GL_EXTENSIONS);
result := AnsiContainsText (AllExtStr, '_geometry_shader'); //GL_EXT_geometry_shader4, ARB_geometry_shader4
end;
function GLVersionError(major, minor: integer): boolean;
const
kEps = 1.0E-6;
var
s: string;
current, req: double;
begin
result := false; //assume OK
//Use GL_VERSION to support OpenGL 2.1
// for OpenGL 3.0+: glGetIntegerv(GL_MAJOR_VERSION, @lMajorV); glGetIntegerv(GL_MINOR_VERSION, @lMinorV);
s := glGetString(GL_VERSION); //"4.1 Intel"
if length(s) > 3 then
s := copy(s, 1, 3);
current := strtofloatdef(s, 2.0)+keps;
req := major + (0.1*minor);
if (current >= req) then exit; //all OK
result := true; //ERROR
end;
function InitGLSL (isStartUp: boolean): boolean;
var
lShader: TShader;
begin
result := true;
if isStartUp then begin
{$IFDEF DGL}
InitOpenGL;
ReadExtensions;
{$ELSE}
//If your compiler does not find Load_GL_version_3_3_CORE you will need to update glext.pp
{$IFDEF COREGL}
gShader.isGeometryShaderSupported := true; //OpenGL core always supports the geometry shader
gShader.TrackAmbient := 0.5;
gShader.TrackDiffuse := 0.7;
gShader.TrackSpecular := 0.2;
if (not Load_GL_version_3_3_CORE) then begin
showmessage('Unable to load OpenGL 3.3 Core (hint: try surficeOld)');
{$ELSE}
if not (Load_GL_VERSION_2_1) then begin
//On Ubuntu 14.04 LTS on VirtualBox with Chromium 19 drivers Load_GL_VERSION_2_1 fails but still works...
//showmessage(format('Unable to load OpenGL %d.%d found %s. Vendor %s. GLSL %s',[GLForm1.GLBox.OpenGLMajorVersion, GLForm1.GLBox.OpenGLMinorVersion, glGetString(GL_VERSION), glGetString(GL_VENDOR),glGetString(GL_SHADING_LANGUAGE_VERSION)]));
showmessage('Unable to load OpenGL 2.1');
{$ENDIF}
halt();
end;
(*if GLVersionError(GLForm1.GLBox.OpenGLMajorVersion, GLForm1.GLBox.OpenGLMinorVersion) then begin
//showmessage(format('Requires OpenGL %d.%d found %s. Vendor %s. GLSL %s',[GLForm1.GLBox.OpenGLMajorVersion, GLForm1.GLBox.OpenGLMinorVersion, glGetString(GL_VERSION), glGetString(GL_VENDOR),glGetString(GL_SHADING_LANGUAGE_VERSION)]));
showmessage('Requires more recent OpenGL');
halt();
end;*)
{$IFNDEF COREGL}
Load_GL_EXT_framebuffer_object;
Load_GL_ARB_framebuffer_object;
Load_GL_EXT_texture_object;
{$IFDEF GEOM_GLEXT} //requires new version of glext.pp with glProgramParameteriEXT
gShader.isGeometryShaderSupported := HasGeometryShaderSupport();
if gShader.isGeometryShaderSupported then begin
if not Assigned(glProgramParameteriEXT) then
SetglProgramParameteriEXT;
if not Assigned(glProgramParameteriEXT) then
gShader.isGeometryShaderSupported := false;
end;
{$ELSE}
gShader.isGeometryShaderSupported := false;
{$ENDIF}
{$ENDIF}
{$ENDIF}
gShader.Vendor := glGetString(GL_VENDOR)+' :: OpenGL '+glGetString(GL_VERSION)+' :: GLSL ' +glGetString(GL_SHADING_LANGUAGE_VERSION);
gShader.program2d := initVertFrag(kVert2D, '', kFrag2D);
gShader.program3dx := initVertFrag(kVert3d, '', kFrag3d);
gShader.programDefault := initVertFrag(kVert3d, '', kFrag3d);
gShader.AOradiusU:= 0;
{$IFDEF COREGL}
if LoadShader(AppDir+'ao3.glsl', lShader) then
gShader.programAoID := initVertFrag(lShader.VertexProgram, lShader.GeometryProgram, lShader.FragmentProgram)
else
gShader.programAoID := initVertFrag(kAoShaderVert, '', kAoShaderFrag);
{$IFDEF TUBES}
if LoadShader(AppDir+'tubes3.glsl', lShader) then
gShader.programTrackID := initVertFrag(lShader.VertexProgram, lShader.GeometryProgram, lShader.FragmentProgram)
else
gShader.programTrackID := initVertFrag(kVert3d, '', kTrackShaderFrag);
{$ELSE}
if LoadShader(AppDir+'tracks3.glsl', lShader) then
gShader.programTrackID := initVertFrag(lShader.VertexProgram, lShader.GeometryProgram, lShader.FragmentProgram)
else
gShader.programTrackID := initVertFrag(kTrackShaderVert, kTrackShaderGeom, kTrackShaderFrag);
{$ENDIF}
{$ELSE}
if (not Assigned(glBindFramebufferEXT)) or
(not Assigned(glBindTexture)) or
(not Assigned(glCheckFramebufferStatus)) then
result := false;//FrameBuffer and Texture not required in OpenGL2.1: avoid problems with VirtualBox/Centos6.6
if LoadShader(AppDir+'ao.glsl', lShader) then
gShader.programAoID := initVertFrag(lShader.VertexProgram, lShader.GeometryProgram, lShader.FragmentProgram)
else
gShader.programAoID := initVertFrag('','', kAoShaderFrag);
//showmessage(AppDir+'tracks.glsl');
gShader.programTrackID := initVertFrag(kTrackShaderVert,'', kTrackShaderFrag);
{$ENDIF}
initFrame(gShader.f1);
initFrame(gShader.f2);
initFrame(gShader.fScreenShot);
end;
//glGetError(); //clear errors
if (length(gShader.VertexProgram) > 0) then begin
glUseProgram(0);
glDeleteProgram(gShader.program3dx);
//glGetError(); //clear error
//glProgramParameteriEXT( gShader.program3d, GL_GEOMETRY_VERTICES_OUT_EXT, 3 );
gShader.program3dx := initVertFrag(gShader.VertexProgram, gShader.GeometryProgram, gShader.FragmentProgram);
if (gShader.program3dx = 0) then //failed to load custom shader - use default
gShader.program3dx := initVertFrag(kVert3d, '', kFrag3d);
gShader.VertexProgram := '';
gShader.GeometryProgram := '';
gShader.FragmentProgram := '';
glUseProgram(0);
{$IFDEF COREGL}
(*glDeleteProgram(gShader.programAoID);
if gShader.isToonAO then
gShader.programAoID := initVertFrag(kAoShaderVert, '', kAoShaderFragToon)