-
Notifications
You must be signed in to change notification settings - Fork 5
/
ter_voxels.pas
424 lines (381 loc) · 9.95 KB
/
ter_voxels.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
//------------------------------------------------------------------------------
//
// DD_TERRAIN: Terrain Generator
// Copyright (C) 2020-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:
// Voxel stuff
//
//------------------------------------------------------------------------------
// E-Mail: [email protected]
// Site : https://sourceforge.net/projects/DD-TERRAIN/
//------------------------------------------------------------------------------
unit ter_voxels;
interface
const
MAXVOXELSIZE = 256;
type
voxelview_t = (vv_none, vv_front, vv_back, vv_left, vv_right, vv_top, vv_down);
voxelitem_t = LongWord;
voxelitem_p = ^voxelitem_t;
voxelbuffer_t = array[0..MAXVOXELSIZE - 1, 0..MAXVOXELSIZE - 1, 0..MAXVOXELSIZE - 1] of voxelitem_t;
voxelbuffer_p = ^voxelbuffer_t;
voxelbuffer2d_t = array[0..MAXVOXELSIZE - 1, 0..MAXVOXELSIZE - 1] of voxelitem_t;
voxelbuffer2d_p = ^voxelbuffer2d_t;
procedure vox_getviewbuffer(const buf: voxelbuffer_p; const size: integer;
const outbuf: voxelbuffer2d_p; const vv: voxelview_t);
procedure vox_shrinkyaxis(const buf: voxelbuffer_p; const size: integer;
const amin, amax: integer);
procedure vox_removenonvisiblecells(const buf: voxelbuffer_p; const size: integer);
procedure VXE_ExportVoxelToSlab6VOX(const voxelbuffer: voxelbuffer_p; const voxelsize: Integer;
const fname: string);
procedure VXE_ExportVoxelToDDVOX(const voxelbuffer: voxelbuffer_p; const voxelsize: Integer;
const fname: string);
implementation
uses
Windows,
Classes,
ter_utils,
ter_quantize,
ter_palettes;
procedure vox_getviewbuffer(const buf: voxelbuffer_p; const size: integer;
const outbuf: voxelbuffer2d_p; const vv: voxelview_t);
var
x, y, z: integer;
c: voxelitem_t;
begin
for x := 0 to size - 1 do
for y := 0 to size - 1 do
outbuf[x, y] := 0;
if vv = vv_front then
begin
for x := 0 to size - 1 do
for y := 0 to size - 1 do
begin
c := 0;
for z := 0 to size - 1 do
if buf[x, y, z] <> 0 then
begin
c := buf[x, y, z];
Break;
end;
outbuf[x, y] := c;
end;
Exit;
end;
if vv = vv_back then
begin
for x := 0 to size - 1 do
for y := 0 to size - 1 do
begin
c := 0;
for z := size - 1 downto 0 do
if buf[x, y, z] <> 0 then
begin
c := buf[x, y, z];
Break;
end;
outbuf[x, y] := c;
end;
Exit;
end;
if vv = vv_left then
begin
for z := 0 to size - 1 do
for y := 0 to size - 1 do
begin
c := 0;
for x := 0 to size - 1 do
if buf[x, y, z] <> 0 then
begin
c := buf[x, y, z];
Break;
end;
outbuf[size - 1 - z, y] := c;
end;
Exit;
end;
if vv = vv_right then
begin
for z := 0 to size - 1 do
for y := 0 to size - 1 do
begin
c := 0;
for x := size - 1 downto 0 do
if buf[x, y, z] <> 0 then
begin
c := buf[x, y, z];
Break;
end;
outbuf[z, y] := c;
end;
Exit;
end;
if vv = vv_top then
begin
for x := 0 to size - 1 do
for z := 0 to size - 1 do
begin
c := 0;
for y := 0 to size - 1 do
if buf[x, y, z] <> 0 then
begin
c := buf[x, y, z];
Break;
end;
outbuf[x, size - 1 - z] := c;
end;
Exit;
end;
if vv = vv_down then
begin
for x := 0 to size - 1 do
for z := 0 to size - 1 do
begin
c := 0;
for y := size - 1 downto 0 do
if buf[x, y, z] <> 0 then
begin
c := buf[x, y, z];
Break;
end;
outbuf[x, z] := c;
end;
Exit;
end;
end;
procedure vox_shrinkyaxis(const buf: voxelbuffer_p; const size: integer;
const amin, amax: integer);
var
bck: voxelbuffer_p;
mn, mx, tmp: integer;
x, y, y1, z: integer;
factor, mne: Extended;
begin
if amin = 0 then
if amax = 255 then
Exit;
if amax = 0 then
if amin = 255 then
Exit;
GetMem(bck, MAXVOXELSIZE * MAXVOXELSIZE * size * SizeOf(voxelitem_t));
for x := 0 to size - 1 do
for y := 0 to size - 1 do
for z := 0 to size - 1 do
bck[x, y, z] := buf[x, y, z];
mn := amin;
mx := amax;
if mn < 0 then
mn := 0
else if mn > 255 then
mn := 255;
if mx < 0 then
mx := 0
else if mx > 255 then
mx := 255;
if mn > mx then
begin
tmp := mn;
mn := mx;
mx := tmp;
end;
for x := 0 to size - 1 do
for z := 0 to size - 1 do
for y := 0 to size - 1 do
buf[x, y, z] := 0;
factor := (mx - mn) / MAXVOXELSIZE;
if size = MAXVOXELSIZE then
begin
for x := 0 to size - 1 do
for z := 0 to size - 1 do
for y := 0 to size - 1 do
begin
y1 := Round(mn + y * factor);
if y1 < 0 then
y1 := 0
else if y1 >= MAXVOXELSIZE then
y1 := MAXVOXELSIZE;
if buf[x, y1, z] = 0 then
buf[x, y1, z] := bck[x, y, z];
end;
end
else
begin
mne := mn * size / MAXVOXELSIZE;
for x := 0 to size - 1 do
for z := 0 to size - 1 do
for y := 0 to size - 1 do
begin
y1 := Round(mne + y * factor);
if y1 < 0 then
y1 := 0
else if y1 >= size then
y1 := size;
if buf[x, y1, z] = 0 then
buf[x, y1, z] := bck[x, y, z];
end;
end;
FreeMem(bck, MAXVOXELSIZE * MAXVOXELSIZE * size * SizeOf(voxelitem_t));
end;
procedure vox_removenonvisiblecells(const buf: voxelbuffer_p; const size: integer);
type
flags_t = array[0..MAXVOXELSIZE - 1, 0..MAXVOXELSIZE - 1, 0..MAXVOXELSIZE - 1] of boolean;
flags_p = ^flags_t;
var
i, j, k: integer;
flags: flags_p;
begin
GetMem(flags, SizeOf(flags_t));
FillChar(flags^, SizeOf(flags_t), Chr(0));
for i := 1 to size - 2 do
begin
for j := 1 to size - 2 do
for k := 1 to size - 2 do
if buf[i, j, k] <> 0 then
if buf[i - 1, j, k] <> 0 then
if buf[i + 1, j, k] <> 0 then
if buf[i, j - 1, k] <> 0 then
if buf[i, j + 1, k] <> 0 then
if buf[i, j, k - 1] <> 0 then
if buf[i, j, k + 1] <> 0 then
flags^[i, j, k] := true;
end;
for i := 1 to size - 2 do
begin
for j := 1 to size - 2 do
for k := 1 to size - 2 do
if flags^[i, j, k] then
buf[i, j, k] := 0;
end;
FreeMem(flags, SizeOf(flags_t));
end;
procedure VXE_ExportVoxelToSlab6VOX(const voxelbuffer: voxelbuffer_p; const voxelsize: Integer;
const fname: string);
var
i: integer;
fvoxelsize: Integer;
x, y, z: integer;
x1, x2, y1, y2, z1, z2: integer;
voxdata: PByteArray;
voxsize: integer;
dpal: array[0..767] of byte;
vpal: array[0..255] of LongWord;
fs: TFileStream;
c: LongWord;
begin
BackupFile(fname);
if voxelsize >= 256 then
begin
x1 := 1; x2 := 255;
y1 := 1; y2 := 255;
z1 := 1; z2 := 255;
fvoxelsize := 255
end
else
begin
x1 := 0; x2 := voxelsize - 1;
y1 := 0; y2 := voxelsize - 1;
z1 := 0; z2 := voxelsize - 1;
fvoxelsize := voxelsize;
end;
voxsize := fvoxelsize * fvoxelsize * fvoxelsize;
GetMem(voxdata, voxsize);
for i := 0 to 255 do
vpal[i] := 0;
if vxe_getquantizevoxelpalette(voxelbuffer, voxelsize, @vpal, 255) then
begin
for i := 0 to 255 do
begin
c := vpal[i];
dpal[3 * i] := GetRValue(c);
dpal[3 * i + 1] := GetGValue(c);
dpal[3 * i + 2] := GetBValue(c);
end;
end
else
begin
for i := 0 to 764 do
dpal[i] := DoomPaletteRaw[i + 3];
for i := 765 to 767 do
dpal[i] := 0;
for i := 0 to 254 do
vpal[i] := RGB(dpal[3 * i], dpal[3 * i + 1], dpal[3 * i + 2]);
end;
fs := TFileStream.Create(fname, fmCreate);
try
for i := 0 to 2 do
fs.Write(fvoxelsize, SizeOf(integer));
i := 0;
for x := x1 to x2 do
for y := y1 to y2 do
for z := z1 to z2 do
begin
c := voxelbuffer[x, z, fvoxelsize - 1 - y];
if c = 0 then
voxdata[i] := 255
else
voxdata[i] := V_FindAproxColorIndex(@vpal, c, 0, 254);
inc(i);
end;
fs.Write(voxdata^, voxsize);
fs.Write(dpal, 768);
finally
fs.Free;
end;
FreeMem(voxdata, voxsize);
end;
procedure VXE_ExportVoxelToDDVOX(const voxelbuffer: voxelbuffer_p; const voxelsize: Integer;
const fname: string);
var
t: TextFile;
xx, yy, zz: integer;
skip: integer;
begin
BackupFile(fname);
AssignFile(t, fname);
Rewrite(t);
Writeln(t, voxelsize);
for xx := 0 to voxelsize - 1 do
for yy := 0 to voxelsize - 1 do
begin
skip := 0;
for zz := 0 to voxelsize - 1 do
begin
if voxelbuffer[xx, yy, zz] = 0 then
begin
Inc(skip);
if zz = voxelsize - 1 then
Writeln(t, 'skip ', skip);
end
else
begin
if skip > 0 then
begin
Write(t, 'skip ', skip, ', ');
skip := 0;
end;
if zz = voxelsize - 1 then
Writeln(t, voxelbuffer[xx, yy, zz])
else
Write(t, voxelbuffer[xx, yy, zz], ', ');
end;
end;
end;
CloseFile(t);
end;
end.