-
Notifications
You must be signed in to change notification settings - Fork 12
/
bgraopengl3d.pas
1051 lines (892 loc) · 33.8 KB
/
bgraopengl3d.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
{ ***************************************************************************
* *
* This file is part of BGRABitmap library which is distributed under the *
* modified LGPL. *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
************************* BGRABitmap library ******************************
- Drawing routines with transparency and antialiasing with Lazarus.
Offers also various transforms.
- These routines allow to manipulate 32bit images in BGRA format or RGBA
format (depending on the platform).
- This code is under modified LGPL (see COPYING.modifiedLGPL.txt).
This means that you can link this library inside your programs for any purpose.
Only the included part of the code must remain LGPL.
- If you make some improvements to this library, please notify here:
http://www.lazarus.freepascal.org/index.php/topic,12037.0.html
********************* Contact : Circular at operamail.com *******************
******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | [email protected]
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRAOpenGL3D;
{$i bgrabitmap.inc}{$H+}
interface
uses {$IFNDEF FPC}Types, BGRAGraphics, GraphType, FPImage, {$ENDIF} BGRATypes, BGRABitmapTypes,
BGRASceneTypes, BGRASSE,
Classes, BGRAMatrix3D,
BGRACanvasGL,
BGRAScene3D,
BGRAOpenGLType,
BGRATransform,
BGRARenderer3D;
type
TAttributeVariable = BGRACanvasGL.TAttributeVariable;
TBGLShader3D = class;
{ TBGLLighting3D }
TBGLLighting3D = class
private
procedure SetUseBuiltIn(AValue: boolean);
protected
FCanvas: TBGLCustomCanvas;
FLights: TList;
FAmbiantLight: TColorF;
FShaderLightingCode: string;
FUseBuiltIn: boolean;
procedure Init;
public
constructor Create(ACanvas: TBGLCustomCanvas; AAmbiantLight: TColorF; ALights: TList);
procedure SetSpecularIndex(AIndex: Integer);
destructor Destroy; override;
property ShaderLightingCode: string read FShaderLightingCode;
property UseOpenGLBuiltInLighting: boolean read FUseBuiltIn write SetUseBuiltIn;
end;
{ TBGLRenderer3D }
TBGLRenderer3D = class(TCustomRenderer3D)
protected
FCanvas: TBGLCustomCanvas;
FHasZBuffer: Boolean;
FGlobalScale: single;
FOptions: TRenderingOptions;
FFactorZ, FAddZ: single;
FLightingGL: TBGLLighting3D;
FLights: TList;
FAmbiantLight: TColorF;
FFar: single;
FOldCulling: TFaceCulling;
FOldMatrix: TAffineMatrix;
FOldProjection, FProjectionMatrix: TMatrix4D;
FShader, FShaderWithTexture: TBGLCustomShader;
FBGRAShader: TBGRAShader3D;
FShadedColorsF: {$IFDEF BDS}arrayofTColorF{$ELSE}array of TColorF{$ENDIF};
FShadedColors: array of TBGRAPixel;
function GetHasZBuffer: boolean; override;
function GetGlobalScale: single; override;
function GetSurfaceWidth: integer; override;
function GetSurfaceHeight: integer; override;
procedure SetProjection(const AValue: TProjection3D); override;
function GetHandlesNearClipping: boolean; override;
function GetHandlesFaceCulling: boolean; override;
procedure InitLighting(AUseOpenGLBuiltInLighting: boolean);
public
constructor Create(ACanvas: TBGLCustomCanvas;
AScene: TBGRAScene3D; AFar: single);
function RenderFace(var ADescription: TFaceRenderingDescription;
{%H-}AComputeCoordinate: TComputeProjectionFunc): boolean; override;
destructor Destroy; override;
property Canvas: TBGLCustomCanvas read FCanvas;
end;
{ TBGLScene3D }
TBGLScene3D = class(TBGRAScene3D)
protected
function LoadBitmapFromFileUTF8(AFilenameUTF8: string): TBGRACustomBitmap; override;
public
procedure RenderGL(ACanvas: TBGLCustomCanvas; AMaxZ: single = 1000); virtual;
end;
{ TUniformVariable }
TUniformVariable = object
private
FProgram: TBGLShader3D;
FVariable: BGRADWord;
procedure Init(AProgram: TBGLShader3D; AVariable: BGRADWord);
end;
{ TUniformVariableSingle }
TUniformVariableSingle = object(TUniformVariable)
private
FValue: single;
procedure SetValue(const AValue: single);
public
procedure Update;
property Value: single read FValue write SetValue;
end;
{ TUniformVariablePointF }
TUniformVariablePointF = object(TUniformVariable)
private
FValue: TPointF;
procedure SetValue(const AValue: TPointF);
public
procedure Update;
property Value: TPointF read FValue write SetValue;
end;
{ TUniformVariablePoint3D }
TUniformVariablePoint3D = object(TUniformVariable)
private
FValue: TPoint3D;
procedure SetValue(const AValue: TPoint3D);
public
procedure Update;
property Value: TPoint3D read FValue write SetValue;
end;
{ TUniformVariableInteger }
TUniformVariableInteger = object(TUniformVariable)
private
FValue: Integer;
procedure SetValue(const AValue: Integer);
public
procedure Update;
property Value: Integer read FValue write SetValue;
end;
{ TUniformVariablePoint }
TUniformVariablePoint = object(TUniformVariable)
private
FValue: TPoint;
procedure SetValue(const AValue: TPoint);
public
procedure Update;
property Value: TPoint read FValue write SetValue;
end;
{ TUniformVariableMatrix4D }
TUniformVariableMatrix4D = object(TUniformVariable)
private
FValue: TMatrix4D;
procedure SetValue(const AValue: TMatrix4D);
public
procedure Update;
property Value: TMatrix4D read FValue write SetValue;
end;
{ TAttributeVariableSingle }
TAttributeVariableSingle = object(TAttributeVariable)
protected
procedure Init(AProgram: TObject; AAttribute: BGRADWord);
end;
{ TAttributeVariablePointF }
TAttributeVariablePointF = object(TAttributeVariable)
protected
procedure Init(AProgram: TObject; AAttribute: BGRADWord);
end;
{ TAttributeVariablePoint3D }
TAttributeVariablePoint3D = object(TAttributeVariable)
protected
procedure Init(AProgram: TObject; AAttribute: BGRADWord);
end;
{ TAttributeVariableInteger }
TAttributeVariableInteger = object(TAttributeVariable)
protected
procedure Init(AProgram: TObject; AAttribute: BGRADWord);
end;
{ TAttributeVariablePoint }
TAttributeVariablePoint = object(TAttributeVariable)
protected
procedure Init(AProgram: TObject; AAttribute: BGRADWord);
end;
{ TBGLShader3D }
TBGLShader3D = class(TBGLCustomShader)
protected
FUsed: boolean;
FCanvas: TBGLCustomCanvas;
FLighting: TBGLCustomLighting;
FVertexShaderSource,
FFragmentShaderSource: string;
FVertexShader,
FFragmentShader,
FProgram: BGRADWord;
function GetUniformVariableSingle(AName: string): TUniformVariableSingle;
function GetUniformVariablePointF(AName: string): TUniformVariablePointF;
function GetUniformVariablePoint3D(AName: string): TUniformVariablePoint3D;
function GetUniformVariableInteger(AName: string): TUniformVariableInteger;
function GetUniformVariablePoint(AName: string): TUniformVariablePoint;
function GetUniformVariableMatrix4D(AName: string): TUniformVariableMatrix4D;
function GetAttributeVariableInteger(AName: string): TAttributeVariableInteger;
function GetAttributeVariablePoint(AName: string): TAttributeVariablePoint;
function GetAttributeVariableSingle(AName: string): TAttributeVariableSingle;
function GetAttributeVariablePointF(AName: string): TAttributeVariablePointF;
function GetAttributeVariablePoint3D(AName: string): TAttributeVariablePoint3D;
procedure SetUniformSingle(AVariable: BGRADWord; const AValue; AElementCount: integer; AComponentCount: integer);
procedure SetUniformInteger(AVariable: BGRADWord; const AValue; AElementCount: integer; AComponentCount: integer);
procedure CheckUsage(AUsing: boolean);
procedure StartUse; override;
procedure EndUse; override;
property Canvas: TBGLCustomCanvas read FCanvas;
public
constructor Create(ACanvas: TBGLCustomCanvas; AVertexShaderSource: string;
AFragmentShaderSource: string; AVaryingVariables: string = '';
AVersion: string = '120');
destructor Destroy; override;
property UniformSingle[AName: string]: TUniformVariableSingle read GetUniformVariableSingle;
property UniformPointF[AName: string]: TUniformVariablePointF read GetUniformVariablePointF;
property UniformPoint3D[AName: string]: TUniformVariablePoint3D read GetUniformVariablePoint3D;
property UniformInteger[AName: string]: TUniformVariableInteger read GetUniformVariableInteger;
property UniformPoint[AName: string]: TUniformVariablePoint read GetUniformVariablePoint;
property UniformMatrix4D[AName: string]: TUniformVariableMatrix4D read GetUniformVariableMatrix4D;
property AttributeSingle[AName: string]: TAttributeVariableSingle read GetAttributeVariableSingle;
property AttributePointF[AName: string]: TAttributeVariablePointF read GetAttributeVariablePointF;
property AttributePoint3D[AName: string]: TAttributeVariablePoint3D read GetAttributeVariablePoint3D;
property AttributeInteger[AName: string]: TAttributeVariableInteger read GetAttributeVariableInteger;
property AttributePoint[AName: string]: TAttributeVariablePoint read GetAttributeVariablePoint;
property IsUsed: boolean read FUsed;
end;
function ProjectionToOpenGL(AProj: TProjection3D; ANear, AFar: Single): TMatrix4D;
implementation
uses SysUtils, BGRAColorInt;
type
{ TShaderWithTexture }
TShaderWithTexture = class(TBGLShader3D)
private
function GetTexture: integer;
procedure SetTexture(AValue: integer);
protected
FTextureUniform: TUniformVariableInteger;
procedure StartUse; override;
public
class function GetCodeForTextureColor: string;
constructor Create(ACanvas: TBGLCustomCanvas; AFragmentShader: string; ATexture: integer = 0);
property Texture: integer read GetTexture write SetTexture;
end;
function ProjectionToOpenGL(AProj: TProjection3D; ANear, AFar: Single): TMatrix4D;
begin
result[1,1] := AProj.Zoom.X; result[2,1] := 0; result[3,1] := -(AProj.Center.x + 0.5); result[4,1] := 0;
result[1,2] := 0; result[2,2] := AProj.Zoom.Y; result[3,2] := -(AProj.Center.y + 0.5); result[4,2] := 0;
result[1,3] := 0; result[2,3] := 0; result[3,3] := -2/(AFar-ANear); result[4,3] := -1 - AFar*result[3,3];
result[1,4] := 0; result[2,4] := 0; result[3,4] := -1; result[4,4] := 0;
end;
{ TUniformVariableMatrix4D }
procedure TUniformVariableMatrix4D.SetValue(const AValue: TMatrix4D);
begin
if CompareMem(@AValue, @FValue, sizeof(FValue)) then Exit;
FValue:=AValue;
if FProgram.IsUsed then Update;
end;
procedure TUniformVariableMatrix4D.Update;
begin
FProgram.SetUniformSingle(FVariable, FValue, 1, 16);
end;
{ TShaderWithTexture }
function TShaderWithTexture.GetTexture: integer;
begin
result := FTextureUniform.Value;
end;
procedure TShaderWithTexture.SetTexture(AValue: integer);
begin
FTextureUniform.Value := AValue;
end;
procedure TShaderWithTexture.StartUse;
begin
inherited StartUse;
FTextureUniform.Update;
end;
class function TShaderWithTexture.GetCodeForTextureColor: string;
begin
result := 'texture2D(texture, texture_coordinate)';
end;
constructor TShaderWithTexture.Create(ACanvas: TBGLCustomCanvas;
AFragmentShader: string; ATexture: integer);
begin
inherited Create(ACanvas,
'void main(void) ' +
'{ ' +
' gl_Position = gl_ProjectionMatrix * gl_Vertex; ' +
' texture_coordinate = vec2(gl_MultiTexCoord0); ' +
' N = gl_Normal; ' +
' V = vec3(gl_Vertex); ' +
'} ',
'uniform sampler2D texture; ' +
AFragmentShader,
'varying vec2 texture_coordinate; ' +
'varying vec3 N; ' +
'varying vec3 V; ');
FTextureUniform := UniformInteger['texture'];
Texture := ATexture;
end;
{ TAttributeVariablePoint3D }
procedure TAttributeVariablePoint3D.Init(AProgram: TObject; AAttribute: BGRADWord);
begin
inherited Init(AProgram,AAttribute,3,True);
end;
{ TAttributeVariablePointF }
procedure TAttributeVariablePointF.Init(AProgram: TObject; AAttribute: BGRADWord);
begin
inherited Init(AProgram,AAttribute,2,True);
end;
{ TAttributeVariableInteger }
procedure TAttributeVariableInteger.Init(AProgram: TObject; AAttribute: BGRADWord);
begin
inherited Init(AProgram,AAttribute,1,False);
end;
{ TAttributeVariablePoint }
procedure TAttributeVariablePoint.Init(AProgram: TObject; AAttribute: BGRADWord);
begin
inherited Init(AProgram,AAttribute,2,False);
end;
{ TAttributeVariableSingle }
procedure TAttributeVariableSingle.Init(AProgram: TObject; AAttribute: BGRADWord);
begin
inherited Init(AProgram,AAttribute,1,True);
end;
{ TUniformVariablePoint }
procedure TUniformVariablePoint.SetValue(const AValue: TPoint);
begin
if (FValue.x=AValue.x) and (FValue.y=AValue.y) then Exit;
FValue:=AValue;
if FProgram.IsUsed then Update;
end;
procedure TUniformVariablePoint.Update;
begin
FProgram.SetUniformInteger(FVariable, FValue, 1, 2);
end;
{ TUniformVariableInteger }
procedure TUniformVariableInteger.SetValue(const AValue: Integer);
begin
if FValue=AValue then Exit;
FValue:=AValue;
if FProgram.IsUsed then Update;
end;
procedure TUniformVariableInteger.Update;
begin
FProgram.SetUniformInteger(FVariable, FValue, 1, 1);
end;
{ TUniformVariablePoint3D }
procedure TUniformVariablePoint3D.SetValue(const AValue: TPoint3D);
begin
if (FValue.x=AValue.x) and (FValue.y=AValue.y) and (FValue.z=AValue.z) then Exit;
FValue:=AValue;
if FProgram.IsUsed then Update;
end;
procedure TUniformVariablePoint3D.Update;
begin
FProgram.SetUniformSingle(FVariable, FValue, 1, 3);
end;
{ TUniformVariablePointF }
procedure TUniformVariablePointF.SetValue(const AValue: TPointF);
begin
if (FValue.x=AValue.x) and (FValue.y=AValue.y) then Exit;
FValue:=AValue;
if FProgram.IsUsed then Update;
end;
procedure TUniformVariablePointF.Update;
begin
FProgram.SetUniformSingle(FVariable, FValue, 1, 2);
end;
{ TUniformVariableSingle }
procedure TUniformVariableSingle.SetValue(const AValue: single);
begin
if FValue=AValue then Exit;
FValue:=AValue;
if FProgram.IsUsed then Update;
end;
procedure TUniformVariableSingle.Update;
begin
FProgram.SetUniformSingle(FVariable, FValue, 1, 1);
end;
{ TUniformVariable }
procedure TUniformVariable.Init(AProgram: TBGLShader3D; AVariable: BGRADWord);
begin
FProgram := AProgram;
FVariable := AVariable;
end;
{ TBGLShader3D }
function TBGLShader3D.GetUniformVariableSingle(AName: string): TUniformVariableSingle;
begin
{$IFDEF FPC}{$PUSH}{$ENDIF}{$hints off}
fillchar(result,sizeof(result),0);
result.Init(self, FCanvas.Lighting.GetUniformVariable(FProgram, AName));
{$IFDEF FPC}{$POP}{$ENDIF}
end;
function TBGLShader3D.GetUniformVariablePointF(AName: string): TUniformVariablePointF;
begin
{$IFDEF FPC}{$PUSH}{$ENDIF}{$hints off}
fillchar(result,sizeof(result),0);
result.Init(self, FCanvas.Lighting.GetUniformVariable(FProgram, AName));
{$IFDEF FPC}{$POP}{$ENDIF}
end;
function TBGLShader3D.GetUniformVariablePoint3D(AName: string): TUniformVariablePoint3D;
begin
{$IFDEF FPC}{$PUSH}{$ENDIF}{$hints off}
fillchar(result,sizeof(result),0);
result.Init(self, FCanvas.Lighting.GetUniformVariable(FProgram, AName));
{$IFDEF FPC}{$POP}{$ENDIF}
end;
function TBGLShader3D.GetUniformVariableInteger(AName: string): TUniformVariableInteger;
begin
{$IFDEF FPC}{$PUSH}{$ENDIF}{$hints off}
fillchar(result,sizeof(result),0);
result.Init(self, FCanvas.Lighting.GetUniformVariable(FProgram, AName));
{$IFDEF FPC}{$POP}{$ENDIF}
end;
function TBGLShader3D.GetUniformVariablePoint(AName: string): TUniformVariablePoint;
begin
{$IFDEF FPC}{$PUSH}{$ENDIF}{$hints off}
fillchar(result,sizeof(result),0);
result.Init(self, FCanvas.Lighting.GetUniformVariable(FProgram, AName));
{$IFDEF FPC}{$POP}{$ENDIF}
end;
function TBGLShader3D.GetUniformVariableMatrix4D(AName: string): TUniformVariableMatrix4D;
begin
{$IFDEF FPC}{$PUSH}{$ENDIF}{$hints off}
fillchar(result,sizeof(result),0);
result.Init(self, FCanvas.Lighting.GetUniformVariable(FProgram, AName));
{$IFDEF FPC}{$POP}{$ENDIF}
end;
procedure TBGLShader3D.CheckUsage(AUsing: boolean);
begin
if AUsing <> FUsed then
begin
if FUsed then raise exception.Create('Shader is in use') else
raise exception.Create('Shader is not in use');
end;
end;
function TBGLShader3D.GetAttributeVariableSingle(AName: string): TAttributeVariableSingle;
begin
{$IFDEF FPC}{$PUSH}{$ENDIF}{$hints off}
fillchar(result,sizeof(result),0);
result.Init(self, FCanvas.Lighting.GetAttribVariable(FProgram, AName));
{$IFDEF FPC}{$POP}{$ENDIF}
end;
function TBGLShader3D.GetAttributeVariablePointF(AName: string): TAttributeVariablePointF;
begin
{$IFDEF FPC}{$PUSH}{$ENDIF}{$hints off}
fillchar(result,sizeof(result),0);
result.Init(self, FCanvas.Lighting.GetAttribVariable(FProgram, AName));
{$IFDEF FPC}{$POP}{$ENDIF}
end;
function TBGLShader3D.GetAttributeVariablePoint3D(AName: string): TAttributeVariablePoint3D;
begin
{$IFDEF FPC}{$PUSH}{$ENDIF}{$hints off}
fillchar(result,sizeof(result),0);
result.Init(self, FCanvas.Lighting.GetAttribVariable(FProgram, AName));
{$IFDEF FPC}{$POP}{$ENDIF}
end;
function TBGLShader3D.GetAttributeVariableInteger(AName: string): TAttributeVariableInteger;
begin
{$IFDEF FPC}{$PUSH}{$ENDIF}{$hints off}
fillchar(result,sizeof(result),0);
result.Init(self, FCanvas.Lighting.GetAttribVariable(FProgram, AName));
{$IFDEF FPC}{$POP}{$ENDIF}
end;
function TBGLShader3D.GetAttributeVariablePoint(AName: string): TAttributeVariablePoint;
begin
{$IFDEF FPC}{$PUSH}{$ENDIF}{$hints off}
fillchar(result,sizeof(result),0);
result.Init(self, FCanvas.Lighting.GetAttribVariable(FProgram, AName));
{$IFDEF FPC}{$POP}{$ENDIF}
end;
procedure TBGLShader3D.SetUniformSingle(AVariable: BGRADWord; const AValue; AElementCount: integer; AComponentCount: integer);
begin
CheckUsage(True);
FCanvas.Lighting.SetUniformSingle(AVariable, AValue, AElementCount, AComponentCount);
end;
procedure TBGLShader3D.SetUniformInteger(AVariable: BGRADWord; const AValue; AElementCount: integer; AComponentCount: integer);
begin
CheckUsage(True);
FCanvas.Lighting.SetUniformInteger(AVariable, AValue, AElementCount, AComponentCount);
end;
constructor TBGLShader3D.Create(ACanvas: TBGLCustomCanvas;
AVertexShaderSource: string; AFragmentShaderSource: string;
AVaryingVariables: string; AVersion: string);
begin
FCanvas := ACanvas;
FLighting := FCanvas.Lighting;
FVertexShaderSource:= '#version ' + AVersion + #10 + AVaryingVariables + #10 + AVertexShaderSource;
FFragmentShaderSource:= '#version ' + AVersion + #10 + AVaryingVariables + #10 + AFragmentShaderSource;
FVertexShader := 0;
FFragmentShader := 0;
FProgram := 0;
try
FVertexShader := FLighting.MakeVertexShader(FVertexShaderSource);
FFragmentShader := FLighting.MakeFragmentShader(FFragmentShaderSource);
FProgram := FLighting.MakeShaderProgram(FVertexShader,FFragmentShader);
except on ex:Exception do
begin
FLighting.DeleteShaderProgram(FProgram);
FLighting.DeleteShaderObject(FFragmentShader);
FLighting.DeleteShaderObject(FVertexShader);
raise ex;
end;
end;
end;
destructor TBGLShader3D.Destroy;
begin
if IsUsed then raise exception.Create('Shader is still in use');
inherited Destroy;
end;
procedure TBGLShader3D.StartUse;
begin
CheckUsage(False);
FLighting.UseProgram(FProgram);
FUsed:= True;
end;
procedure TBGLShader3D.EndUse;
begin
CheckUsage(True);
FLighting.UseProgram(0);
FUsed:= False;
end;
{ TBGLLighting3D }
procedure TBGLLighting3D.SetUseBuiltIn(AValue: boolean);
begin
if FUseBuiltIn=AValue then Exit;
FUseBuiltIn:=AValue;
FCanvas.Lighting.BuiltInLightingEnabled := FUseBuiltIn;
end;
procedure TBGLLighting3D.Init;
var
i: Integer;
v: TPoint3D;
int: single;
num: string;
minInt: string;
colorMult: TColorF;
begin
FShaderLightingCode:=
'void main(void) ' +
'{ ' +
' vec3 L, H; float d; float sat, sumUnsat; vec4 color, clampedColor; vec4 unsat; ' +
' vec3 Idiff = vec3(gl_LightModel.ambient); ' +
' vec4 Ispec = vec4(0,0,0,0); ' +
' vec3 NN = normalize(N); ';
with FCanvas.Lighting do
begin
AmbiantLightF := FAmbiantLight;
for i := 0 to FLights.Count-1 do
with TBGRALight3D(FLights[i]) do
begin
str(GetMinIntensity,minInt);
if IsDirectional then
begin
v := -GetDirection;
v.z := -v.z;
num := IntToStr(AddDirectionalLight(GetColorF, v));
str(GetMinIntensity,minInt);
FShaderLightingCode := FShaderLightingCode +
' L = gl_LightSource['+num+'].position.xyz; ' +
' Idiff = Idiff + (vec3(gl_LightSource['+num+'].diffuse * max(dot(NN,L), '+minInt+') )); ' +
' if (gl_FrontMaterial.shininess > 0) { ' +
' H = normalize(L + vec3(0,0,1)); ' +
' Ispec = Ispec +(gl_LightSource['+num+'].specular * pow(abs(dot(NN,H)), gl_FrontMaterial.shininess*2)); ' +
' } ';
end
else
begin
int := GetIntensity*0.75;
if int > 0 then
begin
v := GetPosition;
v.z := -v.z;
colorMult := ColorFMult(GetColorF, ColorF(int,int,int,1));
num := IntToStr(AddPointLight(colorMult, v, 0,1));
str(GetMinIntensity/int,minInt);
FShaderLightingCode := FShaderLightingCode +
' L = (gl_LightSource['+num+'].position.xyz - V).xyz; ' +
' d = length(L); ' +
' L = L *(1/d); ' +
' Idiff = Idiff + (vec3(gl_LightSource['+num+'].diffuse * max(dot(NN,L)/(d*d), '+minInt+') )); ' +
' if (gl_FrontMaterial.shininess > 0) { ' +
' H = normalize(L + vec3(0,0,1)); ' +
' Ispec = Ispec + (gl_LightSource['+num+'].specular * pow(abs(dot(NN,H))/(d*d), gl_FrontMaterial.shininess*2)); ' +
' } ';
end;
end;
end;
end;
FShaderLightingCode := FShaderLightingCode +
' color = #color# * vec4(Idiff,1) + Ispec; ' +
' clampedColor = clamp(color,0,1); ' +
' sat = dot( color - clampedColor, vec4(1) ); ' +
' if (sat > 0) { ' +
' unsat = vec4(1) - clampedColor; ' +
' sumUnsat = unsat[0]+unsat[1]+unsat[2]; ' +
' if (sumUnsat > 0) { ' +
' sat := sat * (max(max(unsat[0],unsat[1]),unsat[2]) / sumUnsat); ' +
' gl_FragColor = clamp(color + vec4(sat,sat,sat,0),0,1); ' +
' } ' +
' else gl_FragColor = clampedColor; ' +
' } ' +
' else gl_FragColor = clampedColor; ' +
'} ';
end;
constructor TBGLLighting3D.Create(ACanvas: TBGLCustomCanvas; AAmbiantLight: TColorF; ALights: TList);
begin
FCanvas := ACanvas;
FLights := ALights;
FAmbiantLight := AAmbiantLight;
Init;
end;
procedure TBGLLighting3D.SetSpecularIndex(AIndex: Integer);
begin
FCanvas.Lighting.SetSpecularIndex(AIndex);
end;
destructor TBGLLighting3D.Destroy;
begin
FCanvas.Lighting.SetSpecularIndex(0);
FCanvas.Lighting.ClearLights;
UseOpenGLBuiltInLighting := false;
inherited Destroy;
end;
{ TBGLScene3D }
function TBGLScene3D.LoadBitmapFromFileUTF8(AFilenameUTF8: string
): TBGRACustomBitmap;
begin
if BGLBitmapFactory <> nil then
Result:= BGLBitmapFactory.Create(AFilenameUTF8,True)
else
result := inherited LoadBitmapFromFileUTF8(AFilenameUTF8);
end;
procedure TBGLScene3D.RenderGL(ACanvas: TBGLCustomCanvas; AMaxZ: single);
var
renderer: TBGLRenderer3D;
begin
renderer := TBGLRenderer3D.Create(ACanvas, self, AMaxZ);
Render(renderer);
renderer.Free;
end;
{ TBGLRenderer3D }
function TBGLRenderer3D.GetHasZBuffer: boolean;
begin
result := FHasZBuffer;
end;
function TBGLRenderer3D.GetGlobalScale: single;
begin
result := FGlobalScale;
end;
function TBGLRenderer3D.GetSurfaceWidth: integer;
begin
result := FCanvas.Width;
end;
function TBGLRenderer3D.GetSurfaceHeight: integer;
begin
result := FCanvas.Height;
end;
{$IFDEF FPC}{$PUSH}{$ENDIF}{$OPTIMIZATION OFF} //avoid internal error 2012090607
procedure TBGLRenderer3D.SetProjection(const AValue: TProjection3D);
begin
inherited SetProjection(AValue);
FProjectionMatrix := MatrizMult4D(ProjectionToOpenGL(AValue, FOptions.MinZ, FFar),
OrthoProjectionToOpenGL(0,0,FCanvas.Width,FCanvas.Height));
FCanvas.ProjectionMatrix := FProjectionMatrix;
end;
{$IFDEF FPC}{$POP}{$ENDIF}
function TBGLRenderer3D.GetHandlesNearClipping: boolean;
begin
result := true;
end;
function TBGLRenderer3D.GetHandlesFaceCulling: boolean;
begin
result := FShader <> nil;
end;
procedure TBGLRenderer3D.InitLighting(AUseOpenGLBuiltInLighting: boolean);
var
fragmentShaderCode: string;
begin
if not Assigned(FLightingGL) then
begin
FLightingGL := TBGLLighting3D.Create(FCanvas, FAmbiantLight, FLights);
if (FOptions.LightingInterpolation <> liLowQuality) and FCanvas.Lighting.SupportShaders then
begin
fragmentShaderCode := StringReplace(FLightingGL.ShaderLightingCode, '#color#', 'gl_Color', []);
FShader := FCanvas.Lighting.Shader[fragmentShaderCode];
if (FShader = nil) and FCanvas.Lighting.SupportShaders then
begin
FShader := TBGLShader3D.Create(FCanvas,
'void main(void) ' +
'{ ' +
' gl_Position = gl_ProjectionMatrix * gl_Vertex; ' +
' gl_FrontColor = gl_Color; ' +
' gl_BackColor = gl_Color; ' +
' N = gl_Normal; ' +
' V = vec3(gl_Vertex); ' +
'} ',
fragmentShaderCode,
'varying vec3 N; ' +
'varying vec3 V; ');
FCanvas.Lighting.Shader[fragmentShaderCode] := FShader;
end;
fragmentShaderCode := StringReplace(FLightingGL.ShaderLightingCode, '#color#', TShaderWithTexture.GetCodeForTextureColor, []);
FShaderWithTexture := FCanvas.Lighting.Shader[fragmentShaderCode];
if (FShaderWithTexture = nil) and FCanvas.Lighting.SupportShaders then
begin
FShaderWithTexture := TShaderWithTexture.Create(FCanvas, fragmentShaderCode, 0);
FCanvas.Lighting.Shader[fragmentShaderCode] := FShaderWithTexture;
end;
end else
begin
FLightingGL.UseOpenGLBuiltInLighting := AUseOpenGLBuiltInLighting;
if not AUseOpenGLBuiltInLighting then
FBGRAShader := TBGRAShader3D.Create(FAmbiantLight, FLights);
end;
end;
end;
constructor TBGLRenderer3D.Create(ACanvas: TBGLCustomCanvas;
AScene: TBGRAScene3D; AFar: single);
begin
FCanvas := ACanvas;
FOptions := AScene.RenderingOptions;
FLights := AScene.MakeLightList;
FAmbiantLight := AScene.AmbiantLightColorF;
FGlobalScale:= 1;
FHasZBuffer := FOptions.PerspectiveMode = pmZBuffer;
FFactorZ := -2/(FFar-FOptions.MinZ);
FAddZ := -1 - FFar*FFactorZ;
FFar := AFar;
if FHasZBuffer then ACanvas.StartZBuffer;
FOldCulling:= FCanvas.FaceCulling;
FOldMatrix := FCanvas.Matrix;
FCanvas.ResetTransform;
FOldProjection := FCanvas.ProjectionMatrix;
FCanvas.ProjectionMatrix := MatrixIdentity4D;
FShader := nil;
FShaderWithTexture := nil;
InitLighting(False);
end;
function TBGLRenderer3D.RenderFace(var ADescription: TFaceRenderingDescription;
AComputeCoordinate: TComputeProjectionFunc): boolean;
var
NormalCenter3D,PtCenter3D: TPoint3D_128;
ColorCenter: TBGRAPixel;
procedure ComputeCenter;
var j: BGRANativeInt;
begin
with ADescription do
begin
PtCenter3D := Point3D_128_Zero;
NormalCenter3D := Point3D_128_Zero;
for j := 0 to NbVertices-1 do
begin
PtCenter3D := PtCenter3D +Positions3D[j];
NormalCenter3D := NormalCenter3D +Normals3D[j];
end;
PtCenter3D := PtCenter3D *(1/NbVertices);
Normalize3D_128(NormalCenter3D);
ColorCenter := MergeBGRA({$IFDEF BDS}SliceDynArray(Colors{$ELSE}slice(Colors{$ENDIF},NbVertices));
end;
end;
var tex: IBGLTexture;
i,j: BGRANativeInt;
begin
result := true;
if not ProjectionDefined then
raise exception.Create('Projection must be defined before drawing faces');
If ADescription.Texture <> nil then
tex := ADescription.Texture.GetTextureGL as IBGLTexture
else
tex := nil;
with ADescription do
begin
if ADescription.Biface then
FCanvas.FaceCulling := fcNone
else
FCanvas.FaceCulling := fcKeepCW;
if ADescription.Material.GetSpecularOn then
FLightingGL.SetSpecularIndex(ADescription.Material.GetSpecularIndex)
else
FLightingGL.SetSpecularIndex(0);
if tex <> nil then
begin
FCanvas.Lighting.ActiveShader := FShaderWithTexture;
if Assigned(FBGRAShader) then
begin
FBGRAShader.Prepare(ADescription);
if length(FShadedColorsF) < NbVertices then
setlength(FShadedColorsF, NbVertices);
for i := 0 to NbVertices-1 do
FShadedColorsF[i] := BGRAToColorF(ColorIntToBGRA(FBGRAShader.Int65536Apply(Positions3D[i],Normals3D[i],BGRAWhite), true), false);
for i := 0 to NbVertices-1 do
Positions3D[i].z := -Positions3D[i].z;
if NbVertices = 3 then
tex.DrawTriangle({$IFDEF BDS}SSESliceDynArray(Positions3D{$ELSE}slice(Positions3D{$ENDIF},3),{$IFDEF BDS}SliceDynArray(TexCoords{$ELSE}slice(TexCoords{$ENDIF},3),{$IFDEF BDS}SliceDynArray(FShadedColorsF{$ELSE}slice(FShadedColorsF{$ENDIF},3))
else if NbVertices = 4 then
tex.DrawQuad({$IFDEF BDS}SSESliceDynArray(Positions3D{$ELSE}slice(Positions3D{$ENDIF},4),{$IFDEF BDS}SliceDynArray(TexCoords{$ELSE}slice(TexCoords{$ENDIF},4),{$IFDEF BDS}SliceDynArray(FShadedColorsF{$ELSE}slice(FShadedColorsF{$ENDIF},4));
end else
begin
for i := 0 to NbVertices-1 do
begin
Positions3D[i].z := -Positions3D[i].z;
Normals3D[i].z := -Normals3D[i].z;
end;
if NbVertices = 3 then
tex.DrawTriangle({$IFDEF BDS}SSESliceDynArray(Positions3D{$ELSE}slice(Positions3D{$ENDIF},3),{$IFDEF BDS}SSESliceDynArray(Normals3D{$ELSE}slice(Normals3D{$ENDIF},3),{$IFDEF BDS}SliceDynArray(TexCoords{$ELSE}slice(TexCoords{$ENDIF},3))
else if NbVertices = 4 then
tex.DrawQuad({$IFDEF BDS}SSESliceDynArray(Positions3D{$ELSE}slice(Positions3D{$ENDIF},4),{$IFDEF BDS}SSESliceDynArray(Normals3D{$ELSE}slice(Normals3D{$ENDIF},4),{$IFDEF BDS}SliceDynArray(TexCoords{$ELSE}slice(TexCoords{$ENDIF},4));
end;
end
else
begin
FCanvas.Lighting.ActiveShader := FShader;
if Assigned(FBGRAShader) then
begin
FBGRAShader.Prepare(ADescription);
if length(FShadedColors) < NbVertices then
setlength(FShadedColors, NbVertices);
for i := 0 to NbVertices-1 do
FShadedColors[i] := FBGRAShader.Apply(Positions3D[i],Normals3D[i],Colors[i]);
if NbVertices > 4 then
begin
ComputeCenter;
ColorCenter := FBGRAShader.Apply(PtCenter3D,NormalCenter3D,MergeBGRA({$IFDEF BDS}SliceDynArray(Colors{$ELSE}slice(Colors{$ENDIF},NbVertices)));
for i := 0 to NbVertices-1 do
Positions3D[i].z := -Positions3D[i].z;
PtCenter3D.z := -PtCenter3D.z;
i := NbVertices-1;
for j := 0 to NbVertices-1 do
begin
FCanvas.FillTrianglesLinearColor(
[Positions3D[i],Positions3D[j],PtCenter3D],
[FShadedColors[i],FShadedColors[j],ColorCenter]);
i := j;
end;
end else
begin
for i := 0 to NbVertices-1 do
Positions3D[i].z := -Positions3D[i].z;
if NbVertices = 3 then
FCanvas.FillTrianglesLinearColor({$IFDEF BDS}SSESliceDynArray(Positions3D{$ELSE}slice(Positions3D{$ENDIF},3),{$IFDEF BDS}SliceDynArray(FShadedColors{$ELSE}slice(FShadedColors{$ENDIF},3))