-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaze2mesh.cpp
More file actions
349 lines (276 loc) · 7.83 KB
/
Copy pathmaze2mesh.cpp
File metadata and controls
349 lines (276 loc) · 7.83 KB
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
/*
maze2mesh -- Generate mesh from 2D cartesian ASCII description.
Copyright (c) 2025, Eddy Jansson. Licensed under The MIT License.
See https://github.com/eloj/maze2mesh
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <vector>
#include <limits>
#include <format>
#include "meshoptimizer.h"
const float f_min = std::numeric_limits<float>::lowest();
const float f_max = std::numeric_limits<float>::max();
struct Vertex {
float x,y,z;
};
using VertexArray = std::vector<Vertex>;
using IndexBuffer = std::vector<unsigned int>;
using BBox = Vertex[2];
struct Mesh {
Mesh() : bbox({ f_max, f_max, f_max }, { f_min, f_min, f_min }) { }
void optimize(void);
std::string name;
VertexArray vertices;
IndexBuffer indices;
BBox bbox;
};
void Mesh::optimize(void) {
size_t index_count = indices.size();
size_t vertex_count = vertices.size();
printf("Optimizing %s: %zu vertices -> ", name.c_str(), vertex_count);
IndexBuffer remap(vertex_count);
size_t opt_vertex_count = meshopt_generateVertexRemap(&remap[0], &indices[0], index_count, &vertices[0], vertex_count, sizeof(Vertex));
VertexArray opt_vertices(opt_vertex_count);
IndexBuffer opt_indices(index_count);
meshopt_remapIndexBuffer(&opt_indices[0], &indices[0], index_count, &remap[0]);
meshopt_remapVertexBuffer(&opt_vertices[0], &vertices[0], vertex_count, sizeof(Vertex), &remap[0]);
vertices = std::move(opt_vertices);
indices = std::move(opt_indices);
printf("%zu vertices.\n", opt_vertex_count);
}
struct Maze {
int w;
int h;
std::vector<unsigned char> data;
Mesh maze;
Mesh houses;
Mesh floor;
Mesh ceiling;
};
template<>
struct std::formatter<Vertex> {
constexpr auto parse(std::format_parse_context& ctx) {
return ctx.begin();
}
auto format(const Vertex& v, std::format_context& ctx) const {
return std::format_to(ctx.out(), "{},{},{}", v.x, v.y, v.z);
}
};
template<>
struct std::formatter<BBox> {
constexpr auto parse(std::format_parse_context& ctx) {
return ctx.begin();
}
auto format(const BBox& bbox, std::format_context& ctx) const {
return std::format_to(ctx.out(), "{{ {{ {} }}, {{ {} }} }}", bbox[0], bbox[1]);
}
};
bool load_maze(const char *filename, Maze& map) {
FILE *f = fopen(filename, "rb");
if (!f) {
return false;
}
char *line = NULL;
size_t line_size = 0;
ssize_t nread;
int max_w = 0;
int max_h = 0;
// First, just figure out the dimensions.
while ((nread = getline(&line, &line_size, f)) != -1) {
if (!line || !*line || *line == ';') {
continue;
}
int len = (int)strlen(line) - 1;
if (len > max_w) {
max_w = len;
}
++max_h;
}
rewind(f);
map.w = max_w;
map.h = max_h;
map.data.assign(max_w * max_h, 0);
int idx = 0;
while ((nread = getline(&line, &line_size, f)) != -1) {
if (!line || !*line || *line == ';') {
continue;
}
size_t len = strlen(line) - 1;
assert((int)len <= max_w);
assert((int)len + idx <= (int)map.data.size());
memcpy(&map.data[idx], line, len);
idx += map.w;
}
free(line);
fclose(f);
return true;
}
void write_mesh(FILE *f, const Mesh& mesh, int& total_vertex_count) {
if (mesh.vertices.size() == 0) {
return;
}
fprintf(f, "o %s\n", mesh.name.c_str());
for (const Vertex& v : mesh.vertices) {
fprintf(f, "v %f %f %f\n", v.x, v.y, v.z);
}
fprintf(f, "s 0\n");
for (size_t i = 0 ; i < mesh.indices.size() ; i += 3) {
fprintf(f, "f %d %d %d\n", 1 + total_vertex_count + mesh.indices[i + 0], 1 + total_vertex_count + mesh.indices[i + 1], 1 + total_vertex_count + mesh.indices[i + 2]);
}
total_vertex_count += mesh.vertices.size();
}
bool write_map_obj(const char *filename, Maze& map) {
FILE *fout = fopen(filename, "w");
if (!fout) {
return false;
}
fprintf(fout, "# maze2mesh -- https://github.com/eloj/maze2mesh\n");
int total_vertex_count = 0;
write_mesh(fout, map.maze, total_vertex_count);
write_mesh(fout, map.houses, total_vertex_count);
write_mesh(fout, map.floor, total_vertex_count);
write_mesh(fout, map.ceiling, total_vertex_count);
fclose(fout);
printf("Final vertex count: %d\n", total_vertex_count);
return true;
}
void add_bbox_plane(Mesh &mesh, const BBox& bbox, float ypos) {
const int scale = 1;
int base_vrt = mesh.vertices.size();
Vertex rectverts[] = {
{ bbox[1].x, ypos, bbox[1].z },
{ bbox[1].x, ypos, bbox[0].z },
{ bbox[0].x, ypos, bbox[0].z },
{ bbox[0].x, ypos, bbox[1].z }
};
int rectidx[] = { 0, 1, 3, 1, 2, 3 };
for (Vertex& v : rectverts) {
v.x *= scale;
v.y *= scale;
v.z *= scale;
mesh.vertices.push_back(v);
}
for (int i : rectidx) {
mesh.indices.push_back(base_vrt + i);
}
}
void add_box_at(Maze& map, int x, int y, Mesh& mesh) {
const int scale = 1;
int base_vrt = mesh.vertices.size();
Vertex boxverts[] = {
{ 1.0, 1.0, -1.0 },
{ 1.0, 0.0, -1.0 },
{ 1.0, 1.0, 0.0 },
{ 1.0, 0.0, 0.0 },
{ 0.0, 1.0, -1.0 },
{ 0.0, 0.0, -1.0 },
{ 0.0, 1.0, 0.0 },
{ 0.0, 0.0, 0.0 }
};
int boxind[] = {
4, 2, 0, 2, 7, 3,
6, 5, 7, 1, 7, 5,
0, 3, 1, 4, 1, 5,
4, 6, 2, 2, 6, 7,
6, 4, 5, 1, 3, 7,
0, 2, 3, 4, 0, 1
};
for (Vertex& v : boxverts) {
v.x *= scale;
v.y *= scale;
v.z *= scale;
v.x += (x - map.w/2) * scale;
v.z += (y - map.h/2) * scale;
if (v.x < mesh.bbox[0].x) { mesh.bbox[0].x = v.x; }
if (v.y < mesh.bbox[0].y) { mesh.bbox[0].y = v.y; }
if (v.z < mesh.bbox[0].z) { mesh.bbox[0].z = v.z; }
if (v.x > mesh.bbox[1].x) { mesh.bbox[1].x = v.x; }
if (v.y > mesh.bbox[1].y) { mesh.bbox[1].y = v.y; }
if (v.z > mesh.bbox[1].z) { mesh.bbox[1].z = v.z; }
mesh.vertices.push_back(v);
}
for (int i : boxind) {
mesh.indices.push_back(base_vrt + i);
}
}
int main(int argc, char *argv[]) {
const char *filename = argc > 1 ? argv[1] : "data/bt1skarabrae.txt";
bool do_write_tilemap = true;
bool do_zero_unknown_tiles = false;
bool do_meshopt = true;
bool do_floor = true;
bool do_ceil = false;
Maze map;
if (!load_maze(filename, map)) {
fprintf(stderr, "Error loading map '%s': %s\n", filename, strerror(errno));
return EXIT_FAILURE;
}
printf("Loaded %dx%d map '%s'\n", map.w, map.h, filename);
map.maze.name = "maze";
map.houses.name = "houses";
for (int j = 0 ; j < map.h ; ++j) {
for (int i = 0 ; i < map.w ; ++i) {
int idx = j * map.h + i;
switch (map.data[idx]) {
case '*':
add_box_at(map, i, j, map.maze);
printf("#");
break;
case ' ':
printf(" ");
break;
default:
if ((map.data[idx] >= 'A') && (map.data[idx] <= 'Z')) {
add_box_at(map, i, j, map.houses);
printf("%c", map.data[idx]);
} else if (do_zero_unknown_tiles) {
map.data[idx] = 0;
printf("?");
} else {
printf("%c", map.data[idx]);
}
}
}
printf("\n");
}
printf("%s", std::format("Maze bounding box = {}\n", map.maze.bbox).c_str());
// printf(std::format("%f", "House bounding box = {}\n", map.houses.bbox).c_str());
if (do_floor) {
printf("Adding floor rectangle.\n");
map.floor.name = "floor";
add_bbox_plane(map.floor, map.maze.bbox, map.maze.bbox[0].y);
}
if (do_ceil) {
printf("Adding ceiling rectangle.\n");
map.ceiling.name = "ceiling";
add_bbox_plane(map.ceiling, map.maze.bbox, map.maze.bbox[1].y);
}
if (do_meshopt) {
map.maze.optimize();
map.houses.optimize();
}
if (do_write_tilemap) {
const char *outtilemap = "maze1.tilemap.bin";
FILE *f = fopen(outtilemap, "wb");
if (f) {
fwrite(&map.w, sizeof(map.w), 1, f);
fwrite(&map.h, sizeof(map.h), 1, f);
fwrite(&map.data[0], map.data.size(), 1, f);
fclose(f);
printf("Wrote tilemap data to '%s'\n", outtilemap);
} else {
fprintf(stderr, "Error writing tilemap '%s': %s\n", outtilemap, strerror(errno));
return EXIT_FAILURE;
}
}
const char *outfile = "maze1.obj";
if (!write_map_obj(outfile, map)) {
fprintf(stderr, "Error writing mesh '%s': %s\n", outfile, strerror(errno));
return EXIT_FAILURE;
}
printf("Wrote mesh to '%s'\n", outfile);
return EXIT_SUCCESS;
}