forked from thargor6/mb3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonteCarloForm.pas
1197 lines (1141 loc) · 39.7 KB
/
MonteCarloForm.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 MonteCarloForm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, M3Iregister,
Dialogs, StdCtrls, Buttons, ExtCtrls, ComCtrls, TypeDefinitions, Vcl.ExtDlgs,
Vcl.ImgList;
type
TMCForm = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Button9: TSpeedButton;
Button8: TSpeedButton;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Panel4: TPanel;
Panel5: TPanel;
Label1: TLabel;
Label2: TLabel;
TrackBar1: TTrackBar;
Label7: TLabel;
Timer2: TTimer;
ScrollBox1: TScrollBox;
Image1: TImage;
Timer1: TTimer;
ProgressBar1: TProgressBar;
Label8: TLabel;
SaveDialog3: TSaveDialog;
SpeedButton1: TSpeedButton;
SaveDialog6: TSaveDialog;
Button4: TButton;
Label14: TLabel;
Label15: TLabel;
CheckBox5: TCheckBox;
Button6: TButton;
ImageList1: TImageList;
CategoryPanelGroup2: TCategoryPanelGroup;
CategoryPanel6: TCategoryPanel;
CategoryPanel5: TCategoryPanel;
CategoryPanel1: TCategoryPanel;
Label9: TLabel;
Edit21: TEdit;
UpDown3: TUpDown;
CheckBox6: TCheckBox;
CheckBox2: TCheckBox;
Label20: TLabel;
Edit1: TEdit;
UpDown1: TUpDown;
Label21: TLabel;
Edit2: TEdit;
CheckBox3: TCheckBox;
Button5: TButton;
CheckBox4: TCheckBox;
Image2: TImage;
UpDown2: TUpDown;
Label23: TLabel;
Label22: TLabel;
Label6: TLabel;
Edit3: TEdit;
Label24: TLabel;
ComboBox1: TComboBox;
CategoryPanel2: TCategoryPanel;
Label4: TLabel;
Label5: TLabel;
Label2avrgnoise: TLabel;
Label7avrgrays: TLabel;
Label10: TLabel;
Label11: TLabel;
Label12: TLabel;
Label13: TLabel;
Label18: TLabel;
Label19: TLabel;
Label3: TLabel;
Label25: TLabel;
Label26: TLabel;
CheckBox7: TCheckBox;
Label33: TLabel;
Timer3: TTimer;
Label29: TLabel;
Label30: TLabel;
Label31: TLabel;
Label32: TLabel;
TrackBar3: TTrackBar;
CheckBox8: TCheckBox;
TrackBar4: TTrackBar;
procedure TrackBar18KeyPress(Sender: TObject; var Key: Char);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure SaveDialog6TypeChange(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure FormHide(Sender: TObject);
procedure CheckBox2Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure CheckBox4Click(Sender: TObject);
procedure UpDown2Click(Sender: TObject; Button: TUDBtnType);
procedure CategoryPanel2Collapse(Sender: TObject);
procedure CategoryPanel2Expand(Sender: TObject);
procedure Timer3Timer(Sender: TObject);
private
{ Private-Deklarationen }
MCActiveCalcThreads: Integer;
LastUpdate: Cardinal;
LastSaved: Cardinal;
AvrgSqrNoise: Double;
AvrgRCount: Single;
iUpdate: Integer;
bGotZeroCounts: LongBool;
FirstOpenM3C: LongBool;
FName, SaveFName: String;
BokehBMP: array[0..5] of TBitmap;
procedure MCRepaint;
function SizeOK: LongBool;
procedure MCTriggerRepaint;
procedure StartCalc;
procedure FitImageSize;
procedure CalcAvrgNoise;
procedure SetParas;
procedure UpdateParas;
procedure ProofTotalLightAmount(bVerboseIfOK: LongBool);
procedure SetFormSize;
procedure SaveM3C;
procedure ShowTotalCalcTime;
procedure ConvertFromNewMCrecord;
procedure CalcBokeDiscOnBMP(var BokehBMP: TBitmap; nr: Integer);
procedure SetCPanelImages(C: TCategoryPanel; Checked: LongBool);
procedure UpdatePanel(C: TCategoryPanel);
procedure IniHeaderPointers;
protected
procedure WmThreadReady(var Msg: TMessage); message WM_ThreadReady;
public
{ Public-Deklarationen }
siLightMC: array of TMCrecord;
bUserChange: LongBool;
MCparas: TMandHeader10;
MCCalcStop: LongBool;
MCHeaderLightVals: TLightVals;
MCctstats: TCalcThreadStats;
MCHybridCustoms: array[0..5] of TCustomFormula;
MCHAddOn: THeaderCustomAddon;
OPDmc: TOpenPictureDialogM3D;
Authors: AuthorStrings;
end;
var
MCForm: TMCForm;
MCFormCreated: LongBool = False;
implementation
uses Mand, Math3D, PaintThread, ImageProcess, HeaderTrafos, Tiling, CommDlg,
PostProcessForm, DivUtils, CalcMonteCarlo, CustomFormulas, FileHandling,
LightAdjust, Maps, Math, ColorOptionForm;
{$R *.dfm}
procedure TMCForm.WmThreadReady(var Msg: TMessage);
begin
if Msg.LParam = 0 then
begin
Dec(MCActiveCalcThreads); //calc number from calcthreadstat
if (MCActiveCalcThreads < 1) and (Button2.Caption = 'Stop rendering') then
begin
Timer1.Interval := 5;
Timer1.Enabled := True;
end;
end;
end;
procedure TMCForm.MCTriggerRepaint;
begin
Inc(MCRepaintCounter);
Timer2.Enabled := True;
end;
procedure TMCForm.SaveDialog6TypeChange(Sender: TObject);
var S: String;
begin
case SaveDialog6.FilterIndex of
1: SaveDialog6.DefaultExt := 'bmp';
2: SaveDialog6.DefaultExt := 'png';
3: SaveDialog6.DefaultExt := 'jpg';
end;
S := SaveDialog6.Filename;
if SysUtils.DirectoryExists(S) then S := '';
if S <> '' then
case SaveDialog6.FilterIndex of
1: S := ChangeFileExt(S, '.bmp');
2: S := ChangeFileExt(S, '.png');
else
S := '';
end;
if S <> '' then
SendMessage(GetParent(SaveDialog6.Handle), CDM_SETCONTROLTEXT, $480, Longint(PChar(ExtractFileName(S))));
end;
function TMCForm.SizeOK: LongBool;
var w, h: Integer;
begin
w := MCparas.Width;
h := MCparas.Height;
Result := (w > 0) and (h > 0) and (w < 32768) and (h < 32768) and
(Length(siLightMC) = w * h);
end;
procedure TMCForm.SpeedButton1Click(Sender: TObject); //save bmp,png
var i, c: Integer;
s: String;
begin
Val(IniVal[16], i, c);
if c = 0 then SaveDialog6.FilterIndex := i + 1;
SaveDialog6.InitialDir := IniDirs[2];
SetDialogName(SaveDialog6, FName);
if SaveDialog6.Execute then
case SaveDialog6.FilterIndex of
1: SaveBMP(SaveDialog6.FileName, Image1.Picture.Bitmap, pf24bit);
2: SavePNG(SaveDialog6.FileName, Image1.Picture.Bitmap, False);
3: begin
s := '95';
if InputQuery('JPEG quality or size', 'Type in the quality (0..100) or the maximal output filesize in KB (>100):', s) then
SaveJPEGfromBMP(SaveDialog6.FileName, Image1.Picture.Bitmap, StrToIntTrim(s));
// c := StrToIntTrim(InputBox('JPEG quality or size', 'Type in the quality (0..100) or the maximal output filesize in KB (>100):', '95'));
// SaveJPEGfromBMP(SaveDialog6.FileName, Image1.Picture.Bitmap, c);
end;
end;
end;
procedure TMCForm.MCRepaint;
begin
if SizeOK then
begin
Inc(MCRepaintCounter);
PaintMC(@MCparas);
end;
end;
procedure TMCForm.TrackBar18KeyPress(Sender: TObject; var Key: Char);
begin
if Key = '1' then (Sender as TTrackBar).Position := (Sender as TTrackBar).SelStart;
end;
procedure TMCForm.Button1Click(Sender: TObject);
begin
Visible := False;
end;
procedure TMCForm.CalcBokeDiscOnBMP(var BokehBMP: TBitmap; nr: Integer);
var sx, sy, r, fx, fy, sm{, a, b, sml}: Single;
x, y, c, cy, yy, n{, i}: Integer;
p: PByteArray;
ps: PSingle;
// p1, p2: TPSPoint;
sa: array[0..30, 0..30] of Single;
begin
with BokehBMP.Canvas do
begin
Brush.Color := 0;
FillRect(ClipRect);
n := 70; //size for calculation of avrg sum, try to get lo as possible without quality loss
sm := 1 / n;
for x := 0 to 30 do for y := 0 to 30 do sa[x, y] := 0;
for y := -n to n do for x := -n to n do
begin
// if nr < 2 then
begin
if Sqr(x) + Sqr(y) >= n * n then Continue;
r := CalcBokeh(x * sm, y * sm, nr) * sm * 14;
sx := x * r + 15;
sy := y * r + 15;
{ sml := 1;
end
else
begin
if nr > 3 then i := 7 else i := 5;
sx := (x * sm * s05 + s05) * i;
sy := (Abs(y * sm * s05 + s05));
sml := sy;
i := Trunc(sx) mod i;
sx := FracSingle(sx);
if (nr and 1) = 0 then
begin
if sy < 0.94 then sy := sy * 1.03
else sy := sy * (0.9682 + (sy - 0.94) * s05);
end
else sy := sy * (1.5 - sy * s05);
if nr > 3 then
begin //septagon
p1 := @SinCosP7[i];
p2 := @SinCosP7[i + 1];
fx := 0.3;
end
else
begin //pentagon
p1 := @SinCosP5[i];
p2 := @SinCosP5[i + 1];
fx := 0.4;
end;
r := sx * (1 - sx) * fx; //addition to border, make it more roundy
fx := 0.96 + r;
if sy < fx then sy := sy * (1 + r) else sy := 0.96 + (sy - fx) * (r + 0.04);
a := sx * sy;
b := (1 - sx) * sy;
sx := a * p1[0] + b * p2[0];
sy := a * p1[1] + b * p2[1];
sx := sx * 14 + 15;
sy := sy * 14 + 15; }
end;
c := Trunc(sx);
cy := Trunc(sy);
fx := sx - c;
fy := sy - cy;
ps := @sa[cy, c];
ps^ := ps^ + (1 - fx) * (1 - fy); // * sml
Inc(ps);
ps^ := ps^ + fx * (1 - fy);
Inc(ps, 30);
ps^ := ps^ + (1 - fx) * fy;
Inc(ps);
ps^ := ps^ + fx * fy;
end;
sm := sm * sm * 21000;
// if nr > 1 then sm := sm * 1.4;
for x := 0 to 30 do
begin
p := BokehBMP.ScanLine[x + 1];
for y := 0 to 30 do
begin
yy := (y + 1) * 4;
c := Round(sa[y, x] * sm);
p[yy] := c;
p[yy + 1] := c;
p[yy + 2] := c;
end;
end;
end;
end;
procedure TMCForm.CategoryPanel2Collapse(Sender: TObject);
begin //set categoryGroupheight
CategoryPanelGroup2.Height := CategoryPanel1.Height + CategoryPanel2.Height
+ CategoryPanel5.Height + CategoryPanel6.Height;
Label33.Top := CategoryPanelGroup2.Height + 8;
end;
procedure TMCForm.CategoryPanel2Expand(Sender: TObject);
begin //Collapse every other panel
if Sender <> CategoryPanel1 then CategoryPanel1.Collapsed := True;
if Sender <> CategoryPanel2 then CategoryPanel2.Collapsed := True;
if Sender <> CategoryPanel5 then CategoryPanel5.Collapsed := True;
if Sender <> CategoryPanel6 then CategoryPanel6.Collapsed := True;
CategoryPanel2Collapse(Sender);
end;
procedure TMCForm.IniHeaderPointers;
var i: Integer;
begin
MCparas.PCFAddon := @MCHAddOn;
for i := 0 to 5 do MCparas.PHCustomF[i] := @MCHybridCustoms[i];
end;
procedure TMCForm.FormCreate(Sender: TObject);
var i: Integer;
begin
OPDmc := TOpenPictureDialogM3D.Create(Self);
OPDmc.Filter := 'M3D monte carlo file (*.m3c)|*.m3c';
OPDmc.DefaultExt := 'm3c';
MCFormCreated := True;
bUserChange := True;
FirstOpenM3C := True;
MCActiveCalcThreads := 0;
iUpdate := 0;
Image1.Picture.Bitmap.PixelFormat := pf32bit;
Image1.Picture.Bitmap.SetSize(250, 30);
IniHeaderPointers;
MCVMapcalculated := False;
for i := 0 to 5 do IniCustomF(@MCHybridCustoms[i]);
for i := 0 to 5 do
begin
BokehBMP[i] := TBitmap.Create;
BokehBMP[i].PixelFormat := pf32Bit;
BokehBMP[i].SetSize(33, 33);
CalcBokeDiscOnBMP(BokehBMP[i], i);
end;
Image2.Picture.Bitmap.Assign(BokehBMP[0]);
ComboBox1.Hint := 'Box: sharp, good AA' + #13#10 +
'Gauss: bit blurry, very good AA';
end;
procedure TMCForm.FormDestroy(Sender: TObject);
begin
OPDmc.Free;
end;
procedure TMCForm.FormHide(Sender: TObject);
begin
if Button2.Caption = 'Stop rendering' then
if MessageDlg('Should i stop calculations?', mtWarning, [mbYes, mbNo], 0) = mrYes then
Button2.Click;
end;
procedure TMCForm.FormShow(Sender: TObject);
begin
PreComputeHaltonSequence;
end;
procedure TMCForm.TrackBar1Change(Sender: TObject); //contrast
begin
if bUserChange then MCTriggerRepaint;
end;
procedure TMCForm.Timer1Timer(Sender: TObject); //trigger new calc
var i, j, n, ysub: Integer;
c: Cardinal;
xx, yy, ymax: Double;
begin
Timer1.Interval := 400;
i := 0;
n := 0;
yy := 0;
ymax := MCparas.Height / MCctstats.iTotalThreadCount;
xx := 1 / MCparas.Width;
for j := 1 to MCctstats.iTotalThreadCount do
with MCctstats.CTrecords[j] do
begin
i := i + iActualYpos;
if isActive <> 0 then
begin
Inc(n);
yy := yy + MinCD(ymax, Max(0, iActualYpos - j + 1) / MCctstats.iTotalThreadCount +
iActualXpos * xx);
end
else yy := yy + ymax;
end;
if bGotZeroCounts then ysub := 0 else ysub := MCctstats.ctCalcRect.Top;
c := GetTickCount;
if n > 0 then
begin
Label14.Caption := dDateTimeToStr((c - MCctstats.cCalcTime) / MSecsPerDay);
ProgressBar1.Position := i div MCctstats.iTotalThreadCount;
if c - LastUpdate > 3000 then
begin
xx := (c - MCctstats.cCalcTime) * (ymax * MCctstats.iTotalThreadCount - yy) /
(MSecsPerDay * MaxCD(0.1, yy - ysub)) * (MCctstats.iTotalThreadCount / Max(n, 1));
Label15.Caption := 'togo: ' + dDateTimeToStr(xx);
Inc(iUpdate);
if bGotZeroCounts or (iUpdate = 3) then MCTriggerRepaint;
if iUpdate > 2 then iUpdate := 0;
LastUpdate := c;
end;
Exit;
end;
Timer1.Enabled := False;
MCTriggerRepaint;
if Button2.Caption = 'Stop rendering' then
begin
if bGotZeroCounts then MCparas.iCalcTime := (c - MCctstats.cCalcTime) div 100
else Inc(MCparas.iCalcTime, (c - MCctstats.cCalcTime) div 100);
if CheckBox5.Enabled and CheckBox5.Checked and ((c - LastSaved) > 300000) then
begin
SaveM3C;
LastSaved := GetTickCount;
end;
MCparas.MClastY := 0;
StartCalc;
Timer1.Enabled := True;
end
else
begin
ProgressBar1.Visible := False;
Label8.Visible := False;
end;
end;
procedure TMCForm.Timer2Timer(Sender: TObject);
begin
if MCThreadActive = 0 then
begin
Timer2.Enabled := False;
UpdateParas;
MCRepaint;
end;
end;
procedure TMCForm.Timer3Timer(Sender: TObject);
begin
Label33.Top := CategoryPanelGroup2.Height + 8;
if Timer3.Tag > 0 then
begin
Timer3.Tag := Timer3.Tag - 1;
if (Timer3.Tag and 1) = 0 then Label33.Font.Color := clWindowText
else Label33.Font.Color := clRed;
end
else
begin
Label33.Font.Color := clWindowText;
Timer3.Enabled := False;
end;
end;
procedure TMCForm.SetParas;
begin
bUserChange := False;
TrackBar1.Position := MCparas.MCcontrast;
TrackBar4.Position := MCparas.bMCSaturation and $7F;
TrackBar3.Position := (MCparas.Light.TBoptions shr 23) and $3F;
Edit3.Text := ShortFloatToStr(MCparas.MCSoftShadowRadius);
Edit2.Text := D2ByteToStr(MCparas.MCdiffReflects);
UpDown1.Position := MCparas.SRreflectioncount;
UpDown3.Position := MCparas.MCDepth - 1;
Button2.Enabled := True;
Button8.Enabled := True;
Button4.Enabled := Mand3DForm.Button2.Caption <> 'Stop';
CheckBox8.Checked := (MCparas.MCoptions and 1) <> 0;
CheckBox2.Checked := (MCparas.bCalcSRautomatic and 1) <> 0;
CheckBox3.Checked := (MCparas.bCalcSRautomatic and 2) <> 0;
CheckBox4.Checked := (MCparas.bCalcDOFtype and 1) <> 0;
CheckBox6.Checked := (MCparas.MCoptions and 2) <> 0;
CheckBox7.Checked := (MCparas.MCoptions and 4) <> 0;
ComboBox1.ItemIndex := (MCparas.MCoptions shr 3) and 1;
Label22.Caption := IntToStr(Min(6, ((MCparas.MCoptions shr 4) and 7) + 1));
Image2.Picture.Bitmap.Assign(BokehBMP[StrToInt(Label22.Caption) - 1]);
bUserChange := True;
Label14.Caption := '';
Label15.Caption := '';
ShowTotalCalcTime;
end;
procedure TMCForm.UpdateParas;
var d: Double;
begin
MCparas.bCalcSRautomatic := (MCparas.bCalcSRautomatic and 12) or
(Ord(CheckBox2.Checked) and 1) or ((Ord(CheckBox3.Checked) shl 1) and 2);
MCparas.bCalcDOFtype := (MCparas.bCalcDOFtype and $FE) or (Ord(CheckBox4.Checked) and 1);
MCparas.bSliceCalc := 0;
MCparas.bCalc3D := 1;
if not StrToFloatKtry(Edit3.Text, d) then d := 1;
MCparas.MCSoftShadowRadius := SingleToShortFloat(d);
MCparas.MCDepth := UpDown3.Position + 1;
MCparas.SRreflectioncount := UpDown1.Position;
MCparas.bMCSaturation := TrackBar4.Position;
MCparas.MCoptions := (Ord(CheckBox8.Checked) and 1) or ((Ord(CheckBox6.Checked) and 1) shl 1)
or ((Ord(CheckBox7.Checked) and 1) shl 2) or ((ComboBox1.ItemIndex and 1) shl 3)
or (Max(0, StrToInt(Label22.Caption) - 1) shl 4);
MCparas.MCcontrast := TrackBar1.Position; //exposure
MCparas.MCdiffReflects := StrToD2Byte(Edit2.Text);
MCparas.Light.TBoptions := (MCparas.Light.TBoptions and $E07FFFFF) or (TrackBar3.Position shl 23); //gamma/L
end;
procedure TMCForm.UpDown2Click(Sender: TObject; Button: TUDBtnType);
var i: Integer;
begin
i := StrToInt(Label22.Caption);
if Button = btNext then Inc(i) else
if Button = btPrev then Dec(i);
i := Min(5, Max(0, i - 1));
MCparas.MCoptions := (MCparas.MCoptions and $3F) or (i shl 4);
Label22.Caption := IntToStr(i + 1);
Image2.Picture.Bitmap.Assign(BokehBMP[i]);
end;
procedure ModLight(Light: TPLightingParas9; Paras: TPMandHeader10; iOption: Integer);
var i, i2, ii: Integer;
c: Cardinal;
bIsSqr, bCalcT: LongBool;
smax, SpecMul, sd: Single;
sv1, sv2: TSVec;
begin
with Light^ do
begin
bIsSqr := (AdditionalOptions and 1) <> 0;
bCalcT := (Paras.bCalcSRautomatic and 3) = 3;
SpecMul := MaxCS(0.001, Paras.SRamount);
for i := 0 to 9 do for ii := 0 to 1 do if (i < 4) or (ii = 0) then //for inside? spec = dif[3]
begin
if ii = 1 then
begin
c := ICols[i].Color;
sv1 := ColToSVec(c, bIsSqr);
c := c shr 24;
sv2 := ColToSVec(c or (c shl 8) or (c shl 16), bIsSqr);
end else begin
sv1 := ColToSVec(LCols[i].ColorDif, bIsSqr);
sv2 := ColToSVec(LCols[i].ColorSpe, bIsSqr);
end;
if bCalcT then sd := MaxCS(0.001, 1 - (LCols[i].ColorSpe shr 24) * s1d255 * SpecMul) else sd := 1;
case iOption of
1: begin
smax := 1;
for i2 := 0 to 2 do if sv1[i2] * sd + sv2[i2] * SpecMul > 1 then
smax := MinMaxCS(0, (1.001 - sv2[i2] * SpecMul) / (sv1[i2] * sd), smax);
if smax < 1 then ScaleSVectorV(@sv1, smax);
end;
2: begin
smax := MaxCS(MaxCS(sv1[0] * sd + sv2[0] * SpecMul,
sv1[1] * sd + sv2[1] * SpecMul),
sv1[2] * sd + sv2[2] * SpecMul);
if smax > 1 then
begin
smax := 1 / smax;
ScaleSVectorV(@sv1, smax);
ScaleSVectorV(@sv2, smax);
end;
end;
3: begin
smax := 1;
for i2 := 0 to 2 do if sv1[i2] * sd + sv2[i2] * SpecMul > 1 then
smax := MinMaxCS(0, (1.001 - sv1[i2] * sd) / (sv2[i2] * SpecMul), smax);
if smax < 1 then ScaleSVectorV(@sv2, smax);
end;
end;
if bIsSqr then
begin
sv1 := mSqrtSVec(sv1);
sv2 := mSqrtSVec(sv2);
end;
if ii = 1 then
begin
ICols[i].Color := (SVecToColNoScale(ScaleSvector(sv1, s255)) and $FFFFFF) or
(SVecToColNoScale(ScaleSvector(sv2, s255)) shl 24);
end else begin
LCols[i].ColorDif := (LCols[i].ColorDif and $FF000000) or
(SVecToColNoScale(ScaleSvector(sv1, s255)) and $FFFFFF);
LCols[i].ColorSpe := (LCols[i].ColorSpe and $FF000000) or
(SVecToColNoScale(ScaleSvector(sv2, s255)) and $FFFFFF);
end;
end;
end;
end;
procedure TMCForm.ProofTotalLightAmount(bVerboseIfOK: LongBool);
var i, iOption: Integer;
bIsSqr, bToMuch, bCalcT: LongBool;
SpecMul, sd: Single;
sv1, sv2: TSVec;
tmpLight: TLightingParas9;
function ProofCols(const LCol: TLCol8): LongBool;
begin
Result := False; //if calcTransp + Transp > 0 then downscale diffuse
if bCalcT then sd := MaxCS(0.001, 1 - (LCol.ColorSpe shr 24) * s1d255 * SpecMul) else sd := 1;
if (sv1[0] * sd + sv2[0] * SpecMul) > 1.005 then Result := True;
if (sv1[1] * sd + sv2[1] * SpecMul) > 1.005 then Result := True;
if (sv1[2] * sd + sv2[2] * SpecMul) > 1.005 then Result := True;
end;
begin
if (MCparas.bCalcSRautomatic and 1) <> 0 then
with MCparas.Light do
begin
bToMuch := False;
if GetDiffMapNr(@MCparas.Light) <> 0 then
begin
if not CheckBox7.Checked then
begin
ShowMessage('When using a map for the diffuse color,' + #13#10 +
'you have to check ''Autoclip Spec+Diff''' + #13#10 +
'for an automatic clipping of colors.');
CheckBox7.Checked := True;
end;
end
else
begin
bIsSqr := (AdditionalOptions and 1) <> 0;
bCalcT := (MCparas.bCalcSRautomatic and 3) = 3;
SpecMul := MCparas.SRamount;
if (MCparas.bCalcSRautomatic and 1) <> 0 then
for i := 0 to 9 do
begin
sv1 := ColToSVec(LCols[i].ColorDif, bIsSqr);
sv2 := ColToSVec(LCols[i].ColorSpe, bIsSqr);
bToMuch := ProofCols(LCols[i]);
if bToMuch then Break;
end;
end;
if bVerboseIfOK and not bToMuch then ShowMessage('All colors are ok.');
if bToMuch and (MessageDlg('The amount of added diffuse and specular colors are bigger 1,' + #13#10 +
'should i downscale them for a more realistic coloring?', mtWarning, [mbYes, mbNo], 0) = mrYes) then
begin
for iOption := 1 to 3 do
begin
tmpLight := MCparas.Light;
ModLight(@tmpLight, @MCparas, iOption);
PaintSDPreviewColors(@tmpLight, (FColorOptions.FindComponent('Image' + IntToStr(iOption * 2 - 1)) as TImage).Canvas,
(FColorOptions.FindComponent('Image' + IntToStr(iOption * 2)) as TImage).Canvas, 60);
end;
iOption := FColorOptions.ShowModal;
if not (iOption in [1..3]) then Exit;
ModLight(@MCparas.Light, @MCparas, iOption);
end;
end;
end;
procedure TMCForm.SetFormSize;
begin
ClientWidth := Panel3.Width + MCparas.Width;
ClientHeight := Panel1.Height + Panel2.Height + MCparas.Height;
end;
procedure TMCForm.Button3Click(Sender: TObject); //import paras
begin
if FirstOpenM3C then
begin
OPDmc.InitialDir := IniDirs[11];
SaveDialog3.InitialDir := IniDirs[11];
FirstOpenM3C := False;
end;
Mand3DForm.MakeHeader;
IniHeaderPointers;
AssignHeader(@MCparas, @Mand3DForm.MHeader);
IniCFsFromHAddon(MCparas.PCFAddon, MCparas.PHCustomF); //new
Authors := Mand3DForm.Authors;
MCparas.TilingOptions := 0; //used as OldAvrgSqrNoise
MCparas.iReflectsCalcTime := 0; //used as OldAvrgRayCount
MCparas.bSSAO24BorderMirrorSize := 0; //indicator if oldvals are stored
MCparas.iCalcTime := 0;
MCparas.MClastY := 0;
MCparas.sDOFZsharp := (MCparas.sDOFZsharp + MCparas.sDOFZsharp2) * s05;
MCparas.sDOFZsharp2 := MCparas.sDOFZsharp;
ProofTotalLightAmount(False);
MakeLightValsFromHeaderLight(@MCparas, @MCHeaderLightVals, 1, MCparas.bStereoMode);
SetParas;
FName := ChangeFileExtSave(Mand3DForm.Caption, '');
if Copy(FName, 1, 13) = 'Mandelbulb 3D' then FName := 'main params';
Caption := FName;
SetLength(siLightMC, 0);
FitImageSize;
Image1.Picture.Bitmap.Canvas.FillRect(Image1.Picture.Bitmap.Canvas.ClipRect);
Image1.Picture.Bitmap.Canvas.Font.Color := $C000;
Image1.Picture.Bitmap.Canvas.TextOut(8, 8, 'Press ''Start rendering'' to calculate the image');
SetFormSize;
SpeedButton1.Enabled := False;
CheckBox5.Enabled := False;
Label7avrgrays.Caption := '';
Label2avrgnoise.Caption := '';
Label12.Caption := '';
Label13.Caption := '';
Label19.Caption := '';
Label33.Caption := '';
MCVMapcalculated := False;
end;
procedure TMCForm.Button4Click(Sender: TObject); //send paras to main
var x, n: Integer;
begin
UpdateParas;
MCparas.bCalculateHardShadow := 3;
n := 0;
for x := 0 to 5 do if (MCparas.Light.Lights[x].Loption and 3) = 0 then
begin
MCparas.bCalculateHardShadow := MCparas.bCalculateHardShadow or (4 shl x);
MCparas.Light.Lights[x].LFunction := MCparas.Light.Lights[x].LFunction and $CF; //cos
Inc(n);
end;
if n = 1 then MCparas.bCalc1HSsoft := 1 else MCparas.bCalc1HSsoft := 0;
if n = 0 then MCparas.bCalculateHardShadow := 2;
MCparas.AODEdithering := 0;
MCparas.bCalcAmbShadowAutomatic := 253;
MCparas.iOptions := (2 shl 6) + 1;
MCparas.Light.Lights[3].AdditionalByteEx := 0; //HS diffuse shadow amount
MCparas.Light.TBpos[11] := (MCparas.Light.TBpos[11] and $FFFF0000) or 53 or (27 shl 8); //AmbShadow + 2ndreflect amount
MCparas.Light.Lights[2].FreeByte := MCparas.Light.Lights[2].FreeByte and $FE; //turn of 2nd mode
MCparas.Light.AdditionalOptions := MCparas.Light.AdditionalOptions or 32; //use small BGpic for amb
IniHeaderPointers;
AssignHeader(@Mand3DForm.MHeader, @MCparas);
Mand3DForm.MHeader.MClastY := 0;
Mand3DForm.MHeader.TilingOptions := 0;
Mand3DForm.MHeader.iReflectsCalcTime := 0;
Mand3DForm.MHeader.bSSAO24BorderMirrorSize := 0;
Mand3DForm.Authors := Authors;
IniCFsFromHAddon(Mand3DForm.MHeader.PCFAddon, Mand3DForm.MHeader.PHCustomF);
Mand3DForm.SetEditsFromHeader;
LightAdjustForm.SetLightFromHeader(Mand3DForm.MHeader);
Mand3DForm.Caption := FName;
SetSaveDialogNames(FName);
Mand3DForm.ParasChanged;
SetFocus;
end;
procedure TMCForm.Button5Click(Sender: TObject);
begin
UpdateParas;
ProofTotalLightAmount(True);
end;
procedure TMCForm.Button6Click(Sender: TObject); // noise filter
var x, y, wid, l, SLstart, SLoffset, sop: Integer;
s1, s2: Single;
sv, sv2: TSVec;
pmc, pmc2, pmc3, pmc4, pmc5: TPMCrecord;
begin
wid := MCparas.Width;
// l := l - 2 * wid - 2;
sop := SizeOf(TMCrecord);
pmc := @siLightMC[wid + 1];
pmc2 := TPMCrecord(Integer(pmc) - sop);
pmc3 := TPMCrecord(Integer(pmc) + sop);
pmc4 := TPMCrecord(Integer(pmc) - sop * wid);
pmc5 := TPMCrecord(Integer(pmc) + sop * wid);
SLstart := Integer(Image1.Picture.Bitmap.ScanLine[0]);
SLoffset := Integer(Image1.Picture.Bitmap.ScanLine[1]) - SLstart;
for y := 1 to MCparas.Height - 2 do
begin
l := SLstart + y * SLoffset + 4;
for x := 1 to wid - 2 do
begin
sv := ColToSVecFlipRBNoScale(PCardinal(l)^, False);
s1 := 1;
sv2 := ColToSVecFlipRBNoScale(PCardinal(l - SLoffset)^, False);
s2 := 0.25;
AddSVecWeight(@sv, @sv2, s2);
s1 := s1 + s2;
sv2 := ColToSVecFlipRBNoScale(PCardinal(l - 4)^, False);
s2 := 0.25;
AddSVecWeight(@sv, @sv2, s2);
s1 := s1 + s2;
sv2 := ColToSVecFlipRBNoScale(PCardinal(l + 4)^, False);
s2 := 0.25;
AddSVecWeight(@sv, @sv2, s2);
s1 := s1 + s2;
sv2 := ColToSVecFlipRBNoScale(PCardinal(l + SLoffset)^, False);
s2 := 0.25;
AddSVecWeight(@sv, @sv2, s2);
s1 := s1 + s2;
ScaleSVectorV(@sv, 1 / s1);
PCardinal(l)^ := SVecToCol(sv);
{ dT1 := MCRGBtoDouble(@pmc.Ysum);
dtmp := 0; //todo: Ysum avrg of all 5 pixles to subtract...
if pmc2.RayCount <> 0 then dTmp := dTmp + Sqr(dT1 - MCRGBtoDouble(@pmc2.Ysum));
if pmc3.RayCount <> 0 then dTmp := dTmp + Sqr(dT1 - MCRGBtoDouble(@pmc3.Ysum));
if pmc4.RayCount <> 0 then dTmp := dTmp + Sqr(dT1 - MCRGBtoDouble(@pmc4.Ysum));
if pmc5.RayCount <> 0 then dTmp := dTmp + Sqr(dT1 - MCRGBtoDouble(@pmc5.Ysum));
dtmp := Clamp0D(dtmp * 0.25 - sigma);
dT1 := Clamp0D((MCRGBtoDouble(@pmc.Ysqr) - Sqr(dT1)) / (MaxCD(0.1, dT1) * pmc.RayCount) - dtmp);
}
Inc(pmc);
Inc(pmc2);
Inc(pmc3);
Inc(pmc4);
Inc(pmc5);
Inc(l, 4);
end;
end;
Image1.Picture.Bitmap.Modified := True;
end;
procedure TMCForm.SaveM3C;
var f: file;
begin
Screen.Cursor := crHourGlass;
Application.ProcessMessages;
try
AssignFile(f, ChangeFileExtSave(SaveFName, '.m3c'));
Rewrite(f, 1);
InsertAuthorsToPara(@MCparas, Authors);
BlockWrite(f, MCparas, SizeOf(MCparas));
IniHeaderPointers;
MCHAddon.bHCAversion := 16;
BlockWrite(f, MCHAddon, SizeOf(THeaderCustomAddon));
BlockWrite(f, siLightMC[0], SizeOf(TMCrecord) * Length(siLightMC));
CloseFile(f);
finally
Screen.Cursor := crDefault;
end;
end;
procedure TMCForm.Button8Click(Sender: TObject); //save mc
begin
SetDialogName(SaveDialog3, FName);
if SaveDialog3.Execute then
begin
SaveFName := SaveDialog3.FileName;
FName := ChangeFileExtSave(ExtractFileName(SaveFName), '');
SaveM3C;
Caption := FName;
CheckBox5.Enabled := True;
end;
end;
procedure TMCForm.ConvertFromNewMCrecord;
var i: Integer;
mcrN: TMCrecordNew;
begin
for i := 0 to Length(siLightMC) - 1 do
begin
FastMove(siLightMC[i], mcrN, 18);
ConvertNewMCRtoOld(@siLightMC[i], @mcrN);
end;
end;
procedure TMCForm.Button9Click(Sender: TObject); //open mc
var f: file;
begin
MCCalcStop := True;
Inc(MCRepaintCounter);
if FirstOpenM3C then
begin
OPDmc.InitialDir := IniDirs[11];
SaveDialog3.InitialDir := IniDirs[11];
FirstOpenM3C := False;
end;
if OPDmc.Execute then
begin
SetDialogName(SaveDialog3, OPDmc.FileName);
SaveFName := OPDmc.FileName;
CheckBox5.Enabled := True;
Screen.Cursor := crHourGlass;
Application.ProcessMessages;
try
AssignFile(f, ChangeFileExt(OPDmc.FileName, '.m3c'));
Reset(f, 1);
BlockRead(f, MCparas, SizeOf(MCparas));
BlockRead(f, MCHAddon, SizeOf(THeaderCustomAddon));
SetLength(siLightMC, MCparas.Width * MCparas.Height);
BlockRead(f, siLightMC[0], SizeOf(TMCrecord) * Length(siLightMC));
CloseFile(f);
finally
Screen.Cursor := crDefault;
Authors := ExtractAuthorsFromPara(@MCparas);
IniHeaderPointers;
end;
FitImageSize;
Label33.Caption := 'Parameters version: ' + ProgramVersionStr(MCparas.sM3dVersion);
if Abs(M3dVersion - MCparas.sM3dVersion) > 0.0005 then
begin
Label33.Caption := Label33.Caption + #13#10 +
'Version of this program: ' + ProgramVersionStr(M3dVersion) + #13#10 +
'You should use the same program version to avoid' + #13#10 +
'color or other changes in further calculations!';
Timer3.Tag := 8;
Timer3.Enabled := True;
end;
if MCparas.MandId < 43 then MCparas.bSSAO24BorderMirrorSize := 0;
UpdateFormulaOptionAbove20(MCparas);
if (MCparas.MCoptions and 128) <> 0 then ConvertFromNewMCrecord;
Mand3DForm.bOutMessageLoud := True;
try
LoadBackgroundPicT(@MCparas.Light);
finally
Mand3DForm.bOutMessageLoud := False;
end;
if (MCparas.Light.AdditionalOptions and 32) <> 0 then
MakeSmallLMimage(@M3DSmallBGpic, @M3DBackGroundPic);
MakeLightValsFromHeaderLight(@MCparas, @MCHeaderLightVals, 1, MCparas.bStereoMode);
SetParas;
Button2.Caption := 'Start rendering';
MCTriggerRepaint;
CalcAvrgNoise;
SpeedButton1.Enabled := True;
FName := ChangeFileExtSave(ExtractFileName(OPDmc.FileName), '');
Caption := FName;
SetFormSize;
IniCFsFromHAddon(MCparas.PCFAddon, MCparas.PHCustomF);
MCVMapcalculated := False;
end;
end;
procedure TMCForm.FitImageSize;
begin
if (Image1.Picture.Bitmap.Width <> MCparas.Width) or
(Image1.Picture.Bitmap.Height <> MCparas.Height) then
begin
Image1.Picture.Bitmap.SetSize(MCparas.Width, MCparas.Height);
Image1.SetBounds(Image1.Left, Image1.Top, MCparas.Width, MCparas.Height);
end;
end;
procedure TMCForm.CalcAvrgNoise;
var i, l, maxRCount{, wid, sop}: Integer;
pmc{, pmc2, pmc3, pmc4, pmc5}: TPMCrecord;
avgNoise, avgSqrNoise, dT1, maxNoise{, sigma, dtmp}: Double;
begin
l := Length(siLightMC);
if l = 0 then Exit;
// sop := SizeOf(TMCrecord);
maxRCount := 0;
avgSqrNoise := 0;
AvrgRCount := 0;
maxNoise := 0;
bGotZeroCounts := False;
pmc := @siLightMC[0];
for i := 1 to l do
begin
if pmc.RayCount = 0 then bGotZeroCounts := True else
begin
AvrgRCount := AvrgRCount + pmc.RayCount;