-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvxl_voxelexport.pas
205 lines (190 loc) · 5.68 KB
/
vxl_voxelexport.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
//------------------------------------------------------------------------------
//
// 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 model to voxelbuffer
//
//------------------------------------------------------------------------------
// Site : https://sourceforge.net/projects/voxelizer/
//------------------------------------------------------------------------------
unit vxl_voxelexport;
interface
uses
Windows,
Classes,
SysUtils,
Graphics,
vxl_voxels,
models,
vxl_voxelizer;
procedure DT_CreateVoxelFromModel(const t: model_t; const vox: voxelbuffer_p;
const voxsize: integer; const modeltex: TBitmap; const mt: boolean = False);
implementation
uses
vxl_defs,
vxl_multithreading,
vxl_texture;
procedure DT_CreateVoxelFacesFromModel(const mVertCount, mFaceCount: integer;
const mVert: fvec5_pa; const mFace: ivec3_pa; const scale: single;
const vox: voxelbuffer_p; const voxsize: integer; const tex: TTexture;
const opaque: boolean);
var
tri: meshtriangle_t;
i: integer;
ofs: integer;
procedure _make_vertex(const r, g: integer);
begin
tri[g].x := mVert[r].x * scale + ofs;
tri[g].y := voxsize - 1.0 - mVert[r].y * scale;
tri[g].z := voxsize - 1.0 - mVert[r].z * scale - ofs;
tri[g].u := mVert[r].u;
tri[g].v := mVert[r].v;
end;
begin
ofs := voxsize div 2;
for i := 0 to mFaceCount - 1 do
begin
_make_vertex(mFace[i].x, 0);
_make_vertex(mFace[i].y, 1);
_make_vertex(mFace[i].z, 2);
DT_VoxelizeTri(@tri, tex, vox, voxsize, opaque);
end;
end;
type
th_createvoxelparams_t = record
mVertCount, mFaceCount: integer;
mVert: fvec5_pa;
mFace: ivec3_pa;
scale: single;
vox: voxelbuffer_p;
voxsize: integer;
tex: TTexture;
opaque: boolean;
end;
th_createvoxelparams_p = ^th_createvoxelparams_t;
procedure DT_CreateVoxelFacesFromModelMT(const idx, numidxs: integer;
const mVertCount, mFaceCount: integer;
const mVert: fvec5_pa; const mFace: ivec3_pa; const scale: single;
const vox: voxelbuffer_p; const voxsize: integer; const tex: TTexture;
const opaque: boolean);
var
tri: meshtriangle_t;
i: integer;
ofs: integer;
procedure _make_vertex(const r, g: integer);
begin
tri[g].x := mVert[r].x * scale + ofs;
tri[g].y := voxsize - 1.0 - mVert[r].y * scale;
tri[g].z := voxsize - 1.0 - mVert[r].z * scale - ofs;
tri[g].u := mVert[r].u;
tri[g].v := mVert[r].v;
end;
begin
ofs := voxsize div 2;
for i := 0 to mFaceCount - 1 do
if i mod numidxs = idx then
begin
_make_vertex(mFace[i].x, 0);
_make_vertex(mFace[i].y, 1);
_make_vertex(mFace[i].z, 2);
DT_VoxelizeTri(@tri, tex, vox, voxsize, opaque);
end;
end;
function DT_CreateVoxelFacesFromModel_thr(p: iterator_p): integer; stdcall;
var
parms: th_createvoxelparams_p;
begin
parms := p.data;
DT_CreateVoxelFacesFromModelMT(p.idx, p.numidxs, parms.mVertCount, parms.mFaceCount,
parms.mVert, parms.mFace, parms.scale, parms.vox, parms.voxsize, parms.tex, parms.opaque);
Result := 0;
end;
procedure DT_CreateVoxelFromModel(const t: model_t; const vox: voxelbuffer_p;
const voxsize: integer; const modeltex: TBitmap; const mt: boolean = False);
var
xmin, xmax, ymin, ymax, zmin, zmax: single;
i: integer;
scale: single;
cvparms: th_createvoxelparams_t;
tex: TTexture;
begin
if t.mVertCount = 0 then
Exit;
xmin := t.mVert[0].x;
xmax := t.mVert[0].x;
ymin := t.mVert[0].y;
ymax := t.mVert[0].y;
zmin := t.mVert[0].z;
zmax := t.mVert[0].z;
for i := 1 to t.mVertCount - 1 do
begin
if xmin > t.mVert[i].x then
xmin := t.mVert[i].x
else if xmax < t.mVert[i].x then
xmax := t.mVert[i].x;
if ymin > t.mVert[i].y then
ymin := t.mVert[i].y
else if ymax < t.mVert[i].y then
ymax := t.mVert[i].y;
if zmin > t.mVert[i].z then
zmin := t.mVert[i].z
else if zmax < t.mVert[i].z then
zmax := t.mVert[i].z;
end;
ZeroMemory(vox, SizeOf(voxelbuffer_t));
scale := abs(xmin);
if abs(xmax) > scale then
scale := abs(xmax);
if abs(ymin) > scale then
scale := abs(ymin);
if abs(ymax) > scale then
scale := abs(ymax);
if abs(zmin) > scale then
scale := abs(zmin);
if abs(zmax) > scale then
scale := abs(zmax);
scale := 2 * scale;
scale := 2 * t.maxcoord;
{ scale := xmax - xmin;
if ymax - ymin > scale then
scale := ymax - ymin;
if zmax - zmin > scale then
scale := zmax - zmin;}
scale := (voxsize - 1) / scale;
tex := TTexture.Create(modeltex);
if mt then
begin
cvparms.mVertCount := t.mVertCount;
cvparms.mFaceCount := t.mFaceCount;
cvparms.mVert := t.mVert;
cvparms.mFace := t.mFace;
cvparms.scale := scale;
cvparms.vox := vox;
cvparms.voxsize := voxsize;
cvparms.tex := tex;
cvparms.opaque := true;
MT_Iterate(@DT_CreateVoxelFacesFromModel_thr, @cvparms);
end
else
DT_CreateVoxelFacesFromModel(t.mVertCount, t.mFaceCount, t.mVert, t.mFace,
scale, vox, voxsize, tex, true);
tex.Free;
end;
end.