-
Notifications
You must be signed in to change notification settings - Fork 5
/
kvx2mesh.pas
232 lines (203 loc) · 6.05 KB
/
kvx2mesh.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
//------------------------------------------------------------------------------
//
// DD_VOXEL: DelphiDoom Voxel Editor
// Copyright (C) 2013-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:
// KVX to DDMESH
//
//------------------------------------------------------------------------------
// E-Mail: jimmyvalavanis@yahoo.gr
// Site : https://sourceforge.net/projects/delphidoom-voxel-editor/
// https://sourceforge.net/projects/delphidoom/files/DDVoxels/
//------------------------------------------------------------------------------
unit kvx2mesh;
interface
function ConvertKVF2DDMESH(const sf, st: string): boolean;
implementation
uses
vxe_kvx,
vxe_mesh,
voxels,
SysUtils,
Classes;
function ConvertKVF2DDMESH(const sf, st: string): boolean;
var
strm: TFileStream;
pal: array[0..255] of LongWord;
i: integer;
r, g, b: byte;
numbytes: integer;
xsiz, ysiz, zsiz, xpivot, ypivot, zpivot: integer;
xoffset: PIntegerArray;
xyoffset: PSmallIntPArray;
offs: integer;
voxdatasize: integer;
voxdata: PByteArray;
xx, yy, zz: integer;
x1, y1, z1: integer;
endptr: PByte;
slab: kvxslab_p;
s: string;
kvxbuffer: kvxbuffer_p;
vmo: TVoxelMeshOptimizer;
fvoxelsize: integer;
voxelbuffer: voxelbuffer_p;
maxpal: integer;
cc: integer;
palfactor: double;
begin
if not FileExists(sf) then
begin
Result := False;
Exit;
end;
voxelbuffer := vox_getbuffer;
FillChar(voxelbuffer^, SizeOf(voxelbuffer_t), Chr(0));
strm := TFileStream.Create(sf, fmOpenRead or fmShareDenyWrite);
strm.Seek(strm.size - 768, soFromBeginning);
maxpal := 0;
for i := 0 to 255 do
begin
strm.Read(b, SizeOf(Byte));
if b > maxpal then
maxpal := b;
strm.Read(g, SizeOf(Byte));
if g > maxpal then
maxpal := g;
strm.Read(r, SizeOf(Byte));
if r > maxpal then
maxpal := r;
pal[i] := r shl 16 + g shl 8 + b;
if pal[i] = 0 then
pal[i] := $01;
end;
if (maxpal < 255) and (maxpal > 0) then
begin
palfactor := 255 / maxpal;
if palfactor > 4.0 then
palfactor := 4.0;
for i := 0 to 255 do
begin
r := pal[i] shr 16;
g := pal[i] shr 8;
b := pal[i];
cc := round(palfactor * r);
if cc < 0 then
cc := 0
else if cc > 255 then
cc := 255;
r := cc;
cc := round(palfactor * g);
if cc < 0 then
cc := 0
else if cc > 255 then
cc := 255;
g := cc;
cc := round(palfactor * b);
if cc < 0 then
cc := 0
else if cc > 255 then
cc := 255;
b := cc;
pal[i] := r shl 16 + g shl 8 + b;
end;
end;
strm.Seek(0, soFromBeginning);
strm.Read(numbytes, SizeOf(Integer));
strm.Read(xsiz, SizeOf(Integer));
strm.Read(ysiz, SizeOf(Integer));
strm.Read(zsiz, SizeOf(Integer));
fvoxelsize := 1;
while xsiz > fvoxelsize do
fvoxelsize := fvoxelsize * 2;
while ysiz > fvoxelsize do
fvoxelsize := fvoxelsize * 2;
while zsiz > fvoxelsize do
fvoxelsize := fvoxelsize * 2;
if fvoxelsize < 256 then
fvoxelsize := fvoxelsize * 2;
strm.Read(xpivot, SizeOf(Integer));
strm.Read(ypivot, SizeOf(Integer));
strm.Read(zpivot, SizeOf(Integer));
GetMem(xoffset, (xsiz + 1) * SizeOf(Integer));
GetMem(xyoffset, xsiz * SizeOf(PSmallIntArray));
for i := 0 to xsiz - 1 do
GetMem(xyoffset[i], (ysiz + 1) * SizeOf(SmallInt));
strm.Read(xoffset^, (xsiz + 1) * SizeOf(Integer));
for i := 0 to xsiz - 1 do
strm.Read(xyoffset[i]^, (ysiz + 1) * SizeOf(SmallInt));
offs := xoffset[0];
voxdatasize := numbytes - 24 - (xsiz + 1) * 4 - xsiz * (ysiz + 1) * 2;
GetMem(voxdata, voxdatasize);
strm.Read(voxdata^, voxdatasize);
strm.Free;
GetMem(kvxbuffer, SizeOf(kvxbuffer_t));
for xx := 0 to xsiz - 1 do
for yy := 0 to ysiz - 1 do
for zz := 0 to zsiz - 1 do
kvxbuffer[xx, yy, zz] := $FFFF;
for xx := 0 to xsiz - 1 do
begin
for yy := 0 to ysiz - 1 do
begin
endptr := @voxdata[xoffset[xx] + xyoffset[xx][yy + 1] - offs];
slab := @voxdata[xoffset[xx] + xyoffset[xx][yy] - offs];
while Integer(slab) < Integer(endptr) do
begin
for zz := slab.ztop to slab.zleng + slab.ztop - 1 do
kvxbuffer[xx, yy, zz] := slab.col[zz - slab.ztop];
slab := kvxslab_p(integer(slab) + slab.zleng + 3);
end;
end;
end;
x1 := fvoxelsize div 2 - xpivot div 256;
y1 := fvoxelsize div 2 - ypivot div 256;
z1 := fvoxelsize{ div 2} - zpivot div 256;
if x1 < 0 then
x1 := 0;
if y1 < 0 then
y1 := 0;
if z1 < 0 then
z1 := 0;
while x1 + xsiz > fvoxelsize do
dec(x1);
while y1 + ysiz > fvoxelsize do
dec(y1);
while z1 + zsiz > fvoxelsize do
dec(z1);
for xx := x1 to x1 + xsiz - 1 do
for yy := y1 to y1 + ysiz - 1 do
for zz := z1 to z1 + zsiz - 1 do
if kvxbuffer[xx - x1, yy - y1, zz - z1] <> $FFFF then
voxelbuffer[xx, zz, fvoxelsize - yy - 1] := pal[kvxbuffer[xx - x1, yy - y1, zz - z1]];
FreeMem(xoffset, (xsiz + 1) * SizeOf(Integer));
for i := 0 to xsiz - 1 do
FreeMem(xyoffset[i], (ysiz + 1) * SizeOf(SmallInt));
FreeMem(xyoffset, xsiz * SizeOf(PSmallIntArray));
FreeMem(voxdata, voxdatasize);
FreeMem(kvxbuffer, SizeOf(kvxbuffer_t));
vmo := TVoxelMeshOptimizer.Create;
vmo.LoadVoxel(fvoxelsize, voxelbuffer);
vmo.Optimize;
vmo.SaveToFile(st);
vmo.Free;
vox_freebuffer(voxelbuffer);
Result := True;
end;
end.