forked from thargor6/mb3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightAdjust.pas
2493 lines (2362 loc) · 87.6 KB
/
LightAdjust.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit LightAdjust;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ExtCtrls, Buttons, TypeDefinitions, Math3D,
Menus, TrackBarEx, Vcl.ImgList, Vcl.ExtDlgs;
type
TLightAdjustForm = class(TForm)
ColorDialog1: TColorDialog;
Panel3: TPanel;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
TabControl1: TTabControl;
Label8: TLabel;
Label9: TLabel;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
SpeedButton13: TSpeedButton;
SpeedButton14: TSpeedButton;
SpeedButton15: TSpeedButton;
SpeedButton16: TSpeedButton;
SpeedButton17: TSpeedButton;
SpeedButton18: TSpeedButton;
SpeedButton23: TSpeedButton;
SpeedButton20: TSpeedButton;
SpeedButton21: TSpeedButton;
SpeedButton22: TSpeedButton;
SpeedButtonMem: TSpeedButton;
SpeedButton19: TSpeedButton;
SpeedButton24: TSpeedButton;
SpeedButton25: TSpeedButton;
SpeedButton26: TSpeedButton;
SpeedButton27: TSpeedButton;
SpeedButton28: TSpeedButton;
SpeedButton29: TSpeedButton;
CheckBox4: TCheckBox;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
Label2: TLabel;
Label3: TLabel;
TrackBar1: TTrackBar;
TrackBar2: TTrackBar;
TabSheet2: TTabSheet;
Label13: TLabel;
Label14: TLabel;
TrackBar15: TTrackBarEx;
TrackBar16: TTrackBarEx;
Label15: TLabel;
TrackBar17: TTrackBarEx;
UpDown1: TUpDown;
UpDown2: TUpDown;
UpDown3: TUpDown;
ButtonGetPos: TButton;
CheckBox6: TCheckBox;
CheckBox7: TCheckBox;
Panel4: TPanel;
TrackBar18: TTrackBar;
Label16: TLabel;
Label17: TLabel;
PageControl2: TPageControl;
TabSheet3: TTabSheet;
TabSheet4: TTabSheet;
Label4: TLabel;
TrackBar11: TTrackBar;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
TrackBar5: TTrackBar;
TrackBar7: TTrackBar;
SpeedButton3: TSpeedButton;
SpeedButton6: TSpeedButton;
SpeedButton10: TSpeedButton;
SpeedButton11: TSpeedButton;
TrackBar4: TTrackBar;
TrackBar8: TTrackBar;
CheckBox3: TCheckBox;
CheckBox9: TCheckBox;
Label21: TLabel;
Label22: TLabel;
SpeedButton9: TSpeedButton;
TrackBar23: TTrackBar;
Label23: TLabel;
TabSheet5: TTabSheet;
Label6: TLabel;
Label19: TLabel;
Label20: TLabel;
CheckBox8: TCheckBox;
TrackBar20: TTrackBar;
TrackBar21: TTrackBar;
TrackBar22: TTrackBar;
Label24: TLabel;
ComboBox3: TComboBox;
SpeedButton5: TSpeedButton;
SpeedButton7: TSpeedButton;
SpeedButton8: TSpeedButton;
CheckBox11: TCheckBox;
CheckBox12: TCheckBox;
CheckBox13: TCheckBox;
CheckBox5: TCheckBox;
CheckBox10: TCheckBox;
Label28: TLabel;
TrackBar24: TTrackBar;
TabSheet6: TTabSheet;
Label29: TLabel;
Label30: TLabel;
Label31: TLabel;
TrackBar25: TTrackBar;
TrackBar26: TTrackBar;
TrackBar27: TTrackBar;
Image3: TImage;
CheckBox14: TCheckBox;
Panel1: TPanel;
Label5: TLabel;
Label7: TLabel;
SBFineAdj: TSpeedButton;
Label10: TLabel;
Label11: TLabel;
Image1: TImage;
Image2: TImage;
Label12: TLabel;
Label27: TLabel;
TrackBar9: TTrackBar;
TrackBar10: TTrackBar;
TrackBar12: TTrackBar;
TrackBar13: TTrackBar;
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
TrackBar14: TTrackBar;
Panel2: TPanel;
Label34: TLabel;
Label35: TLabel;
Label36: TLabel;
TrackBar28: TTrackBar;
TrackBar29: TTrackBar;
Label37: TLabel;
Image4: TImage;
CheckBox15: TCheckBox;
Label39: TLabel;
Label40: TLabel;
Label41: TLabel;
TrackBar30: TTrackBar;
SpeedButton31: TSpeedButton;
SpeedButton32: TSpeedButton;
CheckBox17: TCheckBox;
PopupMenu1: TPopupMenu;
CopythislighttoLight11: TMenuItem;
CopythislighttoLight21: TMenuItem;
CopythislighttoLight31: TMenuItem;
CopythislighttoLight41: TMenuItem;
CopythislighttoLight51: TMenuItem;
CopythislighttoLight61: TMenuItem;
CheckBox18: TCheckBox;
Image5: TImage;
RadioGroup1: TRadioGroup;
Label38: TLabel;
TrackBar31: TTrackBar;
Fog: TTabSheet;
Label1: TLabel;
FogResetButton: TSpeedButton;
Label18: TLabel;
SpeedButton4: TSpeedButton;
SpeedButton30: TSpeedButton;
TrackBar3: TTrackBar;
TrackBar6: TTrackBar;
TrackBar19: TTrackBar;
CheckBox19: TCheckBox;
Label42: TLabel;
TrackBar32: TTrackBar;
Label43: TLabel;
CheckBox16: TCheckBox;
Label44: TLabel;
Edit21: TEdit;
UpDownDiffMap: TUpDown;
Edit2: TEdit;
UpDownLight: TUpDown;
CheckBox20: TCheckBox;
CheckBox21: TCheckBox;
Label25: TLabel;
ComboBox4: TComboBox;
Edit1: TEdit;
UpDown4: TUpDown;
Label26: TLabel;
ComboBox5: TComboBox;
SpeedButton12: TSpeedButton;
Label32: TLabel;
TrackBar33: TTrackBar;
Label33: TLabel;
Label45: TLabel;
PopupMenu2: TPopupMenu;
ImageList1: TImageList;
N01: TMenuItem;
SpeedButton33: TSpeedButton;
OpenDialogPic: TOpenPictureDialog;
Label46: TLabel;
CheckBox22: TCheckBox;
CheckBox23: TCheckBox;
OpenPictureDialog1: TOpenPictureDialog;
SpeedButton34: TSpeedButton;
PopupMenu3: TPopupMenu;
Insertvolumetriclightcolor1: TMenuItem;
Label47: TLabel;
procedure TrackBar2Change(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure TabControl1Change(Sender: TObject);
// procedure RadioGroup1Click(Sender: TObject);
// procedure RadioGroup2Click(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
procedure TrackBarYangleChange(Sender: TObject);
procedure SpeedButton15Click(Sender: TObject);
procedure SpeedButtonMemClick(Sender: TObject);
procedure SBFineAdjClick(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
procedure CheckBox2Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
procedure CheckBox4Click(Sender: TObject);
procedure PageControl1Change(Sender: TObject);
procedure TrackBar16Change(Sender: TObject);
// procedure CheckBox5Click(Sender: TObject);
procedure UpDown1Click(Sender: TObject; Button: TUDBtnType);
procedure Edit1Change(Sender: TObject);
procedure ButtonGetPosClick(Sender: TObject);
procedure PageControl1Changing(Sender: TObject;
var AllowChange: Boolean);
procedure TabControl1Changing(Sender: TObject;
var AllowChange: Boolean);
procedure FogResetButtonClick(Sender: TObject);
procedure CheckBox6Click(Sender: TObject);
procedure CheckBox7Click(Sender: TObject);
procedure CheckBox8Click(Sender: TObject);
procedure TrackBar21KeyPress(Sender: TObject; var Key: Char);
procedure TrackBar11KeyPress(Sender: TObject; var Key: Char);
procedure SpeedButton9MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ComboBox3Select(Sender: TObject);
procedure ComboBox3DropDown(Sender: TObject);
procedure ComboBox4Change(Sender: TObject);
procedure SpeedButton3MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure SpinButton1Down;
procedure SpinButton1Up;
procedure TrackBar26Change(Sender: TObject);
procedure SpinEdit1Change(Sender: TObject);
procedure FormMouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
procedure SpinEdit2Change(Sender: TObject);
procedure CheckBox15Click(Sender: TObject);
procedure CheckBox16Click(Sender: TObject);
procedure SpeedButton31Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure TabControl1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure CopythislighttoLight11Click(Sender: TObject);
procedure FormHide(Sender: TObject);
procedure TrackBar16MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure UpDown4Click(Sender: TObject; Button: TUDBtnType);
procedure CheckBox21Click(Sender: TObject);
procedure TrackBar33Change(Sender: TObject);
procedure N01Click(Sender: TObject);
procedure TrackBar22Change(Sender: TObject);
procedure CheckBox22Click(Sender: TObject);
procedure Edit2MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure SpeedButton34Click(Sender: TObject);
procedure SpeedButton4MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Insertvolumetriclightcolor1Click(Sender: TObject);
private
{ Private-Deklarationen }
LColHistoMaxN, LColInteriorHistoMaxN, LColInteriorHistoLength: Integer;
OTColHistoMaxN: Integer;
OldTB15Pos: Integer;
OldTB16Pos: Integer;
OldTB17Pos: Integer;
LastBGname: array[0..23] of Byte;
procedure SetPreset(nr: Integer; KeepLights: LongBool);
procedure GetPreset(nr: Integer);
procedure MakeGlyph(SB: TSpeedButton; PNr: Integer);
procedure UpdateTabHeader(nr: Integer);
function MakeDynFogCol: TRGB;
function VisFuncToIndex(const Light: TLight8 {Lop: Byte}): Integer;
function IndexToVisFunc(i: Integer): Byte;
function GetPageContr1index(Loption: Byte): Integer;
function TBChanged: LongBool;
function OverForm(p: TPoint): LongBool;
procedure MakeDepthColList;
function VisLightExBit(const Light: TLight8): Integer;
public
{ Public-Deklarationen }
bUserChange: Boolean;
TBcolStartPos, TBcolStopPos: Integer; // Coarse Color Adjustments
LAtmpLight: TLightingParas9; //ersetzt die privaten obigen cols+lights
LightMaps: array[0..5] of TLightMap;
DiffColMap: TLightMap;
LAposMids: array[0..5] of TPos3D;
ColorFormCreated: LongBool;
procedure SetSButtonColor(ButtonNr: Integer; Color: TRGB);
// procedure WndProc(var Message: TMessage); override;
procedure RepaintColHisto;
procedure MakeHisto;
procedure PutLightFInHeader(var Header: TMandHeader10);
procedure SetLightFromHeader(var Header: TMandHeader10);
procedure SetStartPreset;
procedure SetSDButtonColors;
procedure SetLAplusPosToZero;
procedure UpdateQuickLoadCB;
procedure SetPosLightTo0(tab: Integer);
end;
procedure SetCosTabFunction;
function GetCosTabVal(const Tnr: Integer; const DotP, Rough: Single): Single;
function GetCosTabValSqr(const Tnr: Integer; const DotP, Rough: Single): Single;
function FastPowLUT(base, expo: Single): Single; //for lighting spec calculation
function GetDiffMapNr(LightPars: TPLightingParas9): Integer;
function GetCosTabValNavi(Tnr, i1, i2: Integer): Single;
function GetGaussFuncNavi(iL1, iL2: Integer): Single;
procedure PaintSDPreviewColors(LAtmpLight: TPLightingParas9; CanvasS, CanvasD: TCanvas; Wid: Integer);
var
LightAdjustForm: TLightAdjustForm;
LAFormCreated: LongBool = False;
bLFfirstShow: LongBool = True;
DiffCosTabNavi: array[0..3, 0..127] of Single; // old one for navigator + imageprocess
// GaussTab: array of Single; //for navi spec calculation (power..)
DiffCosTabNsmall: array[0..7, 0..127] of Single;
PowerTabSmall: array[0..127] of Single;
GaussTabSmall: array[0..127] of Single;
LastZoom: Double = 0;
LastPositionDMScale: array[0..3] of Integer = (0,0,0,0);
{ DiffNormalPicArr: array of Cardinal; //-> lightmap ..todo: possible lightmaps for all lights
DNPWidth, DNPHeight: Integer;
DNPstart, DNPYinc: Integer;
DNPXfactor, DNPYfactor: Single; }
Presets: array[1..5] of TLpreset164 =
((AmbTop: $B1AA9F; AmbBot: $3464AA; DepthCol: $B1AA9F; DepthCol2: $3464AA; //Sand
ColDif: ($2537A0, $4E9FD1, $62AEB3, $71808E);
ColSpec: ($0B1026, $132734, $273639, $1C2023);
Lights: ((Loption: 0; LFunction: 51; Lcolor: $FFFFFF; LXangle: 3363; LYangle: 4000), //spec,dif,amb
(Loption: 0; LFunction: 3; Lcolor: $34628F; LXangle: -4915; LYangle: -6500)); TB578pos: (50, 50, 90)),
// $6797C7
(AmbTop: $FF6000; AmbBot: $40B0; DepthCol: $800000; DepthCol2: $50;
ColDif: ($E0, $FF0000, $B800, $ABDD); //Slime
ColSpec: ($F0F0F0, $F0F0F0, $F0F0F0, $F0F0F0);
Lights: ((Loption: 0; LFunction: 3; Lcolor: $FFFFFF; LXangle: 0; LYangle: 4005),
(Loption: 1; LFunction: 3; Lcolor: $FFFFFF; LXangle: 0; LYangle: 0)); TB578pos: (110, 22, 220)),
(AmbTop: $E89660; AmbBot: $304860; DepthCol: $A04B18; DepthCol2: $D0A488;
ColDif: ($1D85F8, $C0C0C0, $47CCF8, $B9B69B); //Metallic
ColSpec: ($1D85F8, $C0C0C0, $47CCF8, $B9B69B);
Lights: ((Loption: 0; LFunction: 2; Lcolor: $AADDFF; LXangle: -637; LYangle: 4915),
(Loption: 0; LFunction: 2; Lcolor: 2894962; LXangle: 3368; LYangle: -6463)); TB578pos: (160, 50, 30)),
(AmbTop: $EC974A; AmbBot: $5976A6; DepthCol: $EC974A; DepthCol2: $5976A6;
ColDif: ($204080, $349120, $1E36C1, $C5AAE6); //Flower
ColSpec: (0, $40491C, $3B3D60, $60487F);
Lights: ((Loption: 0; LFunction: 51; Lcolor: $B9EFFF; LXangle: 0; LYangle: 3000),
(Loption: 1; LFunction: 3; Lcolor: $34628F; LXangle: -4915; LYangle: -6500)); TB578pos: (50, 70, 90)),
// (AmbTop: $A0A0A0; AmbBot: $A0A0A0; DepthCol: $A0A0A0; DepthCol2: $A0A0A0;
(AmbTop: $905838; AmbBot: $8BA8C7; DepthCol: $905838; DepthCol2: $8BA8C7;
ColDif: ($A0A0A0, $A0A0A0, $A0A0A0, $A0A0A0); //Neutral
ColSpec: ($404040, $404040, $404040, $404040);
Lights: ((Loption: 0; LFunction: 52; Lcolor: $B9EFFF; LXangle: 4063; LYangle: 6405),
(Loption: 1; LFunction: 52; Lcolor: $6797C7; LXangle: -4915; LYangle: -7100)); TB578pos: (50, 70, 90)));
CustomPresets: array[6..15] of TLpreset20;
LColHisto: array[0..32767] of Integer;
OTColHisto: array[0..32767] of Integer;
LColInteriorHisto: array of Integer;
TBoldpos: array[0..24] of Integer;
//49 = 32 + 16 + 1 = diff3 + 16pow -> 0+32pow=2
const //LFunction: 3(4)bit spec func + 2bit diff, Pow = 8 shl (LFunction and $07), diff = (LFunction shr 4) and 3
defaultLight8: TLight8 = (Loption: 1; LFunction: 4; Lamp: 10; Lcolor: ($FF, $FF, $FF); LightMapNr: 0;
LXpos: (0,0,0,0,0,0,0); AdditionalByteEx: 0; LYpos: (0,0,0,0,0,0,0); FreeByte: 0; LZpos: (0,0,0,0,0,0,0));
defaultLight7: TLight7 = (Loption: 1; LFunction: 3; Lcolor: $FFFFFF; LXangle: 0; LYangle: 0);
{ StartPreset: TLpreset16 =
(Cols: (5873889, 8837614, $8B491D, 2988346, 12248958, $FFC49F, 11287584, 14579248, 7481121);
Lights: ((Loption: 0; LFunction: 50; Lcolor: $A0E8FF;
LXangle: 3368; LYangle: 5279), (Loption: 1; LFunction: 16; Lcolor: 2307911; //60
LXangle: -2822; LYangle: -7737),(),()); DepthCol: $8B491D; TB578pos: (34, 102, 90); DepthCol2: $FFC49F; Version: 1);
} StartPreset: TLpreset16 =
(Cols: (5873889, 8837614, $8B491D, 2988346, 12248958, $FFC49F, 11287584, 14579248, 7481121);
Lights: ((Loption: 0; LFunction: 53; Lcolor: $A0E8FF; LXangle: 1500; LYangle: 5200),
(Loption: 1; LFunction: 16; Lcolor: 2307911; LXangle: -2822; LYangle: -7737),
(),()); DepthCol: $8B491D; TB578pos: (50, 120, 90); DepthCol2: $FFC49F; Version: 1);
//spec,dif,amb }
{ StartPreset: TLpreset16 =
(Cols: ($58B0F8, $101010, $8B491D, $28A050, $206030, $FFC49F, $A07020, $B0B0B0, $DE7630);
Lights: ((Loption: 0; LFunction: 5; Lcolor: $A0E8FF; LXangle: 0; LYangle: 5000),
(Loption: 1; LFunction: 53; Lcolor: $233747; LXangle: -2822; LYangle: -7737),
(),()); DepthCol: $A05018; TB578pos: (50, 100, 90); DepthCol2: $E0D0B0; Version: 1); //}
implementation
uses Mand, CalcThread, DivUtils, Math, PaintThread, FileHandling, ImageProcess,
Animation, ColorPick, Interpolation, Undo, PostProcessForm, HeaderTrafos,
Maps, Navigator;
{$R *.dfm}
{
Loption: Byte; // bit1: 0: On 1: Off; bit2: lightmap; bit3 = bPosLight, bit4+5 = poslight visible+func, bit6 = global light rel to object, bit7 = HSon
LFunction: Byte; // 4bit spec func + 2bit diff, Spec expo = 8 shl (LFunction and $07), diff = (LFunction shr 4) and 3
Lamp: Word; // Light amplitude for posLight -> exp 8bit shortint + 8bit byte mant for wide range! -> for all lights
Lcolor: TRGB; // RGB 24bit
LightMapNr: Word; // 0: no LM, 1..32000: LMnr, LM works as ambient light was byte, now with ..Ex as word!
// LXpos, LYpos, LZpos: Double; // not posLight: LXpos=LXangle, LYpos=LYangle todo?: only 7 bytes precision + 3 extra bytes for LMNRex,RealSpecExpo + 1 free
// LightMapNrEx: Byte;
LXpos: Double7B;
AdditionalByteEx: Byte; //LVersionEx in Light1 and DiffMapNrEx in Light2
LYpos: Double7B;
FreeByte: Byte;
LZpos: Double7B;
procedure TLightAdjustForm.MakeColTabsFromLightVals(LVals: TLightVals);
var x, y, iL1, iL2: Integer;
sc, sp, dT3: Single;
ps2, ps3: PSingle;
begin
ps2 := @ObjectColDiff[0][0];
ps3 := @ObjectColSpec[0][0];
sc := -LVals.sCStart * LVals.sCmul; //Startval + inc... (0..3) or (3..0)
sp := LVals.sCmul;
// for y := 0 to 1 do
begin
sc := Abs(sc - Trunc(sc * 0.3333333333333333) * 3);
if sc > 2 then
begin
sc := sc - 2;
iL1 := 1;
iL2 := 3;
end else if sc > 1 then
begin
sc := sc - 1;
if sc > 1 then sc := 1;
iL1 := 3;
iL2 := 2;
end else begin
if sc < 0 then sc := 0;
iL1 := 2;
iL2 := 1;
end;
for x := 0 to 32767 do
with LVals do
begin
dT3 := 1 - sc;
ps2^ := sc * PLColS^[iL1, 1, 0] + dT3 * PLColS^[iL2, 1, 0];
Inc(ps2);
ps2^ := sc * PLColS^[iL1, 1, 1] + dT3 * PLColS^[iL2, 1, 1];
Inc(ps2);
ps2^ := sc * PLColS^[iL1, 1, 2] + dT3 * PLColS^[iL2, 1, 2];
ps3^ := sc * PLColS^[iL1, 2, 0] + dT3 * PLColS^[iL2, 2, 0];
Inc(ps3);
ps3^ := sc * PLColS^[iL1, 2, 1] + dT3 * PLColS^[iL2, 2, 1];
Inc(ps3);
ps3^ := sc * PLColS^[iL1, 2, 2] + dT3 * PLColS^[iL2, 2, 2];
sc := sc + sp;
if sc > 1 then
begin
end
else if sc < 0 then
begin
end;
Inc(ps2);
Inc(ps3);
end;
sc := -LVals.sCiStart * LVals.sCimul;
sp := LVals.sCimul;
end;
end; }
function GetDiffMapNr(LightPars: TPLightingParas9): Integer;
begin
Result := LightPars.bColorMap or (LightPars.Lights[1].AdditionalByteEx shl 8);
end;
procedure TLightAdjustForm.MakeHisto;
var x: Integer;
PL: TPsiLight5;
dHL: Double;
const dm: Double = 0.0000305175;
begin
for x := 0 to 32767 do LColHisto[x] := 0;
for x := 0 to 32767 do OTColHisto[x] := 0;
LColInteriorHistoLength := Image2.Width;
SetLength(LColInteriorHisto, LColInteriorHistoLength);
for x := 0 to LColInteriorHistoLength - 1 do LColInteriorHisto[x] := 0;
PL := @Mand3DForm.siLight5[0];
dHL := LColInteriorHistoLength - 1;
for x := 0 to High(Mand3DForm.siLight5) do
begin
if PL.Zpos < 32768 then
begin
if PL.SIgradient < 32768 then
Inc(LColHisto[PL.SIgradient])
else
Inc(LColInteriorHisto[Round(Sqrt(Sqrt((PL.SIgradient - 32768) * dm)) * dHL)]);
Inc(OTColHisto[PL.OTrap and $7FFF]);
end;
Inc(PL);
end;
LColHistoMaxN := 1;
LColInteriorHistoMaxN := 1;
OTColHistoMaxN := 1;
for x := 0 to 32767 do
if LColHisto[x] > LColHistoMaxN then LColHistoMaxN := LColHisto[x];
for x := 0 to 32767 do
if OTColHisto[x] > OTColHistoMaxN then OTColHistoMaxN := OTColHisto[x];
for x := 1 to LColInteriorHistoLength - 1 do
if LColInteriorHisto[x] > LColInteriorHistoMaxN then
LColInteriorHistoMaxN := LColInteriorHisto[x];
RepaintColHisto;
end;
procedure TLightAdjustForm.RepaintColHisto;
var x, y, c, x2, x3, a, x2inc: Integer;
d, dmin, dmul: Double;
begin
y := Image1.Height + 1;
with Image1.Picture.Bitmap do
begin
SetSize(Image1.Width, y - 1);
// Width := Image1.Width;
// Height := y - 1;
if SBFineAdj.Down then
begin
dmin := Sqr((TBcolStartPos + 30) / 90) * 32767 - 10900;
dmul := (Sqr((TBcolStopPos + 30) / 90) * 32767 - 10900 - dmin) / 32767;
x2 := Round(-16384 * dmul + dmin);
end else begin
dmin := 0;
dmul := 1;
x2 := 0;
end;
for x := 0 to Width - 1 do
begin
if SBFineAdj.Down then
x3 := Round((x * 65535 / (Width - 1) - 16384) * dmul + dmin)
else
x3 := Round(Sqr(x * 4 / (3 * (Width - 1))) * 32767 - 10900);
x2inc := Sign(x3 - x2);
a := 0;
d := 0;
if CheckBox2.Checked then
begin
repeat
if (x2 >= 0) and (x2 < 32768) then
begin
d := d + OTColHisto[x2];
Inc(a);
end;
x2 := x2 + x2inc;
until x2 = x3;
if (OTColHistoMaxN <> 0) and (a <> 0) then d := d / (a * OTColHistoMaxN); //was: int overflow
end else begin
repeat
if (x2 >= 0) and (x2 < 32768) then
begin
d := d + LColHisto[x2];
Inc(a);
end;
x2 := x2 + x2inc;
until x2 = x3;
if (LColHistoMaxN <> 0) and (a <> 0) then d := d / (a * LColHistoMaxN);
end;
c := 255 - Round(Sqrt(Sqrt(Clamp01D(d))) * 255);
Canvas.Pen.Color := c or (c shl 8) or (((c and $FE) + 200) shl 15);
Canvas.MoveTo(x, 0);
Canvas.LineTo(x, y);
end;
end;
y := Image2.Height + 1;
if Length(LColInteriorHisto) >= Image2.Width then
with Image2.Picture.Bitmap do
begin
Width := Image2.Width;
Height := y - 1;
for x := 0 to Width - 1 do
begin
d := LColInteriorHisto[x] / LColInteriorHistoMaxN; //read of adr 0 when starting with parameter m3p file! ->CB2.click
if d > 1 then d := 1;
c := 255 - Round(Sqrt(Sqrt(d)) * 255);
Canvas.Pen.Color := c or (c shl 8) or (((c and $FE) + 200) shl 15);
Canvas.MoveTo(x, 0);
Canvas.LineTo(x, y);
end;
end;
end;
{procedure TLightAdjustForm.WndProc(var Message: TMessage);
var xLDif, xRDif, yDif: Integer;
begin
if Message.Msg = WM_Move then
begin
yDif := Abs(Top - Mand3DForm.Top);
if yDif < 17 then
begin
xLDif := Abs(Left + Width - Mand3DForm.Left);
xRDif := Abs(Left - Mand3DForm.Left - Mand3DForm.Width);
if xLDif < 17 then FormsSticky[1] := 2 else
if xRDif < 17 then Mand3DForm.bLightFormStick := 1 else
Mand3DForm.bLightFormStick := 0;
end
else Mand3DForm.bLightFormStick := 0;
end;
inherited WndProc(Message);
end; }
procedure SetCosTabFunction; //.. function to interpolate and less vals in tab. +Also a similar power function with some tabs + ipol
var i, j, k, l: Integer;
d: Double;
e: Extended;
TmpTabSmall: array[0..127] of Single;
begin
for i := 0 to 127 do
begin
d := Cos(i * 0.0490873852123405184); //old for navi: 0 to 2pi angle
DiffCosTabNavi[0][i] := Clamp0D(d); //average: 0.318
DiffCosTabNavi[1][i] := Sqr(Clamp0D(d)); //average: 0.25
DiffCosTabNavi[2][i] := d * s05 + s05; //average: 0.5
DiffCosTabNavi[3][i] := Sqr(d * s05 + s05); //average: 0.375
d := 1 - (i - 2) / 60;
if d > 0.15 then DiffCosTabNsmall[0][i] := (d - 0.08) * 1.0869565 else
if d <= 0 then DiffCosTabNsmall[0][i] := 0 else
DiffCosTabNsmall[0][i] := Power(d, Max(1, (0.505 - d) * 3.8));
DiffCosTabNsmall[1][i] := Sqr(Clamp0D(d));
DiffCosTabNsmall[2][i] := d * s05 + s05;
DiffCosTabNsmall[3][i] := Sqr(d * s05 + s05);
d := 1 - (i - 2) / 380;
if d >= 1 then PowerTabSmall[i] := 1 else
if d <= 0 then PowerTabSmall[i] := 0 else PowerTabSmall[i] := Power(d, 38);
GaussTabSmall[i] := Power(enat, -Sqr(i * 0.0503936));
end;
for k := 0 to 3 do
begin
for j := 0 to 127 do TmpTabSmall[j] := Sqrt(Max0S(DiffCosTabNsmall[k][j]));
for j := 0 to 127 do
begin
e := 0;
for i := 0 to 60 do
begin
l := Abs(j + i - 30);
if l < 128 then e := e + TmpTabSmall[l];
end;
DiffCosTabNsmall[k + 4][j] := Sqr(e * 0.011 + Sqr(e * 0.007)); //DiffCosTabN[k + 4][Round(Min0MaxCS((j - 2) * 16384/121, 16383))];
end;
end;
// showmessage(floattostr(DiffCosTabN[6][16383]) + ' ' + floattostr(DiffCosTabN[3][16383]));
end;
function FastPowLUT(base, expo: Single): Single; //for lighting spec calculation with float exponent!
var w1: Single;
ip: Integer;
w: TSVec;
p1: TPSingleArray;
begin
if base <= 0 then Result := 0 else
if base >= 1 then Result := 1 else
begin
w1 := (1 - base) * 10 * expo;
ip := Trunc(w1);
if ip < 0 then Result := 1 else
if ip > 123 then Result := 13e-7 / (w1 - 120) else
begin
w := MakeSplineCoeff(Frac(w1));
p1 := @PowerTabSmall[ip + 1]; //0
Result := p1[0] * w[0] + p1[1] * w[1] + p1[2] * w[2] + p1[3] * w[3];
{ p1 := @PowerTabSmall[1][ip + 1];
w1 := Min0MaxCS((Expo - 8) * 0.02, 1); //weight LUT 0->1
Result := (Result + w1 * (p1[0] * w[0] + p1[1] * w[1] + p1[2] * w[2] + p1[3] * w[3] - Result)); }
end;
end;
end;
// ebp+8 eax
procedure MakeCubicWeightsFromT(const t: Single; var sv: TSVec); //all weights 6 times bigger!
const s3: Single = 3;
s6: Single = 6;
{begin
sv[3] := t*t*t;
sv[2] := 3*t*t;
sv[0] := -sv[3] + sv[2] - 2*t;
sv[1] := 3*sv[3] - 2*sv[2] - 3*t + 6;
sv[2] := -3*sv[3] + sv[2] + 6*t;
sv[3] := sv[3] - t; }
asm
fld dword [ebp + 8]
fld st
fmul st, st //t*t,t
fld st
fmul st, st(2) //t³,t²,t
fld s3
fmul st(2), st //3, t³=sv[3], 3*t²=sv[2], t
fld st(2) //sv[2], 3, sv[3], sv[2], t
fsub st, st(2) //sv[2]-sv[3], 3, sv[3], sv[2], t
fsub st, st(4) //sv[2]-sv[3]-t, 3, sv[3], sv[2], t
fsub st, st(4) //sv[2]-sv[3]-2*t, 3, sv[3], sv[2], t
fstp dword [eax] //3, sv[3], sv[2], t
fld st(1) //sv[3], 3, sv[3], sv[2], t
fmul st, st(1) //3*sv[3], 3, sv[3], sv[2], t
fsub st, st(3) //3*sv[3]-sv[2], 3, sv[3], sv[2], t
fsub st, st(3) //3*sv[3]-2*sv[2], 3, sv[3], sv[2], t
fld st(4)
fmul st, st(2)
fsubp
fadd s6
fstp dword [eax + 4]
fmul st, st(1)
fsubp st(2), st //t³,3*t²-3*t³,t
fsub st, st(2)
fstp dword [eax + 12] //3*t²-3*t³,t
fxch
fmul s6
faddp
fstp dword [eax + 8]
end;
//0..16383
function GetGaussFuncNavi(iL1, iL2: Integer): Single;
var i1, i2: Integer;
t1, t2: Single;
p1, p2: TPSingleArray;
const s1d128: Single = 1/128;
begin
i1 := iL1 shr 7;
i2 := iL2 shr 7;
t1 := (iL1 and $7F) * s1d128;
t2 := (iL2 and $7F) * s1d128;
if i1 > 126 then
begin
i1 := 126;
t1 := 1;
end;
if i2 > 126 then
begin
i2 := 126;
t2 := 1;
end;
p1 := @GaussTabSmall[i1];
p2 := @GaussTabSmall[i2];
Result := (p1[0] + t1 * (p1[1] - p1[0])) * (p2[0] + t2 * (p2[1] - p2[0]));
end;
// 0..16383 +edges
function GetCosTabValNavi(Tnr, i1, i2: Integer): Single;
var i1t, i2t: Integer;
t1, t2: Single;
p1, p2: TPSingleArray;
const s1d128: Single = 1/128;
begin
i1t := (i1 and $7FFF) shr 1;
i2t := (i2 and $7FFF) shr 1;
t1 := (i1t and $7F) * s1d128;
t2 := (i2t and $7F) * s1d128;
i1t := i1t shr 7;
i2t := i2t shr 7;
if i1t > 126 then
begin
i1t := 126;
t1 := 1;
end;
if i2t > 126 then
begin
i2t := 126;
t2 := 1;
end;
p1 := @DiffCosTabNavi[Tnr][i1t];
p2 := @DiffCosTabNavi[Tnr][i2t];
Result := (p1[0] + t1 * (p1[1] - p1[0])) * (p2[0] + t2 * (p2[1] - p2[0]));
end;
// 0..3 -1..1 0..1 0..1
function GetCosTabVal(const Tnr: Integer; const DotP, Rough: Single): Single;
var ip: Integer;
t: Single;
w: TSVec;
p1: TPSingleArray;
begin //new function for tabs[0..(127)255] (62)122 is midpoint=0 of DotP
t := 62 - 60 * DotP;
ip := Trunc(t) - 1;
if ip < 0 then
begin
ip := 0;
t := 0;
end
else if ip > 124 then
begin
ip := 124;
t := 1;
end
else t := Frac(t);
w := MakeSplineCoeff(t); // MakeCubicWeightsFromT(t, w);
if SupportSSE then
asm
mov edx, Tnr
shl edx, 7
add edx, ip
lea eax, DiffCosTabNsmall + edx * 4
movups xmm2, w
movups xmm0, [eax]
movups xmm1, [eax + $800]
mulps xmm0, xmm2
mulps xmm1, xmm2
movaps xmm3, xmm0
unpcklps xmm3, xmm1
unpckhps xmm0, xmm1
addps xmm3, xmm0
movhlps xmm0, xmm3
addps xmm3, xmm0
movaps xmm2, xmm3
shufps xmm2, xmm2, 1
subss xmm2, xmm3
mulss xmm2, Rough
addss xmm2, xmm3
movss Result, xmm2
end
else
begin
p1 := @DiffCosTabNsmall[Tnr][ip];
Result := p1[0] * w[0] + p1[1] * w[1] + p1[2] * w[2] + p1[3] * w[3];
p1 := @DiffCosTabNsmall[Tnr + 4][ip];
Result := (Result + Rough * (p1[0] * w[0] + p1[1] * w[1] + p1[2] * w[2] + p1[3] * w[3] - Result));
end;
end;
function GetCosTabValSqr(const Tnr: Integer; const DotP, Rough: Single): Single;
var ip: Integer;
t: Single;
w: TSVec;
p1: TPSingleArray;
begin //new function for tabs[0..127] 62 is midpoint=0 of DotP
t := 62 - 60 * DotP;
ip := Trunc(t) - 1;
if ip < 0 then
begin
ip := 0;
t := 0;
end
else if ip > 124 then
begin
ip := 124;
t := 1;
end
else t := Frac(t);
w := MakeSplineCoeff(t);
if SupportSSE then
asm
mov edx, Tnr
shl edx, 7
add edx, ip
lea eax, DiffCosTabNsmall + edx * 4
movups xmm2, w
movups xmm0, [eax]
movups xmm1, [eax + $800]
mulps xmm0, xmm2
mulps xmm1, xmm2
movaps xmm3, xmm0
unpcklps xmm3, xmm1
unpckhps xmm0, xmm1
addps xmm3, xmm0
movhlps xmm0, xmm3
addps xmm3, xmm0
mulps xmm3, xmm3
movaps xmm2, xmm3
shufps xmm2, xmm2, 1
subss xmm2, xmm3
mulss xmm2, Rough
addss xmm2, xmm3
movss Result, xmm2
end
else
begin
p1 := @DiffCosTabNsmall[Tnr][ip];
Result := Sqr(p1[0] * w[0] + p1[1] * w[1] + p1[2] * w[2] + p1[3] * w[3]);
p1 := @DiffCosTabNsmall[Tnr + 4][ip];
Result := (Result + Rough * (Sqr(p1[0] * w[0] + p1[1] * w[1] + p1[2] * w[2] + p1[3] * w[3]) - Result));
end;
end;
procedure TLightAdjustForm.SetLightFromHeader(var Header: TMandHeader10);
var i: Integer;
begin
bUserChange := False;
with Header do
try
SBFineAdj.Down := (Light.TBoptions and $10000) > 0;
TBcolStartPos := Light.TBpos[9];
TBcolStopPos := Light.TBpos[10];
TrackBar14.Position := Light.VarColZpos;
TrackBar3.Position := Light.TBpos[3] and $FFFF;
TrackBar19.Position := Light.TBpos[3] shr 16;
TrackBar20.Position := Light.PicOffsetX;
TrackBar21.Position := Light.PicOffsetY;
TrackBar22.Position := Light.PicOffsetZ;
TrackBar23.Position := ShortInt((Light.TBpos[11] shr 8) and $FF) + 53;
TrackBar24.Position := Light.RoughnessFactor;
TrackBar11.Position := Light.TBpos[11] and $FF;
TrackBar32.Position := Light.Lights[3].AdditionalByteEx;
UpDownDiffMap.Position := GetDiffMapNr(@Light);
CheckBox15.Checked := GetDiffMapNr(@Light) <> 0;
if CheckBox15.Checked then
begin
TrackBar30.Position := (Light.TBpos[8] shr 20) and $FF;
TrackBar29.Position := (Light.TBpos[7] shr 12) and $FF;
TrackBar28.Position := (Light.TBpos[7] shr 20) and $FF;
TrackBar31.Position := Light.Lights[2].AdditionalByteEx;
end else begin
TrackBar30.Position := 128;
TrackBar29.Position := 128;
TrackBar28.Position := 128;
TrackBar31.Position := 30;
end;
for i := 4 to 10 do if not (i in [7, 8]) then
(FindComponent('TrackBar' + IntToStr(i)) as TTrackBar).Position := Light.TBpos[i];
TrackBar7.Position := Light.TBpos[7] and $FFF;
TrackBar8.Position := Light.TBpos[8] and $FFF;
LAtmpLight := Light;
SetSButtonColor(3, Light.AmbCol);
SetSButtonColor(4, MakeDynFogCol);
SetSButtonColor(6, Light.AmbCol2);
SetSButtonColor(10, Light.DepthCol);
SetSButtonColor(11, Light.DepthCol2);
SetSButtonColor(30, Light.DynFogCol2);
SetSDButtonColors;
if (Light.TBoptions and $10000) > 0 then
begin
TrackBar9.Position := Integer(Light.FineColAdj1) - 30;
TrackBar10.Position := Integer(Light.FineColAdj2) - 30;
end;
TrackBar12.Position := (Light.TBoptions and 127);
TrackBar13.Position := (Light.TBoptions shr 7) and 127;
CheckBox1.Checked := (Light.TBoptions and $4000) <> 0;
CheckBox2.Checked := (Light.TBoptions and $20000) <> 0;
CheckBox3.Checked := (Light.TBoptions and $40000) <> 0;
CheckBox12.Checked := (Light.TBoptions and $8000) = 0;
CheckBox13.Checked := (Light.TBoptions and $80000) <> 0; //smooth BGimage on load
TrackBar18.Position := (Light.TBoptions shr 23) and $3F;
CheckBox9.Checked := (Light.TBoptions and $20000000) <> 0; //BG+Amb light rel to object
CheckBox5.Checked := (Light.AdditionalOptions and $80) <> 0;//convert to spherical on load
CheckBox10.Checked := (Light.AdditionalOptions and 1) <> 0; //Internal gamma of 2
RadioGroup1.ItemIndex := Light.Lights[1].FreeByte and 3;
CheckBox16.Checked := (Light.Lights[2].FreeByte and 1) <> 0;
CheckBox17.Checked := (Light.AdditionalOptions and 8) <> 0; //Add BGpic light
CheckBox18.Checked := (Light.AdditionalOptions and 4) <> 0; //DiffMap relative to object. now: y+c comb
CheckBox19.Checked := (Light.Lights[0].FreeByte and 1) <> 0; //Blend DynFog
CheckBox23.Checked := (Light.Lights[0].FreeByte and 2) <> 0; //only add light
//change on new lighting:
CheckBox20.Checked := (Light.AdditionalOptions and 16) <> 0; //fit left+right edges in load
CheckBox21.Checked := (Light.AdditionalOptions and 32) <> 0; //small bg image for ambient
CheckBox22.Checked := (Light.Lights[3].FreeByte and 1) <> 0; //No color interpolation
TrackBar33.Position := Light.Lights[4].AdditionalByteEx;
CheckBox8.Checked := Light.BGbmp[0] <> 0; //BG picture -> try load on para load when Mid > 19!
if (Light.TBoptions and $40000000) > 0 then SpeedButton5.Down := True else
if (Light.TBoptions and $80000000) > 0 then SpeedButton8.Down := True else
SpeedButton7.Down := True;
TabControl1.TabIndex := 0;
TabControl1Change(Self);
SetLAplusPosToZero;
for i := 0 to 5 do if (Light.Lights[i].Loption and 12) > 0 then SetPosLightTo0(i);
UpdateTabHeader(-1);
if ColorFormCreated and ColorForm.Visible then ColorForm.RepaintImage(@Light, False);
if not AnsiSameText(CustomFtoStr(Light.BGbmp), CustomFtoStr(LastBGname)) then
begin
MakeLMPreviewImage(Image5, @M3DBackGroundPic);
FastMove(LAtmpLight.BGbmp, LastBGname, 24);
end;
finally
bUserChange := True;
end;
end;
procedure TLightAdjustForm.PutLightFInHeader(var Header: TMandHeader10);
var i: Cardinal;
begin
with Header do
begin
Light := LAtmpLight;
for i := 3 to 11 do Light.TBpos[i] :=
(FindComponent('TrackBar' + IntToStr(i)) as TTrackBar).Position;
Light.TBpos[3] := Light.TBpos[3] or (TrackBar19.Position shl 16); //fog far offset
Light.TBpos[11] := Light.TBpos[11] or ((ShortInt(TrackBar23.Position - 53) shl 8) and $FF00); //ambient 2nd reflection