forked from thargor6/mb3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalcMonteCarlo.pas
2093 lines (2006 loc) · 82.2 KB
/
CalcMonteCarlo.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 CalcMonteCarlo;
interface
uses
Classes, Math3D, TypeDefinitions, Windows, ImageProcess;
type
HaltonRec = packed record
HRx, HRy: Word; // 0..65535 rect distribution
HDx, HDy: Word; // 0..65535 -32767..32767 disc distribution -> after shifting
end;
PHaltonRec = ^HaltonRec;
TMCCalcThread = class(TThread)
private
{ Private-Deklarationen }
seed: Integer;
TransDiConst: Single;
sAbsorption: Single;
bCalcReflects: LongBool;
bCalcTrans: LongBool;
bCalcTransR: LongBool;
bTransFlipInside: LongBool;
bOnlyDIFS: LongBool;
bDoDOF: LongBool;
bSkipNonZeroC: LongBool;
bRit1: LongBool;
bSecantSearch: LongBool;
bDiffReflectsBigEnough: LongBool;
bNormSDamount: LongBool;
bGaussAA: LongBool;
sDOFaperture: Single;
sDOFZsharp: Single;
SRLightAmount: Single;
sDynFog: Single;
sLightScatteringMul: Single;
HSmaxLmul: Single;
AMBmaxL: Single;
FOVy1d: Single;
iDiffReflects: Integer;
iBokehNr: Integer;
iActRayNr: Integer;
HaltonX, HaltonY: Single;
HaltonDiscX, HaltonDiscY: Single;
HaltonShiftX, HaltonShiftY: Single;
pStartPos: TPVec3D;
PMCrecord: TPMCrecord;
TotalLight: TSVec;
// BGMapFuncDirect: TGetLightMapPixel;
// BGMapFuncSphere: TGetLightMapPixelSphere;
smatrix: TSMatrix3;
Normals, VPosStart: TVec3D;
HS6: array[0..5] of TSVec; //Single; //smooth HS amount 0..1 as color for transp
tmpObjCol: TLightSD;
Iteration3Dext: TIteration3Dext;
procedure CalculateVgradsFOV(x, y: Single);
procedure CalculateNormals(var NN: Single; raydir: TPVec3D);
procedure CalculateNormalsOnSmoothIt(var NN: Single; raydir: TPVec3D);
procedure doBinSearchIt(var ZZ: Double; VgradsFOVit: TPVec3D);
procedure minLengthToCutPlane(var dLength: Double; dLimit: Double; vPos: TPPos3D; Vec: TPVec3D);
function CalcColor(PsiLight: TPsiLight5; zPos: Single): TLightSD;
procedure CalcRay(ZZ: Double; VgradsFOVit: TVec3D; tAbsorb: TSVec; Rit: Integer);
function CalcBGLight(Vec: TPVec3D; const UseAmbLight, bNNipolBG: LongBool): TSVec;
function CalcVisLights(VPos: TSVec; ViewVec: TPVec3D; PsiLight: TPsiLight5; var BGdecrease, DepthDec: Single{; bIsBG: LongBool}): TSVec;
function GetRand: Double;
function GenSphereSVecOm: TSVec;
function CalcN(x, y: Integer; pmc: TPMCrecord): Integer;
function AddLight(camPos, vPos: TSVec): TSVec;
// procedure CalcPosLightShapeMC(var flux, transp: Single; LightNr: Integer);
procedure DoDOF;
function CalcPhongLight(siLight: TPsiLight5; ReflectVec: TPVec3D): TLightSD;
function CalcPhongLightNoHS(siLight: TPsiLight5; ReflectVec: TPVec3D): TLightSD;
procedure DoDynFog(var actDE: Double; var StepCount: Single; const RSFmul, LastStepWidth: Single);
procedure CalcHSMC;
// function Calc1HSMC(const nr: Integer): Single;
procedure CalcNormalsOnCutMC(CutPlane: Integer);
procedure VaryVecSphere(Vec: TPVec3D);
procedure CalcBokehMC(var xx, yy: Single);
procedure GetSubPixelShift(var xx, yy: Single);
public
{ Public-Deklarationen }
MaxAmbDepth: Integer;
MaxSpecDepth: Integer;
AvrgRCount: Single;
AvrgVari: Double; //for estimating the current raycount to calculate for each pixel
LVals: TLightVals;
MCTparas: TMCTparameter;
protected
procedure Execute; override;
end;
function CalcBokeh(const xx, yy: Single; BokehNr: Integer): Single;
function CalcMCT(Header: TPMandHeader10; PLightVals: TPLightVals;
PsiLight5: TPsiLight5; PCTS: TPCalcThreadStats;
AvrgSqrNoise: Double; sAvrgRCount: Single;
bSkipNonZeroCounts: LongBool): Boolean;
procedure PreComputeHaltonSequence;
var
HaltonSequence: array[0..65535] of HaltonRec;
SinCosP5: array[0..5] of TSPoint;
SinCosP7: array[0..7] of TSPoint;
MCVMapcalculated: LongBool;
implementation
uses Mand, Math, DivUtils, formulas, CustomFormulas, LightAdjust, Calc,
HeaderTrafos, PaintThread, Maps, MonteCarloForm;
procedure PreComputeSinCos;
var i: Integer;
begin
for i := 0 to 5 do SinCosS(i * PiM2 * 0.2, SinCosP5[i][0], SinCosP5[i][1]);
for i := 0 to 7 do SinCosS(i * PiM2 / 7, SinCosP7[i][0], SinCosP7[i][1]);
end;
function halton(index, base: Integer): Double;
var f, d: Double;
i: Integer;
begin
Result := 0;
f := 1 / base;
d := f;
i := index;
while i > 0 do
begin
Result := Result + f * (i mod base);
i := Trunc(i * d);
f := f * d;
end;
end;
procedure PreComputeHaltonSequence;
var xx, yy: Double;
i: Integer;
begin
for i := 0 to 65535 do
begin
xx := halton(i + 1, 2);
yy := halton(i + 1, 3);
HaltonSequence[i].HRx := Round(xx * 65535);
HaltonSequence[i].HRy := Round(yy * 65535);
xx := halton(i + 1, 5); //4 disc distri done after shifting
yy := halton(i + 1, 7); //9 5,7 because of correlations between 3,9 and 2,4!
HaltonSequence[i].HDx := Round(xx * 65535); //[7,11] if every 5th value, [5,11] if every 7th value
HaltonSequence[i].HDy := Round(yy * 65535);
end;
end;
function CalcMCT(Header: TPMandHeader10; PLightVals: TPLightVals;
PsiLight5: TPsiLight5; PCTS: TPCalcThreadStats;
AvrgSqrNoise: Double; sAvrgRCount: Single;
bSkipNonZeroCounts: LongBool): Boolean;
var x, ThreadCount: Integer;
sRI, sAMBmaxL: Single;
CalcReflects, bDiffReflectsBigEnough: LongBool;
MCTparas: TMCTparameter;
MCThread: array of TMCCalcThread;
begin
Result := False;
try
ThreadCount := Min(Mand3DForm.UpDown3.Position, Header.Height);
bGetMCTPverbose := False;
Header.dZend := MaxCD(Header.dZend - Header.dZstart, LengthOfSize(Header) * 0.1 * Header.dStepWidth) + Header.dZstart;
x := Header.TilingOptions; //used in mc for other things
Header.TilingOptions := 0;
MCTparas := getMCTparasFromHeader(Header^, True); //calcHybridFormulas -> usage of own calcformulas! ->formulaclass
Header.TilingOptions := x;
Result := MCTparas.bMCTisValid;
if Result then
begin
PLightVals.SRLightAmount := Min0MaxCS(Header.SRamount, 100);
CalcHSVecsFromLights(PLightVals, @MCTparas);
MCTparas.pSiLight := PsiLight5;
MCTparas.CalcRect.Top := Header.MClastY;
MCTparas.PLVals := PLightVals;
MCTparas.PCalcThreadStats := PCTS;
MCTparas.iSmNormals := 0;
MCTparas.sRoughness := 0;
MCTparas.calcHardShadow := 0;
for x := 0 to 5 do if (Header.Light.Lights[x].Loption and 3) = 0 then
MCTparas.calcHardShadow := MCTparas.calcHardShadow or (4 shl x); //no softHS, noLightmapHS }
SetLength(MCThread, ThreadCount);
end;
finally
end;
if Result then
begin
if (not MCVMapcalculated) and ((Header.bVolLightNr and 7) > 0) then
begin
MCForm.Label15.Caption := 'calculating volumetric map...';
CalcVolLightMap(Header, PLightVals);
MCForm.Label15.Caption := '';
MCVMapcalculated := True;
end;
bDiffReflectsBigEnough := PLightVals.bBackBMP and (M3DBackGroundPic.LMWidth * Header.MCdiffReflects > 5000);
sRI := Header.sTRIndex;
sAMBmaxL := MinCS(MCTparas.DEAOmaxL * LengthOfSize(Header) * 3, MCTparas.Zend * 255 / 1.75);
PCTS.ctCalcRect := Rect(0, Header.MClastY, Header.Width - 1, Header.Height - 1);
MCTparas.CalcRect := PCTS.ctCalcRect;
CalcReflects := (Header.bCalcSRautomatic and 1) <> 0;
for x := 1 to ThreadCount do
begin
PCTS.CTrecords[x].iActualYpos := 0;
PCTS.CTrecords[x].iActualXpos := 0;
PCTS.CTrecords[x].i64DEsteps := 0;
PCTS.CTrecords[x].iDEAvrCount := 0;
PCTS.CTrecords[x].i64Its := 0;
PCTS.CTrecords[x].iItAvrCount := 0;
MCTparas.iThreadId := x;
try
MCThread[x - 1] := TMCCalcThread.Create(True);
AssignLightVal(@MCThread[x - 1].LVals, PLightVals);
MCTparas.PLVals := @MCThread[x - 1].LVals;
MCThread[x - 1].FreeOnTerminate := True;
MCThread[x - 1].MCTparas := MCTparas;
MCThread[x - 1].AvrgVari := MaxCD(0.001, AvrgSqrNoise); //calc before from whole image
MCThread[x - 1].AvrgRCount := sAvrgRCount;
MCThread[x - 1].bSkipNonZeroC := bSkipNonZeroCounts;
MCThread[x - 1].Priority := cTPrio[Mand3DForm.ComboBox2.ItemIndex];
MCThread[x - 1].smatrix := NormaliseMatrixToS(1, @Header.hVGrads);
MCThread[x - 1].maxAmbDepth := Header.MCDepth;
MCThread[x - 1].maxSpecDepth := Header.SRreflectioncount;
MCThread[x - 1].bCalcReflects := CalcReflects;
MCThread[x - 1].bCalcTrans := CalcReflects and ((Header.bCalcSRautomatic and 2) <> 0);
MCThread[x - 1].bOnlyDIFS := (Header.bCalcSRautomatic and 4) <> 0;
MCThread[x - 1].bSecantSearch := (Header.MCoptions and 2) <> 0;
MCThread[x - 1].bNormSDamount := CalcReflects and ((Header.MCoptions and 4) <> 0);
MCThread[x - 1].iDiffReflects := Header.MCdiffReflects;
MCThread[x - 1].TransDiConst := sRI;
MCThread[x - 1].sAbsorption := Header.sTransmissionAbsorption * Header.dStepWidth;
MCThread[x - 1].sLightScatteringMul := Header.sTRscattering / 330;
MCThread[x - 1].SRLightAmount := PLightVals.SRLightAmount;
MCThread[x - 1].AMBmaxL := sAMBmaxL;
MCThread[x - 1].bDoDOF := (Header.bCalcDOFtype and 1) <> 0;
MCThread[x - 1].sDOFaperture := MaxCS(0.0000001, Header.sDOFaperture * s05);
MCThread[x - 1].sDOFZsharp := MaxCS(0.01, Header.sDOFZsharp * Header.Width);
MCThread[x - 1].FOVy1d := 1 / NonZero(MCTparas.FOVy);
MCThread[x - 1].bDiffReflectsBigEnough := bDiffReflectsBigEnough; //to interpolate map nearest neighbour
MCThread[x - 1].iBokehNr := (Header.MCoptions shr 4) and 7;
MCThread[x - 1].bGaussAA := (Header.MCoptions and 8) <> 0;
// MCThread[x - 1].BGMapFuncSphere := BGMapFuncSphere;
PCTS.CTrecords[x].isActive := 1;
// PCTS.CTprios[x] := Header.iThreadPriority;
PCTS.CThandles[x] := MCThread[x - 1];
except
ThreadCount := x - 1;
Break;
end;
end;
PCTS.HandleType := 1;
for x := 0 to ThreadCount - 1 do MCThread[x].MCTparas.iThreadCount := ThreadCount;
PCTS.iTotalThreadCount := ThreadCount;
PCTS.cCalcTime := GetTickCount;
if bSkipNonZeroCounts then Dec(PCTS.cCalcTime, Header.iCalcTime * 100);
for x := 0 to ThreadCount - 1 do MCThread[x].Start;
end;
end;
function CalcBokeh(const xx, yy: Single; BokehNr: Integer): Single;
var u, r, s: Single;
begin
r := Sqrt(xx * xx + yy * yy);
if (BokehNr and 1) = 0 then Result := 1 else Result := 1.5 - r * s05;
if BokehNr = 0 then
begin
if r < 0.94 then Result := 1.03 else
if r < 1 then Result := 0.9682 + (r - 0.94) * s05;
end
else if BokehNr > 1 then
begin
{ if BokehNr < 4 then u := 0.92 + Sqr(Cos(ArcTan2(xx, yy) * 5) * s05 + s05) * 0.17
else u := 0.95 + Sqr(Cos(ArcTan2(xx, yy) * 7) * s05 + s05) * 0.09;
if r < u then u := u * 0.3 + 0.7 else u := (r - u) / (1 - u) * 0.05 + u; }
if BokehNr < 4 then u := 0.92 + Sqr(Cos(ArcTan2(xx, yy) * 5) * s05 + s05) * 0.12
else u := 0.94 + Sqr(Cos(ArcTan2(xx, yy) * 7) * s05 + s05) * 0.08;
s := u - 0.04;
if r < s then u := 1 else u := (r - s) / (1 - s) * 0.05 + s;
Result := Result * u;
end;
end;
{TMCCalcThread}
function TMCCalcThread.CalcN(x, y: Integer; pmc: TPMCrecord): Integer;
var i, wid, n, n2, avgRC: Integer;
maxYdiff, maxYdiff2, dT2, dT, dT3, maxNoise, dm: Double;
function GetMulti(Raycount: PWord): Double;
begin
if RayCount^ < 2 then Result := 1
else Result := 1 / RayCount^;
end;
begin
wid := MCTparas.iMandWidth;
maxYdiff := 0;
maxYdiff2 := 0;
maxNoise := 0;
avgRC := 0;
n := 0;
n2 := 0;
dT2 := MCRGBtoDouble(@pmc.Ysum);
if y > 1 then
begin
i := Integer(@pmc.Ysum) - wid * SizeOf(TMCrecord);
dT := MCRGBtoDouble(TPRGB(i)); //Ysum
maxYdiff := Sqr(dT - dT2);
maxNoise := (MCRGBtoDouble(TPRGB(i + 3)) - Sqr(dT)) {* GetMulti(PWord(i + 6))};
avgRC := TPMCrecord(i).RayCount;
Inc(n);
if x > 1 then
begin
i := i - SizeOf(TMCrecord);
dT3 := MCRGBtoDouble(TPRGB(i)); //Ysum
maxYdiff := maxYdiff + Sqr(dT2 - dT3); //use Ydiffs from neighbour to neighbour, single noise pixels get so less weight
maxYdiff2 := Sqr(dT - dT3);
maxNoise := MaxCD(maxNoise, (MCRGBtoDouble(TPRGB(i + 3)) - Sqr(dT3)) {* GetMulti(PWord(i + 6))});
avgRC := avgRC + TPMCrecord(i).RayCount;
Inc(n);
Inc(n2);
end;
if x < wid then
begin
i := Integer(@pmc.Ysum) + SizeOf(TMCrecord) - wid * SizeOf(TMCrecord);
dT3 := MCRGBtoDouble(TPRGB(i)); //Ysum
maxYdiff := maxYdiff + Sqr(dT2 - dT3);
maxYdiff2 := maxYdiff2 + Sqr(dT - dT3);
maxNoise := MaxCD(maxNoise, (MCRGBtoDouble(TPRGB(i + 3)) - Sqr(dT3)) {* GetMulti(PWord(i + 6))});
avgRC := avgRC + TPMCrecord(i).RayCount;
Inc(n);
Inc(n2);
end;
end;
if x > 1 then
begin
i := Integer(@pmc.Ysum) - SizeOf(TMCrecord);
dT := MCRGBtoDouble(TPRGB(i)); //Ysum
maxYdiff := maxYdiff + Sqr(dT - dT2);
maxNoise := MaxCD(maxNoise, (MCRGBtoDouble(TPRGB(i + 3)) - Sqr(dT)) {* GetMulti(PWord(i + 6))});
avgRC := avgRC + TPMCrecord(i).RayCount;
Inc(n);
end;
if x < wid then
begin
i := Integer(@pmc.Ysum) + SizeOf(TMCrecord);
dT3 := MCRGBtoDouble(TPRGB(i)); //Ysum
maxYdiff := maxYdiff + Sqr(dT3 - dT2);
if x > 1 then
begin
maxYdiff2 := maxYdiff2 + Sqr(dT - dT3);
Inc(n2);
end;
maxNoise := MaxCD(maxNoise, (MCRGBtoDouble(TPRGB(i + 3)) - Sqr(dT3)) {* GetMulti(PWord(i + 6))});
avgRC := avgRC + TPMCrecord(i).RayCount;
Inc(n);
end;
if y < MCTparas.iMandHeight then
begin
i := Integer(@pmc.Ysum) + wid * SizeOf(TMCrecord);
dT := MCRGBtoDouble(TPRGB(i)); //Ysum
maxYdiff := maxYdiff + Sqr(dT - dT2);
maxNoise := MaxCD(maxNoise, (MCRGBtoDouble(TPRGB(i + 3)) - Sqr(dT)) {* GetMulti(PWord(i + 6))});
avgRC := avgRC + TPMCrecord(i).RayCount;
Inc(n);
if x > 1 then
begin
i := i - SizeOf(TMCrecord);
dT3 := MCRGBtoDouble(TPRGB(i)); //Ysum
maxYdiff := maxYdiff + Sqr(dT2 - dT3);
maxYdiff2 := maxYdiff2 + Sqr(dT - dT3);
maxNoise := MaxCD(maxNoise, (MCRGBtoDouble(TPRGB(i + 3)) - Sqr(dT3)) {* GetMulti(PWord(i + 6))});
avgRC := avgRC + TPMCrecord(i).RayCount;
Inc(n);
Inc(n2);
end;
if x < wid then
begin
i := Integer(@pmc.Ysum) + SizeOf(TMCrecord) + wid * SizeOf(TMCrecord);
dT3 := MCRGBtoDouble(TPRGB(i)); //Ysum
maxYdiff := maxYdiff + Sqr(dT2 - dT3);
maxYdiff2 := maxYdiff2 + Sqr(dT - dT3);
maxNoise := MaxCD(maxNoise, (MCRGBtoDouble(TPRGB(i + 3)) - Sqr(dT3)) {* GetMulti(PWord(i + 6))});
avgRC := avgRC + TPMCrecord(i).RayCount;
Inc(n);
Inc(n2);
end;
end;
if n < 1 then n := 1;
if n2 < 1 then n2 := 1;
maxYdiff := maxYdiff / n; //contrast to neighbours
maxYdiff2 := maxYdiff2 / n2; //contrast of neighbours
dT3 := (MCRGBtoDouble(@pmc.Ysqr) - Sqr(dT2)) / ((Clamp0D(dT2) + 0.01) {* pmc.RayCount}); //vari
if (PMCrecord.Zbyte and 128) = 0 then dm := 0 else
dm := Clamp0D(MaxCD(maxYdiff, maxYdiff2) - 20 * AvrgVari) * MinMaxCD(-4, pmc.RayCount - avgRC / n, 4);// - Clamp0D(maxYdiff - maxYdiff2) * 0.5;// * MinMaxCD(-4, (pmc.RayCount - avgRC / n), 4);
//hdr scale Y
dT := Clamp0D(MaxCD(dT3, maxNoise) / MaxCD(0.3, (dT2 / Sqrt(1 + Sqr(dT2 * 0.9))) * pmc.RayCount) - dm);
dT2 := dT * 4 / AvrgVari;
if dT2 >= 1 then
Result := Round(MinCD(AvrgRCount * s05 + 8, dT2))
else
if GetRand * 1.1 - 0.1 > Clamp0D(dT2) then Result := 0 else Result := 1;
end;
procedure TMCCalcThread.CalcNormalsOnCutMC(CutPlane: Integer);
var NN: Double;
begin
with MCTparas do
begin
if CutPlane <> 0 then
begin
Dec(CutPlane);
if Abs(Vgrads[2, CutPlane]) < 1e-40 then NN := -1e40 else NN := -1 / Vgrads[2, CutPlane];
Normals[0] := Vgrads[0, CutPlane] * NN;
Normals[1] := Vgrads[1, CutPlane] * NN;
Normals[2] := -1;
end else begin
Normals[0] := 0;
Normals[1] := 0;
Normals[2] := -1;
end;
RotateVectorReverse(@Normals, @Vgrads);
NormaliseVectorVar(Normals);
end;
end;
procedure TMCCalcThread.CalcHSMC;
var itmp2, itmp: Integer;
RLastDE, RLastStepWidth, RStepFactorDiff, sTmp, ZZ2mul, ZZ: Single;
dTmp, dT1, MaxLHS, ZZ2, dMaxL: Double;
IC, HSVec, NVec: TVec3D;
ObjColor: TLightSD;
hsiLight: TsiLight5;
label label2;
procedure HSminLengthToCutPlane(HVec: TPVec3D; var dLength: Double);
var dT: Double;
begin
with MCTparas do
begin
if ((iCutOptions and 1) <> 0) and (Abs(HVec[0]) > 1e-20) then
begin
dT := (dCOX - pIt3Dext.C1) / HVec[0];
if (dT > 0) and (dT < dLength) then dLength := dT;
end;
if ((iCutOptions and 2) <> 0) and (Abs(HVec[1]) > 1e-20) then
begin
dT := (dCOY - pIt3Dext.C2) / HVec[1];
if (dT > 0) and (dT < dLength) then dLength := dT;
end;
if ((iCutOptions and 4) <> 0) and (Abs(HVec[2]) > 1e-20) then
begin
dT := (dCOZ - pIt3Dext.C3) / HVec[2];
if (dT > 0) and (dT < dLength) then dLength := dT;
end;
end;
end;
begin
with MCTparas do
begin
ZZ := Abs(mZZ);
mCopyVec(@IC, @pIt3Dext.C1);
MaxLHS := HSmaxLmul * (1 + s05 * Min(ZZ, Zend * 0.4) * Clamp0D(FOVy) / iMandHeight);
for itmp2 := 0 to 5 do if PLVals.iLightOption[itmp2] = 0 then
begin
HS6[itmp2] := cSVec1c4; //(1,1,1,1)
mCopyVec(@pIt3Dext.C1, @IC);
ZZ2 := mZZ;
msDEstop := DEstop * (1 + ZZ * mctDEstopFactor);
dT1 := MaxLHS;
if (PLVals.iLightPos[itmp2] and 1) <> 0 then //calculate LightVec from position
begin
HSVec := SubtractVectors(HSvecs[itmp2], @pIt3Dext.C1);
dTmp := SqrLengthOfVec(HSVec);
if dTmp > PLVals.sLmaxL[itmp2] * sHSmaxLengthMultiplier then Continue;
if dTmp < Sqr(dT1 * StepWidth) then dT1 := Sqrt(dTmp) / StepWidth;
NormaliseVectorTo(StepWidth, @HSVec);
end
else HSVec := ScaleVector(HSvecs[itmp2], -1);
if iCutOptions <> 0 then HSminLengthToCutPlane(@HSVec, dT1);
if dT1 > s0001 then
begin
if DotOfVectors(@Normals, @HSVec) < 0 then
begin
ClearSVec(HS6[itmp2]);
Continue;
end;
ZZ2mul := DotOfVectorsNormalize(@HSVec, @mVgradsFOV);
RStepFactorDiff := 2; //1
dMaxL := dT1;
dTmp := CalcDE(pIt3Dext, @MCTparas);
repeat
RLastDE := dTmp;
dTmp := MinCS(dTmp * sZstepDiv * RStepFactorDiff, MaxCS(msDEstop, 0.4) * mctMH04ZSD);
RLastStepWidth := dTmp;
dT1 := dT1 - dTmp;
mAddVecWeight(@pIt3Dext.C1, @HSVec, dTmp);
ZZ2 := ZZ2 + dTmp * ZZ2mul;
msDEstop := DEstop * (1 + Abs(ZZ2) * mctDEstopFactor);
dTmp := CalcDE(pIt3Dext, @MCTparas);
if (pIt3Dext.ItResultI >= MaxItsResult) or (dTmp < msDEstop) then Break;
if dTmp > RLastDE + RLastStepWidth then dTmp := RLastDE + RLastStepWidth;
if RLastDE > dTmp + 1e-30 then
begin
sTmp := RLastStepWidth / (RLastDE - dTmp);
if sTmp < 1 then
RStepFactorDiff := maxCS(s05, sTmp)
else
RStepFactorDiff := 1;
end
else RStepFactorDiff := 1;
until dT1 < 0;
if dT1 > s001 then
begin
if bCalcTransR then
begin
if Iteration3Dext.ItResultI < MaxItsResult then
begin
{ itmp := 8;
RLastDE := RLastStepWidth * -0.5;
while (itmp > 0) and (Abs(dTmp - msDEstop) > 0.01) do
begin
ZZ := ZZ + RLastDE * ZZ2mul;
mAddVecWeight(@Iteration3Dext.C1, @HSVec, -RLastDE);
msDEstop := DEstop * (1 + Abs(ZZ) * mctDEstopFactor);
dTmp := CalcDE(@Iteration3Dext, @MCTparas);
if dTmp < msDEstop then RLastDE := Abs(RLastDE) * -0.55
else RLastDE := Abs(RLastDE) * 0.55;
Dec(itmp);
end; }
itmp := 3;
while (itmp > 0) and (Abs(dTmp - msDEstop) > s001) do
begin
RLastDE := NotZero(RLastDE - dTmp);
RStepFactorDiff := RLastStepWidth * (dTmp - msDEstop) / RLastDE;
if dTmp < msDEstop then
begin
if (RStepFactorDiff >= 0) or (RStepFactorDiff < Abs(RLastStepWidth) * -0.94) then
RLastStepWidth := Abs(RLastStepWidth) * sm05
else RLastStepWidth := RStepFactorDiff;
end else begin
if (RStepFactorDiff <= 0) or (RStepFactorDiff > Abs(RLastStepWidth) * 0.94) then
RLastStepWidth := Abs(RLastStepWidth) * s05
else RLastStepWidth := RStepFactorDiff;
end;
RLastDE := dTmp;
ZZ2 := ZZ2 + RLastStepWidth * ZZ2mul;
mAddVecWeight(@Iteration3Dext.C1, @HSVec, RLastStepWidth);
msDEstop := DEstop * (1 + Abs(ZZ2) * mctDEstopFactor);
dTmp := CalcDE(@Iteration3Dext, @MCTparas);
Dec(itmp);
end;
end else begin
dTmp := ZZ2;
doBinSearchIt(dTmp, @HSVec);
ZZ2 := ZZ2 + (dTmp - ZZ2) * ZZ2mul;
end;
mZZ := Abs(ZZ2); //backup mZZ?
mCopyVec(@NVec, @Normals); //backup normals...
if NormalsOnDE then CalculateNormals(sTmp, @HSVec)
else CalculateNormalsOnSmoothIt(sTmp, @HSVec);
if Iteration3Dext.ItResultI < MaxItsResult then
sTmp := 32767 - (sTmp + dColPlus + mctColVarDEstopMul * ln(msDEstop * StepWidth)) * mctsM
else sTmp := 32767 - sTmp * mctsM;
MinMaxClip15bit(sTmp, hsiLight.SIgradient);
itmp := Integer(mPsiLight);
mPsiLight := @hsiLight;
if ColorOnIt <> 0 then RMdoColorOnIt(@MCTparas);
RMdoColor(@MCTparas);
mPsiLight := TPsiLight5(itmp);
// sRoughness := 0;
CalcZposAndRough(@hsiLight, @MCTparas, ZZ2); //fp invalid op ZZ2 = 139498078 dtmp>>>>maxhls
ObjColor := CalcColor(@hsiLight, mZZ * StepWidth + sZZstmitDif); //dif+spec holds colors zPos for ColZmultiplier, must be ZZ
itmp := DEoptionResult;
if (ObjColor[0][3] > 0) and ((not bOnlyDIFS) or (bOnlyDIFS and (DEoptionResult = 20))) then
begin
if bInAndOutside then
begin
MultiplySVectorsV(@HS6[itmp2], SVecPow(ObjColor[1], sAbsorption));
goto label2;
end;
HS6[itmp2][3] := Sqr(DotOfVectors(@Normals, @HSVec) / StepWidth); //downscale by change of lightangle, spec more
ScaleSVectorV(@HS6[itmp2], HS6[itmp2][3]); //only first3 comps
//check only at random position if still in? better: calc refraction, go inside + step until boarder or maxL, calc lightabsorption
bInsideRendering := not bInsideRendering;
bCalcInside := not bCalcInside;
mAddVecWeight(@Iteration3Dext.C1, @HSVec, dT1); //step towards light and step back
dTmp := GetRand * 8 * MaxCS(msDEstop, 1);
repeat
dTmp := -1 - Abs(dTmp);
dT1 := dT1 + dTmp;
mAddVecWeight(@Iteration3Dext.C1, @HSVec, dTmp);
dTmp := CalcDE(@Iteration3Dext, @MCTparas);
until (dTmp > msDEstop) or (dT1 <= 0);
bInsideRendering := not bInsideRendering;
bCalcInside := not bCalcInside;
if itmp <> DEoptionResult then HS6[itmp2] := cSVec0 else
MultiplySVectorsV(@HS6[itmp2], ScaleSVector(SVecPow(ObjColor[1], Clamp0D(dT1) * sAbsorption),
ObjColor[0][3] * MaxOfSVec(@ObjColor[0])));
end
else ScaleSVectorV(@HS6[itmp2], MinCS(1, Sqr(Sqr(Sqr((dMaxL - dT1) / MaxLHS)))));
label2: mCopyVec(@Normals, @NVec);
end
else ScaleSVectorV(@HS6[itmp2], MinCS(1, Sqr(Sqr(Sqr((dMaxL - dT1) / MaxLHS)))));
end;
end;
end;
mCopyVec(@pIt3Dext.C1, @IC);
end;
end;
procedure MakeDiscFromHalton(var xx, yy: Single);
var sd, cd: Single;
begin
xx := xx * 2 - 1;
yy := yy * 2 - 1;
if xx > Abs(yy) then
begin
SinCosS(sPiM025 * yy / NotZero(xx), sd, cd);
yy := xx;
end
else if xx > yy then
begin
SinCosS(sPiM025 * (6 - xx / NotZero(yy)), sd, cd);
yy := - yy;
end
else if xx > - yy then
SinCosS(sPiM025 * (2 - xx / NotZero(yy)), sd, cd)
else begin
SinCosS(sPiM025 * (4 + yy / NotZero(xx)), sd, cd);
yy := - xx;
end;
xx := sd * yy;
yy := cd * yy;
end;
procedure GetHalton2Dshifts(var s1, s2: Single; x, y: Integer);
var i2, i: Integer;
begin
{$IFDEF DEBUG} {$Q-} {$R-} {$ENDIF}
i := x * $343FD + $269EC3;
i2 := y * $343FD + $269EC3;
i2 := (x shl 15) xor y xor (i shl 9) xor (i2 shl 5);
i2 := i2 * $343FD + $269EC3;
i2 := i2 * $343FD + $269EC3;
s1 := (i2 and $7FFFFFFF) * dSeedMul;
i2 := i2 * $343FD + $269EC3;
s2 := (i2 and $7FFFFFFF) * dSeedMul;
{$IFDEF DEBUG} {$Q+} {$R+} {$ENDIF}
end;
function TMCCalcThread.GetRand: Double;
asm //begin result := random; end;
{$IFDEF DEBUG} {$Q-} {$R-} {$ENDIF}
imul edx, [eax + seed], $343FD
add edx, $269EC3
mov [eax + seed], edx
and edx, $7FFFFFFF
push edx
fild dword [esp]
fmul dSeedMul
pop edx
{$IFDEF DEBUG} {$Q+} {$R+} {$ENDIF}
end;
function TMCCalcThread.GenSphereSVecOm: TSVec; //fullsphere
{var u, v, s, c, r: Single; //edx
begin
if not bDoDoF then //eax + bDoDOF for poslights
begin
u := HaltonDiscX;
v := HaltonDiscY;
end else begin
u := GetRand;
v := GetRand;
end;
r := 2 * Sqrt(v * (1 - v));
SinCosS(PiM2 * u, s, c);
Result[0] := r * c;
Result[1] := r * s;
Result[2] := 1 - 2 * v;
Result[3] := 0; // }
asm
cmp dword [eax + TMCCalcThread.bDoDOF], 0
jnz @@1
fld dword [eax + TMCCalcThread.HaltonDiscY]
fld dword [eax + TMCCalcThread.HaltonDiscX]
jmp @@2
@@1:
push edx
call GetRand
call GetRand
pop edx
@@2:
fmul PiM2
fsincos //cos,sin,v
fld1
fsub st, st(3)
fmul st, st(3)
fsqrt
fadd st, st //r,cos,sin,v
fmul st(2), st
fmulp //c',s',v
fstp dword [edx]
fstp dword [edx + 4]
fadd st, st
fld1
fsubrp
fstp dword [edx + 8]
xor eax, eax
mov [edx + 12], eax //}
end;
{function TMCCalcThread.GenSphereSVecOm: TSVec; //hemisphere!
var a, u, v, s: Double;
begin
if not bDoDoF then
begin
u := HaltonDiscX;
v := HaltonDiscY;
s := u * u + v * v;
end
else
repeat
u := GetRand * 2 - 1;
v := GetRand * 2 - 1;
s := u * u + v * v;
until s <= 1;
a := 2 - Sqrt(1 - s);
Result[0] := a * u;
Result[1] := a * v;
Result[2] := 2 * s - 1;
Result[3] := 0;
end; }
procedure TMCCalcThread.VaryVecSphere(Vec: TPVec3D);
var sd: TLightSD;
s1, s2, s3, s4: Single;
begin
MakeOrthoVecs(Vec, @sd);
if not MCTparas.bCalcAmbShadow then
begin
SinCosS(HaltonX * PiM2, s1, s2);
s3 := HaltonY;
end else begin
SinCosS(GetRand * PiM2, s1, s2);
s3 := GetRand;
end;
s4 := Sqrt(s3);
Vec^ := AddVecF(AddSVectorsToDVec(ScaleSVector(sd[0], s1 * s4),
ScaleSVector(sd[1], s2 * s4)), ScaleVector(Vec^, Sqrt(1 - s3)));
end;
function TMCCalcThread.CalcColor(PsiLight: TPsiLight5; zPos: Single): TLightSD; //proof zpos
var dTmp2, dTmp: Single;
SV2, st: TSVec;
SV1: TVec3D;
begin
with LVals do
begin
Result[1][0] := sColZmul * zPos;
if PsiLight.SIgradient > 32767 then CalcColorsInside(@Result[1], @Result[0], PsiLight, @LVals)
else CalcColors(@Result[1], @Result[0], PsiLight, @LVals);
if iColOnOT > 1 then
begin //diff map
if bYCcomb then st := Result[1]; //backup for y-c combi
if iColOnOT > 3 then
begin
if iColOnOT > 5 then
begin
SV1 := ScaleVector(TPVec3D(@Iteration3Dext.C1)^, lvMapScale); //add MidPos to ObjPos, scale, + DCLOffset, Frac in a seperate function with more precision!
SV2 := DVecToSVec(Normals);
ScaleSVectorV(@SV2, 1 / (Abs(SV2[0]) + Abs(SV2[1]) + Abs(SV2[2])));
AbsSVecVar(SV2);
if iColOnOT > 7 then
begin
if SV1[0] < 0 then SV1[0] := SV1[0] + 1 - Round(SV1[0]);
if SV1[1] < 0 then SV1[1] := SV1[1] + 1 - Round(SV1[1]);
if SV1[2] < 0 then SV1[2] := SV1[2] + 1 - Round(SV1[2]);
Result[1] := Add3SVectors(ScaleSVector(GetLightMapPixel(Frac(SV1[1] + DCLMapOffX), Frac(SV1[2] + DCLMapOffY), DiffColLightMap, bCalcPixColSqr, 1), SV2[0]),
ScaleSVector(GetLightMapPixel(Frac(SV1[0] + DCLMapOffX), Frac(SV1[2] + DCLMapOffY), DiffColLightMap, bCalcPixColSqr, 1), SV2[1]),
ScaleSVector(GetLightMapPixel(Frac(SV1[0] + DCLMapOffX), Frac(SV1[1] + DCLMapOffY), DiffColLightMap, bCalcPixColSqr, 1), SV2[2]));
end
else
begin
SV1[0] := Sin(SV1[0]);
SV1[1] := Sin(SV1[1]);
SV1[2] := Sin(SV1[2]);
SV1[0] := SV1[1] * SV2[0] + SV1[0] * (SV2[1] + SV2[2]) + DCLMapOffX;
SV1[1] := SV1[2] * (SV2[0] + SV2[1]) + SV1[1] * SV2[2] + DCLMapOffY;
if SV1[0] < 0 then SV1[0] := SV1[0] + 1 - Round(SV1[0]);
if SV1[1] < 0 then SV1[1] := SV1[1] + 1 - Round(SV1[1]);
Result[1] := GetLightMapPixel(Frac(SV1[0]), Frac(SV1[1]), DiffColLightMap, bCalcPixColSqr, 1);
end;
end
else
begin
SV2 := DVecToSVec(Normals);
RotateSVectorReverseS(@SV2, @smatrix);
Result[1] := GetLightMapPixelSphere(SV2,//MakeSVecFromNormals(PsiLight),
@DiffColLightMap.PicRotMatrix, DiffColLightMap, bCalcPixColSqr);
end;
end
else
begin //rotate x,y 2d before
dTmp := (PsiLight.OTrap and $7FFF) * 3.05186851e-5 - s05; //because it was calculated as arctan2:2pi * 5215
dTmp2 := PsiLight.SIgradient * 3.05186851e-5 - s05;
Result[1] := GetLightMapPixel(Frac((DCLMapRotCos * dTmp + DCLMapRotSin * dTmp2 + DCLMapOffX) * lvMapScale),
Frac((DCLMapRotCos * dTmp2 - DCLMapRotSin * dTmp + DCLMapOffY) * lvMapScale), DiffColLightMap, bCalcPixColSqr, 1);
end;
if bYCcomb then Result[1] := ScaleSVector(st, YofSVec(@Result[1]) / (s001 + YofSVec(@st)));
end;
if bNormSDamount then
begin
if bCalcTrans then dTmp2 := 1 - Result[0][3] else dTmp2 := 1;
dTmp := MaxCS(MaxCS(Result[0][0] * SRLightAmount + Result[1][0] * dTmp2,
Result[0][1] * SRLightAmount + Result[1][1] * dTmp2),
Result[0][2] * SRLightAmount + Result[1][2] * dTmp2);
if dTmp > 1 then
begin
dTmp := 1 / dTmp;
ScaleSVectorV(@Result[0], dTmp);
ScaleSVectorV(@Result[1], dTmp);
end;
end;
end;
end;
procedure TMCCalcThread.minLengthToCutPlane(var dLength: Double; dLimit: Double; vPos: TPPos3D; Vec: TPVec3D);
var dTmp: Double;
begin
with MCTparas do
begin
if ((iCutOptions and 1) <> 0) and (Abs(Vec[0]) > 1e-20) then
begin
dTmp := (dCOX - vPos[0]) / Vec[0];
if (dTmp > dLimit) and (dTmp < dLength) then dLength := dTmp;
end;
if ((iCutOptions and 2) <> 0) and (Abs(Vec[1]) > 1e-20) then
begin
dTmp := (dCOY - vPos[1]) / Vec[1];
if (dTmp > dLimit) and (dTmp < dLength) then dLength := dTmp;
end;
if ((iCutOptions and 4) <> 0) and (Abs(Vec[2]) > 1e-20) then
begin
dTmp := (dCOZ - vPos[2]) / Vec[2];
if (dTmp > dLimit) and (dTmp < dLength) then dLength := dTmp;
end;
end;
end;
procedure TMCCalcThread.CalculateNormals(var NN: Single; raydir: TPVec3D);
var Noffset, No, Ntmp: Double;
Scale: Single;
i: Integer;
begin
with MCTparas do //only towards 1 side and flip sides randomly
begin
Iteration3Dext.CalcSIT := True;
if (seed and 128) = 0 then Scale := 0.15 else Scale := -0.15;
No := CalcDE(@Iteration3Dext, @MCTparas);
NN := Iteration3Dext.SmoothItD; //for coloring
Iteration3Dext.CalcSIT := False;
i := 4;
repeat
if i < 4 then Scale := (GetRand - s05) * s05;
Noffset := MinCS(1, DEstop) * (1 + mZZ * mctDEstopFactor) * Scale * StepWidth;
Ntmp := Iteration3Dext.C3;
Iteration3Dext.C3 := Iteration3Dext.C3 + Noffset;
Normals[2] := (CalcDE(@Iteration3Dext, @MCTparas) - No) * Scale; //Zgradient
Iteration3Dext.C3 := Ntmp;
Ntmp := Iteration3Dext.C1;
Iteration3Dext.C1 := Iteration3Dext.C1 + Noffset;
Normals[0] := (CalcDE(@Iteration3Dext, @MCTparas) - No) * Scale; //Xgradient
Iteration3Dext.C1 := Ntmp;
Ntmp := Iteration3Dext.C2;
Iteration3Dext.C2 := Iteration3Dext.C2 + Noffset;
Normals[1] := (CalcDE(@Iteration3Dext, @MCTparas) - No) * Scale; //Ygradient
Iteration3Dext.C2 := Ntmp;
Ntmp := Sqr(Normals[0]) + Sqr(Normals[1]) + Sqr(Normals[2]);
if (Ntmp > d1em100) and (DotOfVectors(@Normals, raydir) < 0) then
begin
ScaleVectorV(@Normals, 1 / Sqrt(Ntmp));
Break;
end;
Dec(i);
until i = 0;
if i = 0 then Normals := NormaliseVectorTo(-1, raydir^);
end;
end;
procedure TMCCalcThread.CalculateNormalsOnSmoothIt(var NN: Single; raydir: TPVec3D);
var Noffset, Ntmp: Double;
Scale: Single;
i: Integer;
begin
with MCTparas do
begin // and 1 is always the same?
if (seed and 128) = 0 then Scale := 0.15 else Scale := -0.15;
Iteration3Dext.CalcSIT := True;
mMandFunction(@Iteration3Dext.C1);
NN := Iteration3Dext.SmoothItD;
i := 4;
repeat
if i < 4 then Scale := (GetRand - s05) * s05;
Noffset := MinCS(1, DEstop) * (1 + mZZ * mctDEstopFactor) * Scale * StepWidth;
Ntmp := Iteration3Dext.C3;
Iteration3Dext.C3 := Iteration3Dext.C3 + Noffset;
mMandFunction(@Iteration3Dext.C1);
Normals[2] := (NN - Iteration3Dext.SmoothItD) * Scale; //Zgradient
Iteration3Dext.C3 := Ntmp;
Ntmp := Iteration3Dext.C1;
Iteration3Dext.C1 := Iteration3Dext.C1 + Noffset;
mMandFunction(@Iteration3Dext.C1);
Normals[0] := (NN - Iteration3Dext.SmoothItD) * Scale; //Xgradient
Iteration3Dext.C1 := Ntmp;
Ntmp := Iteration3Dext.C2;
Iteration3Dext.C2 := Iteration3Dext.C2 + Noffset;
mMandFunction(@Iteration3Dext.C1);
Normals[1] := (NN - Iteration3Dext.SmoothItD) * Scale; //Ygradient
Iteration3Dext.C2 := Ntmp;
Ntmp := Sqr(Normals[0]) + Sqr(Normals[1]) + Sqr(Normals[2]);
if (Ntmp > d1em100) and (DotOfVectors(@Normals, raydir) < 0) then
begin
ScaleVectorV(@Normals, 1 / Sqrt(Ntmp));
Break;
end;
Dec(i);
until i = 0;
Iteration3Dext.CalcSIT := False;
if i = 0 then Normals := NormaliseVectorTo(-1, raydir^);
end;
end;
procedure TMCCalcThread.CalculateVgradsFOV(x, y: Single);
begin
with MCTparas do
begin
CAFX := (s05 * iMandWidth - x - 1) * FOVXmul;
CAFY := (y / iMandHeight - s05) * FOVy;
if MCTCameraOptic = 1 then
begin
mVgradsFOV[0] := -CAFX;
mVgradsFOV[1] := CAFY;
mVgradsFOV[2] := mctPlOpticZ;
NormaliseVectorVar(mVgradsFOV);
end
else if MCTCameraOptic = 2 then
BuildViewVectorDSphereFOV(CAFY, CAFX, @mVgradsFOV)
else BuildViewVectorDFOV(CAFY, CAFX, @mVgradsFOV);
RotateVectorReverse(@mVgradsFOV, @VGrads);
end;
end;
procedure TMCCalcThread.CalcBokehMC(var xx, yy: Single);
var s{, a, b, radd}: Single;
// i, i2: Integer;
// p1, p2: TPSPoint;
begin
// if iBokehNr < 2 then
begin //disc
xx := FracSingle(HaltonSequence[iActRayNr].HDx * d1d65535 + HaltonShiftX);
yy := FracSingle(HaltonSequence[iActRayNr].HDy * d1d65535 + HaltonShiftY);
MakeDiscFromHalton(xx, yy);
s := CalcBokeh(xx, yy, iBokehNr) * sDOFaperture * sDOFZsharp * MCTparas.StepWidth;
xx := xx * s;
yy := yy * s;
{ end
else
begin
if iBokehNr > 3 then i := 7 else i := 5;
i2 := Integer(@HaltonSequence[iActRayNr div i]);
xx := FracSingle(PHaltonRec(i2).HDx * d1d65535 + HaltonShiftX);
yy := Sqrt(FracSingle(PHaltonRec(i2).HDy * d1d65535 + HaltonShiftY));
if (iBokehNr and 1) = 0 then
begin
if yy < 0.94 then yy := yy * 1.03
else yy := yy * (0.9682 + (yy - 0.94) * s05);
end
else yy := yy * (1.5 - yy * s05);
i := iActRayNr mod i;
if iBokehNr > 3 then
begin //septagon
p1 := @SinCosP7[i];
p2 := @SinCosP7[i + 1];
s := 0.3;
end
else
begin //pentagon
p1 := @SinCosP5[i];
p2 := @SinCosP5[i + 1];
s := 0.4;
end;
radd := xx * (1 - xx) * s; //addition to border, make it more roundy
s := 0.96 + radd;
if yy < s then yy := yy * (1 + radd) else yy := 0.96 + (yy - s) * (radd + 0.04);
yy := yy * sDOFaperture * sDOFZsharp * MCTparas.StepWidth;
a := xx * yy;