forked from thargor6/mb3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalc.pas
2856 lines (2753 loc) · 101 KB
/
Calc.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 Calc;
interface
uses Windows, TypeDefinitions, Math3D, FormulaClass;
function CalcMandT(Header: TPMandHeader10; PLightVals: TPLightVals; PCTS: TPCalcThreadStats;
PsiLight5: TPsiLight5; hSLoffset, FSIstart, FSIoffset: Integer; hRect: TRect): Boolean;
function CalcDEfull(It3Dex: TPIteration3Dext; mctp: Pointer{PMCTparameter}): Double;
function CalcDEanalytic(It3Dex: TPIteration3Dext; mctp: Pointer{PMCTparameter}): Double;
//function CalcDE4point(It3Dex: TPIteration3Dext; mctp: Pointer): Double;
procedure CalcHS(PMCT: PMCTparameter; hsPsiLight: TPsiLight5; y: Integer);
procedure IniIt3D(PMCT: PMCTparameter; It3Dex: TPIteration3Dext);
//procedure RegulationUpdate(const ActDE, LastDE, LastStep: Double; var DEmul: Single);
procedure CalcZposAndRough(siLight: TPsiLight5; mct: PMCTparameter; const ZZ: Double);
procedure DelayCalcPart(ThreadCount: Integer; PCTS: TPCalcThreadStats);
//procedure FreeCalcMaps;
procedure IniCalcMaps({MCTparas: PMCTparameter;} Header: TPMandHeader10);
procedure CopyFormulas(const OldFormulas: array of TCustomFormula;
MCTparas: PMCTparameter; var CFormulas: array of TFormulaClass);
procedure RayMarch(RMrec: TPRaymarchRec);
function SplineIpolMap(x, y: Double; PCalcMap: TPLightMap): TVec3D;
procedure RMdoBinSearch(pMCTparas: PMCTparameter; var DE, RLastStepWidth{, LastDE}: Double);
procedure RMCalculateVgradsFOV(pMCTparas: PMCTparameter; ix: Integer);
procedure RMmaxLengthToCutPlane(pMCTparas: PMCTparameter; var dLength: Double; var itmp: Integer; vPos: TPPos3D);
procedure RMdoColorOnIt(pMCTparas: PMCTparameter);
procedure RMdoColor(pMCTparas: PMCTparameter);
procedure RMcalcNanglesForCut(pMCTparas: PMCTparameter; CutPlane: Integer);
procedure RMdoBinSearchIt(pMCTparas: PMCTparameter; var ZZ: Double);
procedure RMdoSecantSearch(pMCTparas: PMCTparameter; var DE, RLastStepWidth, RLastDE: Double);
procedure RMCalculateNormals(pMCTparas: PMCTparameter; var NN: Single);
procedure RMCalculateNormalsOnSmoothIt(pMCTparas: PMCTparameter; var NN: Single);
procedure RMCalculateStartPos(pMCTparas: PMCTparameter; ix, iy: Integer);
procedure RMCalcRoughness(N: TPVec3D; var sRough: Single; dt2, dsG: PDouble);
function CalcDEnoADE(It3Dex: TPIteration3Dext; mctp: Pointer): Double;
function RMcalcVLight(StepCount: Single): Integer;
//function CalcDEfullNoCol(It3Dex: TPIteration3Dext; mctp: Pointer): Double;
var CalcMaps: array[0..2] of TLightMap; //max 3 lightmaps? todo: Global Map Handler with depencies + exclusive free+load
MapIsMissing: LongBool;
// CalcDE: TCaldDEfunction = CalcDEfull;
implementation
uses Mand, LightAdjust, CalcThread, CalcThread2D, HeaderTrafos, Math, Maps,
Navigator, SysUtils, Types, DivUtils, Forms, ImageProcess, FileHandling,
MapSequences;
//{$CODEALIGN 8}
{procedure FreeCalcMaps;
begin
if Mand3DForm.Button2.Caption <> 'Stop' then
begin
FreeLightMap(CalcMaps[0]);
FreeLightMap(CalcMaps[1]);
FreeLightMap(CalcMaps[2]);
end;
end; }
function RMcalcVLight(StepCount: Single): Integer;
asm
push ecx
fld dword [ebp + 8]
fistp dword [esp]
mov eax, [esp]
cmp eax, 16383
jle @1
mov eax, 16383
@1: bsr ecx, eax
jz @2
sub ecx, 6
jle @2
shr eax, cl
shl ecx, 7
or eax, ecx
@2: pop ecx
end;
{var i, i2: Integer; //ebp+8
begin
if StepCount > 16383 then i := 16383 else i := Round(StepCount);
i2 := 0;
while i > 127 do
begin
i := i shr 1;
Inc(i2);
end;
Result := i or (i2 shl 7);
end; }
procedure IniCalcMaps(Header: TPMandHeader10); //todo: scan Formulas for 'MAP' + integer and get value from varpointer
var i, i2, j, n, c, Mcount: Integer;
bIsIpol: LongBool;
MA: array[0..2] of Integer;
begin
Mcount := 0;
bIsIpol := (PTHeaderCustomAddon(Header.PCFAddon).bOptions1 and 3) = 1;
if bIsIpol then i2 := 2 else i2 := 5;
for i := 0 to i2 do if bIsIpol or (PTHeaderCustomAddon(Header.PCFAddon).Formulas[i].iItCount <> 0) then
for j := 0 to Min(16, PTHeaderCustomAddon(Header.PCFAddon).Formulas[i].iOptionCount) - 1 do
begin
if PTCustomFormula(Header.PHCustomF[i]).byOptionTypes[j] = 2 then //RangeCheck
if Pos('MAP', UpperCase(PTCustomFormula(Header.PHCustomF[i]).sOptionStrings[j])) > 0 then
begin
n := Round(MinMaxCS(0, PTHeaderCustomAddon(Header.PCFAddon).Formulas[i].dOptionValue[j], 33000));
if (n > 0) and (n < 32001) and (Mcount < 3) then
begin
for c := 0 to Mcount - 1 do if MA[c] = n then n := 0; //already in list
if n > 0 then begin
MA[Mcount] := n;
Inc(Mcount);
end;
end;
end;
end;
for i := 0 to 2 do
begin
j := 0;
c := 0;
while j < Mcount do // delete already loaded maps from list
begin
if (CalcMaps[i].LMnumber = MA[j]) and
((TMapSequenceListProvider.GetInstance.GetSequence(MA[j]) = nil) or
(CalcMaps[i].LMframe = TMapSequenceFrameNumberHolder.GetCurrFrameNumber)) then begin
Dec(Mcount);
for n := j to Mcount - 1 do MA[n] := MA[n + 1];
Inc(c);
end;
Inc(j);
end;
if c = 0 then FreeLightMap(@CalcMaps[i]);
end;
for i := 0 to Mcount - 1 do
begin
for j := 0 to 2 do if CalcMaps[j].LMnumber = 0 then
begin
LoadLightMapNr(MA[i], @CalcMaps[j]);
Break;
end;
end;
{ FreeLightMap(CalcMaps[0]); //no, mark maps as notused, afterwards free only ehich are still notused!
FreeLightMap(CalcMaps[1]);
FreeLightMap(CalcMaps[2]); }
end;
procedure CopyFormulas(const OldFormulas: array of TCustomFormula;
MCTparas: PMCTparameter; var CFormulas: array of TFormulaClass);
var i: Integer;
begin
for i := 0 to 5 do
begin
CFormulas[i] := TFormulaClass.Create;
CFormulas[i].AssignOld(@OldFormulas[i]);
MCTparas.fHPVar[i] := CFormulas[i].pConstPointer16;
MCTparas.fHybrid[i] := ThybridIteration2(CFormulas[i].pCodePointer);
end;
end;
function CalcMandT(Header: TPMandHeader10; PLightVals: TPLightVals; PCTS: TPCalcThreadStats;
PsiLight5: TPsiLight5; hSLoffset, FSIstart, FSIoffset: Integer; hRect: TRect): Boolean;
var x, ThreadCount: Integer;
MCTparas: TMCTparameter;
MandCalcThread: array of TMandCalcThread;
MandCalcThread2D: array of T2DcalcThread;
OldVerbose: Boolean;
begin
ThreadCount := Min(Mand3DForm.UpDown3.Position, Header.Height);
try
MCTparas := getMCTparasFromHeader(Header^, True);
Result := MCTparas.bMCTisValid;
if Result then begin
MCTparas.pSiLight := PsiLight5;
MCTparas.SLoffset := hSLoffset;
MCTparas.FSIstart := FSIstart;
MCTparas.FSIoffset := FSIoffset;
MCTparas.PLVals := PLightVals;
MCTparas.PCalcThreadStats := PCTS;
MCTparas.CalcRect := hRect;
if MCTparas.calc3D then SetLength(MandCalcThread, ThreadCount)
else SetLength(MandCalcThread2D, ThreadCount);
end;
// for x := 0 to 511 do DEreduce1[x] := 1;
// for x := 0 to 511 do DEreduce2[x] := 1;
{ for x := 0 to 16383 do begin
DEColStat[x] := 0;
DEColStatCount[x] := 0;
end;
// for x := 0 to 255 do RoughStat[x] := 0; //histo test }
except
Mand3DForm.OutMessage('There was an error, please report bug and send the current parameters!');
Result := False;
end;
if Result then
begin
PCTS.ctCalcRect := hRect;
for x := 1 to ThreadCount do
begin
PCTS.CTrecords[x].iActualYpos := -1;
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;
PCTS.CTrecords[x].MaxIts := 0;
MCTparas.iThreadId := x;
if MCTparas.calc3D then
begin
try
MandCalcThread[x - 1] := TMandCalcThread.Create(True);
MandCalcThread[x - 1].FreeOnTerminate := True;
{$ifdef PARAMS_PER_THREAD}
OldVerbose := bGetMCTPverbose;
try
bGetMCTPverbose := False;
MCTparas := getMCTparasFromHeader(Header^, True);
MCTparas.pSiLight := PsiLight5;
MCTparas.SLoffset := hSLoffset;
MCTparas.FSIstart := FSIstart;
MCTparas.FSIoffset := FSIoffset;
MCTparas.PLVals := PLightVals;
MCTparas.PCalcThreadStats := PCTS;
MCTparas.CalcRect := hRect;
finally
bGetMCTPverbose := OldVerbose;
end;
{$endif PARAMS_PER_THREAD}
MandCalcThread[x - 1].MCTparas := MCTparas;
MandCalcThread[x - 1].Priority := cTPrio[Mand3DForm.ComboBox2.ItemIndex];
PCTS.CTrecords[x].isActive := 1;
PCTS.CThandles[x] := MandCalcThread[x - 1];
// CopyFormulas(calcHybridCustoms, @MandCalcThread[x - 1].MCTparas, MandCalcThread[x - 1].Formulas);
except
ThreadCount := x - 1;
Break;
end;
end else begin
try
MandCalcThread2D[x - 1] := T2DcalcThread.Create(True);
MandCalcThread2D[x - 1].FreeOnTerminate := True;
{$ifdef PARAMS_PER_THREAD}
OldVerbose := bGetMCTPverbose;
try
bGetMCTPverbose := False;
MCTparas := getMCTparasFromHeader(Header^, True);
MCTparas.pSiLight := PsiLight5;
MCTparas.SLoffset := hSLoffset;
MCTparas.FSIstart := FSIstart;
MCTparas.FSIoffset := FSIoffset;
MCTparas.PLVals := PLightVals;
MCTparas.PCalcThreadStats := PCTS;
MCTparas.CalcRect := hRect;
finally
bGetMCTPverbose := OldVerbose;
end;
{$endif PARAMS_PER_THREAD}
MandCalcThread2D[x - 1].MCTparas := MCTparas;
MandCalcThread2D[x - 1].Priority := cTPrio[Mand3DForm.ComboBox2.ItemIndex];
PCTS.CTrecords[x].isActive := 1;
PCTS.CThandles[x] := MandCalcThread2D[x - 1];
except
ThreadCount := x - 1;
Break;
end;
end;
end;
PCTS.HandleType := 1;
if MCTparas.calc3D then
begin
for x := 0 to ThreadCount - 1 do MandCalcThread[x].MCTparas.iThreadCount := ThreadCount;
end else begin
for x := 0 to ThreadCount - 1 do MandCalcThread2D[x].MCTparas.iThreadCount := ThreadCount;
end;
PCTS.iTotalThreadCount := ThreadCount;
PCTS.cCalcTime := GetTickCount;
Header.bHScalculated := 0;
if MCTparas.calc3D then
begin
for x := 0 to ThreadCount - 1 do MandCalcThread[x].Start;
end else
for x := 0 to ThreadCount - 1 do MandCalcThread2D[x].Start;
end;
end;
{function CR(de, R: Double): Double;
var rR: Double;
begin
rR := Min0MaxCD(de/R, 1);
Result := 2*rR*rR*rR - 3*rR*rR + 1;
end; }
function CalcDEanalytic(It3Dex: TPIteration3Dext; mctp: Pointer): Double;
begin
with PMCTparameter(mctp)^ do
begin
if It3Dex.DEoption = 20 then //dIFS
begin
It3Dex.Rold := msDEstop * sStepWm103; //for heightmap formulas speedup, absolute DEstop
It3Dex.RStopD := It3Dex.Rold; //if already closer then stop iterating...
It3Dex.bIsInsideRender := bInsideRendering;
end;
Result := mMandFunctionDE(@It3Dex.C1) * dDEscale;
MaxItsResult := It3Dex.MaxIt;
if It3Dex.DEoption = 20 then Inc(MaxItsResult) else
if Result < msDEstop * s025 then Result := msDEstop * s025; //MaxCDvar(var ds, ddest: Double);
DEoptionResult := It3Dex.DEoption;
if bCalcInside then
begin
if DEoptionResult = 20 then //DEoptionResult.. in DEcomb the one who rules the raystep (for faster insides in decomb)
Result := msDEstop * 2 - Result
else
begin
Result := msDEstop * 4 - Result * 3;
if Result >= msDEstop then Dec(It3Dex.ItResultI);
end;
end;
end;
end;
{function CalcDEanalytic(It3Dex: TPIteration3Dext; mctp: Pointer): Double;
asm //eax edx
push esi //ecx can be changed in functions!
push ebx //the whale2 testpars were bad
lea esi, eax + 136
lea ebx, edx + 180
cmp dword [esi + TIteration3Dext.DEoption - 136], 20 //TMCTparameter
jnz @1
fld dword [ebx + TMCTparameter.msDEstop - 180]
fmul dword [ebx + TMCTparameter.sStepWm103 - 180]
fst qword [esi + TIteration3Dext.Rold - 136]
fstp qword [esi + TIteration3Dext.RStopD - 136]
mov eax, [ebx + TMCTparameter.bInsideRendering - 180]
mov [esi + TIteration3Dext.bIsInsideRender - 136], eax
@1: lea eax, esi + TIteration3Dext.C1 - 136
call dword [ebx + TMCTparameter.mMandFunctionDE - 180]
fmul dword [ebx + TMCTparameter.dDEscale - 180]
mov eax, [esi + TIteration3Dext.MaxIt - 136]
mov [ebx + TMCTparameter.MaxItsResult - 180], eax
cmp dword [esi + TIteration3Dext.DEoption - 136], 20
jnz @2
inc dword [ebx + TMCTparameter.MaxItsResult - 180]
jmp @4
@2: fld dword [ebx + TMCTparameter.msDEstop - 180]
fmul s025 //msDEstop*0.25, Result
fcom
fwait
fnstsw ax
shr ah, 1
jc @3
fxch
@3: fstp st //result
@4: mov eax, [esi + TIteration3Dext.DEoption - 136]
mov [ebx + TMCTparameter.DEoptionResult - 180], eax
cmp dword [ebx + TMCTparameter.bCalcInside - 180], 0
jz @5
fld dword [ebx + TMCTparameter.msDEstop - 180]
fadd st, st
cmp eax, 20
jnz @6
fsubrp //result:=msDEstop*2-result
jmp @5
@6: fadd st, st
fxch //result, msDEstop*4
fmul s3
fsubp //result:=msDEstop*4-result*3
fcom dword [ebx + TMCTparameter.msDEstop - 180]
fstsw ax
sahf
jb @5
dec dword [esi + TIteration3Dext.ItResultI - 136]
@5: pop ebx
pop esi
end; }
procedure RestoreF1DEcomb(DEcombRec: TPBufDEcomb; mctp: PMCTparameter);
begin // restore first hybrid vals
with mctp^ do
begin
pIt3Dext.MaxIt := iMaxIt;
pIt3Dext.DEoption := DEoption;
PInteger(@pIt3Dext.iRepeatFrom)^ := PInteger(@RepeatFrom1)^;
pIt3Dext.EndTo := wEndTo;
bCalcInside := bInsideRendering;
IsCustomDE := IsCustomDE1;
dDEscale := DEcombRec.bufMCTdDEscale;
mMandFunction := DEcombRec.bufMCTMandFunction;
mMandFunctionDE := DEcombRec.bufMCTMandFunctionDE;
end;
end;
procedure PrepareF1DEcomb(DEcombRec: TPBufDEcomb; mctp: PMCTparameter);
begin //load second hybrid part
with mctp^ do
begin
PInteger(@pIt3Dext.iRepeatFrom)^ := PInteger(@RepeatFrom2)^;
pIt3Dext.EndTo := iEnd2;
DEcombRec.bufMCTMandFunction := mMandFunction;
DEcombRec.bufMCTMandFunctionDE := mMandFunctionDE;
mMandFunction := mMandFunction2;
mMandFunctionDE := mMandFunctionDE2;
DEcombRec.bufIt3DItResultI := pIt3Dext.ItResultI;
DEcombRec.bufIt3DOtrap := pIt3Dext.Otrap;
DEcombRec.bufIt3DSmoothItD := pIt3Dext.SmoothItD;
DEcombRec.bufMCTdDEscale := dDEscale;
DEcombRec.bufMCTmaxItsResult := MaxItsResult;
dDEscale := dDEscale2;
IsCustomDE := IsCustomDE2;
bCalcInside := False;
pIt3Dext.MaxIt := iMaxitF2;
pIt3Dext.DEoption := DEoption2;
end;
end;
{procedure Prepare3Dgrad(DEcombRec: TPBufDEcomb; It3Dex: TPIteration3Dext);
begin
DEcombRec.bufMCTCalcSIT := It3Dex.CalcSIT;
DEcombRec.bufIt3DItResultI := It3Dex.MaxIt;
DEcombRec.bufIt3DOtrap := It3Dex.Rout;
DEcombRec.bufIt3DSmoothItD := It3Dex.SmoothItD;
It3Dex.CalcSIT := False;
It3Dex.MaxIt := It3Dex.ItResultI;
It3Dex.RStop := Sqr(It3Dex.RStop) * 64;
mCopyVec(@DEcombRec.bufVec3D, @It3Dex.C1);
end;
procedure Restore3Dgrad(DEcombRec: TPBufDEcomb; It3Dex: TPIteration3Dext);
begin
It3Dex.ItResultI := It3Dex.MaxIt; //new, restore ItResult
It3Dex.Rout := DEcombRec.bufIt3DOtrap;
It3Dex.MaxIt := DEcombRec.bufIt3DItResultI;
It3Dex.SmoothItD := DEcombRec.bufIt3DSmoothItD;
It3Dex.CalcSIT := DEcombRec.bufMCTCalcSIT;
mCopyVec(@It3Dex.C1, @DEcombRec.bufVec3D);
end; }
{
procedure NotVar(var b: LongBool);
asm
cmp dword [eax], 0
je @@1
mov dword [eax], 0
ret
@@1:
mov dword [eax], -1
end; }
function CalcDEnoADE(It3Dex: TPIteration3Dext; mctp: Pointer): Double;
var bufMCTCalcSIT, bufMaxIt: Integer;
// bufIt3DSmoothItD: Single;
bufD, bufRout, Rst, wt, dt: Double;
begin
with PMCTparameter(mctp)^ do
begin
mMandFunction(@It3Dex.C1);
if bInsideRendering and (It3Dex.ItResultI = It3Dex.MaxIt) then Result := 0 else
begin
if It3Dex.Rout < d1em200 then Result := 0 else
begin
{ Prepare3Dgrad(@DEcombRec, It3Dex);
mAddVecWeight(@It3Dex.C1, @Vgrads[2], mctDEoffset);
mMandFunction(@It3Dex.C1);
dt := Sqr(DEcombRec.bufIt3DOtrap - It3Dex.Rout);
mCopyAddVecWeight(@It3Dex.C1, @DEcombRec.bufVec3D, @Vgrads[1], mctDEoffset);
mMandFunction(@It3Dex.C1);
wt := Sqr(DEcombRec.bufIt3DOtrap - It3Dex.Rout);
mCopyAddVecWeight(@It3Dex.C1, @DEcombRec.bufVec3D, @Vgrads[0], mctDEoffset);
mMandFunction(@It3Dex.C1);
Rst := Sqr(DEcombRec.bufIt3DOtrap - It3Dex.Rout); }
// bufIt3DSmoothItD := It3Dex.SmoothItD;
bufMCTCalcSIT := PInteger(@It3Dex.CalcSIT)^; //bytebool
bufMaxIt := It3Dex.MaxIt;
bufRout := It3Dex.Rout;
bufD := It3Dex.C1;
It3Dex.CalcSIT := False;
It3Dex.MaxIt := It3Dex.ItResultI;
It3Dex.RStop := Rstop3D;
It3Dex.C1 := It3Dex.C1 + mctDEoffset;
mMandFunction(@It3Dex.C1);
dt := Sqr(bufRout - It3Dex.Rout);
It3Dex.C1 := bufD;
bufD := It3Dex.C2;
It3Dex.C2 := It3Dex.C2 + mctDEoffset;
mMandFunction(@It3Dex.C1);
wt := Sqr(bufRout - It3Dex.Rout);
It3Dex.C2 := bufD;
bufD := It3Dex.C3;
It3Dex.C3 := It3Dex.C3 + mctDEoffset;
mMandFunction(@It3Dex.C1);
Rst := Sqr(bufRout - It3Dex.Rout);
It3Dex.C3 := bufD;
{ v := AddVecF(AddVecF(ScaleVector(TPVec3D(@Vgrads[0])^, Rst),
ScaleVector(TPVec3D(@Vgrads[1])^, wt)),
ScaleVector(TPVec3D(@Vgrads[2])^, dt));
dd := DotOfVectorsNormalize(@v, @mVGradsFOV); }
{ if Rst > wt then
begin
if Rst > dt then dd := DotOfVectorsNormalize(@Vgrads[0], @mVGradsFOV)
else dd := DotOfVectorsNormalize(@Vgrads[2], @mVGradsFOV);
end
else if wt > dt then
dd := DotOfVectorsNormalize(@Vgrads[1], @mVGradsFOV)
else dd := DotOfVectorsNormalize(@Vgrads[2], @mVGradsFOV);
sZstepDiv2 := sZstepDiv + (1 - sZstepDiv) * (0.5 - Abs(dd) * 0.5);}
Result := bufRout * Ln(bufRout) * dDEscale / (Sqrt(Rst + wt + dt) + mctDEoffset006);
It3Dex.Rout := bufRout;
It3Dex.ItResultI := It3Dex.MaxIt;
It3Dex.MaxIt := bufMaxIt;
// It3Dex.SmoothItD := bufIt3DSmoothItD;
PInteger(@It3Dex.CalcSIT)^ := bufMCTCalcSIT;
It3Dex.RStop := dRStop;
// mCopyVec(@It3Dex.C1, @DEcombRec.bufVec3D);
end;
end;
MaxItsResult := It3Dex.MaxIt;
if Result < msDEstop * 0.25 then Result := msDEstop * 0.25; //MaxCDvar(DS025, Rst);
DEoptionResult := It3Dex.DEoption;
if bCalcInside then
begin
Result := msDEstop * 4 - Result * 3;
if (Result >= msDEstop) then Dec(It3Dex.ItResultI);
end;
end;
end;
function CalcDEfull(It3Dex: TPIteration3Dext; mctp: Pointer{PMCTparameter}): Double;
var DEcombRec: TBufDEcomb;
DS025, RD1, Rst, wt, dt: Double;
label go1;
begin
with PMCTparameter(mctp)^ do
begin
DS025 := msDEstop * 0.25;
if IsCustomDE then
begin
if It3Dex.DEoption = 20 then //dIFS
begin
It3Dex.Rold := msDEstop * sStepWm103; //for heightmap formulas speedup, absolute DEstop
It3Dex.bIsInsideRender := bInsideRendering;
It3Dex.RStopD := msDEstop * sStepWm103;
end;
Result := mMandFunctionDE(@It3Dex.C1) * dDEscale;
wt := Abs(It3Dex.w);
dt := Abs(It3Dex.Deriv1);
end
else
begin //3d gradient
mMandFunction(@It3Dex.C1);
if bInsideRendering and (It3Dex.ItResultI = It3Dex.MaxIt) then Result := 0 else
begin
if It3Dex.Rout < d1em200 then Result := 0 else
begin
DEcombRec.bufMCTCalcSIT := PLongBool(@It3Dex.CalcSIT)^; //bytebool
DEcombRec.bufIt3DItResultI := It3Dex.MaxIt;
DEcombRec.bufIt3DOtrap := It3Dex.Rout;
DEcombRec.bufVec3D[0] := It3Dex.C1;
It3Dex.CalcSIT := False;
It3Dex.MaxIt := It3Dex.ItResultI;
It3Dex.RStop := Rstop3D;
It3Dex.C1 := It3Dex.C1 + mctDEoffset;
mMandFunction(@It3Dex.C1);
dt := Sqr(DEcombRec.bufIt3DOtrap - It3Dex.Rout);
It3Dex.C1 := DEcombRec.bufVec3D[0];
DEcombRec.bufVec3D[0] := It3Dex.C2;
It3Dex.C2 := It3Dex.C2 + mctDEoffset;
mMandFunction(@It3Dex.C1);
wt := Sqr(DEcombRec.bufIt3DOtrap - It3Dex.Rout);
It3Dex.C2 := DEcombRec.bufVec3D[0];
DEcombRec.bufVec3D[0] := It3Dex.C3;
It3Dex.C3 := It3Dex.C3 + mctDEoffset;
mMandFunction(@It3Dex.C1);
Rst := Sqr(DEcombRec.bufIt3DOtrap - It3Dex.Rout);
It3Dex.C3 := DEcombRec.bufVec3D[0];
Result := DEcombRec.bufIt3DOtrap * Ln(DEcombRec.bufIt3DOtrap) * dDEscale /
(Sqrt(Rst + wt + dt) + mctDEoffset006);
It3Dex.Rout := DEcombRec.bufIt3DOtrap;
It3Dex.ItResultI := It3Dex.MaxIt;
It3Dex.MaxIt := DEcombRec.bufIt3DItResultI;
PLongBool(@It3Dex.CalcSIT)^ := DEcombRec.bufMCTCalcSIT;
It3Dex.RStop := dRStop;
// It3Dex.SmoothItD := DEcombRec.bufIt3DSmoothItD;
{ Prepare3Dgrad(@DEcombRec, It3Dex);
// try //test
mAddVecWeight(@It3Dex.C1, @Vgrads[2], mctDEoffset);
mMandFunction(@It3Dex.C1);
dt := Sqr(DEcombRec.bufIt3DOtrap - It3Dex.Rout);
mCopyAddVecWeight(@It3Dex.C1, @DEcombRec.bufVec3D, @Vgrads[1], mctDEoffset);
mMandFunction(@It3Dex.C1);
wt := Sqr(DEcombRec.bufIt3DOtrap - It3Dex.Rout);
mCopyAddVecWeight(@It3Dex.C1, @DEcombRec.bufVec3D, @Vgrads[0], mctDEoffset);
mMandFunction(@It3Dex.C1);
Rst := Sqr(DEcombRec.bufIt3DOtrap - It3Dex.Rout);
Result := DEcombRec.bufIt3DOtrap * Ln(DEcombRec.bufIt3DOtrap) * dDEscale / //div0 in ln if Rout=0
(Sqrt(Rst + wt + dt) + (mctDEoffset * s006));
// except end;
It3Dex.Rout := DEcombRec.bufIt3DOtrap;
It3Dex.ItResultI := It3Dex.MaxIt;
It3Dex.MaxIt := DEcombRec.bufIt3DItResultI;
It3Dex.SmoothItD := DEcombRec.bufIt3DSmoothItD;
It3Dex.CalcSIT := DEcombRec.bufMCTCalcSIT;
It3Dex.RStop := dRStop;
mCopyVec(@It3Dex.C1, @DEcombRec.bufVec3D); }
end;
end;
end;
MaxItsResult := iMaxIt;
if It3Dex.DEoption = 20 then Inc(MaxItsResult) else MaxCDvar(DS025, Result);
DEoptionResult := It3Dex.DEoption;
if FormulaType > 0 then //DEcomb type 1:min 2:max 3:maxInv 4:miS1 5:miS2 6: mix f1 first 7: mix f2-6 first
begin
PrepareF1DEcomb(@DEcombRec, mctp); //bCalcInside disabled here, is done at end for whole hybrid (bInsideRendering still set for dIFS)
// try//test
if FormulaType = 3 then bCalcInside := not bCalcInside;//NotVar(bCalcInside);
if IsCustomDE then Rst := CalcDEanalytic(It3Dex, mctp)
else Rst := CalcDEnoADE(It3Dex, mctp);
case FormulaType of
1: if Rst >= Result then goto go1 else Result := Rst; //minDE comb
2..3: if Rst > Result then Result := Rst else //..3: MaxInv: Invert the DE of one formula and combine "max" to cut off parts
begin
go1: It3Dex.ItResultI := DEcombRec.bufIt3DItResultI;
It3Dex.SmoothItD := DEcombRec.bufIt3DSmoothItD;
It3Dex.Otrap := DEcombRec.bufIt3DOtrap;
MaxItsResult := DEcombRec.bufMCTmaxItsResult;
DEoptionResult := DEoption;
end;
4..5: begin
if Rst >= Result then //< PMCTparameter(mctp).MaxItsResult := PMCTparameter(mctp).iMaxitF2 else
begin
It3Dex.ItResultI := DEcombRec.bufIt3DItResultI;
MaxItsResult := DEcombRec.bufMCTmaxItsResult;
DEoptionResult := DEoption;
end;
if FormulaType = 4 then //linear
Result := MinCD(Rst - Clamp0D(sDEcombSmooth - Result),
Result - Clamp0D(sDEcombSmooth - Rst))
else
begin //smooth
RD1 := Clamp0D(sDEcombSmooth - Result - msDEstop);
dt := Clamp0D(sDEcombSmooth - Rst - msDEstop);
Result := MinCD(Rst - RD1 * (sDEcombSmooth - dt) / sDEcombSmooth,
Result - dt * (sDEcombSmooth - RD1) / sDEcombSmooth);
end;
Rst := Abs(Rst);
wt := Abs(Result);
dt := 1 / (wt + Rst + s1em10);
It3Dex.SmoothItD := (It3Dex.SmoothItD * wt + DEcombRec.bufIt3DSmoothItD * Rst) * dt;
It3Dex.Otrap := (It3Dex.Otrap * wt + DEcombRec.bufIt3DOtrap * Rst) * dt;
end;
else
begin //6..7 dIFS mix: first common fractal until bailout or maxits, then dIFS for distance.. 6: Mix, first f1..fendh1
case DEmixCol of //only in calcDEcol function, not on every step (no calcsit etc)!?
0: begin
It3Dex.SmoothItD := It3Dex.SmoothItD + DEcombRec.bufIt3DSmoothItD;
It3Dex.Otrap := (It3Dex.Otrap + DEcombRec.bufIt3DOtrap) * 0.5;
end;
1: begin
It3Dex.SmoothItD := DEcombRec.bufIt3DSmoothItD;
It3Dex.Otrap := DEcombRec.bufIt3DOtrap;
end;
//2: color of second formula, is already
end;
case DEoption of
5, 6: Result := Rst / dt; //It3Dex.Deriv1;
2,11: Result := Rst / wt; //It3Dex.w;
else
begin
Result := Rst * Power(Abs(FmixPow), -1 - DEcombRec.bufIt3DItResultI); // other method with additional value
end;
end;
end;
end;
// finally //test
RestoreF1DEcomb(@DEcombRec, mctp);
{ pIt3Dext.MaxIt := iMaxIt;
pIt3Dext.DEoption := DEoption;
PInteger(@pIt3Dext.iRepeatFrom)^ := PInteger(@RepeatFrom1)^;
pIt3Dext.EndTo := wEndTo;
bCalcInside := bInsideRendering;
IsCustomDE := IsCustomDE1;
dDEscale := DEcombRec.bufMCTdDEscale;
mMandFunction := DEcombRec.bufMCTMandFunction;
mMandFunctionDE := DEcombRec.bufMCTMandFunctionDE; }
// end;
end;
if bCalcInside then
begin
if DEoptionResult = 20 then Result := msDEstop * 2 - Result else
begin
Result := msDEstop * 4 - Result * 3;
if (Result >= msDEstop) then Dec(It3Dex.ItResultI); //else probs with decomb and rpow3-> no stop...
end; //after bin search this can produce noise because of both sides possible?
end;
end;
end;
//dsG negative?
procedure RMCalcRoughness(N: TPVec3D; var sRough: Single; dt2, dsG: PDouble);
{begin // eax edx ecx [ebp+8]
// dT2 := Noffset * 0.5 / (dM * StepSNorm);
sRough := Clamp01D(Sqrt(d1em100 + dSG^ * 7 * Sqr(dt2^) / //was: fp overflow vals about 1e-100 ?
(d1em100 + Sqr(N[0]) + Sqr(N[1]) + Sqr(N[2]))) - 0.05); //}
asm
cmp SupportSSE2, 0
jz @@1
movupd xmm0, [eax]
movsd xmm1, [eax + 16]
movsd xmm2, [ecx]
mulpd xmm0, xmm0
mulsd xmm1, xmm1
mulsd xmm2, xmm2
addsd xmm1, xmm0
mov eax, [ebp + 8]
unpckhpd xmm0, xmm0
mulsd xmm2, [eax]
addsd xmm1, xmm0
mulsd xmm2, d7
addsd xmm1, d1em40
addsd xmm2, d1em40
xorpd xmm3, xmm3
divsd xmm2, xmm1
maxsd xmm2, xmm3
sqrtsd xmm2, xmm2
subsd xmm2, d005
maxsd xmm2, xmm3
minsd xmm2, d1p0
cvtsd2ss xmm4, xmm2
movss [edx], xmm4
jmp @end
@@1:
fld qword [eax]
fmul st, st
fld qword [eax + 8]
fmul st, st
faddp
fld qword [eax + 16]
fmul st, st
faddp
fadd d1em100
mov eax, [ebp + 8]
fld qword [ecx]
fmul st, st
fmul qword [eax]
fmul s7
fadd d1em100
fdivrp
ftst
fnstsw ax
shr ah, 1
jnc @1
fstp st
fldz
@1: fsqrt
fld s005 //0.05, sR'
fcom st(1)
fnstsw ax
shr ah, 1
jc @up
fcompp
xor eax, eax
mov [edx], eax
jmp @end
@up:
fsubp
fld1
fcomp st(1)
fnstsw ax
and ah, 41H
jz @up2
fstp st
fld1
@up2:
fstp dword [edx]
@end:
end;
procedure RMCalculateNormals(pMCTparas: PMCTparameter; var NN: Single);
var itmp, iy, ix, SmoothN: Integer;
// bIR: LongBool;
Noffset, NN2, NN1, dNN, dT2, dM, dS, dSG, StepSNorm: Double;
N, Vx, Vy, CT1: TVec3D;
sd: TLightSD;
begin
with pMCTparas^ do
begin
// bIR := bInsideRendering;
{ if bInsideRendering then
begin
// bCalcInside := True;
bInsideRendering := False; //to calc 4point DE also if ItResult=Maxit, does not help
// msDEstop := msDEstop * 2; //to avoid decreasing ItResultI, does not help
// end; }
// if not IsCustomDE then Inc(pIt3Dext.maxIt, 4); //test
pIt3Dext.CalcSIT := True;
Noffset := MinCS(1, DEstop) * (1 + mZZ * mctDEstopFactor) * 0.15;
mCopyVec(@CT1, @pIt3Dext.C1);
dNN := CalcDE(pIt3Dext, pMCTparas);
NN := pIt3Dext.SmoothItD; //for coloring
pIt3Dext.CalcSIT := False;
if iSmNormals = 8 then
begin
ClearDVec(N);
StepSNorm := Noffset * 1.3333;
for itmp := -2 to 2 do
for iy := -2 to 2 do
for ix := -2 to 2 do
if (itmp or iy or ix) <> 0 then
begin
mCopyAddVecWeight(@pIt3Dext.C1, @CT1, @Vgrads[2], itmp * StepSNorm);
mAddVecWeight(@pIt3Dext.C1, @Vgrads[1], iy * StepSNorm);
mAddVecWeight(@pIt3Dext.C1, @Vgrads[0], ix * StepSNorm);
dT2 := CalcDE(pIt3Dext, pMCTparas);
if itmp <> 0 then N[2] := N[2] + dT2 / itmp;
if iy <> 0 then N[1] := N[1] + dT2 / iy;
if ix <> 0 then N[0] := N[0] + dT2 / ix;
end;
ScaleVectorV(@N, 0.0075);
end
else
begin
mAddVecWeight(@pIt3Dext.C1, @Vgrads[2], Noffset);
N[2] := CalcDE(pIt3Dext, pMCTparas); //Zgradient
mAddVecWeight(@pIt3Dext.C1, @Vgrads[2], -2 * Noffset);
N[2] := (N[2] - CalcDE(pIt3Dext, pMCTparas)) * s05;
mCopyAddVecWeight(@pIt3Dext.C1, @CT1, @Vgrads[0], Noffset);
N[0] := CalcDE(pIt3Dext, pMCTparas); //Xgradient
mAddVecWeight(@pIt3Dext.C1, @Vgrads[0], -2 * Noffset);
N[0] := (N[0] - CalcDE(pIt3Dext, pMCTparas)) * s05;
mCopyAddVecWeight(@pIt3Dext.C1, @CT1, @Vgrads[1], Noffset);
N[1] := CalcDE(pIt3Dext, pMCTparas); //Ygradient
mAddVecWeight(@pIt3Dext.C1, @Vgrads[1], -2 * Noffset);
N[1] := (N[1] - CalcDE(pIt3Dext, pMCTparas)) * s05;
end;
if iSmNormals > 0 then //smoothed with estimation of roughness, eg deviation from mid val
begin
Noffset := Noffset * 2;
if iSmNormals < 8 then
begin
mCopyAddVecWeight(@pIt3Dext.C1, @CT1, @Vgrads[0], -Noffset); //smooth mid point
dNN := dNN + CalcDE(pIt3Dext, pMCTparas);
mAddVecWeight(@pIt3Dext.C1, @Vgrads[0], 2 * Noffset);
dNN := dNN + CalcDE(pIt3Dext, pMCTparas);
mCopyAddVecWeight(@pIt3Dext.C1, @CT1, @Vgrads[1], -Noffset);
dNN := dNN + CalcDE(pIt3Dext, pMCTparas);
mAddVecWeight(@pIt3Dext.C1, @Vgrads[1], 2 * Noffset);
dNN := (dNN + CalcDE(pIt3Dext, pMCTparas)) * 0.2;
end;
SmoothN := iSmNormals;
StepSNorm := Noffset * 3 / (SmoothN + s05);
dM := SmoothN * 2;
CreateXYVecsFromNormals(@N, @Vx, @Vy); //make ortho vector x,y related to normals
RotateVectorReverse(@Vx, @Vgrads);
RotateVectorReverse(@Vy, @Vgrads);
mCopyAddVecWeight(@pIt3Dext.C1, @CT1, @Vx, -SmoothN * StepSNorm);
NN1 := 0;
dS := 0;
for itmp := -SmoothN to SmoothN do
begin
if itmp <> 0 then
begin
dT2 := (CalcDE(pIt3Dext, pMCTparas) - dNN) / itmp;
NN1 := NN1 + dT2;
dS := dS + Sqr(dT2);
end;
mAddVecWeight(@pIt3Dext.C1, @Vx, StepSNorm);
end;
dSG := dS * dM - Sqr(NN1);
mCopyAddVecWeight(@pIt3Dext.C1, @CT1, @Vy, -SmoothN * StepSNorm);
NN2 := 0;
dS := 0;
for itmp := -SmoothN to SmoothN do
begin
if itmp <> 0 then
begin
dT2 := (CalcDE(pIt3Dext, pMCTparas) - dNN) / itmp;
NN2 := NN2 + dT2;
dS := dS + Sqr(dT2);
end;
mAddVecWeight(@pIt3Dext.C1, @Vy, StepSNorm);
end;
dT2 := Noffset * 0.5 / (dM * StepSNorm);
dSG := dSG + dS * dM - Sqr(NN2);
RMCalcRoughness(@N, sRoughness, @dT2, @dSG);
// sRoughness := Clamp01D(Sqrt(d1em100 + dSG * 7 * Sqr(dT2) / //was: fp overflow vals about 1e-100 ?
// (d1em100 + Sqr(N[0]) + Sqr(N[1]) + Sqr(N[2]))) - 0.05); //also called sqrt invalid! neg number?
if iSmNormals < 8 then
begin
N[0] := N[0] + NN1 * dT2;
N[1] := N[1] + NN2 * dT2;
end;
end;
mCopyVec(@pIt3Dext.C1, @CT1);
MakeWNormalsFromDVec(TPLNormals(mPsiLight), @N);
if PInteger(@mPsiLight.NormalX)^ = 0 then
if mPsiLight.NormalZ > 0 then mPsiLight.NormalZ := 32767
else mPsiLight.NormalZ := -32767;
// if not IsCustomDE then Dec(pIt3Dext.maxIt, 4); //test
// msDEstop := msDEstop * 0.5;
// bInsideRendering := bIR;
end;
end;
procedure RMCalculateNormalsOnSmoothIt(pMCTparas: PMCTparameter; var NN: Single);
var itmp, SmoothN, iy, ix: Integer;
Noffset, NN2, NN1, StepSNorm: Double;
dT2, dM, dS, dSG: Double;
N, CT1, Vx, Vy : TVec3D;
begin
with pMCTparas^ do
begin
Noffset := MinCS(1, DEstop) * (1 + mZZ * mctDEstopFactor) * 0.15;
pIt3Dext.CalcSIT := True;
mCopyVec(@CT1, @pIt3Dext.C1);
mMandFunction(@pIt3Dext.C1);
NN := pIt3Dext.SmoothItD;
if iSmNormals = 8 then
begin
ClearDVec(N);
StepSNorm := Noffset * 1.3333;
for itmp := -2 to 2 do
for iy := -2 to 2 do
for ix := -2 to 2 do
if (itmp or iy or ix) <> 0 then
begin
mCopyAddVecWeight(@pIt3Dext.C1, @CT1, @Vgrads[2], itmp * StepSNorm);
mAddVecWeight(@pIt3Dext.C1, @Vgrads[1], iy * StepSNorm);
mAddVecWeight(@pIt3Dext.C1, @Vgrads[0], ix * StepSNorm);
mMandFunction(@pIt3Dext.C1);
dT2 := pIt3Dext.SmoothItD;
if itmp <> 0 then N[2] := N[2] - dT2 / itmp;
if iy <> 0 then N[1] := N[1] - dT2 / iy;
if ix <> 0 then N[0] := N[0] - dT2 / ix;
end;
ScaleVectorV(@N, 0.0075);
end
else
begin
mAddVecWeight(@pIt3Dext.C1, @Vgrads[2], Noffset);
mMandFunction(@pIt3Dext.C1);
N[2] := pIt3Dext.SmoothItD; //Zgradient
mAddVecWeight(@pIt3Dext.C1, @Vgrads[2], -2 * Noffset);
mMandFunction(@pIt3Dext.C1);
N[2] := (pIt3Dext.SmoothItD - N[2]) * s05;
mCopyAddVecWeight(@pIt3Dext.C1, @CT1, @Vgrads[0], Noffset);
mMandFunction(@pIt3Dext.C1);
N[0] := pIt3Dext.SmoothItD; //Xgradient
mAddVecWeight(@pIt3Dext.C1, @Vgrads[0], -2 * Noffset);
mMandFunction(@pIt3Dext.C1);
N[0] := (pIt3Dext.SmoothItD - N[0]) * s05;
mCopyAddVecWeight(@pIt3Dext.C1, @CT1, @Vgrads[1], Noffset);
mMandFunction(@pIt3Dext.C1);
N[1] := pIt3Dext.SmoothItD; //Ygradient
mAddVecWeight(@pIt3Dext.C1, @Vgrads[1], -2 * Noffset);
mMandFunction(@pIt3Dext.C1);
N[1] := (pIt3Dext.SmoothItD - N[1]) * s05;
end;
if iSmNormals > 0 then //smoothed with estimation of roughness, eg deviation from mid val
begin
Noffset := Noffset * 2;
mCopyAddVecWeight(@pIt3Dext.C1, @CT1, @Vgrads[0], -Noffset); //smooth mid point
mMandFunction(@pIt3Dext.C1);
NN := NN + pIt3Dext.SmoothItD;
mAddVecWeight(@pIt3Dext.C1, @Vgrads[0], 2 * Noffset);
mMandFunction(@pIt3Dext.C1);
NN := NN + pIt3Dext.SmoothItD;
mCopyAddVecWeight(@pIt3Dext.C1, @CT1, @Vgrads[1], -Noffset);
mMandFunction(@pIt3Dext.C1);
NN := NN + pIt3Dext.SmoothItD;
mAddVecWeight(@pIt3Dext.C1, @Vgrads[1], 2 * Noffset);
mMandFunction(@pIt3Dext.C1);
NN := (NN + pIt3Dext.SmoothItD) * 0.2;
SmoothN := iSmNormals;
StepSNorm := Noffset * 3 / (SmoothN + s05);
dM := SmoothN * 2;
//make ortho vector x,y related to normals
CreateXYVecsFromNormals(@N, @Vx, @Vy);
RotateVectorReverse(@Vx, @Vgrads);
RotateVectorReverse(@Vy, @Vgrads);
mCopyAddVecWeight(@pIt3Dext.C1, @CT1, @Vx, -SmoothN * StepSNorm);
NN1 := 0;
dS := 0;
for itmp := -SmoothN to SmoothN do
begin
if itmp <> 0 then
begin
mMandFunction(@pIt3Dext.C1);
dT2 := (NN - pIt3Dext.SmoothItD) / itmp;
NN1 := NN1 + dT2;
dS := dS + Sqr(dT2);
end;
mAddVecWeight(@pIt3Dext.C1, @Vx, StepSNorm);
end;
dSG := dS * dM - Sqr(NN1);
mCopyAddVecWeight(@pIt3Dext.C1, @CT1, @Vy, -SmoothN * StepSNorm);
NN2 := 0;