-
Notifications
You must be signed in to change notification settings - Fork 12
/
bgraopengl.pas
1731 lines (1472 loc) · 50 KB
/
bgraopengl.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 | mailedivando@gmail.com
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRAOpenGL;
{$i bgrabitmap.inc}{$H+}
interface
uses
Classes, SysUtils, FPImage, BGRATypes,{$IFNDEF FPC}Types, GraphType, {$ENDIF}BGRAGraphics,
BGRAOpenGLType, BGRASpriteGL, BGRACanvasGL, GL, GLext, GLU, BGRABitmapTypes,
BGRAFontGL, BGRASSE, BGRAMatrix3D;
type
TBGLCustomCanvas = BGRACanvasGL.TBGLCustomCanvas;
TBGLSprite = TBGLDefaultSprite;
IBGLTexture = BGRAOpenGLType.IBGLTexture;
IBGLFont = BGRAOpenGLType.IBGLFont;
IBGLRenderedFont = BGRAFontGL.IBGLRenderedFont;
TOpenGLResampleFilter = BGRAOpenGLType.TOpenGLResampleFilter;
TOpenGLBlendMode = BGRAOpenGLType.TOpenGLBlendMode;
TBGLPath = BGRACanvasGL.TBGLPath;
TWaitForGPUOption = BGRAOpenGLType.TWaitForGPUOption;
TBGLCustomElementArray = BGRACanvasGL.TBGLCustomElementArray;
TBGLCustomArray = BGRACanvasGL.TBGLCustomArray;
TOpenGLPrimitive = BGRAOpenGLType.TOpenGLPrimitive;
TTextLayout = BGRAGraphics.TTextLayout;
const
tlTop = BGRAGraphics.tlTop;
tlCenter = BGRAGraphics.tlCenter;
tlBottom = BGRAGraphics.tlBottom;
type
{ TBGLContext }
TBGLContext = object
private
function GetHeight: integer;
function GetWidth: integer;
public
Canvas: TBGLCustomCanvas;
Sprites: TBGLCustomSpriteEngine;
property Width: integer read GetWidth;
property Height: integer read GetHeight;
end;
{ TBGLFrameBuffer }
TBGLFrameBuffer = class(TBGLCustomFrameBuffer)
protected
FHeight: integer;
FMatrix: TAffineMatrix;
FProjectionMatrix: TMatrix4D;
FTexture: IBGLTexture;
FFrameBufferId, FRenderBufferId: GLuint;
FWidth: integer;
FSettingMatrices: boolean;
function GetTexture: IBGLTexture; override;
function GetHandle: pointer; override;
function GetHeight: integer; override;
function GetMatrix: TAffineMatrix; override;
function GetProjectionMatrix: TMatrix4D; override;
function GetWidth: integer; override;
procedure SetMatrix(AValue: TAffineMatrix); override;
procedure SetProjectionMatrix(AValue: TMatrix4D); override;
public
constructor Create(AWidth,AHeight: integer);
function MakeTextureAndFree: IBGLTexture; override;
destructor Destroy; override;
end;
const
orfBox = BGRAOpenGLType.orfBox;
orfLinear = BGRAOpenGLType.orfLinear;
obmNormal = BGRAOpenGLType.obmNormal;
obmAdd = BGRAOpenGLType.obmAdd;
obmMultiply = BGRAOpenGLType.obmMultiply;
wfgQueueAllCommands = BGRAOpenGLType.wfgQueueAllCommands;
wfgFinishAllCommands = BGRAOpenGLType.wfgFinishAllCommands;
opPoints = BGRAOpenGLType.opPoints;
opLineStrip = BGRAOpenGLType.opLineStrip;
opLineLoop = BGRAOpenGLType.opLineLoop;
opLines = BGRAOpenGLType.opLines;
opTriangleStrip = BGRAOpenGLType.opTriangleStrip;
opTriangleFan = BGRAOpenGLType.opTriangleFan;
opTriangles = BGRAOpenGLType.opTriangles;
type
{ TBGLBitmap }
TBGLBitmap = class(TBGLCustomBitmap)
protected
function GetOpenGLMaxTexSize: integer; override;
end;
function BGLTexture(ARGBAData: PBGRADWord; AllocatedWidth,AllocatedHeight, ActualWidth,ActualHeight: integer): IBGLTexture; overload;
function BGLTexture(AFPImage: TFPCustomImage): IBGLTexture; overload;
function BGLTexture(ABitmap: TBitmap): IBGLTexture; overload;
function BGLTexture(AWidth, AHeight: integer; Color: TColor): IBGLTexture; overload;
function BGLTexture(AWidth, AHeight: integer; Color: TBGRAPixel): IBGLTexture; overload;
function BGLTexture(AFilenameUTF8: string): IBGLTexture; overload;
function BGLTexture(AFilenameUTF8: string; AWidth, AHeight: integer; AResampleFilter: TResampleFilter = rfBox): IBGLTexture; overload;
function BGLTexture(AStream: TStream): IBGLTexture; overload;
function BGLSpriteEngine: TBGLCustomSpriteEngine;
function BGLCanvas: TBGLCustomCanvas;
procedure BGLViewPort(AWidth,AHeight: integer); overload;
procedure BGLViewPort(AWidth,AHeight: integer; AColor: TBGRAPixel); overload;
function BGLFont({%H-}AName: string; {%H-}AEmHeight: integer; {%H-}AStyle: TFontStyles = []): IBGLRenderedFont; overload;
function BGLFont({%H-}AName: string; {%H-}AEmHeight: integer; {%H-}AColor: TBGRAPixel; {%H-}AStyle: TFontStyles = []): IBGLRenderedFont; overload;
function BGLFont({%H-}AName: string; {%H-}AEmHeight: integer; {%H-}AColor: TBGRAPixel; {%H-}AOutlineColor: TBGRAPixel; {%H-}AStyle: TFontStyles = []): IBGLRenderedFont; overload;
function BGLFont({%H-}AName: string; {%H-}AEmHeight: integer; ARenderer: TBGRACustomFontRenderer; ARendererOwned: boolean = true): IBGLRenderedFont; overload;
type
{ TBGLElementArray }
TBGLElementArray = class(TBGLCustomElementArray)
protected
FElements: packed array of GLuint;
FBuffer: GLuint;
function GetCount: integer; override;
public
constructor Create(const AElements: array of integer); override;
procedure Draw(ACanvas: TBGLCustomCanvas; APrimitive: TOpenGLPrimitive; AAttributes: array of TAttributeVariable); override;
destructor Destroy; override;
end;
{ TBGLArray }
TBGLArray = class(TBGLCustomArray)
protected
FBufferAddress: pointer;
FCount: integer;
FRecordSize: integer;
function GetCount: integer; override;
function GetRecordSize: integer; override;
public
constructor Create(ABufferAddress: Pointer; ACount: integer; ARecordSize: integer); override;
destructor Destroy; override;
end;
implementation
uses BGRABlurGL, BGRATransform{$IFDEF BGRABITMAP_USE_LCL}, BGRAText, BGRATextFX{$ENDIF};
type
TBlendFuncSeparateProc = procedure(sfactorRGB: GLenum; dfactorRGB: GLenum; sfactorAlpha: GLenum; dfactorAlpha: GLenum); {$IFDEF Windows} stdcall; {$ELSE} cdecl; {$ENDIF}
function PrimitiveToOpenGL(AValue: TOpenGLPrimitive): GLenum;
begin
case AValue of
opPoints: result := GL_POINTS;
opLineStrip: result := GL_LINE_STRIP;
opLineLoop: result := GL_LINE_LOOP;
opLines: result := GL_LINES;
opTriangleStrip: result := GL_TRIANGLE_STRIP;
opTriangleFan: result := GL_TRIANGLE_FAN;
opTriangles: result := GL_TRIANGLES;
else
raise exception.Create('Unknown primitive type');
end;
end;
procedure NeedOpenGL2_0;
begin
if @glUseProgram = nil then
begin
if not Load_GL_version_2_0 then
raise exception.Create('Cannot load OpenGL 2.0');
end;
end;
function CheckOpenGL2_0: boolean;
begin
if @glUseProgram = nil then
begin
result := Load_GL_version_2_0;
end
else
result := true;
end;
var
BGLCanvasInstance: TBGLCustomCanvas;
glBlendFuncSeparate: TBlendFuncSeparateProc;
glBlendFuncSeparateFetched: boolean;
const
GL_COMBINE_ARB = $8570;
GL_COMBINE_RGB_ARB = $8571;
GL_SOURCE0_RGB_ARB = $8580;
GL_PRIMARY_COLOR_ARB = $8577;
type
{ TBGLTexture }
TBGLTexture = class(TBGLCustomTexture)
protected
FFlipX,FFlipY: Boolean;
function GetOpenGLMaxTexSize: integer; override;
function CreateOpenGLTexture(ARGBAData: PBGRADWord; AAllocatedWidth, AAllocatedHeight, AActualWidth, AActualHeight: integer; RGBAOrder: boolean): TBGLTextureHandle; override;
procedure UpdateOpenGLTexture(ATexture: TBGLTextureHandle; ARGBAData: PBGRADWord; AAllocatedWidth, AAllocatedHeight, AActualWidth,AActualHeight: integer; RGBAOrder: boolean); override;
class function SupportsBGRAOrder: boolean; override;
procedure SetOpenGLTextureSize(ATexture: TBGLTextureHandle; AAllocatedWidth, AAllocatedHeight, AActualWidth, AActualHeight: integer); override;
procedure ComputeOpenGLFramesCoord(ATexture: TBGLTextureHandle; FramesX: Integer=1; FramesY: Integer=1); override;
function GetOpenGLFrameCount(ATexture: TBGLTextureHandle): integer; override;
function GetEmptyTexture: TBGLTextureHandle; override;
procedure FreeOpenGLTexture(ATexture: TBGLTextureHandle); override;
procedure UpdateGLResampleFilter(ATexture: TBGLTextureHandle; AFilter: TOpenGLResampleFilter); override;
procedure InternalSetColor(const AColor: TBGRAPixel);
procedure DoDrawTriangleOrQuad(const APoints: array of TPointF;
const APointsZ: array of Single; const APoints3D: array of TPoint3D_128;
const ANormals3D: array of TPoint3D_128; const ATexCoords: array of TPointF;
const AColors: array of TColorF); override;
procedure DoDraw(pt1,pt2,pt3,pt4: TPointF; AColor: TBGRAPixel);
procedure DoStretchDraw(x,y,w,h: single; AColor: TBGRAPixel); override;
procedure DoStretchDrawAngle(x,y,w,h,angleDeg: single; rotationCenter: TPointF; AColor: TBGRAPixel); override;
procedure DoDrawAffine(Origin, HAxis, VAxis: TPointF; AColor: TBGRAPixel); override;
procedure Init(ATexture: TBGLTextureHandle; AWidth,AHeight: integer; AOwned: boolean); override;
procedure NotifyInvalidFrameSize; override;
procedure NotifyErrorLoadingFile(AFilenameUTF8: string); override;
function NewEmpty: TBGLCustomTexture; override;
function NewFromTexture(ATexture: TBGLTextureHandle; AWidth,AHeight: integer): TBGLCustomTexture; override;
function Duplicate: TBGLCustomTexture; override;
public
procedure ToggleFlipX; override;
procedure ToggleFlipY; override;
procedure Bind(ATextureNumber: integer); override;
function FilterBlurMotion(ARadius: single; ABlurType: TRadialBlurType; ADirection: TPointF): IBGLTexture; override;
function FilterBlurRadial(ARadius: single; ABlurType: TRadialBlurType): IBGLTexture; override;
end;
POpenGLTexture = ^TOpenGLTexture;
TOpenGLTexture = record
ID: GLuint;
AllocatedWidth,AllocatedHeight,ActualWidth,ActualHeight: integer;
FramesCoord: array of array[0..3] of TPointF;
end;
{ TBGLCanvas }
TBGLCanvas = class(TBGLCustomCanvas)
protected
FMatrix: TAffineMatrix;
FProjectionMatrix: TMatrix4D;
FBlendMode: TOpenGLBlendMode;
FLighting: TBGLCustomLighting;
FFaceCulling: TFaceCulling;
function GetLighting: TBGLCustomLighting; override;
function GetMatrix: TAffineMatrix; override;
procedure SetMatrix(const AValue: TAffineMatrix); override;
function GetProjectionMatrix: TMatrix4D; override;
procedure SetProjectionMatrix(const AValue: TMatrix4D); override;
function GetFaceCulling: TFaceCulling; override;
procedure SetFaceCulling(AValue: TFaceCulling); override;
procedure InternalSetColor(const AColor: TBGRAPixel); override;
procedure InternalSetColorF(const AColor: TColorF); override;
procedure InternalStartPutPixel(const pt: TPointF); override;
procedure InternalStartPolyline(const pt: TPointF); override;
procedure InternalStartPolygon(const pt: TPointF); override;
procedure InternalStartTriangleFan(const pt: TPointF); override;
procedure InternalContinueShape(const pt: TPointF); overload; override;
procedure InternalContinueShape(const pt: TPoint3D); overload; override;
procedure InternalContinueShape(const pt: TPoint3D_128); overload; override;
procedure InternalContinueShape(const pt, normal: TPoint3D_128); overload; override;
procedure InternalEndShape; override;
procedure InternalStartBlend; override;
procedure InternalEndBlend; override;
procedure InternalStartBlendTriangles; override;
procedure InternalStartBlendQuads; override;
procedure InternalEndBlendTriangles; override;
procedure InternalEndBlendQuads; override;
procedure EnableScissor(AValue: TRect); override;
procedure DisableScissor; override;
function GetBlendMode: TOpenGLBlendMode; override;
procedure SetBlendMode(AValue: TOpenGLBlendMode); override;
procedure SetActiveFrameBuffer(AValue: TBGLCustomFrameBuffer); override;
public
destructor Destroy; override;
procedure Fill(AColor: TBGRAPixel); override;
procedure StartZBuffer; override;
procedure EndZBuffer; override;
procedure WaitForGPU(AOption: TWaitForGPUOption); override;
function GetImage(x, y, w, h: integer): TBGRACustomBitmap; override;
function CreateFrameBuffer(AWidth, AHeight: integer): TBGLCustomFrameBuffer; override;
end;
{ TBGLLighting }
TBGLLighting = class(TBGLCustomLighting)
protected
FLightUsage: array[0..7] of boolean;
FCurrentSpecularIndex: single;
FAmbiantLightF: TColorF;
FBuiltInLighting: boolean;
function MakeShaderObject(AShaderType: GLenum; ASource: string): GLuint;
function AddLight(AColor: TColorF): integer;
function GetSupportShaders: boolean; override;
procedure SetAmbiantLightF(AAmbiantLight: TColorF); override;
function GetAmbiantLightF: TColorF; override;
function GetBuiltInLightingEnabled: boolean; override;
procedure SetBuiltInLightingEnabled(AValue: boolean); override;
public
constructor Create;
function AddDirectionalLight(AColor: TColorF; ADirection: TPoint3D): integer; override;
function AddPointLight(AColor: TColorF; APosition: TPoint3D; ALinearAttenuation, AQuadraticAttenuation: single): integer; override;
procedure ClearLights; override;
function RemoveLight(AIndex: integer): boolean; override;
procedure SetSpecularIndex(AIndex: integer); override;
function MakeVertexShader(ASource: string): BGRADWord; override;
function MakeFragmentShader(ASource: string): BGRADWord; override;
function MakeShaderProgram(AVertexShader, AFragmentShader: BGRADWord): BGRADWord; override;
procedure UseProgram(AProgram: BGRADWord); override;
procedure DeleteShaderObject(AShader: BGRADWord); override;
procedure DeleteShaderProgram(AProgram: BGRADWord); override;
function GetUniformVariable(AProgram: BGRADWord; AName: string): BGRADWord; override;
function GetAttribVariable(AProgram: BGRADWord; AName: string): BGRADWord; override;
procedure SetUniformSingle(AVariable: BGRADWord; const AValue; AElementCount, AComponentCount: integer); override;
procedure SetUniformInteger(AVariable: BGRADWord; const AValue; AElementCount, AComponentCount: integer); override;
procedure BindAttribute(AAttribute: TAttributeVariable); override;
procedure UnbindAttribute(AAttribute: TAttributeVariable); override;
end;
{ TBGLFrameBuffer }
procedure TBGLFrameBuffer.SetMatrix(AValue: TAffineMatrix);
begin
if FSettingMatrices then Exit;
FSettingMatrices := true;
FMatrix:=AValue;
if FCanvas <> nil then
TBGLCustomCanvas(FCanvas).Matrix := AValue;
FSettingMatrices := false;
end;
function TBGLFrameBuffer.GetMatrix: TAffineMatrix;
begin
result := FMatrix;
end;
function TBGLFrameBuffer.GetTexture: IBGLTexture;
begin
result := FTexture.FlipY;
end;
function TBGLFrameBuffer.GetHandle: pointer;
begin
result := @FFrameBufferId;
end;
function TBGLFrameBuffer.GetHeight: integer;
begin
result := FHeight;
end;
function TBGLFrameBuffer.GetProjectionMatrix: TMatrix4D;
begin
result := FProjectionMatrix;
end;
function TBGLFrameBuffer.GetWidth: integer;
begin
result := FWidth;
end;
procedure TBGLFrameBuffer.SetProjectionMatrix(AValue: TMatrix4D);
begin
if FSettingMatrices then Exit;
FSettingMatrices := true;
FProjectionMatrix:= AValue;
if FCanvas <> nil then
TBGLCustomCanvas(FCanvas).ProjectionMatrix := AValue;
FSettingMatrices := false;
end;
constructor TBGLFrameBuffer.Create(AWidth, AHeight: integer);
var frameBufferStatus: GLenum;
begin
if not Load_GL_version_3_0 then
raise exception.Create('Cannot load OpenGL 3.0');
FWidth := AWidth;
FHeight := AHeight;
FTexture := BGLTextureFactory.Create(nil,AWidth,AHeight,AWidth,AHeight);
//depth and stencil
glGenRenderbuffers(1, @FRenderBufferId);
glBindRenderbuffer(GL_RENDERBUFFER, FRenderBufferId);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, AWidth,AHeight);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glGenFramebuffers(1, @FFrameBufferId);
glBindFramebuffer(GL_FRAMEBUFFER, FFrameBufferId);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, PGLuint(FTexture.Handle)^, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, FFrameBufferId);
frameBufferStatus:= glCheckFramebufferStatus(GL_FRAMEBUFFER);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
if frameBufferStatus <> GL_FRAMEBUFFER_COMPLETE then
begin
glDeleteFramebuffers(1, @FFrameBufferId);
glDeleteRenderbuffers(1, @FRenderBufferId);
FTexture := nil;
raise exception.Create('Error ' + inttostr(frameBufferStatus) + ' while initializing frame buffer');
end;
UseOrthoProjection;
Matrix := AffineMatrixIdentity;
end;
function TBGLFrameBuffer.MakeTextureAndFree: IBGLTexture;
begin
result := FTexture;
FTexture := nil;
Free;
end;
destructor TBGLFrameBuffer.Destroy;
begin
glDeleteFramebuffers(1, @FFrameBufferId);
glDeleteRenderbuffers(1, @FRenderBufferId);
FTexture := nil;
inherited Destroy;
end;
procedure ApplyBlendMode(ABlendMode: TOpenGLBlendMode);
var
srcBlend : BGRALongWord;
dstBlend : BGRALongWord;
begin
case ABlendMode of
obmAdd:
begin
srcBlend := GL_SRC_ALPHA;
dstBlend := GL_ONE;
end;
obmMultiply:
begin
srcBlend := GL_ZERO;
dstBlend := GL_SRC_COLOR;
end
else
begin
srcBlend := GL_SRC_ALPHA;
dstBlend := GL_ONE_MINUS_SRC_ALPHA;
end;
end;
if not glBlendFuncSeparateFetched then
begin
glBlendFuncSeparate := TBlendFuncSeparateProc(wglGetProcAddress('glBlendFuncSeparate'));
glBlendFuncSeparateFetched := true;
end;
if Assigned(glBlendFuncSeparate) then
glBlendFuncSeparate( srcBlend, dstBlend, GL_ONE, GL_ONE_MINUS_SRC_ALPHA )
else
glBlendFunc( srcBlend, dstBlend );
end;
function BGLTexture(ARGBAData: PBGRADWord; AllocatedWidth, AllocatedHeight,
ActualWidth, ActualHeight: integer): IBGLTexture;
begin
result := TBGLTexture.Create(ARGBAData,AllocatedWidth, AllocatedHeight,
ActualWidth, ActualHeight);
end;
function BGLTexture(AFPImage: TFPCustomImage): IBGLTexture;
begin
result := TBGLTexture.Create(AFPImage);
end;
function BGLTexture(ABitmap: TBitmap): IBGLTexture;
begin
result := TBGLTexture.Create(ABitmap);
end;
function BGLTexture(AWidth, AHeight: integer; Color: TColor): IBGLTexture;
begin
result := TBGLTexture.Create(AWidth,AHeight,Color);
end;
function BGLTexture(AWidth, AHeight: integer; Color: TBGRAPixel): IBGLTexture;
begin
result := TBGLTexture.Create(AWidth,AHeight,Color);
end;
function BGLTexture(AFilenameUTF8: string): IBGLTexture;
begin
result := TBGLTexture.Create(AFilenameUTF8);
end;
function BGLTexture(AFilenameUTF8: string; AWidth, AHeight: integer; AResampleFilter: TResampleFilter): IBGLTexture;
begin
result := TBGLTexture.Create(AFilenameUTF8, AWidth, AHeight, AResampleFilter);
end;
function BGLTexture(AStream: TStream): IBGLTexture;
begin
result := TBGLTexture.Create(AStream);
end;
function BGLSpriteEngine: TBGLCustomSpriteEngine;
begin
result := BGRASpriteGL.BGLSpriteEngine;
end;
procedure BGLViewPort(AWidth, AHeight: integer; AColor: TBGRAPixel);
begin
BGLViewPort(AWidth,AHeight);
BGLCanvas.Fill(AColor);
end;
function BGLFont(AName: string; AEmHeight: integer; AStyle: TFontStyles = []): IBGLRenderedFont;
begin
{$IFDEF BGRABITMAP_USE_LCL}
result := BGLFont(AName, AEmHeight, TLCLFontRenderer.Create);
result.Style := AStyle;
{$ELSE}
result := nil;
raise exception.Create('LCL renderer not available');
{$ENDIF}
end;
function BGLFont(AName: string; AEmHeight: integer; AColor: TBGRAPixel;
AStyle: TFontStyles): IBGLRenderedFont;
begin
{$IFDEF BGRABITMAP_USE_LCL}
result := BGLFont(AName, AEmHeight, TLCLFontRenderer.Create);
result.Color := AColor;
result.Style := AStyle;
{$ELSE}
result := nil;
raise exception.Create('LCL renderer not available');
{$ENDIF}
end;
function BGLFont(AName: string; AEmHeight: integer; AColor: TBGRAPixel;
AOutlineColor: TBGRAPixel; AStyle: TFontStyles = []): IBGLRenderedFont;
{$IFDEF BGRABITMAP_USE_LCL}
var renderer: TBGRATextEffectFontRenderer;
begin
renderer := TBGRATextEffectFontRenderer.Create;
renderer.OuterOutlineOnly:= true;
renderer.OutlineColor := AOutlineColor;
renderer.OutlineVisible := true;
result := BGLFont(AName, AEmHeight, renderer, true);
result.Color := AColor;
result.Style := AStyle;
end;
{$ELSE}
begin
result := nil;
raise exception.Create('LCL renderer not available');
end;
{$ENDIF}
function BGLFont(AName: string; AEmHeight: integer;
ARenderer: TBGRACustomFontRenderer;
ARendererOwned: boolean): IBGLRenderedFont;
var f: TBGLRenderedFont;
begin
f:= TBGLRenderedFont.Create(ARenderer, ARendererOwned);
f.Name := AName;
f.EmHeight := AEmHeight;
result := f;
end;
function BGLCanvas: TBGLCustomCanvas;
begin
result := BGLCanvasInstance;
end;
procedure BGLViewPort(AWidth, AHeight: integer);
begin
BGLCanvas.Width := AWidth;
BGLCanvas.Height := AHeight;
BGLCanvas.UseOrthoProjection;
BGLCanvas.Matrix := AffineMatrixIdentity;
BGLCanvas.FaceCulling := fcNone;
end;
{ TBGLArray }
function TBGLArray.GetCount: integer;
begin
result := FCount;
end;
function TBGLArray.GetRecordSize: integer;
begin
result := FRecordSize;
end;
constructor TBGLArray.Create(ABufferAddress: pointer; ACount: integer;
ARecordSize: integer);
var b: GLuint;
begin
NeedOpenGL2_0;
FBufferAddress:= ABufferAddress;
FCount := ACount;
FRecordSize:= ARecordSize;
glGenBuffers(1, @b);
FBuffer := b;
glBindBuffer(GL_ARRAY_BUFFER, FBuffer);
glBufferData(GL_ARRAY_BUFFER, FCount*FRecordSize, FBufferAddress, GL_STATIC_DRAW);
end;
destructor TBGLArray.Destroy;
var b: GLuint;
begin
b := FBuffer;
glDeleteBuffers(1, @b);
inherited Destroy;
end;
{ TBGLElementArray }
function TBGLElementArray.GetCount: integer;
begin
result := length(FElements);
end;
constructor TBGLElementArray.Create(const AElements: array of integer);
var bufferSize: integer;
i: BGRANativeInt;
begin
NeedOpenGL2_0;
setlength(FElements,length(AElements));
bufferSize := length(FElements)*sizeof(integer);
for i := 0 to high(FElements) do
FElements[i] := AElements[i];
glGenBuffers(1, @FBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, FBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, bufferSize, @FElements[0], GL_STATIC_DRAW);
end;
procedure TBGLElementArray.Draw(ACanvas: TBGLCustomCanvas; APrimitive: TOpenGLPrimitive; AAttributes: array of TAttributeVariable);
var
i: BGRANativeInt;
begin
for i := 0 to high(AAttributes) do
ACanvas.Lighting.BindAttribute(AAttributes[i]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, FBuffer);
glDrawElements(PrimitiveToOpenGL(APrimitive), Count, GL_UNSIGNED_INT, nil);
for i := 0 to high(AAttributes) do
ACanvas.Lighting.UnbindAttribute(AAttributes[i]);
end;
destructor TBGLElementArray.Destroy;
begin
glDeleteBuffers(1, @FBuffer);
inherited Destroy;
end;
{ TBGLLighting }
procedure TBGLLighting.SetAmbiantLightF(AAmbiantLight: TColorF);
begin
FAmbiantLightF := AAmbiantLight;
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, @AAmbiantLight);
end;
constructor TBGLLighting.Create;
begin
FAmbiantLightF := ColorF(1,1,1,1);
end;
function TBGLLighting.AddPointLight(AColor: TColorF; APosition: TPoint3D; ALinearAttenuation, AQuadraticAttenuation: single): integer;
var
v: TPoint3D_128;
begin
result := AddLight(AColor);
if result <> -1 then
begin
v := Point3D_128(APosition);
v.t := 1;
glLightfv(GL_LIGHT0 + result, GL_POSITION, @v);
glLightf(GL_LIGHT0 + result, GL_CONSTANT_ATTENUATION, 0);
glLightf(GL_LIGHT0 + result, GL_LINEAR_ATTENUATION, ALinearAttenuation);
glLightf(GL_LIGHT0 + result, GL_QUADRATIC_ATTENUATION, AQuadraticAttenuation);
end;
end;
procedure TBGLLighting.ClearLights;
var
i: Integer;
begin
for i := 0 to High(FLightUsage) do
if FLightUsage[i] then
RemoveLight(i);
end;
function TBGLLighting.AddDirectionalLight(AColor: TColorF; ADirection: TPoint3D): integer;
var
v: TPoint3D_128;
begin
result := AddLight(AColor);
if result <> -1 then
begin
v := Point3D_128(ADirection);
Normalize3D_128(v);
v.t := 0;
glLightfv(GL_LIGHT0 + result, GL_POSITION, @v);
end;
end;
procedure TBGLLighting.SetSpecularIndex(AIndex: integer);
var c: TColorF;
newIndex: single;
begin
newIndex := AIndex*0.5;
if newIndex < 0 then newIndex := 0;
if newIndex > 128 then newIndex := 128;
if newIndex <> FCurrentSpecularIndex then
begin
if newIndex = 0 then
c := ColorF(0,0,0,1)
else
c := ColorF(1,1,1,1);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, newIndex);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, @c);
FCurrentSpecularIndex := newIndex;
end;
end;
function TBGLLighting.MakeVertexShader(ASource: string): BGRADWord;
begin
result := MakeShaderObject(GL_VERTEX_SHADER, ASource);
end;
function TBGLLighting.MakeFragmentShader(ASource: string): BGRADWord;
begin
result := MakeShaderObject(GL_FRAGMENT_SHADER, ASource);
end;
function TBGLLighting.GetAmbiantLightF: TColorF;
begin
result := FAmbiantLightF;
end;
function TBGLLighting.GetBuiltInLightingEnabled: boolean;
begin
result := FBuiltInLighting;
end;
procedure TBGLLighting.SetBuiltInLightingEnabled(AValue: boolean);
begin
if AValue = FBuiltInLighting then exit;
FBuiltInLighting:= AValue;
if AValue then
begin
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, @FAmbiantLightF);
glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 0);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,1);
end else
begin
glDisable(GL_LIGHTING);
end;
end;
function TBGLLighting.MakeShaderObject(AShaderType: GLenum; ASource: string
): GLuint;
var
psource: PChar; //@{$IFDEF FPC}PChar{$ELSE}PAnsiChar{$ENDIF};
sourceLen: GLint;
shaderId: GLuint;
shaderOk: GLint;
log: string;
logLen: GLint;
begin
NeedOpenGL2_0;
if ASource = '' then
raise exception.Create('Empty code file provided');
shaderId := glCreateShader(AShaderType);
psource := @ASource[1];
sourceLen := length(ASource);
glShaderSource(shaderId, 1, @psource, @sourceLen);
glCompileShader(shaderId);
glGetShaderiv(shaderId, GL_COMPILE_STATUS, @shaderOk);
if not (shaderOk <> 0) then
begin
//retrieve error log
glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, @logLen);
setlength(log, logLen);
if logLen > 0 then
glGetShaderInfoLog(shaderId, logLen, nil, @log[1]);
glDeleteShader(shaderId);
raise exception.Create('Failed to compile shader: ' + log);
end;
result := shaderId;
end;
function TBGLLighting.AddLight(AColor: TColorF): integer;
var
i: Integer;
black: TColorF;
begin
for i := 0 to high(FLightUsage) do
if not FLightUsage[i] then
begin
result := i;
FLightUsage[i] := true;
black := ColorF(0,0,0,1);
glLightfv(GL_LIGHT0 + i, GL_AMBIENT, @black);
glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, @AColor);
glLightfv(GL_LIGHT0 + i, GL_SPECULAR, @AColor);
glEnable(GL_LIGHT0 + i);
exit;
end;
result := -1;
end;
function TBGLLighting.GetSupportShaders: boolean;
begin
result := CheckOpenGL2_0;
end;
function TBGLLighting.MakeShaderProgram(AVertexShader, AFragmentShader: BGRADWord): BGRADWord;
var
programOk: GLint;
shaderProgram: GLuint;
log: string;
logLen: GLint;
begin
NeedOpenGL2_0;
shaderProgram := glCreateProgram();
glAttachShader(shaderProgram, AVertexShader);
glAttachShader(shaderProgram, AFragmentShader);
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, @programOk);
if not (programOk <> 0) then
begin
//retrieve error log
glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, @logLen);
setlength(log, logLen);
if logLen > 0 then
glGetProgramInfoLog(shaderProgram, logLen, nil, @log[1]);
glDeleteProgram(shaderProgram);
raise exception.Create('Failed to link shader program: ' + log);
end;
result := shaderProgram;
end;
procedure TBGLLighting.UseProgram(AProgram: BGRADWord);
begin
NeedOpenGL2_0;
glUseProgram(AProgram);
end;
procedure TBGLLighting.DeleteShaderObject(AShader: BGRADWord);
begin
NeedOpenGL2_0;
if AShader<> 0 then
glDeleteShader(AShader);
end;
procedure TBGLLighting.DeleteShaderProgram(AProgram: BGRADWord);
begin
NeedOpenGL2_0;
if AProgram<> 0 then
glDeleteProgram(AProgram);
end;
function TBGLLighting.GetUniformVariable(AProgram: BGRADWord; AName: string): BGRADWord;
begin
NeedOpenGL2_0;
result := glGetUniformLocation(AProgram, @AName[1]);
end;
function TBGLLighting.GetAttribVariable(AProgram: BGRADWord; AName: string): BGRADWord;
begin
NeedOpenGL2_0;
result := glGetAttribLocation(AProgram, @AName[1]);
end;
procedure TBGLLighting.SetUniformSingle(AVariable: BGRADWord;
const AValue; AElementCount, AComponentCount: integer);
begin
NeedOpenGL2_0;
case AComponentCount of
1: glUniform1fv(AVariable, AElementCount, @AValue);
2: glUniform2fv(AVariable, AElementCount, @AValue);
3: glUniform3fv(AVariable, AElementCount, @AValue);
4: glUniform4fv(AVariable, AElementCount, @AValue);
9: glUniformMatrix3fv(AVariable, AElementCount, GL_FALSE, @AValue);
16: glUniformMatrix4fv(AVariable, AElementCount, GL_FALSE, @AValue);
else
raise exception.Create('Unexpected number of components');
end;
end;
procedure TBGLLighting.SetUniformInteger(AVariable: BGRADWord;
const AValue; AElementCount, AComponentCount: integer);
begin
NeedOpenGL2_0;
case AComponentCount of
1: glUniform1iv(AVariable, AElementCount, @AValue);
2: glUniform2iv(AVariable, AElementCount, @AValue);
3: glUniform3iv(AVariable, AElementCount, @AValue);
4: glUniform4iv(AVariable, AElementCount, @AValue);
else
raise exception.Create('Unexpected number of components');
end;
end;
procedure TBGLLighting.BindAttribute(AAttribute: TAttributeVariable);
var t: GLenum;
begin
glBindBuffer(GL_ARRAY_BUFFER, AAttribute.Source.Handle);
if AAttribute.IsFloat then
t := GL_FLOAT
else
t := GL_INT;
glVertexAttribPointer(AAttribute.Handle, AAttribute.VectorSize,t,GL_FALSE,
AAttribute.Source.RecordSize, {%H-}pointer(BGRAPtrInt(AAttribute.RecordOffset)));
glEnableVertexAttribArray(AAttribute.Handle);
end;
procedure TBGLLighting.UnbindAttribute(AAttribute: TAttributeVariable);
begin
glDisableVertexAttribArray(AAttribute.Handle);
end;
function TBGLLighting.RemoveLight(AIndex: integer): boolean;
begin
if (AIndex >= 0) and (AIndex <= high(FLightUsage)) and
FLightUsage[AIndex] then
begin
glDisable(GL_LIGHT0 + AIndex);