forked from thargor6/mb3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDivUtils.pas
1668 lines (1546 loc) · 48.4 KB
/
DivUtils.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 DivUtils;
interface
uses FileHandling, TypeDefinitions, Math3D, StdCtrls, Types, Classes, Windows;
type
TRegisters = record
RegEAX, RegEBX, RegECX, RegEDX: Integer;
end;
TP6 = array[0..6] of Pointer;
procedure SaveHeaderPointers(Header: TPMandHeader10; var p6: TP6);
procedure InsertHeaderPointers(Header: TPMandHeader10; const p6: TP6);
procedure CPU_Supported;
function StrToIntTrim(s: String): Integer;
function ThreeBytesTo4Chars(PB: PByte): AnsiString;
function FourCharsTo3Bytes(s: AnsiString; var i: Integer): LongBool;
function StrToFloatK(S: String): Double;
function NonZero(d: Double): Double;
function StrLastWord(S: String): String;
function StrLastWords(S: String): String;
function StrFirstWord(S: String): String;
function StrFirstWordAfterEqual(S: String): String;
function FloatToStrSingle(s: Single): String;
function ShortFloatToStr(sf: ShortFloat): String;
function PhysikMemAvail: Cardinal;
procedure Delay(Milliseconds: Integer);
function getHiQmillis: Double;
function UpCaseN(s: String; n: Integer): String;
function SwapColor(C: Cardinal): Cardinal;
function StrSecondWord(S: String): String;
function IntToTimeStr(i: Integer): String;
function StrToFloatKtry(S: String; var d: Double): LongBool;
function CustomFtoStr(cf: array of Byte): AnsiString;
procedure PutStringInCustomF(var cf: array of Byte; s: AnsiString);
function NumberOfCPUs: Integer;
function StrOnlyNumbers(S: String): String;
function GetZPos(x, y: Integer; Header: TPMandHeader10; SL: TPSiLight5): Double;
function AllAutoProcessings(Header: TPMandHeader10): Integer;
function cIpol2colFlip(Col1, Col2: Cardinal; w1: Single): Cardinal;
procedure CalcStepWidth(Header: TPMandHeader10);
procedure CalcRealPosOffsetsFromImagePos(x, y: Integer; Header: TPMandHeader10; SL: TPSiLight5; Pos3: TPPos3D);
procedure CalcPPZvals(var Header: TMandHeader10; var Zcorr, ZcMul, ZZstmitDif: Double);
procedure PutStringInLightFilename(var fn: array of Byte; s: AnsiString);
function GetStringFromLightFilename(var fn: array of Byte): AnsiString;
function StepWidthFOV(zz: Double; Header: TPMandHeader10): Double;
function Zdistance(LiPos: TPVec3D; Header: TPMandHeader10): Double;
procedure CalcXYposOfPosLight(pos3: TPVec3D; Header: TPMandHeader10; var X, Y: Double);
function SameCustomFNames(PA1, PA2: PTHeaderCustomAddon; AltNr: Integer): Boolean;
function StrToIntTry(s: String; default: Integer): Integer;
function EditToInt(var E: TEdit; var i: Integer): Boolean;
procedure FastMove(const Source; var Dest; count: Integer);
//function MakeViewVecFromHeader(const x, y: Double; Header: TPMandHeader10): TVec3D;
procedure MinMaxClip15bit(var s: Single; var w: Word);
function CardinalToRGB(c: Cardinal): TRGB;
function RGBtoCardinal(rgb: TRGB): Cardinal;
function RGBtoSVecScale(rgb: TRGB; scale: Single): TSVec;
function SVecToCol(sv: TSVec): Cardinal;
function SVecToColSqrt(sv: TSVec): Cardinal;
function DiscFreeMB(disc: String; var FreeMB: Integer): LongBool;
function ColToSVec(c: Cardinal; bSqr: LongBool): TSVec;
function ColAToSVec(c: Cardinal; bSqr: LongBool): TSVec;
function ColToSVecFlipRBNoScale(c: Cardinal; bSqr: LongBool): TSVec;
function ColToSVecNoScale(c: Cardinal): TSVec;
function ColToSVecScale(c: Cardinal; scale: Single): TSVec;
function RGBColToSVecNoScaleSQR(rgb: TRGB): TSVec;
function RGBColToSVecNoScale(rgb: TRGB): TSVec;
function SVecToColNoScale(sv: TSVec): Cardinal;
function SVecToColNoScaleFlipXZ(var sv: TSVec): Cardinal;
function WordColToSVecScale(w3: TP3word; scale: Single): TSVec;
function WordColToSVecDownScale(w: TP3word): TSVec;
function WordColToSVecFlipRBScale(w3: TP3word; scale: Single; bSqr: LongBool): TSVec;
function SVecToWordCol(sv: TSVec): T3word;
function FlipRBCol(c: Cardinal): Cardinal;
function FlipXZsvec(sv: TSVec): TSVec;
function ColToSVecFlipRBc(c: Cardinal): TSVec;
function DoubleToMCRGB(const d: Double): TRGB;
procedure DoubleToMCRGBv(const d: Double; mcrgb: TPRGB);
procedure SVecToMCRGB(MCrecord: TPMCrecord; Light: TSVec);
function MCRGBToSVec(MCrecord: TPMCrecord): TSVec;
function MCRGBtoDouble(rgb: TPRGB): Double; //24bit to -1..7
function IntToStrL(i, l: Integer): String;
function DVecFromLightPos(var Light: TLight8): TVec3D;
procedure SetLightPosFromDVec(var Light: TLight8; var v: TVec3D);
procedure FlipInt(var i1, i2: Integer);
procedure FlipSingle(var i1, i2: Single);
function THreadPrioToByte(TP: TTHreadPriority): Byte;
function dDateTimeToStr(ms: TDateTime): String;
function ByteToThreadPrio(b: Byte): TTHreadPriority;
procedure BringToFront2(h1: HWND);
procedure GetTilingInfosFromHeader(Header: TPMandHeader10; var TileRect: TRect; var Crop: Integer);
procedure SetTilingInfosInHeader(Header: TPMandHeader10; var BigRenderData: TBigRenderData; TilePos: TPoint);
function GetTileSize(Header: TPMandHeader10): TPoint;
procedure DisableTiling(Header: TPMandHeader10);
procedure GetPaintTileSizes(Header: TPMandHeader10; var wid, hei, Xplus, Yplus: Integer);
function SetThreadExecutionState(esFlags: EXECUTION_STATE): EXECUTION_STATE;
function ProgramVersionStr(sver: Single): String;
function D2ByteToStr(b: Byte): String; //0.00 to 2.50
function StrToD2Byte(s: String): Byte;
function DateToStrHistory(dt: TDateTime): String;
function TimeToStrHistory(dt: TDateTime): String;
procedure IniLVal(LVal: TPLightVals);
function MaxOfColor(c: Cardinal): Cardinal;
function TransparencyToColor(I: Cardinal): Cardinal;
function ColToSVecFlipRB_word(w3: TP3word): TSVec;
function CPchangeToDPI(i: Integer): Integer;
function ExtractAuthorsFromPara(para10: TPMandHeader10): AuthorStrings;
procedure InsertAuthorsToPara(Header: TPMandHeader10; var AStr: AuthorStrings);
function CheckAuthorValid(const AStr: String): LongBool;
procedure ConvertNewMCRtoOld(mcrO: TPMCrecord; mcrN: TPMCrecordNew);
function GetFirstNumberFromFilename(S: String): String;
var
SupportMMX : Boolean = False;
SupportSSE : Boolean = False;
SupportSSE2: Boolean = False;
SupportSSE3: Boolean = False;
SupportSSSE3: Boolean = False;
SupportSSE41: Boolean = False;
hasSIMDlevel: Integer = 0;
SIMDbuf: array[0..511] of Byte;
PAligned16: Pointer; //for aligned constants to use in SSE2 asm
HiQCounterMul: Double;
Fi64: Int64;
implementation
uses SysUtils, Math, formulas, Forms, HeaderTrafos, Mand, Graphics, Maps;
{$CODEALIGN 16}
procedure SaveHeaderPointers(Header: TPMandHeader10; var p6: TP6);
var i: Integer;
begin
for i := 0 to 5 do p6[i] := Header.PHCustomF[i];
p6[6] := Header.PCFAddon;
end;
procedure InsertHeaderPointers(Header: TPMandHeader10; const p6: TP6);
var i: Integer;
begin
for i := 0 to 5 do Header.PHCustomF[i] := p6[i];
Header.PCFAddon := p6[6];
end;
function CheckAuthorValid(const AStr: String): LongBool;
var i, l, nv: Integer;
begin
Result := True;
l := Length(AStr);
if l = 0 then Exit else if l > 16 then
begin
Result := False;
Exit;
end;
nv := 0;
for i := 1 to l do
if Ord(AStr[i]) in [32, 48..57, 65..90, 97..122] then Inc(nv);
Result := nv > (l div 2);
end;
function ExtractAuthorsFromPara(para10: TPMandHeader10): AuthorStrings;
var i, i2, n, bitoffset, byteoffset, bitrest: Integer;
p: PByte;
begin
Result[1] := '';
if para10.MandId < 41 then
begin
Result[0] := '?';
Exit;
end;
Result[0] := '';
for n := 0 to 1 do
begin
p := @para10.PHCustomF[0];
Inc(p, n * 14);
for i := 0 to 15 do
begin
bitoffset := i * 7;
byteoffset := bitoffset div 8;
bitrest := bitoffset - byteoffset * 8;
i2 := (PInteger(Integer(p) + byteoffset)^ shr bitrest) and $7F;
if i2 = 0 then Break;
Result[n] := Result[n] + Chr(i2);
end;
end;
if (Result[0] = '') or not CheckAuthorValid(Result[0]) then Result[0] := '?';
if not CheckAuthorValid(Result[1]) then Result[1] := '';
end;
procedure InsertAuthorsToPara(Header: TPMandHeader10; var AStr: AuthorStrings);
var i2, n, byteindex, ASindex, bitsleft, l: Integer;
p: PByte;
begin
Header.MandId := actMandId;
Header.sM3dVersion := M3dVersion;
for n := 0 to 1 do
begin
p := @Header.PHCustomF[0];
Inc(p, n * 14);
l := Length(AStr[n]);
ASindex := 1;
bitsleft := 0;
i2 := 0;
for byteindex := 0 to 13 do
begin
while bitsleft < 8 do
begin
if ASindex <= l then
begin
i2 := i2 or (Integer(Ord(AStr[n][ASindex]) and $7F) shl bitsleft);
Inc(ASindex);
Inc(bitsleft, 7);
end
else bitsleft := 200;
end;
p^ := i2 and $FF;
Inc(p);
i2 := i2 shr 8;
Dec(bitsleft, 8);
end;
end;
end;
function CPchangeToDPI(i: Integer): Integer;
begin
Result := (i * Screen.PixelsPerInch) div 96;
end;
function TransparencyToColor(I: Cardinal): Cardinal;
begin
I := I shr 24;
Result := I or (I shl 8) or (I shl 16);
end;
procedure IniLVal(LVal: TPLightVals);
var i: Integer;
begin
LVal.iColOnOT := 0;
for i := 0 to 5 do LVal.iLightOption[i] := 1;
LVal.PLValigned := TPLValigned((Integer(@LVal.LColSbuf[0]) + 15) and $FFFFFFF0);
LVal.DiffColLightMap := nil;
for i := 0 to 5 do LVal.LLightMaps[i] := nil;
end;
function DateToStrHistory(dt: TDateTime): String;
begin
Result := FormatDateTime('yyyy"-"mm"-"dd', dt);
end;
function TimeToStrHistory(dt: TDateTime): String;
begin
Result := FormatDateTime('hh"h"nn"m"ss"s"', dt);
end;
function D2ByteToStr(b: Byte): String; //0.00 to 2.50
begin
Result := IntToStr(b);
if Length(Result) < 3 then Result := StringOfChar('0', 3 - Length(Result)) + Result;
Result := Result[1] + '.' + Result[2] + Result[3];
end;
function StrToD2Byte(s: String): Byte;
var d: Double;
begin
if not TryStrToFloat(Trim(s), d) then d := 0;
Result := Round(Min0MaxCD(d, 2.5) * 100);
end;
function SetThreadExecutionState(esFlags: EXECUTION_STATE): EXECUTION_STATE;
var
// _STES: SetThreadExecutionState(esFlags: EXECUTION_STATE): EXECUTION_STATE; stdcall;
_STES: function(ESFlags: EXECUTION_STATE): EXECUTION_STATE; stdcall;
K32: HMODULE;
begin
K32 := GetModuleHandle(PChar('kernel32.dll'));
@_STES := GetProcAddress(K32, PChar('SetThreadExecutionState'));
if @_STES <> nil then Result := _STES(esFlags);
end;
procedure GetTilingInfosFromHeader(Header: TPMandHeader10; var TileRect: TRect; var Crop: Integer);
var i: Integer;
Tcount, Tpos, Tsize: TPoint;
begin
i := Header.TilingOptions; //32bits: 1..7:TileCountH 8..14:TileCountW 15..21:TilePosH 22..28:TilePosV 29..30: Downscale(AA)
Crop := Max(1, (i shr 28) and 3); //downscale 1..3
Tcount := Point(i and $7F, (i shr 7) and $7F);
Tpos := Point((i shr 14) and $7F, (i shr 21) and $7F);
Tsize := Point(Header.Width div Tcount.X, Header.Height div Tcount.Y);
TileRect := Rect(Tpos.X * Tsize.X - Crop, Tpos.Y * Tsize.Y - Crop,
(Tpos.X + 1) * Tsize.X + Crop - 1, (Tpos.Y + 1) * Tsize.Y + Crop - 1);
end;
procedure GetPaintTileSizes(Header: TPMandHeader10; var wid, hei, Xplus, Yplus: Integer);
var i, Crop: Integer;
Tcount, Tpos, Tsize: TPoint;
begin
if Header.TilingOptions = 0 then
begin
Xplus := 0;
Yplus := 0;
wid := Header.Width;
hei := Header.Height;
end else begin
i := Header.TilingOptions;
Crop := Max(1, (i shr 28) and 3);
Tcount := Point(i and $7F, (i shr 7) and $7F);
Tpos := Point((i shr 14) and $7F, (i shr 21) and $7F);
Tsize := Point(Header.Width div Tcount.X, Header.Height div Tcount.Y);
Xplus := Tpos.X * Tsize.X - Crop;
Yplus := Tpos.Y * Tsize.Y - Crop;
wid := Tsize.X + 2 * Crop;
hei := Tsize.Y + 2 * Crop;
end;
end;
function GetTileSize(Header: TPMandHeader10): TPoint;
var Tcount: TPoint;
Crop: Integer;
begin
if Header.TilingOptions = 0 then
Result := Point(Header.Width, Header.Height)
else
begin
Crop := Max(1, (Header.TilingOptions shr 28) and 3);
Tcount := Point(Header.TilingOptions and $7F, (Header.TilingOptions shr 7) and $7F);
Result := Point(Header.Width div Tcount.X + 2 * Crop, Header.Height div Tcount.Y + 2 * Crop);
end;
Result.X := Max(1, Min(30000, Result.X));
Result.Y := Max(1, Min(30000, Result.Y));
end;
procedure DisableTiling(Header: TPMandHeader10);
var TileRect: TRect;
Crop: Integer;
begin
if Header.TilingOptions <> 0 then
begin
GetTilingInfosFromHeader(Header, TileRect, Crop);
Header.Width := TileRect.Right - TileRect.Left + 1 - 2 * Crop;
Header.Height := TileRect.Bottom - TileRect.Top + 1 - 2 * Crop;
Header.TilingOptions := 0;
end;
end;
procedure SetTilingInfosInHeader(Header: TPMandHeader10; var BigRenderData: TBigRenderData; TilePos: TPoint);
begin
with BigRenderData do
begin
Header.TilingOptions := (brTileCountH and $7F) or ((brTileCountV and $7F) shl 7) or
((TilePos.X and $7F) shl 14) or ((TilePos.Y and $7F) shl 21) or ((brDownScale and 3) shl 28);
Header.Width := brWidth;
Header.Height := brHeight;
Header.bImageScale := Max(1, brDownScale);
end;
end;
procedure BringToFront2(h1: HWND);
//begin
// SwitchToThisWindow(h1, False); {x = false: Size unchanged, x = true: normal size}
//function ForceForegroundWindow(h1wnd: THandle): Boolean;
const
SPI_GETFOREGROUNDLOCKTIMEOUT = $2000;
SPI_SETFOREGROUNDLOCKTIMEOUT = $2001;
var
ForegroundThreadID: DWORD;
ThisThreadID: DWORD;
timeout: DWORD;
b: LongBool;
begin
if IsIconic(h1) then ShowWindow(h1, SW_RESTORE);
if GetForegroundWindow <> h1 then
begin
SetFocus(h1);
// if (Win32Platform = VER_PLATFORM_WIN32_NT)
// Windows 98/2000 doesn't want to foreground a window when some other
// window has keyboard focus
if ((Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion > 4)) or
((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and
((Win32MajorVersion > 4) or ((Win32MajorVersion = 4) and
(Win32MinorVersion > 0)))) then
begin
// Code from Karl E. Peterson, www.mvps.org/vb/sample.htm
// Converted to Delphi by Ray Lischner
// Published in The Delphi Magazine 55, page 16
b := False;
ForegroundThreadID := GetWindowThreadProcessID(GetForegroundWindow, nil);
ThisThreadID := GetWindowThreadPRocessId(h1, nil);
if AttachThreadInput(ThisThreadID, ForegroundThreadID, True) then
begin
BringWindowToTop(h1); // IE 5.5 related hack
SetForegroundWindow(h1);
AttachThreadInput(ThisThreadID, ForegroundThreadID, False);
b := (GetForegroundWindow = h1);
end;
if not b then
begin
// Code by Daniel P. Stasinski
SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, @timeout, 0);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(0),
SPIF_SENDCHANGE);
BringWindowToTop(h1); // IE 5.5 related hack
SetForegroundWindow(h1);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(timeout), SPIF_SENDCHANGE);
end;
end
else
begin
BringWindowToTop(h1); // IE 5.5 related hack
SetForegroundWindow(h1);
end;
end;{ ForceForegroundWindow }
end;
function THreadPrioToByte(TP: TTHreadPriority): Byte;
var i: Integer;
begin
Result := 2;
for i := 0 to 4 do if TP = cTPrio[i] then
begin
Result := i;
Break;
end;
end;
function ByteToThreadPrio(b: Byte): TTHreadPriority;
begin
Result := cTPrio[Min(4, Max(0, b))];
end;
procedure FlipInt(var i1, i2: Integer);
var i: Integer;
begin
i := i1;
i1 := i2;
i2 := i;
end;
procedure FlipSingle(var i1, i2: Single);
var i: Single;
begin
i := i1;
i1 := i2;
i2 := i;
end;
function DVecFromLightPos(var Light: TLight8): TVec3D;
begin
Result[0] := D7BtoDouble(Light.LXpos);
Result[1] := D7BtoDouble(Light.LYpos);
Result[2] := D7BtoDouble(Light.LZpos);
end;
procedure SetLightPosFromDVec(var Light: TLight8; var v: TVec3D);
begin
Light.LXpos := DoubleToD7B(v[0]);
Light.LYpos := DoubleToD7B(v[1]);
Light.LZpos := DoubleToD7B(v[2]);
end;
function MCRGBtoDouble(rgb: TPRGB): Double; //24bit to -1..7 (~400 log)
begin
Result := (PInteger(rgb)^ and $FFFFFF) * 4.7683718662483612447000291764754e-7 - 1;
if Result > 2 then Result := Exp(Result - 1) - enat + 2;
end;
function DoubleToMCRGB(const d: Double): TRGB;
var c: Cardinal;
dt: Double;
begin
if d > 2 then dt := Ln(d - 2 + enat) + 2 else dt := d + 1;
c := Round(Min0MaxCD(dt, 8) * 2097151.875);
PWord(@Result)^ := c and $FFFF;
Result[2] := (c shr 16) and $FF;
end;
procedure DoubleToMCRGBv(const d: Double; mcrgb: TPRGB);
var c: Cardinal;
dt: Double;
begin
if d > 2 then dt := Ln(d - 2 + enat) + 2 else dt := d + 1;
c := Round(Min0MaxCD(dt, 8) * 2097151.875);
PCardinal(mcrgb)^ := (PCardinal(mcrgb)^ and $FF000000) or (c and $FFFFFF);
end;
{ TMCrecord = packed record
Red, Green, Blue: TRGB; //Each of them 24 bit: float 0..4 (-1..7)? stretched to 24 bit int
Ysum, Ysqr: TRGB; //15 bytes ..U,V,Ysum instead... 3 bytes left for Zdepth as Word + Options as word
RayCount: Word; //u,v as smallint + Zdepth as Cardinal?
Zbyte: Byte; //18 bytes
end;
TPMCrecord = ^TMCrecord;
TMCrecordNew = packed record
U, V: SmallInt; //4 bytes -32.768..32.767 (* 0.001)
Ysum, Ysqr: TRGB; //10 bytes
wRayCount: Word; //12 bytes
cZpos: Cardinal; //16 bytes
wStats: Word; //18 bytes
end; }
procedure ConvertNewMCRtoOld(mcrO: TPMCrecord; mcrN: TPMCrecordNew);
var vec: TVec3D;
begin
mcrO.Ysum := mcrN.Ysum;
mcrO.Ysqr := mcrN.Ysqr;
mcrO.RayCount := mcrN.wRayCount;
vec[0] := MCRGBtoDouble(@mcrN.Ysum);
vec[1] := SignedSqr(mcrN.U * 0.001);
vec[2] := SignedSqr(mcrN.V * 0.001);
YUV2RGBd(@vec);
DoubleToMCRGBv(vec[0], @mcrO.Red);
DoubleToMCRGBv(vec[1], @mcrO.Green);
DoubleToMCRGBv(vec[2], @mcrO.Blue);
mcrO.Zbyte := 128;
if mcrN.cZpos = $FFFFFFFF then mcrO.Zbyte := 0;
end;
function NoiseOfMCRGB(MCrecord: TPMCrecord): Double;
var dTmp: Double;
begin
if MCrecord.RayCount = 0 then Result := 0 else
begin
dTmp := MCRGBtoDouble(@MCrecord.Ysum);
Result := (MCRGBtoDouble(@MCrecord.Ysqr) - Sqr(dTmp)) / (MaxCD(0.01, dTmp) * MCrecord.RayCount);
end;
end;
procedure SVecToMCRGB(MCrecord: TPMCrecord; Light: TSVec);
begin
DoubleToMCRGBv(Light[0], @MCrecord.Red);
DoubleToMCRGBv(Light[1], @MCrecord.Green);
DoubleToMCRGBv(Light[2], @MCrecord.Blue);
end;
function MCRGBToSVec(MCrecord: TPMCrecord): TSVec;
begin
Result[0] := MCRGBtoDouble(@MCrecord.Red);
Result[1] := MCRGBtoDouble(@MCrecord.Green);
Result[2] := MCRGBtoDouble(@MCrecord.Blue);
Result[3] := 0;
end;
function dDateTimeToStr(ms: TDateTime): String;
var i: Integer;
begin
i := Trunc(ms);
if i >= 1 then
Result := IntToStr(i) + FormatDateTime('"d"h"h"nn"m"', ms)
else
Result := FormatDateTime('h"h"nn"m"ss"s"', ms); // 'h:nn:ss'
end;
function ColToSVecFlipRBc(c: Cardinal): TSVec;
{begin
Result[0] := ((c shr 16) and $FF);
Result[1] := ((c shr 8) and $FF);
Result[2] := (c and $FF); }
asm
add esp, -4
mov ecx, eax
shr ecx, 16
and ecx, $FF
mov [esp], ecx
fild dword [esp]
fstp dword [edx]
mov ecx, eax
shr ecx, 8
and ecx, $FF
mov [esp], ecx
fild dword [esp]
fstp dword [edx + 4]
and eax, $FF
mov [esp], eax
fild dword [esp]
fstp dword [edx + 8]
pop edx
end;
function ColToSVecFlipRB_word(w3: TP3word): TSVec;
begin
Result[0] := w3[2];
Result[1] := w3[1];
Result[2] := w3[0];
end;
{asm
add esp, -4
mov ecx, eax
shr ecx, 16
and ecx, $FF
mov [esp], ecx
fild dword [esp]
fstp dword [edx]
mov ecx, eax
shr ecx, 8
and ecx, $FF
mov [esp], ecx
fild dword [esp]
fstp dword [edx + 4]
and eax, $FF
mov [esp], eax
fild dword [esp]
fstp dword [edx + 8]
pop edx
end; }
function ColAToSVecFlipRBc(c: Cardinal): TSVec;
{begin
Result[0] := (c shr 16) and $FF;
Result[1] := (c shr 8) and $FF;
Result[2] := c and $FF);
Result[3] := c shr 24; }
asm
mov ecx, eax
shr ecx, 24
push ecx
fild dword [esp]
fstp dword [edx + 12]
mov ecx, eax
shr ecx, 16
and ecx, $FF
mov [esp], ecx
fild dword [esp]
fstp dword [edx]
mov ecx, eax
shr ecx, 8
and ecx, $FF
mov [esp], ecx
fild dword [esp]
fstp dword [edx + 4]
and eax, $FF
mov [esp], eax
fild dword [esp]
fstp dword [edx + 8]
pop edx
end;
function DiscFreeMB(disc: String; var FreeMB: Integer): LongBool;
var FreeAvailable, TotalSpace, TotalFree: TLargeInteger;
pc: array[0..2] of Char;
begin
StrPCopy(pc, Copy(disc, 1, 2)); //'C:';
Result := GetDiskFreeSpaceEx(pc, FreeAvailable, TotalSpace, @TotalFree);
if Result then
FreeMB := TotalSpace div (1024 * 1024);
end;
function FlipXZsvec(sv: TSVec): TSVec;
begin
Result[0] := sv[2];
Result[1] := sv[1];
Result[2] := sv[0];
Result[3] := sv[3];
end;
function FlipRBCol(c: Cardinal): Cardinal;
begin
Result := ((c and $FF) shl 16) or (c and $FF00) or ((c shr 16) and $FF);
end;
function ColToSVec(c: Cardinal; bSqr: LongBool): TSVec;
begin
if bSqr then
begin
Result[0] := Sqr(c and $FF) * 0.0000153787; // 0.0000304;
Result[1] := Sqr((c shr 8) and $FF) * 0.0000153787;
Result[2] := Sqr((c shr 16) and $FF) * 0.0000153787;
end else begin
Result[0] := (c and $FF) * s1d255;
Result[1] := ((c shr 8) and $FF) * s1d255;
Result[2] := ((c shr 16) and $FF) * s1d255;
end;
Result[3] := 0;
end;
function ColAToSVec(c: Cardinal; bSqr: LongBool): TSVec;
begin
if bSqr then
begin
Result[0] := Sqr(c and $FF) * 0.0000153787;
Result[1] := Sqr((c shr 8) and $FF) * 0.0000153787;
Result[2] := Sqr((c shr 16) and $FF) * 0.0000153787;
Result[3] := Sqr(c shr 24) * 0.0000153787;
end else begin
Result[0] := (c and $FF) * s1d255;
Result[1] := ((c shr 8) and $FF) * s1d255;
Result[2] := ((c shr 16) and $FF) * s1d255;
Result[3] := (c shr 24) * s1d255;
end;
end;
function ColToSVecScale(c: Cardinal; scale: Single): TSVec;
begin
Result[0] := (c and $FF) * scale;
Result[1] := ((c shr 8) and $FF) * scale;
Result[2] := ((c shr 16) and $FF) * scale;
Result[3] := 0;
end;
function ColToSVecFlipRBNoScale(c: Cardinal; bSqr: LongBool): TSVec;
begin
if bSqr then
begin
Result[0] := Sqr((c shr 16) and $FF) * s1d255;
Result[1] := Sqr((c shr 8) and $FF) * s1d255;
Result[2] := Sqr(c and $FF) * s1d255;
end else begin
Result[0] := (c shr 16) and $FF;
Result[1] := (c shr 8) and $FF;
Result[2] := c and $FF;
end;
Result[3] := 0;
end;
function ColToSVecNoScale(c: Cardinal): TSVec;
begin
Result[0] := c and $FF;
Result[1] := (c shr 8) and $FF;
Result[2] := (c shr 16) and $FF;
Result[3] := 0;
end;
function WordColToSVecScale(w3: TP3word; scale: Single): TSVec;
begin
Result[0] := w3[0] * scale; //.. in x87
Result[1] := w3[1] * scale;
Result[2] := w3[2] * scale;
end;
function WordColToSVecFlipRBScale(w3: TP3word; scale: Single; bSqr: LongBool): TSVec;
begin
if bSqr then
begin
Result[0] := Sqr(w3[2] * d1d65535) * scale;
Result[1] := Sqr(w3[1] * d1d65535) * scale;
Result[2] := Sqr(w3[0] * d1d65535) * scale;
end
else
begin
Result[0] := w3[2] * d1d65535 * scale; //.. in x87
Result[1] := w3[1] * d1d65535 * scale;
Result[2] := w3[0] * d1d65535 * scale;
end;
end;
function WordColToSVecDownScale(w: TP3word): TSVec;
begin
Result[0] := w[0] * d1d256;
Result[1] := w[1] * d1d256;
Result[2] := w[2] * d1d256;
Result[3] := 0;
end;
function RGBColToSVecNoScale(rgb: TRGB): TSVec;
begin
Result[0] := rgb[0];
Result[1] := rgb[1];
Result[2] := rgb[2];
Result[3] := 0;
end;
function RGBColToSVecNoScaleSQR(rgb: TRGB): TSVec;
begin
Result[0] := Sqr(Integer(rgb[0])) * s1d255;
Result[1] := Sqr(Integer(rgb[1])) * s1d255;
Result[2] := Sqr(Integer(rgb[2])) * s1d255;
Result[3] := 0;
end;
function SVecToWordCol(sv: TSVec): T3word;
begin
Result[0] := Round(sv[0]);
Result[1] := Round(sv[1]);
Result[2] := Round(sv[2]);
end;
function SVecToColNoScale(sv: TSVec): Cardinal;
{begin // eax eax
sv := mMinMaxSVec(0, 255, sv);
Result := Round(sv[0]) or (Round(sv[1]) shl 8) or (Round(sv[2]) shl 16); }
asm
add esp, -16
push 0
push $437f0000
lea edx, [esp + 8]
call [mMinMaxSVec]
fld dword [esp]
fistp word [esp]
fld dword [esp + 4]
fistp word [esp + 1]
fld dword [esp + 8]
fistp word [esp + 2]
mov eax, [esp]
add esp, 16
end;
function SVecToColNoScaleFlipXZ(var sv: TSVec): Cardinal;
{begin //eax eax
sv := mMinMaxSVec(0, 255, sv);
Result := Round(sv[2]) or (Round(sv[1]) shl 8) or (Round(sv[0]) shl 16);}
asm
add esp, -16
push 0
push $437f0000
lea edx, [esp + 8] //2x pushed, +8 is esp..esp+16 for svec
call [mMinMaxSVec] //mMinMaxSVec(const smin, smax: Single; const V1: TSVec): TSVec; ret8
fld dword [esp + 8] // ebp+12, ebp+8, eax edx
fistp word [esp + 8]
fld dword [esp + 4]
fistp word [esp + 9]
fld dword [esp]
fistp word [esp + 10]
mov eax, [esp + 8]
add esp, 16
end;
function SVecToCol(sv: TSVec): Cardinal;
begin
Result := Round(Min0MaxCS(sv[2], s255)) or (Round(Min0MaxCS(sv[1], s255)) shl 8) or (Round(Min0MaxCS(sv[0], s255)) shl 16);
end;
function SVecToColSqrt(sv: TSVec): Cardinal; //not in use
begin
Result := Round(Sqrt(Min0MaxCS(sv[2], s255) * s255)) or
(Round(Sqrt(Min0MaxCS(sv[1], s255) * s255)) shl 8) or
(Round(Sqrt(Min0MaxCS(sv[0], s255) * s255)) shl 16);
end;
function MaxOfColor(c: Cardinal): Cardinal;
begin
Result := Max(Max(c and 255, (c shr 8) and 255), (c shr 16) and 255);
end;
function CardinalToRGB(c: Cardinal): TRGB;
begin
Result[0] := c and $FF;
Result[1] := (c shr 8) and $FF;
Result[2] := (c shr 16) and $FF;
end;
function RGBtoCardinal(rgb: TRGB): Cardinal;
begin
Result := rgb[0] or (rgb[1] shl 8) or (rgb[2] shl 16);
end;
function RGBtoSVecScale(rgb: TRGB; scale: Single): TSVec;
begin
Result[0] := rgb[0] * scale;
Result[1] := rgb[1] * scale;
Result[2] := rgb[2] * scale;
Result[3] := 0;
end;
procedure MinMaxClip15bit(var s: Single; var w: Word);
const s32767: Single = 32767;
asm
cmp SupportSSE, 0
jz @@1
movss xmm0, [eax]
xorps xmm1, xmm1
minss xmm0, s32767
maxss xmm0, xmm1
cvtss2si eax, xmm0
mov word [edx], ax
ret
@@1:
fld dword [eax]
ftst
fnstsw ax
and ah, 41H
jz @biggerThanZero
fstp st(0)
mov word [edx], 0
jmp @e
@biggerThanZero:
fcom s32767
fnstsw ax
shr ah, 1
jc @SmallerThanS3
fstp st(0)
mov word [edx], 32767
jmp @e
@SmallerThanS3:
fistp word [edx]
@e:
end;
{function LnXP1(const X: Extended): Extended;
asm
FLDLN2
MOV AX,WORD PTR X+8 // exponent
FLD X
CMP AX,$3FFD // .4225
JB @@1
FLD1
FADD
FYL2X
JMP @@2
@@1:
FYL2XP1
@@2:
FWAIT
end; }
// bCalcAmbShadowAutomatic: Byte; //#149
// bCalcDOFtype: Byte; //#181 bit1: calc or not, bit 2+3: passes bit4: function sorted/forward
// bCalc3D: Byte; //#344
function AllAutoProcessings(Header: TPMandHeader10): Integer; //fill in for each processingtype the bit if enabled
begin //bits:
with Header^ do //0: not calculating, 1: main calculation, 2: normals On Z, 3: hard shadow, 4: AO, 5: AO2, 6: SelfReflections, 7: DOF (8: repaint)
begin //nums: 1 2 4 8 16 32 64
Result := 1;
if bCalc3D > 0 then
begin
if (byCalcNsOnZBufAuto and 1) > 0 then Result := Result or 2;
if ((bCalculateHardShadow and 1) > 0) and ((bCalculateHardShadow and $FC) > 0) then Result := Result or 4;
if (bCalcAmbShadowAutomatic and 1) > 0 then Result := Result or 8;
if (bCalcSRautomatic and 1) > 0 then Result := Result or 32;
if (bCalcDOFtype and 1) > 0 then Result := Result or 64;
end;
end;
end;
procedure CalcXYposOfPosLight(pos3: TPVec3D; Header: TPMandHeader10; var X, Y: Double);
var dS, dx, dy, dFOV, PlOpticZ: Double;
n: Integer;
M: TMatrix3;
V0, V1, VFOV: TVec3D;
begin
with Header^ do //not working yet
begin
dFOV := dFOVy * Pid180;// / Height;
dx := Min(1.5, Max(0.01, dFOV * 0.5));
PlOpticZ := Cos(dx) * dx / Sin(dx);
dS := 2.1345 / (dZoom * Width);
M := hVGrads;
NormaliseMatrixTo(dS, @M);
V0 := GetRealMidPos(Header);
{ V0[0] := dXmid;
V0[1] := dYmid;
V0[2] := dZmid; }
mAddVecWeight(@V0, @M[2], dZstart - dZmid); //StartPos0
NormaliseMatrixTo(1, @M);
X := 0;
Y := 0;
n := 50;
repeat
V1 := V0;
mAddVecWeight(@V1, @M[0], X * dS); //move to xy offsets
mAddVecWeight(@V1, @M[1], Y * dS);
V1[0] := pos3[0] - V1[0]; //new vec to light
V1[1] := pos3[1] - V1[1];
V1[2] := pos3[2] - V1[2];
NormaliseVectorVar(V1);
RotateVectorReverse(@V1, @M); //rotate back, should then be equal to FOV vector
if V1[2] < 0.01 then
begin //in back of viewer
X := 0;
Y := 0;
Break;
end;
dx := Y * dFOV;
dy := -X * dFOV;
if (Header.bPlanarOptic and 1) > 0 then
begin
VFOV[0] := -dy;
VFOV[1] := dx;
VFOV[2] := PlOpticZ;
NormaliseVectorVar(VFOV);
end
else BuildViewVectorDFOV(dx, dy, @VFOV);
dx := ArcSinSafe(V1[0] - VFOV[0]) * dFOV;
dy := ArcSinSafe(V1[1] - VFOV[1]) * dFOV; // angleX := (0.5 * iMandWidth - ix) * FOVy / iMandHeight; -> ix := -angleX * Height / FOVy + 0.5 * Width;
X := X + dx;
Y := Y + dy;
if X < -Width then X := -Width else if X > Width then X := Width;
if Y < -Height then Y := -Height else if Y > Height then Y := Height;
Dec(n);
until (n = 0) or (Abs(dx) + Abs(dy) < 0.1);
end;
end;
//lightpos, rel. to mid vals!
function Zdistance(LiPos: TPVec3D; Header: TPMandHeader10): Double; // for poslight moving, returns ZZ
var d: Double;
i: Integer;
M: TMatrix3;
V: TVec3D;
begin
with Header^ do
begin
d := 2.1345 / (dZoom * Width); //todo: rotate M according to viewvec!
M := hVGrads;
NormaliseMatrixTo(d, @M);
{ V[0] := dXmid;
V[1] := dYmid;
V[2] := dZmid; }
V := cDVec0;
mAddVecWeight(@V, @M[2], (dZstart - dZmid) / d);
// AddVecWeight(@V, @M[0], X);
// AddVecWeight(@V, @M[1], Y);