-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.pas
1756 lines (1542 loc) · 46.2 KB
/
main.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
//------------------------------------------------------------------------------
//
// DD_MODEL: DelphiDOOM Procedural Model Editor
// Copyright (C) 2017-2021 by Jim Valavanis
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// DESCRIPTION:
// Main Form
//
//------------------------------------------------------------------------------
// E-Mail: jimmyvalavanis@yahoo.gr
// Site : https://sourceforge.net/projects/delphidoom-procedural-modeler/
//------------------------------------------------------------------------------
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ComCtrls, Buttons, Clipbrd, ExtDlgs, pngimage, xTGA, jpeg,
zBitmap, Menus, ImgList, StdCtrls, dglOpenGL;
type
TForm1 = class(TForm)
StatusBar1: TStatusBar;
SavePictureDialog1: TSavePictureDialog;
MainMenu1: TMainMenu;
File1: TMenuItem;
OpenTexture1: TMenuItem;
SaveAs3D: TMenuItem;
N1: TMenuItem;
Exit1: TMenuItem;
Model1: TMenuItem;
CopyTexture1: TMenuItem;
Copy3d: TMenuItem;
N2: TMenuItem;
PasteTexture1: TMenuItem;
Help1: TMenuItem;
About1: TMenuItem;
OpenPictureDialog1: TOpenPictureDialog;
Timer1: TTimer;
TexturePopupMenu: TPopupMenu;
PreviewPopupMenu: TPopupMenu;
Copy2: TMenuItem;
Save1: TMenuItem;
Open2: TMenuItem;
Copy3: TMenuItem;
Paste2: TMenuItem;
Save2: TMenuItem;
exture1: TMenuItem;
Pewview1: TMenuItem;
New1: TMenuItem;
SaveAs2: TMenuItem;
N3: TMenuItem;
Compile1: TMenuItem;
SaveDialog1: TSaveDialog;
Open1: TMenuItem;
OpenDialog1: TOpenDialog;
FileMenuHistoryItem0: TMenuItem;
FileMenuHistoryItem1: TMenuItem;
FileMenuHistoryItem2: TMenuItem;
FileMenuHistoryItem3: TMenuItem;
FileMenuHistoryItem4: TMenuItem;
FileMenuHistoryItem5: TMenuItem;
FileMenuHistoryItem6: TMenuItem;
FileMenuHistoryItem7: TMenuItem;
FileMenuHistoryItem8: TMenuItem;
FileMenuHistoryItem9: TMenuItem;
N4: TMenuItem;
EditorPopupMenu: TPopupMenu;
New2: TMenuItem;
Save3: TMenuItem;
SaveAs1: TMenuItem;
Open3: TMenuItem;
Options1: TMenuItem;
N5: TMenuItem;
SaveAsTexture1: TMenuItem;
N6: TMenuItem;
SaveAs3: TMenuItem;
N7: TMenuItem;
Mask1: TMenuItem;
MaskSaveAs4: TMenuItem;
MaskCopy1: TMenuItem;
N8: TMenuItem;
MaskPopupMenu: TPopupMenu;
MaskSaveAs: TMenuItem;
MaskCopy: TMenuItem;
StartUpTimer: TTimer;
MainToolbarPanel: TPanel;
ExitButton1: TSpeedButton;
AboutButton1: TSpeedButton;
NewButton1: TSpeedButton;
OptionsButton1: TSpeedButton;
Window1: TMenuItem;
TileHorizontal1: TMenuItem;
Cascade1: TMenuItem;
TileVertical1: TMenuItem;
ArrangeIcons1: TMenuItem;
N9: TMenuItem;
ShowEditorWindow1: TMenuItem;
ShowTextureWindow1: TMenuItem;
ShowPreviewWindow1: TMenuItem;
ShowMaskWindow1: TMenuItem;
SaveDialog2: TSaveDialog;
Export1: TMenuItem;
OpenDialog2: TOpenDialog;
Import1: TMenuItem;
PK3Button1: TSpeedButton;
SavePK3Dialog: TSaveDialog;
procedure FormCreate(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
procedure OpenGLPanelResize(Sender: TObject);
procedure TexturePasteClick(Sender: TObject);
procedure Copy3dButtonClick(Sender: TObject);
procedure Save3dButtonClick(Sender: TObject);
procedure About1Click(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure TextureCopyClick(Sender: TObject);
procedure TextureOpenClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Model1Click(Sender: TObject);
procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure New1Click(Sender: TObject);
procedure CodeEditorChange(Sender: TObject);
procedure EditorSaveClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure OpenGLPanelMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure OpenGLPanelMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure OpenGLPanelMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure OpenGLPanelDblClick(Sender: TObject);
procedure FormMouseWheelDown(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
procedure FormMouseWheelUp(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure PaintBox1Responder(const X, Y: Integer);
procedure PaintBox2Responder(const X, Y: Integer);
procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure EditorSaveAsClick(Sender: TObject);
procedure EditorCompileClick(Sender: TObject);
procedure EditorOpenClick(Sender: TObject);
procedure File1Click(Sender: TObject);
procedure Options1Click(Sender: TObject);
procedure PaintBoxViewFrontPaint(Sender: TObject);
procedure PaintBoxViewTopPaint(Sender: TObject);
procedure PaintBoxViewMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure PaintBoxViewMouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure PaintBoxViewMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
procedure SaveTexture1Click(Sender: TObject);
procedure MaskCopyClick(Sender: TObject);
procedure MaskSaveAsClick(Sender: TObject);
procedure PaintBox2Paint(Sender: TObject);
procedure PaintBox2MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure PaintBox2MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure PaintBox2MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure StartUpTimerTimer(Sender: TObject);
procedure TileHorizontal1Click(Sender: TObject);
procedure TileVertical1Click(Sender: TObject);
procedure Cascade1Click(Sender: TObject);
procedure ArrangeIcons1Click(Sender: TObject);
procedure ShowEditorWindow1Click(Sender: TObject);
procedure ShowPreviewWindow1Click(Sender: TObject);
procedure ShowTextureWindow1Click(Sender: TObject);
procedure ShowMaskWindow1Click(Sender: TObject);
procedure MainToolbarPanelResize(Sender: TObject);
procedure Window1Click(Sender: TObject);
procedure Export1Click(Sender: TObject);
procedure Import1Click(Sender: TObject);
procedure PK3Button1Click(Sender: TObject);
private
{ Private declarations }
devparm: boolean;
procedure Idle(Sender: TObject; var Done: Boolean);
procedure Hint(Sender: TObject);
procedure DoRenderGL2D;
procedure DoRenderGL3D;
procedure CreateGLTexture;
procedure UpdateEnable;
procedure Get3dPreviewBitmap(const b: TBitmap);
procedure GetMaskTextureBitmap(const b: TBitmap);
procedure InvalidatePaintBox;
procedure TestImage;
procedure CreateDrawBuffer;
function CheckCanClose: boolean;
procedure DoNewModel;
procedure UpdateStatusBar;
procedure ClearOutput;
procedure Compile;
procedure UpdateFrameListBox;
procedure GLCalcFrame(const frm: integer);
procedure DoSaveCodeEditor;
procedure SetFileName(const fname: string);
function DoOpenCodeEditor(const fname: string): boolean;
function DoOpenDMXFile(const fname: string): boolean;
procedure OnLoadCodeEditorFileMenuHistory(Sender: TObject; const fname: string);
procedure EraseStigma2d(const pb: TPaintBox; const buf: TBitmap);
procedure PaintStigma2d(const pb: TPaintBox);
procedure ResetStigma3d;
procedure UpdateStigma3d;
procedure PaintStigma3d(const pb: TPaintBox);
procedure PaintBoxViewResponder(Sender: TObject; const X, Y: integer);
procedure CreateMaskBuffer;
procedure StartUp;
public
{ Public declarations }
procedure SetCurrentFrame(const frm: integer);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
mdl_filemenuhistory,
optionsfrm,
SynUnicode,
mdl_globals,
mdl_utils,
mdl_gl,
mdl_defs,
mdl_model,
mdl_script_functions,
mdl_pk3writer,
frm_preview,
frm_texture,
frm_editor,
frm_mask;
function CheckParam(const parm: string): integer;
var
i: integer;
uParm: string;
begin
uParm := UpperCase(parm);
for i := 1 to ParamCount do
if UpperCase(ParamStr(i)) = uParm then
begin
Result := i;
Exit;
end;
Result := -1;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Scaled := False;
globals.mdl := TDDModelLoader.Create;
devparm := CheckParam('-devparm') > 0;
PK3Button1.Visible := devparm;
if not PK3Button1.Visible then
AboutButton1.Left := PK3Button1.Left;
DecimalSeparator := '.';
globals.EditorFormCreated := False;
globals.TextureFormCreated := False;
globals.MaskFormCreated := False;
globals.PreviewFormCreated := False;
globals.Initialized := False;
globals.hintcnt := 0;
globals.filemenuhistory := TFileMenuHistory.Create(self);
globals.filemenuhistory.MenuItem0 := FileMenuHistoryItem0;
globals.filemenuhistory.MenuItem1 := FileMenuHistoryItem1;
globals.filemenuhistory.MenuItem2 := FileMenuHistoryItem2;
globals.filemenuhistory.MenuItem3 := FileMenuHistoryItem3;
globals.filemenuhistory.MenuItem4 := FileMenuHistoryItem4;
globals.filemenuhistory.MenuItem5 := FileMenuHistoryItem5;
globals.filemenuhistory.MenuItem6 := FileMenuHistoryItem6;
globals.filemenuhistory.MenuItem7 := FileMenuHistoryItem7;
globals.filemenuhistory.MenuItem8 := FileMenuHistoryItem8;
globals.filemenuhistory.MenuItem9 := FileMenuHistoryItem9;
globals.filemenuhistory.OnOpen := OnLoadCodeEditorFileMenuHistory;
mdl_LoadSettingFromFile(ChangeFileExt(ParamStr(0), '.ini'));
globals.filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory9));
globals.filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory8));
globals.filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory7));
globals.filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory6));
globals.filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory5));
globals.filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory4));
globals.filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory3));
globals.filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory2));
globals.filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory1));
globals.filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory0));
globals.mousedown1 := False;
globals.mousedown2 := False;
globals.stmousedown := False;
globals.buffer := TBitmap.Create;
globals.drawbuffer := TBitmap.Create;
globals.maskbuffer := TBitmap.Create;
globals.maskdrawbuffer := TBitmap.Create;
globals.glpanx := 0;
globals.glpany := 0;
globals.glmousedown := 0;
InitOpenGL;
ReadExtensions;
ReadImplementationProperties;
StartUpTimer.Enabled := True;
end;
procedure TForm1.StartUp;
var
pfd: TPIXELFORMATDESCRIPTOR;
pf: Integer;
begin
if globals.Initialized then
Exit;
// OpenGL initialisieren
globals.dc := GetDC(PreviewForm.OpenGLPanel.Handle);
// PixelFormat
pfd.nSize := SizeOf(pfd);
pfd.nVersion := 1;
pfd.dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER or 0;
pfd.iPixelType := PFD_TYPE_RGBA; // PFD_TYPE_RGBA or PFD_TYPEINDEX
pfd.cColorBits := 32;
pf := ChoosePixelFormat(globals.dc, @pfd); // Returns format that most closely matches above pixel format
SetPixelFormat(globals.dc, pf, @pfd);
globals.rc := wglCreateContext(globals.dc); // Rendering Context = window-glCreateContext
wglMakeCurrent(globals.dc, globals.rc); // Make the DC (Form1) the rendering Context
// Initialize GL environment variables
glInit;
ResetCamera;
OpenGLPanelResize(nil); // sets up the perspective
TestImage;
globals.gllist2d := glGenLists(1);
globals.gllist3d := glGenLists(1);
Application.OnIdle := Idle;
Application.OnHint := Hint;
DoNewModel;
globals.Initialized := True;
end;
function stigma2Dx(const sx: single; const pb: TPaintBox): integer;
begin
Result := GetIntInRange(Round(sx * pb.Width), 0, pb.Width);
end;
function stigma2Dy(const sy: single; const pb: TPaintBox): integer;
begin
Result := GetIntInRange(Round(sy * pb.Height), 0, pb.Height);
end;
procedure TForm1.EraseStigma2d(const pb: TPaintBox; const buf: TBitmap);
var
i: integer;
stx, sty: integer;
begin
stx := stigma2Dx(globals.stigma2d.x, pb);
sty := stigma2Dy(globals.stigma2d.y, pb);
pb.Canvas.Pixels[stx, sty] := buf.Canvas.Pixels[stx, sty];
for i := 1 to 2 do
begin
pb.Canvas.Pixels[stx - i, sty] := buf.Canvas.Pixels[stx - i, sty];
pb.Canvas.Pixels[stx + i, sty] := buf.Canvas.Pixels[stx + i, sty];
pb.Canvas.Pixels[stx, sty + i] := buf.Canvas.Pixels[stx, sty + i];
pb.Canvas.Pixels[stx, sty - i] := buf.Canvas.Pixels[stx, sty - i];
end;
end;
procedure TForm1.PaintStigma2d(const pb: TPaintBox);
var
i: integer;
stx, sty: integer;
begin
stx := stigma2Dx(globals.stigma2d.x, pb);
sty := stigma2Dy(globals.stigma2d.y, pb);
pb.Canvas.Pixels[stx, sty] := RGB(255, 255, 0);
for i := 1 to 2 do
begin
pb.Canvas.Pixels[stx - i, sty] := RGB(255, 255, 0);
pb.Canvas.Pixels[stx + i, sty] := RGB(255, 255, 0);
pb.Canvas.Pixels[stx, sty + i] := RGB(255, 255, 0);
pb.Canvas.Pixels[stx, sty - i] := RGB(255, 255, 0);
end;
end;
procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
TextureForm.PaintBox1.Canvas.Draw(0, 0, globals.drawbuffer);
PaintStigma2d(TextureForm.PaintBox1);
PaintStigma2d(MaskForm.PaintBox2);
end;
procedure TForm1.OpenGLPanelResize(Sender: TObject);
begin
glViewport(0, 0, PreviewForm.OpenGLPanel.Width, PreviewForm.OpenGLPanel.Height);
glMatrixMode(GL_PROJECTION);
glLoadMatrixF(@IdentityMatrix);
InfinitePerspective(64.0, PreviewForm.OpenGLPanel.Width / PreviewForm.OpenGLPanel.Height, 0.01);
glMatrixMode(GL_MODELVIEW);
globals.glneedrecalc := True;
end;
procedure TForm1.Idle(Sender: TObject; var Done: Boolean);
var
newglHorzPos, newglVertPos: integer;
begin
newglHorzPos := PreviewForm.ScrollBox1.HorzScrollBar.Position;
newglVertPos := PreviewForm.ScrollBox1.VertScrollBar.Position;
if (newglHorzPos <> globals.lastglHorzPos) or (newglVertPos <> globals.lastglVertPos) then
begin
globals.lastglVertPos := newglVertPos;
globals.lastglHorzPos := newglHorzPos;
globals.glneedrefresh := True;
end;
if not globals.glneedrecalc then
if not globals.glneedrefresh then
Exit; // jval: We don't need to render :)
DoRenderGL3D;
Done := False;
globals.glneedrecalc := False;
globals.glneedrefresh := False;
end;
function stigma2glX(const ss: integer): GLfloat;
begin
Result := ss / 32;
end;
function stigma2glY(const ss: integer): GLfloat;
begin
Result := -ss / 32;
end;
function stigma2glZ(const ss: integer): GLfloat;
begin
Result := ss / 32;
end;
procedure TForm1.DoRenderGL2D;
begin
glViewport(0, 0, glGetTextureSize, glGetTextureSize);
glBeginScene;
glEnable2D;
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, bitmaptexture);
glColor3f(1.0, 1.0, 1.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glCallList(globals.gllist2d);
glDisable2D;
end;
procedure TForm1.DoRenderGL3D;
begin
glBeginScene;
glRenderAxes;
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, bitmaptexture);
glColor3f(1.0, 1.0, 1.0);
if opt_renderwireframe then
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
else
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glCallList(globals.gllist3d);
if opt_renderstigma then
begin
glDisable(GL_TEXTURE_2D);
glColor3f(1.0, 1.0, 0.0);
glDrawSphere(stigma2glX(globals.stigma3d.x), stigma2glY(globals.stigma3d.y), stigma2glZ(globals.stigma3d.z), 0.05);
glEnable(GL_TEXTURE_2D);
glColor3f(1.0, 1.0, 1.0);
end;
glEndScene(globals.dc);
end;
procedure TForm1.UpdateEnable;
begin
TextureForm.PasteTextureButton1.Enabled := Clipboard.HasFormat(CF_BITMAP);
UpdateStatusBar;
end;
procedure TForm1.TexturePasteClick(Sender: TObject);
begin
if Clipboard.HasFormat(CF_BITMAP) then
begin
globals.buffer.LoadFromClipboardFormat(CF_BITMAP, ClipBoard.GetAsHandle(cf_Bitmap), 0);
CreateGLTexture;
InvalidatePaintBox;
globals.glneedrefresh := True;
end;
end;
procedure TForm1.Hint(Sender: TObject);
begin
if Application.Hint <> '' then
globals.hintcnt := 8;
StatusBar1.Panels[2].Text := Application.Hint;
end;
procedure TForm1.Copy3dButtonClick(Sender: TObject);
var
b: TBitmap;
begin
b := TBitmap.Create;
try
DoRenderGL3D; // JVAL: For some unknown reason this must be called before glReadPixels
Get3dPreviewBitmap(b);
Clipboard.Assign(b);
finally
b.Free;
end;
end;
procedure TForm1.Save3dButtonClick(Sender: TObject);
var
b: TBitmap;
begin
if SavePictureDialog1.Execute then
begin
b := TBitmap.Create;
try
DoRenderGL3D; // JVAL: For some unknown reason this must be called before glReadPixels
Get3dPreviewBitmap(b);
BackupFile(SavePictureDialog1.FileName);
b.SaveToFile(SavePictureDialog1.FileName);
finally
b.Free;
end;
end;
end;
procedure TForm1.Get3dPreviewBitmap(const b: TBitmap);
type
long_a = array[0..$FFFF] of LongWord;
Plong_a = ^long_a;
var
L, buf: Plong_a;
w, h: integer;
i, j: integer;
idx: integer;
begin
w := PreviewForm.OpenGLPanel.Width;
h := PreviewForm.OpenGLPanel.Height;
b.Width := w;
b.Height := h;
b.PixelFormat := pf32bit;
GetMem(L, w * h * SizeOf(LongWord));
glReadPixels(0, 0, w, h, GL_BGRA, GL_UNSIGNED_BYTE, L);
idx := 0;
for j := 0 to h - 1 do
begin
buf := b.ScanLine[h - j - 1];
for i := 0 to w - 1 do
begin
buf[i] := L[idx];
Inc(idx);
end;
end;
FreeMem(L, w * h * SizeOf(LongWord));
end;
procedure TForm1.GetMaskTextureBitmap(const b: TBitmap);
type
long_a = array[0..$FFFF] of LongWord;
Plong_a = ^long_a;
var
L, buf: Plong_a;
w, h: integer;
i, j: integer;
idx: integer;
begin
w := glGetTextureSize;
h := glGetTextureSize;
b.Width := w;
b.Height := h;
b.PixelFormat := pf32bit;
GetMem(L, w * h * SizeOf(LongWord));
glReadPixels(0, 0, w, h, GL_BGRA, GL_UNSIGNED_BYTE, L);
idx := 0;
for j := 0 to h - 1 do
begin
buf := b.ScanLine[h - j - 1];
for i := 0 to w - 1 do
begin
buf[i] := L[idx];
Inc(idx);
end;
end;
FreeMem(L, w * h * SizeOf(LongWord));
end;
resourcestring
rsTitle = 'DelphiDOOM Procedural Modeler';
procedure TForm1.About1Click(Sender: TObject);
begin
MessageBox(
Handle,
PChar(Format('%s'#13#10'Version %s'#13#10#13#10'A tool for creating MODELS for the DelphiDOOM engine.'#13#10'© 2017-2021, jvalavanis@gmail.com', [rsTitle, I_VersionBuilt])),
PChar(rsTitle),
MB_OK or MB_ICONINFORMATION or MB_APPLMODAL);
end;
procedure TForm1.Exit1Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.TextureCopyClick(Sender: TObject);
begin
Clipboard.Assign(globals.buffer);
end;
procedure TForm1.TextureOpenClick(Sender: TObject);
var
p: TPicture;
begin
if OpenPictureDialog1.Execute then
begin
p := TPicture.Create;
try
p.LoadFromFile(OpenPictureDialog1.FileName);
globals.buffer.PixelFormat := pf32bit;
if p.Graphic.Width <> 0 then
begin
globals.buffer.Width := p.Graphic.Width;
globals.buffer.Height := p.Graphic.Height;
globals.buffer.Canvas.Draw(0, 0, p.Graphic)
end
else
begin
globals.buffer.Width := p.Bitmap.Width;
globals.buffer.Height := p.Bitmap.Height;
globals.buffer.Canvas.Draw(0, 0, p.Bitmap)
end;
CreateGLTexture;
finally
p.Free;
end;
InvalidatePaintBox;
globals.glneedrecalc := True;
end;
end;
procedure TForm1.InvalidatePaintBox;
begin
TextureForm.PaintBox1.Width := globals.buffer.Width;
TextureForm.PaintBox1.Height := globals.buffer.Height;
CreateDrawBuffer;
TextureForm.PaintBox1.Invalidate;
end;
procedure QSortGrays(const A: PIntegerArray; const Len: integer);
function Gray(const i: Integer): Double; { CCIR 601 grayscale}
begin
Result := GetRValue(i) * 0.2989 + GetGValue(i) * 0.5870 + GetBValue(i) * 0.1140;
end;
procedure qsortI(l, r: Integer);
var
i, j: integer;
t: integer;
d: double;
begin
repeat
i := l;
j := r;
d := (Gray(A[l]) + Gray(A[r])) / 2;
repeat
while Gray(A[i]) < d do
inc(i);
while Gray(A[j]) > d do
dec(j);
if i <= j then
begin
t := A[i];
A[i] := A[j];
A[j] := t;
inc(i);
dec(j);
end;
until i > j;
if l < j then
qsortI(l, j);
l := i;
until i >= r;
end;
begin
if Len > 1 then
qsortI(0, Len - 1);
end;
procedure TForm1.TestImage;
var
idx, i, j: integer;
A: array[0..63] of TColor;
C: array[0..3] of byte;
r, g, b: integer;
begin
C[0] := 0;
C[1] := 85;
C[2] := 170;
C[3] := 255;
idx := 0;
for b := 0 to 3 do
for g := 0 to 3 do
for r := 0 to 3 do
begin
A[idx] := RGB(C[r], C[g], C[b]);
Inc(idx);
end;
QSortGrays(@A, 64);
globals.buffer.Width := 256;
globals.buffer.Height := 256;
globals.buffer.PixelFormat := pf32bit;
globals.buffer.Canvas.Pen.Style := psClear;
globals.buffer.Canvas.Pen.Color := $0;
globals.buffer.Canvas.Brush.Style := bsSolid;
globals.buffer.Canvas.Brush.Color := 0;
idx := 0;
for j := 0 to 7 do
for i := 0 to 7 do
begin
globals.buffer.Canvas.Brush.Color := A[idx];
Inc(idx);
globals.buffer.Canvas.FillRect(Rect(i * 32, j * 32, (i + 1) * 32, (j + 1) * 32));
end;
CreateGLTexture;
InvalidatePaintBox;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
UpdateEnable;
if globals.hintcnt > 0 then
Dec(globals.hintcnt);
if globals.hintcnt = 0 then
Application.Hint := '';
end;
procedure TForm1.Model1Click(Sender: TObject);
begin
PasteTexture1.Enabled := Clipboard.HasFormat(CF_BITMAP);
end;
procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
PaintBox1Responder(X, Y);
globals.paintX := X;
globals.paintY := Y;
end;
procedure TForm1.CreateGLTexture;
begin
gld_CreateTexture(globals.buffer);
end;
procedure TForm1.CreateDrawBuffer;
begin
globals.drawbuffer.Width := globals.buffer.Width;
globals.drawbuffer.Height := globals.buffer.Height;
globals.drawbuffer.Canvas.Draw(0, 0, globals.buffer);
end;
function TForm1.CheckCanClose: boolean;
var
ret: integer;
begin
if globals.changed then
begin
ret := MessageBox(Handle, 'Do you want to save changes?', PChar(rsTitle), MB_YESNOCANCEL or MB_ICONQUESTION or MB_APPLMODAL);
if ret = idCancel then
begin
Result := False;
exit;
end;
if ret = idNo then
begin
Result := True;
exit;
end;
if ret = idYes then
begin
EditorSaveClick(self);
Result := not globals.changed;
exit;
end;
end;
Result := True;
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
Application.OnIdle := nil;
if CheckCanClose = False then
begin
CanClose := False;
Application.OnIdle := Idle;
end
else
CanClose := True;
end;
procedure TForm1.New1Click(Sender: TObject);
begin
if not CheckCanClose then
Exit;
DoNewModel;
ResetCamera;
end;
procedure TForm1.CodeEditorChange(Sender: TObject);
begin
globals.changed := True;
end;
procedure TForm1.EditorSaveClick(Sender: TObject);
begin
if globals.ffilename = '' then
begin
EditorSaveAsClick(Sender);
Exit;
end;
DoSaveCodeEditor;
end;
const
NEWMODELCODE =
'model model1;'#13#10 +
'begin'#13#10 +
' SetFrame(1);'#13#10 +
' glbegin(GL_QUADS);'#13#10 +
' glTexCoord2f(0,1); glVertex3f(-0.5,-0.5, 0);'#13#10 +
' glTexCoord2f(1,1); glVertex3f( 0.5,-0.5, 0);'#13#10 +
' glTexCoord2f(1,0); glVertex3f( 0.5, 0.5, 0);'#13#10 +
' glTexCoord2f(0,0); glVertex3f(-0.5, 0.5, 0);'#13#10 +
' glEnd;'#13#10 +
' SetFrame(0);'#13#10 +
' CallFrame(1);'#13#10 +
'end.'#13#10;
procedure TForm1.DoNewModel;
begin
globals.CodeEditor.Lines.Text := NEWMODELCODE;
globals.changed := false;
globals.glneedrecalc := True;
SetFileName('');
globals.paintX := 0;
globals.paintY := 0;
EditorForm.PageControl1.ActivePageIndex := 0;
ClearOutput;
ResetStigma3d;
Compile;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
glDeleteLists(globals.gllist2d, 1);
glDeleteLists(globals.gllist3d, 1);
gld_ShutDownTexture;
globals.buffer.Free;
globals.drawbuffer.Free;
globals.maskbuffer.Free;
globals.maskdrawbuffer.Free;
globals.mdl.Free;
gld_ShutDownTexture;
wglMakeCurrent(0, 0);
wglDeleteContext(globals.rc);
stringtobigstring(globals.filemenuhistory.PathStringIdx(0), @opt_filemenuhistory0);
stringtobigstring(globals.filemenuhistory.PathStringIdx(1), @opt_filemenuhistory1);
stringtobigstring(globals.filemenuhistory.PathStringIdx(2), @opt_filemenuhistory2);
stringtobigstring(globals.filemenuhistory.PathStringIdx(3), @opt_filemenuhistory3);
stringtobigstring(globals.filemenuhistory.PathStringIdx(4), @opt_filemenuhistory4);
stringtobigstring(globals.filemenuhistory.PathStringIdx(5), @opt_filemenuhistory5);
stringtobigstring(globals.filemenuhistory.PathStringIdx(6), @opt_filemenuhistory6);
stringtobigstring(globals.filemenuhistory.PathStringIdx(7), @opt_filemenuhistory7);
stringtobigstring(globals.filemenuhistory.PathStringIdx(8), @opt_filemenuhistory8);
stringtobigstring(globals.filemenuhistory.PathStringIdx(9), @opt_filemenuhistory9);
mdl_SaveSettingsToFile(ChangeFileExt(ParamStr(0), '.ini'));
globals.filemenuhistory.Free;
end;
procedure TForm1.OpenGLPanelMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button in [mbLeft, mbRight] then
begin
globals.glpanx := X;
globals.glpany := Y;
if Button = mbLeft then
globals.glmousedown := 1
else
globals.glmousedown := 2;
end;
end;
procedure TForm1.OpenGLPanelMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
begin
UpdateStatusBar;
if globals.glmousedown = 0 then
exit;
if globals.glmousedown = 1 then
begin
camera.ay := camera.ay + (globals.glpanx - X);
camera.ax := camera.ax + (globals.glpany - Y);
end
else
begin
camera.x := camera.x + (globals.glpanx - X) / PreviewForm.OpenGLPanel.Width * (camera.z - 1.0);
if camera.x < -6.0 then
camera.x := -6.0
else if camera.x > 6.0 then
camera.x := 6.0;
camera.y := camera.y - (globals.glpany - Y) / PreviewForm.OpenGLPanel.Width * (camera.z - 1.0);
if camera.y < -6.0 then
camera.y := -6.0
else if camera.y > 6.0 then
camera.y := 6.0;
end;
globals.glneedrefresh := True;
globals.glpanx := X;