-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuopengl_graphikengine.pas
2054 lines (1971 loc) · 70.8 KB
/
uopengl_graphikengine.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
(******************************************************************************)
(* uOpenGLGraphikEngine.pas ??.??.???? *)
(* *)
(* Version : 0.11 *)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* Support : www.Corpsman.de *)
(* *)
(* Description : This unit tries to help the user to use graphiks with OpenGL *)
(* without the need of special knowlege in bitmanipulations. *)
(* *)
(* The unit is in alpha-state there are a lot functions that *)
(* work not correct, please read the implementation part *)
(* carefully before use. *)
(* *)
(* License : See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(* Warranty : There is no warranty, neither in correctness of the *)
(* implementation, nor anything other that could happen *)
(* or go wrong, use at your own risk. *)
(* *)
(* Known Issues: none *)
(* *)
(* History : 0.01 - Initial version ( exported from balanced ) *)
(* 0.02 - Hinzufügen der RenderBillboard Routine *)
(* 0.03 - Ändern RenderQuad zur unterstützung für negative Width*)
(* Damit Bugfix Spiegelungsbug für 180° rotation *)
(* 0.04 - added glcolor() *)
(* 0.05 - neuer Stretch mode Nearest Neighbour interpolation *)
(* 0.06 - Methode RemoveGraphik *)
(* 0.07 - LoadAlphaPNGGraphik *)
(* 0.08 - Fix Memleaks *)
(* 0.09 - Fix LoadAlphaColorGraphik *)
(* 0.10 - Fix speedup graphik loading *)
(* 0.11 - Fix LoadAlphaGraphikItem did not respect png *)
(* transparency *)
(* *)
(******************************************************************************)
Unit uopengl_graphikengine;
{$IFDEF FPC}
{$MODE objFPC}{$H+}
{$ENDIF}
Interface
// Je nach dem wie mans Braucht mus hier angepasst werden!!
{.$DEFINE USE_GL}
(*
Ist dieser Schalter an, werden diverse Informationen in die Konsole geschrieben
- Anzeige des Allokierten OpenGLSpeichers
- Anzeige der Anzahl der Allokierten Texturen
*)
{.$DEFINE DEBUGGOUTPUT}
Uses
// Die OpenGL Schnittstelle
{$IFDEF USE_GL}
gl, glu,
{$ELSE}
dglopengl,
{$ENDIF}
// IDE spezifisches
LCLType, // HBitmap type
IntfGraphics, // TLazIntfImage type
fpImage, // TFPColor type
// lazjpg, // ist in "Lazarus/Components/Image" zu finden, dieses Packete mus installiert werden !!
Graphics, // TBitmap
//, // Für PNG Dateien
// dialogs, // Debugg
ugraphics, // TRGB
Classes, // Function Point
math, // SinCos
uvectormath, // Mathe Operationen
sysutils; // Fileexists, ..
Type
TStretchMode = (
smNone, // Kein Stretching, wirft eine Exception bei Größen <> 2^n
smStretch, // Die Textur wird mittels Stretchdraw bilinear hochgesampelt
smStretchHard, // Die Textur wird via Nearest Neighbour gestretcht (besonders wichtig bei Binärer Transparenz)
smClamp // Die ZielTextur wird groß gemacht, und die Quelltextur Oben Links reinkopiert.
);
TFRect = Record
Left, Top, Right, Bottom: Single;
End;
TSubImage = Record
ImageData: Integer; // Pointer auf die Texture
ImageRect: TFrect; // Ausschnitt der Textur der Gerendert werden soll
Width: Integer; // Breite auf dem Monitor
Height: Integer; // Höhe auf dem Monitor
End;
TGraphikItem = Record
Image: Integer;
Name: String;
IsAlphaImage: Boolean; // Wenn True, dann wurde Image mit AlphaKanal geladen und kann geblendet werden
Stretched: TStretchMode;
OrigWidth: integer;
OrigHeight: integer;
StretchedWidth: integer;
StretchedHeight: integer;
End;
{ TOpenGL_GraphikEngine }
TOpenGL_GraphikEngine = Class
private
FImages: Array Of TGraphikItem;
{$IFDEF DEBUGGOUTPUT}
OpenGLBufCount: int64;
{$ENDIF}
public
Constructor create;
Destructor destroy; override;
Function GetInfo(Value: String): TGraphikItem;
Function GetInfo(Value: integer): TGraphikItem;
Function Find(Value: String; ExceptionOnNotExists: Boolean = True): integer; // Gibt die Textur wieder die unter dem Namen gespeichert ist. Pfadangaben nur bei doppeldeutigen namen notwendig.
Function FindItem(Value: String; ExceptionOnNotExists: Boolean = True): TGraphikItem; // Gibt die Textur wieder die unter dem Namen gespeichert ist. Pfadangaben nur bei doppeldeutigen namen notwendig.
Procedure Clear; // Alles Freigeben
(*
Funktionen die Machen was sie sollen
*)
Function LoadGraphik(Filename: String; Stretch: TStretchmode = smNone): Integer; overload; // Laden einer Graphik ohne Alphakanal
Function LoadGraphikItem(Filename: String; Stretch: TStretchmode = smNone): TGraphikItem; overload; // Laden einer Graphik ohne Alphakanal
Function LoadGraphik(Const Graphik: TBitmap; Name: String; Stretch: TStretchmode = smNone): Integer; overload; // Laden einer Graphik ohne Alphakanal
Function LoadAlphaColorGraphik(Filename: String; Color: TRGB; Stretch: TStretchmode = smNone): Integer; overload; // Lädt eine Alphagraphik und setzt den Wert von Color = Transparent.
Function LoadAlphaColorGraphik(Const Graphik: TBitmap; Name: String; Color: TRGB; Stretch: TStretchmode = smNone): Integer; overload; // Lädt eine Alphagraphik und setzt den Wert von Color = Transparent.
Function LoadAlphaColorGraphikItem(Const Graphik: TBitmap; Name: String; Color: TRGB; Stretch: TStretchmode = smNone): TGraphikItem; overload;
(*
Funktionen die NICHT Machen was sie sollen
Sie werden aber trotzdem in einigen Projekten genutzt und Funktionieren dort.
*)
Function LoadAlphaValueGraphik(Filename: String; AlphaValue: byte; Stretch: TStretchmode = smNone): Integer; Deprecated; // Laden einer Graphik, und Vorgeben eines Gesammten Alpha wertes
Function LoadAlphaGraphik(Filename: String; Stretch: TStretchmode = smNone): Integer; overload; // Laden einer Transparenten Graphik, clfuchsia = Transparent
Function LoadAlphaGraphikItem(Filename: String; Stretch: TStretchmode = smNone): TGraphikItem; overload; // Laden einer Transparenten Graphik, clfuchsia = Transparent
Function LoadAlphaGraphik(Const Graphik, AlphaMask: Tbitmap; Name: String; Stretch: TStretchmode = smNone): integer; overload; // Lädt eine Graphik aus TBitmap, und Lädt den Alphakanal aus den Luminanzdaten von Alphamask, Name dient zum späteren Wiederfinden
Function LoadAlphaGraphik(Graphik, AlphaMask: String; Stretch: TStretchmode = smNone): integer; overload; // Lädt eine Graphik aus TBitmap, und Lädt den Alphakanal aus den Luminanzdaten von Alphamask
Function LoadAlphaGraphikItem(Graphik, AlphaMask: String; Stretch: TStretchmode = smNone): TGraphikItem; overload; // Lädt eine Graphik aus TBitmap, und Lädt den Alphakanal aus den Luminanzdaten von Alphamask
Function LoadAlphaPNGGraphik(Graphik: String; Stretch: TStretchmode = smNone): integer; // Lädt eine .png Graphik und nutzt deren Alpha Kanal als Alpha
(*
* Wenn eine Graphik Explicit nicht mehr gecached werden soll, true wenn sie gefunden und gelöscht werden konnte
*)
Function RemoveGraphik(Value: TGraphikItem): Boolean; overload;
Function RemoveGraphik(Value: integer): Boolean; overload;
End;
Const
Fuchsia: TRGB = (r: 255; g: 0; b: 255);
Black: TRGB = (r: 0; g: 0; b: 0);
White: TRGB = (r: 255; g: 255; b: 255);
Var
OpenGL_GraphikEngine: TOpenGL_GraphikEngine;
(*
Rendert in 2D ein Quad
ACHTUNG Bei Bildern mit der KantenLänge 1 Kommt blödsinn = kein Bild raus !!
ACHTUNG Diese Routinen funktionieren nicht immer mit eingeschalteten CullFacing !!
*)
Procedure RenderAlphaQuad(Top, Left: Single; Image: TGraphikItem); overload; // Fertig Getestet // WTF: warum ist hier top und left vertauscht ?
Procedure RenderAlphaQuad(Middle: Tpoint; Width, Height, Angle: Integer; Texture: integer = 0); overload; // Fertig Getestet
Procedure RenderAlphaRQuad(TopLeft, BottomRight: TPoint; Angle: Integer; RotatebyOrigin: Boolean = False; Texture: Integer = 0); overload; // Fertig Getestet
Procedure RenderAlphaRQuad(TopLeft, BottomRight: TVector2; Angle: Integer; RotatebyOrigin: Boolean = False; Texture: Integer = 0); overload; // Fertig Getestet
Procedure RenderAlphaImage(Value: TSubImage);
Procedure RenderQuad(Top, Left: Single; Image: TGraphikItem); overload; // WTF: warum ist hier top und left vertauscht ?
Procedure RenderQuad(Middle: TVector2; Angle: Single; Image: TGraphikItem); overload;
Procedure RenderQuad(Middle: Tpoint; Width, Height, Angle: Integer; Texture: integer = 0); overload; // Fertig Getestet
Procedure RenderQuad(Middle: TVector2; Width, Height, Angle: Single; Texture: integer = 0); overload; // Fertig Getestet
Procedure RenderQuad(TopLeft, BottomRight: TPoint; Angle: Integer; RotatebyOrigin: Boolean = False; Texture: Integer = 0); overload; // Fertig Getestet
Procedure RenderQuad(TopLeft, BottomRight: TVector2; Angle: Integer; RotatebyOrigin: Boolean = False; Texture: Integer = 0); overload; // Fertig Getestet
(*
Rendert einen Kreis, ohne Füllung
*)
Procedure RenderCircle(Middle: TVector2; Steps, Radius: Integer);
(*
Rendert ein Billboard, ACHTUNG bei mehr als einem Bildboard Pro Renderschritt, sollte das "Hohlen" der Modelviewmatrix ausgelagert werden !!
Mittels Dimension kann man die Ausbreitung in x und y Richtung angeben. Position ist stets der Mittelpunkt der textur
*)
Procedure RenderBillboard(Position: TVector3; Dimension: TVector2; Texture: integer = 0);
(*
Erstellt einen Screenshot aus dem Aktuellen Framebuffer,
inclusive Ermittlung der Auflösung.
*)
Function OpenGLScreenshot: TBitmap;
(*
* Setzt die OpenGLFarbe auf den Wert von TColor
* Alpha = 0 = Opak
*)
Procedure glColor(Color: TColor; Alpha: byte = 0);
Function FRect(Top, Left, Bottom, Right: Single): TFRect;
Implementation
{$IFDEF DEBUGGOUTPUT}
Function FileSizetoString(Value: Int64): String;
Var
s: Char;
r: Int64;
Begin
s := ' ';
r := 0;
If value > 1024 Then Begin
s := 'K';
r := value Mod 1024;
value := value Div 1024;
End;
If value > 1024 Then Begin
s := 'M';
r := value Mod 1024;
value := value Div 1024;
End;
If value > 1024 Then Begin
s := 'G';
r := value Mod 1024;
value := value Div 1024;
End;
If value > 1024 Then Begin
s := 'T';
r := value Mod 1024;
value := value Div 1024;
End;
If value > 1024 Then Begin
s := 'P';
r := value Mod 1024;
value := value Div 1024;
End;
If (r Div 100) <> 0 Then
result := inttostr(value) + ',' + inttostr(r Div 100) + s + 'B'
Else
result := inttostr(value) + s + 'B'
End;
{$ENDIF}
(*
* Der Folgende Code bassiert auf dieser Grundlage :
* http://wiki.delphigl.com/index.php/Sphärisches_Billboard
*)
Procedure RenderBillboard(Position: TVector3; Dimension: TVector2;
Texture: integer);
Var
up, right: TVector3;
Matrix: TMatrix4x4;
Begin
If Texture <> 0 Then Begin
glBindTexture(gl_texture_2d, Texture);
End;
(*
* Dieser Code muss theoretisch nur ein mal gerechnet werden ( so lange sich die Augposition nicht ändert )
*)
glGetFloatv(GL_MODELVIEW_MATRIX, @Matrix[0, 0]);
glColor4f(1, 1, 1, 1);
// Auslesen des Right und Up Vektors ( das geht nur wenn man annimmt das die ModelView Matrix Orthogonal ist )
Right := V3(Matrix[0, 0], Matrix[1, 0], Matrix[2, 0]);
Up := V3(Matrix[0, 1], Matrix[1, 1], Matrix[2, 1]);
// Das Billboard auf die gewünschte Größe Skalieren
glbegin(GL_QUADS);
(*
* Mit unterschiedlichen Positionen/ Dimensionen könnten nun mehrere Billboards gerendert werden.
*)
right := ScaleV3(Dimension.x / 2, NormV3(right));
up := ScaleV3(Dimension.y / 2, NormV3(up));
//Der eigentliche Renderschritt
glTexCoord2d(1, 1);
glVertex3f(Position.x + Right.x + Up.x, Position.y + Right.y + Up.y, Position.z + Right.z + Up.z);
glTexCoord2d(0, 1);
glVertex3f(Position.x - Right.x + Up.x, Position.y - Right.y + Up.y, Position.z - Right.z + Up.z);
glTexCoord2d(0, 0);
glVertex3f(Position.x - Right.x - Up.x, Position.y - Right.y - Up.y, Position.z - Right.z - Up.z);
glTexCoord2d(1, 0);
glVertex3f(Position.x + Right.x - Up.x, Position.y + Right.y - Up.y, Position.z + Right.z - Up.z);
glend();
End;
Function OpenGLScreenshot: TBitmap;
Var
dim: Array[0..3] Of Integer;
c: Array Of Array[0..3] Of Byte;
z, i, j: integer;
TempIntfImg: TLazIntfImage;
ImgHandle, ImgMaskHandle: HBitmap;
CurColor: TFPColor;
Begin
// Auslesen der Framebuffer Auflösung
glGetIntegerv(GL_VIEWPORT, @dim[0]);
// Erstellen des Bitmaps
result := TBitmap.create;
result.pixelformat := pf24bit;
result.width := dim[2];
result.height := dim[3];
TempIntfImg := TLazIntfImage.Create(0, 0);
TempIntfImg.LoadFromBitmap(result.Handle, result.MaskHandle);
c := Nil;
setlength(c, dim[2] * dim[3]);
// Auslesen des Framebuffers in einen temporären Speicher
glReadPixels(dim[0], dim[1], dim[2], dim[3], GL_RGBA, GL_UNSIGNED_BYTE, @c[0, 0]);
// Umschreiben des Temporären Speichers in das TBitmap
z := 0;
For j := 0 To result.height - 1 Do
For i := 0 To result.width - 1 Do Begin
CurColor.red := c[z][0] * 256;
CurColor.green := c[z][1] * 256;
CurColor.blue := c[z][2] * 256;
// c[z][3] wäre der Alphakanal, aber den Braucht man ja hier nicht ...
//TempIntfImg.Colors[i, j] := CurColor;
TempIntfImg.Colors[i, result.height - 1 - j] := CurColor;
inc(z);
End;
TempIntfImg.CreateBitmaps(ImgHandle, ImgMaskHandle, false);
result.Handle := ImgHandle;
result.MaskHandle := ImgMaskHandle;
TempIntfImg.free;
End;
Procedure glColor(Color: TColor; Alpha: byte);
Var
r, g, b: Byte;
Begin
r := color And $FF;
g := (color Shr 8) And $FF;
b := (color Shr 16) And $FF;
glColor4ub(r, g, b, Alpha);
End;
Function FRect(Top, Left, Bottom, Right: Single): TFRect;
Begin
result.Top := Top;
result.Left := Left;
result.Bottom := Bottom;
result.Right := Right;
End;
Procedure RenderAlphaImage(Value: TSubImage);
Var
b: {$IFDEF USE_GL}Byte{$ELSE}Boolean{$ENDIF};
Begin
B := glIsEnabled(gl_Blend);
If Not (b{$IFDEF USE_GL} = 1{$ENDIF}) Then
glenable(gl_Blend);
glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
glbindtexture(GL_TEXTURE_2d, Value.ImageData);
glbegin(gl_quads);
glTexCoord2f(Value.ImageRect.left, Value.ImageRect.top);
glvertex3f(0, 0, 0);
glTexCoord2f(Value.ImageRect.Right, Value.ImageRect.top);
glvertex3f(value.Width, 0, 0);
glTexCoord2f(Value.ImageRect.right, Value.ImageRect.Bottom);
glvertex3f(value.width, Value.Height, 0);
glTexCoord2f(Value.ImageRect.left, Value.ImageRect.Bottom);
glvertex3f(0, Value.height, 0);
glend;
If Not (b{$IFDEF USE_GL} = 1{$ENDIF}) Then
gldisable(gl_blend);
End;
Function IsPowerOfTwo(Value: Integer): Boolean;
Var
i: Integer;
Begin
i := 1;
While i < Value Do
i := i Shl 1;
result := i = Value;
End;
Function GetNextPowerOfTwo(Value: integer): Integer;
Var
i: Integer;
Begin
i := 1;
While i < Value Do
i := i Shl 1;
result := i;
End;
Procedure RenderCircle(Middle: TVector2; Steps, Radius: Integer);
Var
delta: Double;
s, c: extended;
i: Integer;
Begin
Delta := 2 * Pi / Steps;
glbegin(gl_Line_Loop);
For i := 0 To Steps - 1 Do Begin
sincos(i * Delta, s, c);
glvertex3f(middle.x + c * Radius, middle.y + s * Radius, 0);
End;
glend;
End;
Procedure RenderAlphaQuad(Top, Left: Single; Image: TGraphikItem);
Var
b: {$IFDEF USE_GL}Byte{$ELSE}Boolean{$ENDIF};
Begin
B := glIsEnabled(gl_Blend);
If Not (b{$IFDEF USE_GL} = 1{$ENDIF}) Then
glenable(gl_Blend);
glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
RenderQuad(Top, Left, Image);
If Not (b{$IFDEF USE_GL} = 1{$ENDIF}) Then
gldisable(gl_blend);
End;
Procedure RenderAlphaQuad(Middle: Tpoint; Width, Height, Angle: Integer; Texture: integer = 0);
Var
b: {$IFDEF USE_GL}Byte{$ELSE}Boolean{$ENDIF};
Begin
B := glIsEnabled(gl_Blend);
If Not (b{$IFDEF USE_GL} = 1{$ENDIF}) Then
glenable(gl_Blend);
glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
RenderQuad(Middle, Width, height, angle, texture);
If Not (b{$IFDEF USE_GL} = 1{$ENDIF}) Then
gldisable(gl_blend);
End;
Procedure RenderAlphaRQuad(TopLeft, BottomRight: TVector2; Angle: Integer; RotatebyOrigin: Boolean = False; Texture: Integer = 0); overload; // Fertig Getestet
Var
b: {$IFDEF USE_GL}Byte{$ELSE}Boolean{$ENDIF};
Begin
B := glIsEnabled(gl_Blend);
If Not (b{$IFDEF USE_GL} = 1{$ENDIF}) Then
glenable(gl_Blend);
glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
RenderQuad(Topleft, Bottomright, angle, RotatebyOrigin, texture);
If Not (b{$IFDEF USE_GL} = 1{$ENDIF}) Then
gldisable(gl_blend);
End;
Procedure RenderAlphaRQuad(TopLeft, BottomRight: TPoint; Angle: Integer; RotatebyOrigin: Boolean = False; Texture: Integer = 0);
Var
b: {$IFDEF USE_GL}Byte{$ELSE}Boolean{$ENDIF};
Begin
B := glIsEnabled(gl_Blend);
If Not (b{$IFDEF USE_GL} = 1{$ENDIF}) Then
glenable(gl_Blend);
glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
RenderQuad(Topleft, Bottomright, angle, RotatebyOrigin, texture);
If Not (b{$IFDEF USE_GL} = 1{$ENDIF}) Then
gldisable(gl_blend);
End;
Procedure RenderQuad(TopLeft, BottomRight: TVector2; Angle: Integer; RotatebyOrigin: Boolean = False; Texture: Integer = 0); overload; // Fertig Getestet
Var
w2, h2: Single;
Begin
If RotatebyOrigin Then Begin
If Texture <> 0 Then
glBindTexture(gl_texture_2d, Texture);
glpushmatrix;
w2 := (BottomRight.x - TopLeft.x) / 2;
h2 := (BottomRight.y - TopLeft.y) / 2;
If Angle <> 0 Then
glRotatef(angle, 0, 0, 1);
gltranslatef((TopLeft.x + BottomRight.x) / 2, (TopLeft.y + BottomRight.y) / 2, 0);
glbegin(gl_quads);
glTexCoord2f(0, 1);
glvertex3f(-w2, -h2, 0);
glTexCoord2f(1, 1);
glvertex3f(w2, -h2, 0);
glTexCoord2f(1, 0);
glvertex3f(w2, h2, 0);
glTexCoord2f(0, 0);
glvertex3f(-w2, h2, 0);
glend;
glpopmatrix;
End
Else
// RenderQuad(point((TopLeft.x + BottomRight.x) Shr 1, (TopLeft.y + BottomRight.y) Shr 1), BottomRight.x - TopLeft.x, TopLeft.y - BottomRight.y, angle, Texture);
// RenderQuad(Fpoint((TopLeft.x + BottomRight.x) / 2, (TopLeft.y + BottomRight.y) / 2), abs(BottomRight.x - TopLeft.x), abs(TopLeft.y - BottomRight.y), angle, Texture);
RenderQuad(v2((TopLeft.x + BottomRight.x) / 2, (TopLeft.y + BottomRight.y) / 2), abs(BottomRight.x - TopLeft.x), abs(TopLeft.y - BottomRight.y), angle, Texture);
End;
Procedure RenderQuad(TopLeft, BottomRight: TPoint; Angle: Integer; RotatebyOrigin: Boolean = False; Texture: Integer = 0);
Var
w2, h2: integer;
Begin
If RotatebyOrigin Then Begin
If Texture <> 0 Then
glBindTexture(gl_texture_2d, Texture);
glpushmatrix;
w2 := (BottomRight.x - TopLeft.x) Div 2;
h2 := (BottomRight.y - TopLeft.y) Div 2;
If Angle <> 0 Then
glRotatef(angle, 0, 0, 1);
gltranslatef((TopLeft.x + BottomRight.x) Div 2, (TopLeft.y + BottomRight.y) Div 2, 0);
glbegin(gl_quads);
glTexCoord2f(0, 1);
glvertex3f(-w2, -h2, 0);
glTexCoord2f(1, 1);
glvertex3f(w2, -h2, 0);
glTexCoord2f(1, 0);
glvertex3f(w2, h2, 0);
glTexCoord2f(0, 0);
glvertex3f(-w2, h2, 0);
glend;
glpopmatrix;
End
Else
// RenderQuad(point((TopLeft.x + BottomRight.x) Shr 1, (TopLeft.y + BottomRight.y) Shr 1), BottomRight.x - TopLeft.x, TopLeft.y - BottomRight.y, angle, Texture);
RenderQuad(point((TopLeft.x + BottomRight.x) Div 2, (TopLeft.y + BottomRight.y) Div 2), abs(BottomRight.x - TopLeft.x), abs(TopLeft.y - BottomRight.y), angle, Texture);
End;
Procedure RenderQuad(Middle: TVector2; Width, Height, Angle: Single; Texture: integer = 0); overload; // Fertig Getestet
Var
w2, h2: Single;
Begin
If Texture <> 0 Then
glBindTexture(gl_texture_2d, Texture);
glpushmatrix;
gltranslatef(middle.x, middle.y, 0);
w2 := Width / 2;
h2 := height / 2;
If Angle <> 0 Then
glRotatef(angle, 0, 0, 1);
glbegin(gl_quads);
glTexCoord2f(0, 1);
glvertex3f(-w2, h2, 0);
glTexCoord2f(1, 1);
glvertex3f(w2, h2, 0);
glTexCoord2f(1, 0);
glvertex3f(w2, -h2, 0);
glTexCoord2f(0, 0);
glvertex3f(-w2, -h2, 0);
glend;
// Der Code macht Probleme mit glCullface
// glbegin(gl_quads);
// glTexCoord2f(0, 1);
// glvertex3f(-w2, -h2, 0);
// glTexCoord2f(1, 1);
// glvertex3f(w2, -h2, 0);
// glTexCoord2f(1, 0);
// glvertex3f(w2, h2, 0);
// glTexCoord2f(0, 0);
// glvertex3f(-w2, h2, 0);
// glend;
glpopmatrix;
End;
Procedure RenderQuad(Top, Left: Single; Image: TGraphikItem);
Var
tw, th: Single;
Begin
Case Image.Stretched Of
smClamp: Begin
tw := Image.OrigWidth / Image.StretchedWidth;
th := Image.OrigHeight / Image.StretchedHeight;
End;
smNone, smStretch, smStretchHard: Begin
tw := 1;
th := 1;
End;
End;
glBindTexture(gl_texture_2d, image.Image);
glpushmatrix;
gltranslatef(left, top, 0);
glbegin(gl_quads);
glTexCoord2f(0, th);
glvertex3f(0, image.OrigHeight, 0);
glTexCoord2f(tw, th);
glvertex3f(image.OrigWidth, image.OrigHeight, 0);
glTexCoord2f(tw, 0);
glvertex3f(image.OrigWidth, 0, 0);
glTexCoord2f(0, 0);
glvertex3f(0, 0, 0);
glend;
glpopmatrix;
End;
Procedure RenderQuad(Middle: TVector2; Angle: Single; Image: TGraphikItem);
Begin
glPushMatrix;
gltranslatef(middle.x, middle.y, 0);
If Angle <> 0 Then
glRotatef(angle, 0, 0, 1);
glTranslatef(-Image.OrigWidth / 2, -image.OrigHeight / 2, 0);
RenderQuad(0, 0, Image);
glPopMatrix;
End;
Procedure RenderQuad(Middle: Tpoint; Width, Height, Angle: Integer; Texture: integer = 0);
Var
w2, h2: Integer;
Begin
If Texture <> 0 Then
glBindTexture(gl_texture_2d, Texture);
glpushmatrix;
gltranslatef(middle.x, middle.y, 0);
w2 := Width Div 2;
h2 := height Div 2;
If Angle <> 0 Then
glRotatef(angle, 0, 0, 1);
glbegin(gl_quads);
glTexCoord2f(0, 1);
glvertex3f(-w2, -h2, 0);
glTexCoord2f(1, 1);
glvertex3f(w2, -h2, 0);
glTexCoord2f(1, 0);
glvertex3f(w2, h2, 0);
glTexCoord2f(0, 0);
glvertex3f(-w2, h2, 0);
glend;
glpopmatrix;
End;
{ TGraphikEngine }
Constructor TOpenGL_GraphikEngine.create;
Begin
Inherited create;
{$IFDEF DEBUGGOUTPUT}
OpenGLBufCount := 0;
{$ENDIF}
End;
Destructor TOpenGL_GraphikEngine.destroy;
Begin
Clear;
End;
Procedure TOpenGL_GraphikEngine.Clear;
Var
i: Integer;
Begin
For i := 0 To high(Fimages) Do
If Fimages[i].image <> 0 Then
glDeleteTextures(1, @Fimages[i].image);
setlength(Fimages, 0);
{$IFDEF DEBUGGOUTPUT}
OpenGLBufCount := 0;
{$ENDIF}
End;
Function TOpenGL_GraphikEngine.LoadGraphik(Filename: String; Stretch: TStretchmode): Integer;
Begin
result := LoadGraphikItem(Filename, Stretch).Image;
End;
Function TOpenGL_GraphikEngine.LoadGraphikItem(Filename: String; Stretch: TStretchmode): TGraphikItem;
Var
OpenGLData: Array Of Array[0..2] Of Byte;
Data: String;
b, b2: Tbitmap;
IntfImg1: TLazIntfImage;
CurColor: TFPColor;
jp: TJPEGImage;
png: TPortableNetworkGraphic;
c, j, i: Integer;
bool: {$IFDEF USE_GL}Byte{$ELSE}Boolean{$ENDIF};
ow, oh, nw, nh: INteger;
Begin
If FileExists(Filename) Then Begin
Data := LowerCase(Filename);
// Graphik bereits geladen
For i := 0 To high(Fimages) Do Begin
If Fimages[i].Name = Data Then Begin
result := Fimages[i];
{$IFDEF DEBUGGOUTPUT}
writeln('TGraphikEngine.LoadGraphik(' + Filename + ')');
writeln('OpenGL Buffer : ' + FileSizetoString(OpenGLBufCount));
writeln('TextureCount : ' + inttostr(high(Fimages) + 1));
{$ENDIF}
exit;
End;
End;
// Graphik mus geladen werden
b := TBitmap.create;
If lowercase(ExtractFileExt(Filename)) = '.jpg' Then Begin
jp := TJPEGImage.create;
jp.LoadFromFile(Filename);
b.assign(jp);
jp.free;
End
Else If lowercase(ExtractFileExt(Filename)) = '.png' Then Begin
png := TPortableNetworkGraphic.create;
png.LoadFromFile(Filename);
b.assign(png);
png.free;
End
Else Begin
b.LoadFromFile(Filename);
End;
// create the raw image
IntfImg1 := TLazIntfImage.Create(0, 0);
nw := b.width;
nh := b.height;
ow := b.width;
oh := b.height;
{$IFDEF DEBUGGOUTPUT}
writeln('TGraphikEngine.LoadGraphik(' + Filename + ')');
writeln('Orig size : ' + inttostr(b.width) + 'x' + inttostr(b.height));
{$ENDIF}
Case Stretch Of
smNone: Begin
End;
smStretchHard, smStretch: Begin
nw := GetNextPowerOfTwo(b.width);
nh := GetNextPowerOfTwo(b.height);
If (nw <> b.width) Or (nh <> b.height) Then Begin
b2 := TBitmap.create;
b2.PixelFormat := pf24bit;
b2.width := nw;
b2.height := nh;
If Stretch = smStretch Then Begin
Stretchdraw(b2.canvas, rect(0, 0, nw, nh), b, imBilinear);
End
Else Begin
Stretchdraw(b2.canvas, rect(0, 0, nw, nh), b, imNearestNeighbour);
End;
b.free;
b := b2;
End;
End;
smClamp: Begin
nw := GetNextPowerOfTwo(b.width);
nh := GetNextPowerOfTwo(b.height);
If (nw <> b.width) Or (nh <> b.height) Then Begin
b2 := TBitmap.create;
b2.PixelFormat := pf24bit;
b2.width := nw;
b2.height := nh;
b2.canvas.Draw(0, 0, b);
b.free;
b := b2;
End;
End;
End;
// load the raw image from the bitmap handles
IntfImg1.LoadFromBitmap(B.Handle, B.MaskHandle);
{$IFDEF DEBUGGOUTPUT}
writeln('OpenGL size : ' + inttostr(b.width) + 'x' + inttostr(b.height));
OpenGLBufCount := OpenGLBufCount + (b.width * b.height * 3);
writeln('OpenGL Buffer : ' + FileSizetoString(OpenGLBufCount));
{$ENDIF}
If IsPowerOfTwo(b.width) And IsPowerOfTwo(b.Height) Then Begin
// Laden der Graphikdaten
opengldata := Nil;
setlength(opengldata, b.width * b.height);
c := 0;
For j := 0 To b.height - 1 Do Begin
For i := 0 To b.width - 1 Do Begin
CurColor := IntfImg1.Colors[i, j];
OpenGLData[c, 0] := CurColor.Red Div 256;
OpenGLData[c, 1] := CurColor.green Div 256;
OpenGLData[c, 2] := CurColor.blue Div 256;
inc(c);
End;
End;
// Übergeben an OpenGL
bool := glIsEnabled(GL_TEXTURE_2D);
If Not (Bool{$IFDEF USE_GL} = 1{$ENDIF}) Then
glEnable(GL_TEXTURE_2D);
glGenTextures(1, @Result);
glBindTexture(GL_TEXTURE_2D, result.Image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, gl_RGB, b.width, b.height, 0, GL_RGB, GL_UNSIGNED_BYTE, @OpenGLData[0]);
If Not (Bool{$IFDEF USE_GL} = 1{$ENDIF}) Then
gldisable(GL_TEXTURE_2D);
IntfImg1.free;
b.free;
// Übernehmen in die Engine
setlength(Fimages, high(Fimages) + 2);
Fimages[high(Fimages)].Image := Result.Image;
Fimages[high(Fimages)].Name := data;
Fimages[high(Fimages)].Stretched := stretch;
Fimages[high(Fimages)].OrigWidth := ow;
Fimages[high(Fimages)].OrigHeight := oh;
Fimages[high(Fimages)].StretchedWidth := nw;
Fimages[high(Fimages)].StretchedHeight := nh;
Fimages[high(Fimages)].IsAlphaImage := false;
result := Fimages[high(Fimages)];
{$IFDEF DEBUGGOUTPUT}
writeln('TextureCount : ' + inttostr(high(Fimages) + 1));
{$ENDIF}
End
Else
Raise Exception.create('Error Image ' + extractfilename(Filename) + ' has invalid Width / Height, has to be 2^x.');
End
Else Begin
result.Image := 0;
Raise Exception.create('Error could not find "' + Filename + '".');
End;
End;
Function TOpenGL_GraphikEngine.LoadGraphik(Const Graphik: TBitmap; Name: String; Stretch: TStretchmode): Integer; // Laden einer Graphik ohne Alphakanal
Var
OpenGLData: Array Of Array[0..2] Of Byte;
Data: String;
b, b2: Tbitmap;
IntfImg1: TLazIntfImage;
CurColor: TFPColor;
c, j, i: Integer;
bool: {$IFDEF USE_GL}Byte{$ELSE}Boolean{$ENDIF};
ow, oh, nw, nh: Integer;
Begin
Data := LowerCase(name);
// Graphik bereits geladen
For i := 0 To high(Fimages) Do
If Fimages[i].Name = Data Then Begin
result := Fimages[i].Image;
{$IFDEF DEBUGGOUTPUT}
writeln('TGraphikEngine.LoadGraphik(' + name + ')');
writeln('OpenGL Buffer : ' + FileSizetoString(OpenGLBufCount));
writeln('TextureCount : ' + inttostr(high(Fimages) + 1));
{$ENDIF}
exit;
End;
// Graphik mus geladen werden
b := TBitmap.create;
b.assign(Graphik);
// create the raw image
IntfImg1 := TLazIntfImage.Create(0, 0);
nw := b.width;
nh := b.height;
ow := b.width;
oh := b.height;
{$IFDEF DEBUGGOUTPUT}
writeln('TGraphikEngine.LoadGraphik(' + name + ')');
writeln('Orig size : ' + inttostr(b.width) + 'x' + inttostr(b.height));
{$ENDIF}
Case Stretch Of
smNone: Begin
End;
smStretchHard, smStretch: Begin
nw := GetNextPowerOfTwo(b.width);
nh := GetNextPowerOfTwo(b.height);
If (nw <> b.width) Or (nh <> b.height) Then Begin
b2 := TBitmap.create;
b2.PixelFormat := pf24bit;
b2.width := nw;
b2.height := nh;
// b2.canvas.StretchDraw(rect(0, 0, nw, nh), b);
If Stretch = smStretch Then Begin
Stretchdraw(b2.canvas, rect(0, 0, nw, nh), b, imBilinear);
End
Else Begin
Stretchdraw(b2.canvas, rect(0, 0, nw, nh), b, imNearestNeighbour);
End;
b.free;
b := b2;
End;
End;
smClamp: Begin
nw := GetNextPowerOfTwo(b.width);
nh := GetNextPowerOfTwo(b.height);
If (nw <> b.width) Or (nh <> b.height) Then Begin
b2 := TBitmap.create;
b2.PixelFormat := pf24bit;
b2.width := nw;
b2.height := nh;
b2.canvas.Draw(0, 0, b);
b.free;
b := b2;
End;
End;
End;
// load the raw image from the bitmap handles
IntfImg1.LoadFromBitmap(B.Handle, B.MaskHandle);
{$IFDEF DEBUGGOUTPUT}
writeln('OpenGL size : ' + inttostr(b.width) + 'x' + inttostr(b.height));
OpenGLBufCount := OpenGLBufCount + (b.width * b.height * 3);
writeln('OpenGL Buffer : ' + FileSizetoString(OpenGLBufCount));
{$ENDIF}
If IsPowerOfTwo(b.width) And IsPowerOfTwo(b.Height) Then Begin
// Laden der Graphikdaten
opengldata := Nil;
setlength(opengldata, b.width * b.height);
c := 0;
For j := 0 To b.height - 1 Do Begin
For i := 0 To b.width - 1 Do Begin
CurColor := IntfImg1.Colors[i, j];
OpenGLData[c, 0] := CurColor.Red Div 256;
OpenGLData[c, 1] := CurColor.green Div 256;
OpenGLData[c, 2] := CurColor.blue Div 256;
inc(c);
End;
End;
// Übergeben an OpenGL
glGenTextures(1, @Result);
bool := glIsEnabled(GL_TEXTURE_2D);
If Not (Bool{$IFDEF USE_GL} = 1{$ENDIF}) Then
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, result);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, gl_RGB, b.width, b.height, 0, GL_RGB, GL_UNSIGNED_BYTE, @OpenGLData[0]);
If Not (Bool{$IFDEF USE_GL} = 1{$ENDIF}) Then
gldisable(GL_TEXTURE_2D);
IntfImg1.free;
// Übernehmen in die Engine
setlength(Fimages, high(Fimages) + 2);
Fimages[high(Fimages)].Image := Result;
Fimages[high(Fimages)].Name := data;
Fimages[high(Fimages)].Stretched := stretch;
Fimages[high(Fimages)].OrigWidth := ow;
Fimages[high(Fimages)].OrigHeight := oh;
Fimages[high(Fimages)].StretchedWidth := nw;
Fimages[high(Fimages)].StretchedHeight := nh;
Fimages[high(Fimages)].IsAlphaImage := false;
{$IFDEF DEBUGGOUTPUT}
writeln('TextureCount : ' + inttostr(high(Fimages) + 1));
{$ENDIF}
End
Else
Raise Exception.create('Error Image ' + extractfilename(name) + ' has invalid Width / Height, has to be 2^x.');
b.free;
End;
Function TOpenGL_GraphikEngine.GetInfo(Value: String): TGraphikItem;
Var
i: Integer;
Begin
Value := LowerCase(Value);
For i := 0 To high(Fimages) Do
If pos(value, Fimages[i].Name) <> 0 Then Begin
result := Fimages[i];
exit;
End;
Raise Exception.create('Error could not find "' + Value + '" in List.');
End;
Function TOpenGL_GraphikEngine.GetInfo(Value: integer): TGraphikItem;
Var
i: Integer;
Begin
For i := 0 To high(Fimages) Do
If Fimages[i].Image = Value Then Begin
result := Fimages[i];
exit;
End;
Raise Exception.create('Error could not find image "' + inttostr(value) + '" in List.');
End;
Function TOpenGL_GraphikEngine.Find(Value: String; ExceptionOnNotExists: Boolean
): integer;
Var
i: Integer;
Begin
result := 0;
Value := LowerCase(Value);
For i := 0 To high(Fimages) Do Begin
If pos(value, Fimages[i].Name) <> 0 Then Begin
result := Fimages[i].Image;
exit;
End;
End;
If ExceptionOnNotExists Then Begin
(*
Will Man Debuggen so ist es manchmal sinnvoller eine Message zu haben, anstatt einer Exception
*)
// showmessage('Error Could not Find "' + Value + '" in List.');
Raise Exception.create('Error Could not Find "' + Value + '" in List.');
End;
End;
Function TOpenGL_GraphikEngine.FindItem(Value: String;
ExceptionOnNotExists: Boolean): TGraphikItem;
Var
i: Integer;
Begin
result.Image := 0;
Value := LowerCase(Value);
For i := 0 To high(Fimages) Do Begin
If pos(value, Fimages[i].Name) <> 0 Then Begin
result := Fimages[i];
exit;
End;
End;
If ExceptionOnNotExists Then Begin
(*
Will Man Debuggen so ist es manchmal sinnvoller eine Message zu haben, anstatt einer Exception
*)