-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfrm_exportvoxel.pas
374 lines (334 loc) · 9.59 KB
/
frm_exportvoxel.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
//------------------------------------------------------------------------------
//
// Voxelizer
// Copyright (C) 2021-2022 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:
// Export Voxel Form
//
//------------------------------------------------------------------------------
// Site : https://sourceforge.net/projects/voxelizer/
//------------------------------------------------------------------------------
unit frm_exportvoxel;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ExtCtrls, Buttons, models, vxl_voxels;
type
TExportVoxelForm = class(TForm)
BottomPanel: TPanel;
ButtonPanel: TPanel;
OKButton1: TButton;
CancelButton1: TButton;
MainPanel: TPanel;
PreviewGroupBox: TGroupBox;
Image1: TImage;
SaveVoxelDialog: TSaveDialog;
SizeRadioGroup: TRadioGroup;
PatchRadioGroup: TRadioGroup;
frontSpeedButton: TSpeedButton;
backSpeedButton: TSpeedButton;
leftSpeedButton: TSpeedButton;
rightSpeedButton: TSpeedButton;
topSpeedButton: TSpeedButton;
bottomSpeedButton: TSpeedButton;
OpenPaletteDialog: TOpenDialog;
ChooseOtherPalettePanel: TPanel;
LoadPaletteSpeedButton: TSpeedButton;
OtherPaletteEdit: TEdit;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
Panel1: TPanel;
Label3: TLabel;
SelectFileButton: TSpeedButton;
FileNameEdit: TEdit;
Bevel2: TBevel;
Panel2: TPanel;
Label1: TLabel;
SelectZipFileButton: TSpeedButton;
ZIPFileNameEdit: TEdit;
SaveZIPDialog: TSaveDialog;
VoxelTypeRadioGroup: TRadioGroup;
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FileNameEditChange(Sender: TObject);
procedure SelectFileButtonClick(Sender: TObject);
procedure SizeRadioGroupClick(Sender: TObject);
procedure SpeedButtonClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure LoadPaletteSpeedButtonClick(Sender: TObject);
procedure PatchRadioGroupClick(Sender: TObject);
procedure PageControl1Change(Sender: TObject);
procedure SelectZipFileButtonClick(Sender: TObject);
procedure VoxelTypeRadioGroupClick(Sender: TObject);
private
{ Private declarations }
protected
model: model_t;
modeltex: TBitmap;
vox: voxelbuffer_p;
procedure UpdateControls;
procedure UpdateEnable;
procedure CreateVoxel;
public
{ Public declarations }
voxsize: integer;
procedure SetModelVoxelParams(const amodel: model_t; const avox: voxelbuffer_p;
amodeltex: TBitmap);
end;
implementation
{$R *.dfm}
uses
vxl_defs,
vxl_utils,
vxl_voxelexport;
procedure TExportVoxelForm.FormCreate(Sender: TObject);
var
i: integer;
begin
model := nil;
modeltex := nil;
vox := nil;
DoubleBuffered := True;
for i := 0 to ComponentCount - 1 do
if Components[i].InheritsFrom(TWinControl) then
if not (Components[i] is TListBox) then
(Components[i] as TWinControl).DoubleBuffered := True;
Image1.Picture.Bitmap.PixelFormat := pf32bit;
PageControl1.ActivePageIndex := 0;
voxsize := opt_voxsize;
if voxsize <> 32 then
if voxsize <> 64 then
if voxsize <> 128 then
if voxsize <> 256 then
voxsize := 128;
if voxsize = 32 then
SizeRadioGroup.ItemIndex := 0
else if voxsize = 64 then
SizeRadioGroup.ItemIndex := 1
else if voxsize = 128 then
SizeRadioGroup.ItemIndex := 2
else
SizeRadioGroup.ItemIndex := 3;
if opt_voxpal in [0..6] then
PatchRadioGroup.ItemIndex := opt_voxpal
else
PatchRadioGroup.ItemIndex := 0;
ChooseOtherPalettePanel.Visible := (PatchRadioGroup.ItemIndex = 6) and (PatchRadioGroup.Visible);
OtherPaletteEdit.Text := bigstringtostring(@opt_customvoxpal);
if opt_zipvoxtype in [0..1] then
VoxelTypeRadioGroup.ItemIndex := opt_zipvoxtype
else
VoxelTypeRadioGroup.ItemIndex := 0;
end;
procedure TExportVoxelForm.SetModelVoxelParams(const amodel: model_t; const avox: voxelbuffer_p;
amodeltex: TBitmap);
begin
model := amodel;
vox := avox;
modeltex := amodeltex;
CreateVoxel;
UpdateControls;
end;
procedure TExportVoxelForm.CreateVoxel;
begin
if (model = nil) or (vox = nil) or (modeltex = nil) then
Exit;
Screen.Cursor := crHourglass;
try
case SizeRadioGroup.ItemIndex of
0: voxsize := 32;
1: voxsize := 64;
2: voxsize := 128;
else
voxsize := 256;
end;
DT_CreateVoxelFromModel(model, vox, voxsize, modeltex);
finally
Screen.Cursor := crDefault;
end;
end;
function RGBSwap(const l: LongWord): LongWord;
var
A: packed array[0..3] of byte;
tmp: byte;
begin
PLongWord(@A)^ := l;
tmp := A[0];
A[0] := A[2];
A[2] := tmp;
Result := PLongWord(@A)^;
end;
procedure TExportVoxelForm.UpdateControls;
var
b, b2: TBitmap;
buf2d: voxelbuffer2d_t;
v: voxelview_t;
x, y: integer;
ln: PLongWordArray;
begin
ChooseOtherPalettePanel.Visible := (PatchRadioGroup.ItemIndex = 6) and (PatchRadioGroup.Visible);
if (model = nil) or (vox = nil) or (modeltex = nil) then
Exit;
b := TBitmap.Create;
try
b.Width := 256;
b.Height := 256;
b.PixelFormat := pf32bit;
b.Canvas.Brush.Style := bsSolid;
b.Canvas.Brush.Color := clBlack;
b.Canvas.Pen.Style := psSolid;
b.Canvas.Pen.Color := clBlack;
b.Canvas.FillRect(Rect(0, 0, 256, 256));
if frontSpeedButton.Down then
v := vv_front
else if backSpeedButton.Down then
v := vv_back
else if leftSpeedButton.Down then
v := vv_left
else if rightSpeedButton.Down then
v := vv_right
else if topSpeedButton.Down then
v := vv_top
else if bottomSpeedButton.Down then
v := vv_down
else
v := vv_none;
case SizeRadioGroup.ItemIndex of
0: voxsize := 32;
1: voxsize := 64;
2: voxsize := 128;
else
voxsize := 256;
end;
vox_getviewbuffer(vox, voxsize, @buf2d, v);
b2 := TBitmap.Create;
try
b2.Width := voxsize;
b2.Height := voxsize;
b2.PixelFormat := pf32bit;
for y := 0 to b2.Height - 1 do
begin
ln := b2.ScanLine[y];
for x := 0 to b2.Width - 1 do
ln[x] := RGBSwap(buf2d[x, y]);
end;
b.Canvas.StretchDraw(Rect(0, 0, 256, 256), b2);
Image1.Picture.Bitmap.Canvas.Draw(0, 0, b);
finally
b2.Free;
end;
Image1.Invalidate;
finally
b.Free;
end;
end;
procedure TExportVoxelForm.FormShow(Sender: TObject);
begin
UpdateEnable;
UpdateControls;
end;
procedure TExportVoxelForm.UpdateEnable;
var
uName, uExt: string;
e: boolean;
begin
if PageControl1.ActivePageIndex = 0 then
begin
uName := Trim(FileNameEdit.Text);
e := uName <> '';
if e then
begin
uExt := UpperCase(ExtractFileExt(uName));
e := (uExt = '.DDVOX') or (uExt = '.VOX');
end;
PatchRadioGroup.Visible := uExt = '.VOX';
end
else
begin
uName := Trim(FileNameEdit.Text);
e := uName <> '';
PatchRadioGroup.Visible := VoxelTypeRadioGroup.ItemIndex = 1;
end;
ChooseOtherPalettePanel.Visible := (PatchRadioGroup.ItemIndex = 6) and (PatchRadioGroup.Visible);
OKButton1.Enabled := e;
end;
procedure TExportVoxelForm.FileNameEditChange(Sender: TObject);
begin
UpdateEnable;
UpdateControls;
end;
procedure TExportVoxelForm.SelectFileButtonClick(Sender: TObject);
begin
if SaveVoxelDialog.Execute then
begin
FileNameEdit.Text := SaveVoxelDialog.FileName;
FileNameEdit.Hint := SaveVoxelDialog.FileName;
OKButton1.Enabled := True;
end;
end;
procedure TExportVoxelForm.SizeRadioGroupClick(Sender: TObject);
begin
CreateVoxel;
UpdateControls;
end;
procedure TExportVoxelForm.SpeedButtonClick(Sender: TObject);
begin
UpdateControls;
end;
procedure TExportVoxelForm.FormDestroy(Sender: TObject);
begin
opt_voxsize := voxsize;
opt_voxpal := PatchRadioGroup.ItemIndex;
stringtobigstring(OtherPaletteEdit.Text, @opt_customvoxpal);
opt_zipvoxtype := VoxelTypeRadioGroup.ItemIndex;
end;
procedure TExportVoxelForm.LoadPaletteSpeedButtonClick(Sender: TObject);
begin
if OpenPaletteDialog.Execute then
begin
OtherPaletteEdit.Text := OpenPaletteDialog.FileName;
OtherPaletteEdit.Hint := OpenPaletteDialog.FileName;
end;
end;
procedure TExportVoxelForm.PatchRadioGroupClick(Sender: TObject);
begin
ChooseOtherPalettePanel.Visible := (PatchRadioGroup.ItemIndex = 6) and (PatchRadioGroup.Visible);
if ChooseOtherPalettePanel.Visible then
if OtherPaletteEdit.Text = '' then
LoadPaletteSpeedButtonClick(Sender);
end;
procedure TExportVoxelForm.PageControl1Change(Sender: TObject);
begin
UpdateEnable;
end;
procedure TExportVoxelForm.SelectZipFileButtonClick(Sender: TObject);
begin
if SaveZIPDialog.Execute then
begin
ZipFileNameEdit.Text := SaveZIPDialog.FileName;
ZipFileNameEdit.Hint := SaveZIPDialog.FileName;
OKButton1.Enabled := True;
end;
end;
procedure TExportVoxelForm.VoxelTypeRadioGroupClick(Sender: TObject);
begin
UpdateEnable;
end;
end.