-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchunkindex.cpp
executable file
·430 lines (382 loc) · 12.3 KB
/
chunkindex.cpp
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
424
425
426
427
428
429
430
#include "chunkindex.hpp"
#include "chunk.hpp"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <set>
unsigned DivUp(unsigned a, unsigned b) {
return (a-1) / b + 1;
}
ChunkIndex::ChunkIndex(unsigned _xlen, unsigned _ylen, unsigned _zlen) {
x_len = _xlen; y_len = _ylen; z_len = _zlen;
}
ChunkGrid::ChunkGrid(unsigned _xlen, unsigned _ylen, unsigned _zlen)
: ChunkIndex(_xlen, _ylen, _zlen) {
Init(_xlen, _ylen, _zlen);
}
void ChunkGrid::Render(
const glm::vec3& pos, const glm::vec3& scale,
const glm::mat3& orientation, const glm::vec3& anchor) {
glm::mat4 M(orientation);
M = glm::scale(M, scale);
M = glm::translate(M, glm::inverse(orientation) * pos / scale);
M = glm::translate(M, -anchor);
for (int xx=0; xx < xdim; xx++) {
for (int yy=0; yy < ydim; yy++) {
for (int zz=0; zz < ydim; zz++) {
glm::vec3 tr(float(xx * Chunk::size),
float(yy * Chunk::size),
float(zz * Chunk::size));
glm::mat4 M_chunk = glm::translate(M, tr);
int ix = IX(xx, yy, zz);
Chunk* chk = chunks[ix];
if (chk->is_dirty) {
Chunk* neighs[26] = { NULL };
GetNeighbors(chk, neighs);
chk->BuildBuffers(neighs);
}
chunks[ix]->Render(M_chunk);
}
}
}
}
#ifdef WIN32
extern void GlmMat4ToDirectXMatrix(DirectX::XMMATRIX* out, const glm::mat4& m);
void ChunkGrid::Render_D3D11(
const glm::vec3& pos, const glm::vec3& scale,
const glm::mat3& orientation, const glm::vec3& anchor) {
glm::vec3 pos11 = pos;
pos11.z *= -1;
glm::mat3 orientation1 = orientation;
//orientation1[2][0] *= -1;
//orientation1[2][1] *= -1;
//orientation1[2][2] *= -1;
glm::mat4 M(orientation1);
/*
M = glm::scale(M, scale);
M = glm::translate(M, t);
glm::vec3 anchor1 = anchor; anchor1.z = -anchor1.z;
M = glm::translate(M, anchor1);
*/
const float eps = 0.01f;
M = glm::scale(M, scale + glm::vec3(eps, eps, eps));
glm::vec3 t = glm::inverse(orientation1) * pos11 / scale;
M = glm::translate(M, t);
glm::vec3 anchor1 = anchor; anchor1.z = -anchor1.z;
M = glm::translate(M, -anchor1);
for (int xx = 0; xx < xdim; xx++) {
for (int yy = 0; yy < ydim; yy++) {
for (int zz = 0; zz < zdim; zz++) {
glm::vec3 tr(float(xx * Chunk::size),
float(yy * Chunk::size),
float(zz * Chunk::size) * -1);
glm::mat4 M_chunk = glm::translate(M, tr);
int ix = IX(xx, yy, zz);
Chunk* chk = chunks[ix];
if (chk->is_dirty) {
Chunk* neighs[26] = { NULL };
GetNeighbors(chk, neighs);
chk->BuildBuffers(neighs);
}
DirectX::XMMATRIX M1;
GlmMat4ToDirectXMatrix(&M1, M_chunk);
chunks[ix]->Render_D3D11(M1);
}
}
}
}
void ChunkGrid::RecordRenderCommand_D3D12(
ChunkPass* chunk_pass,
const glm::vec3& pos,
const glm::vec3& scale,
const glm::mat3& orientation,
const glm::vec3& anchor,
const DirectX::XMMATRIX& V,
const DirectX::XMMATRIX& P) {
glm::vec3 pos11 = pos;
pos11.z *= -1;
glm::mat3 orientation1 = orientation;
glm::mat4 M(orientation1);
const float eps = 0.01f;
M = glm::scale(M, scale + glm::vec3(eps, eps, eps));
glm::vec3 t = glm::inverse(orientation1) * pos11 / scale;
M = glm::translate(M, t);
glm::vec3 anchor1 = anchor; anchor1.z = -anchor1.z;
M = glm::translate(M, -anchor1);
for (int xx = 0; xx < xdim; xx++) {
for (int yy = 0; yy < ydim; yy++) {
for (int zz = 0; zz < zdim; zz++) {
glm::vec3 tr(float(xx * Chunk::size),
float(yy * Chunk::size),
float(zz * Chunk::size) * -1);
glm::mat4 M_chunk = glm::translate(M, tr);
int ix = IX(xx, yy, zz);
Chunk* chk = chunks[ix];
if (chk->is_dirty) {
Chunk* neighs[26] = { NULL };
GetNeighbors(chk, neighs);
chk->BuildBuffers(neighs);
}
DirectX::XMMATRIX M1;
GlmMat4ToDirectXMatrix(&M1, M_chunk);
chunks[ix]->RecordRenderCommand_D3D12(chunk_pass, M1, V, P);
}
}
}
}
#endif
Chunk* ChunkGrid::GetChunk(int x, int y, int z, int* local_x, int* local_y, int* local_z) {
int xx = x / Chunk::size,
yy = y / Chunk::size,
zz = z / Chunk::size;
if (local_x)
*local_x = x % Chunk::size;
if (local_y)
*local_y = y % Chunk::size;
if (local_z)
*local_z = z % Chunk::size;
if (xx < xdim && yy < ydim && zz < zdim && xx >= 0 && yy >= 0 && zz >= 0) {
Chunk* chk = chunks.at(IX(xx,yy,zz));
return chk;
} else return NULL;
}
void ChunkGrid::SetVoxel(unsigned x, unsigned y, unsigned z, int v) {
int lx, ly, lz;
Chunk* chk = GetChunk(x, y, z, &lx, &ly, &lz);
if (chk)
chk->SetVoxel(lx, ly, lz, v);
}
void ChunkGrid::SetVoxel(const glm::vec3& p, int vox) {
int xx = int(p.x / Chunk::size),
yy = int(p.y / Chunk::size),
zz = int(p.z / Chunk::size),
local_x = int(p.x) % Chunk::size,
local_y = int(p.y) % Chunk::size,
local_z = int(p.z) % Chunk::size;
int ix = IX(xx, yy, zz);
if (ix >= 0 && ix < chunks.size()) {
Chunk* chk = chunks.at(ix);
chk->SetVoxel(local_x, local_y, local_z, vox);
Chunk* neighs[26] = { NULL };
GetNeighbors(chk, neighs);
chk->BuildBuffers(neighs);
}
}
int ChunkGrid::GetVoxel(unsigned x, unsigned y, unsigned z) {
unsigned xx = x / Chunk::size,
yy = y / Chunk::size,
zz = z / Chunk::size,
local_x = x % Chunk::size,
local_y = y % Chunk::size,
local_z = z % Chunk::size;
if (xx < xdim && yy < ydim && zz < zdim) {
Chunk* chk = chunks.at(IX(xx,yy,zz));
return chk->GetVoxel(local_x, local_y, local_z);
} else return 0;
}
bool ChunkGrid::GetNeighbors(Chunk* chunk, Chunk* neighs[26]) {
int xx, yy, zz;
FromIX(chunk->idx, xx, yy, zz);
const int xyzs[] = {
IX(xx + 1, yy, zz), // [0]
IX(xx - 1, yy, zz), // [1]
IX(xx, yy + 1, zz), // [2]
IX(xx, yy - 1, zz), // [3]
IX(xx, yy, zz + 1), // [4]
IX(xx, yy, zz - 1), // [5]
IX(xx + 1, yy + 1, zz + 1), // [6]
IX(xx + 1, yy + 1, zz), // [7]
IX(xx + 1, yy + 1, zz - 1), // [8]
IX(xx + 1, yy, zz + 1), // [9]
IX(xx + 1, yy, zz - 1), // [10]
IX(xx + 1, yy - 1, zz + 1), // [11]
IX(xx + 1, yy - 1, zz), // [12]
IX(xx + 1, yy - 1, zz - 1), // [13]
IX(xx, yy + 1, zz + 1), // [14]
IX(xx, yy + 1, zz - 1), // [15]
IX(xx, yy - 1, zz + 1), // [16]
IX(xx, yy - 1, zz - 1), // [17]
IX(xx - 1, yy + 1, zz + 1), // [18]
IX(xx - 1, yy + 1, zz), // [19]
IX(xx - 1, yy + 1, zz - 1), // [20]
IX(xx - 1, yy, zz + 1), // [21]
IX(xx - 1, yy, zz - 1), // [22]
IX(xx - 1, yy - 1, zz + 1), // [23]
IX(xx - 1, yy - 1, zz), // [24]
IX(xx - 1, yy - 1, zz - 1), // [25]
};
const unsigned xyzdim = xdim * ydim * zdim;
for (int i = 0; i < 26; i++) {
const unsigned xyz = xyzs[i];
if (xyz < xyzdim) { neighs[i] = chunks[xyz]; }
}
return true;
}
void ChunkGrid::Init(unsigned _xlen, unsigned _ylen, unsigned _zlen) {
x_len = _xlen; y_len = _ylen; z_len = _zlen;
for (Chunk* c : chunks) {
delete c;
}
chunks.clear();
xdim = DivUp(_xlen, Chunk::size);
ydim = DivUp(_ylen, Chunk::size);
zdim = DivUp(_zlen, Chunk::size);
unsigned xyzdim = xdim * ydim * zdim;
chunks.resize(xyzdim);
for (unsigned i=0; i<xyzdim; i++) {
chunks[i] = new Chunk();
chunks[i]->idx = i;
}
}
ChunkGrid::ChunkGrid(const char* vox_fn) {
FILE* f;
fopen_s(&f, vox_fn, "rb");
long long file_size;
fseek(f, 0, SEEK_END);
file_size = ftell(f);
fseek(f, 0, SEEK_SET);
char magic[4];
assert (fread(magic, sizeof(char), 4, f) == 4);
assert (magic[0] == 'V');
assert (magic[1] == 'O');
assert (magic[2] == 'X');
assert (magic[3] == ' ');
int ver;
assert (fread(&ver, sizeof(int), 1, f) == 1);
bool done = false; // For Ver >= 150 read voxels discard everything else
int curr_size[3]; // X Y Z
while (ftell(f) < file_size && done == false) {
char chunk[5];
assert (fread(chunk, sizeof(char), 4, f) == 4);
chunk[4] = 0x00;
int size_content, size_children;
assert (fread(&size_content, sizeof(int), 1, f) == 1);
assert (fread(&size_children, sizeof(int), 1, f) == 1);
if (!strcmp(chunk, "MAIN")) {
continue; // Read next chunk
} else if (!strcmp(chunk, "PACK")) {
int n;
assert (fread(&n, sizeof(int), 1, f) == 1);
} else if (!strcmp(chunk, "SIZE")) {
assert (fread(curr_size, sizeof(int), 3, f) == 3);
} else if (!strcmp(chunk, "XYZI")) {
unsigned num_voxels;
assert (fread(&num_voxels, sizeof(int), 1, f) == 1);
printf("File: %s, version=%d, size=%ld B, %u voxels, %d x %d x %d\n",
vox_fn, ver, file_size,
num_voxels, curr_size[0], curr_size[1], curr_size[2]);
// Y and Z are swapped
Init(curr_size[0], curr_size[2], curr_size[1]);
int* xyzi = new int[num_voxels];
assert (fread(xyzi, sizeof(int), num_voxels, f) == num_voxels);
for (unsigned i=0; i<num_voxels; i++) {
int tmp = xyzi[i],
val = ((tmp & 0xFF000000) >> 24),
y = (tmp & 0x00FF0000) >> 16,
z = curr_size[1] - ((tmp & 0x0000FF00) >> 8),
x = tmp & 0xFF;
SetVoxel(x, y, z, val);
}
delete xyzi;
done = true;
} else if (!strcmp(chunk, "RGBA")) {
printf("RGBA Palette, skipped\n");
assert (0 == fseek(f, 256 * sizeof(int), SEEK_CUR));
} else if (!strcmp(chunk, "MATT")) {
int mat_id, mat_type, property;
float weight;
assert (fread(&mat_id, sizeof(int), 1, f) == 1);
assert (fread(&mat_type, sizeof(int), 1, f) == 1);
assert (fread(&weight, sizeof(float), 1, f) == 1);
assert (fread(&property, sizeof(int), 1, f) == 1);
int p = property; unsigned nbits = 0;
for (int i=0; i<8; i++) {
if (p & 1) nbits ++;
p >>= 1;
}
float property_value[8];
assert (fread(property_value, sizeof(int), nbits, f) == nbits);
}
}
fclose(f);
}
bool ChunkGrid::IntersectPoint(const glm::vec3& p) {
return false;
}
ChunkGrid::ChunkGrid(const ChunkGrid& other) {
chunks.resize(other.chunks.size());
for (unsigned i=0; i<chunks.size(); i++) {
chunks[i] = new Chunk(*(other.chunks.at(i)));
}
xdim = other.xdim; ydim = other.ydim; zdim = other.zdim;
x_len = other.x_len; y_len = other.y_len; z_len = other.z_len;
}
void ChunkGrid::Fill(int vox) {
std::set<Chunk*> dirty;
for (unsigned x=0; x<x_len; x++) {
int xx = int(x / Chunk::size);
for (unsigned y=0; y<y_len; y++) {
int yy = int(y / Chunk::size);
for (unsigned z=0; z<z_len; z++) {
int zz = int(z / Chunk::size);
int local_x = x % Chunk::size,
local_y = y % Chunk::size,
local_z = z % Chunk::size;
int ix = IX(xx, yy, zz);
if (ix >= 0 && ix < chunks.size()) {
Chunk* chk = chunks.at(ix);
chk->SetVoxel(local_x, local_y, local_z, vox);
dirty.insert(chk);
}
}
}
}
for (Chunk* c : dirty) {
Chunk* dummy[26] = { NULL };
c->BuildBuffers(dummy);
}
}
void ChunkGrid::SetVoxelSphere(const glm::vec3& p, float radius, int v) {
std::set<Chunk*> touched;
int r = int(radius) + 1;
for (int dx = -r; dx <= r; dx ++) {
for (int dy = -r; dy <= r; dy ++) {
for (int dz = -r; dz <= r; dz ++) {
const float dsq = dx*dx + dy*dy + dz*dz;
if (dsq <= radius * radius) {
int lx, ly, lz;
Chunk* chk = GetChunk(int(p.x+dx), int(p.y+dy), int(p.z+dz),
&lx, &ly, &lz);
touched.insert(chk);
chk->SetVoxel(lx, ly, lz, v);
}
} } }
for (Chunk* c : touched) {
Chunk* dummy[26] = { NULL };
c->BuildBuffers(dummy);
}
}
void ChunkGrid::FromIX(int ix, int& x, int& y, int& z) {
x = ix / (ydim * zdim);
ix -= x * ydim * zdim;
y = ix / zdim;
z = ix % zdim;
}
bool AABB::IntersectRay(const glm::vec3& o, const glm::vec3& d) {
glm::vec3 inv_dir = 1.0f / d;
glm::vec3 t0 = (lb - o) * inv_dir;
glm::vec3 t1 = (ub - o) * inv_dir;
if (inv_dir.x < 0) {
std::swap(t0.x, t1.x);
}
if (inv_dir.y < 0) {
std::swap(t0.y, t1.y);
}
if (inv_dir.z < 0) {
std::swap(t0.z, t1.z);
}
float t00 = std::max(t0.z, std::max(t0.y, t0.x));
float t11 = std::min(t1.z, std::min(t1.y, t1.x));
if (0 <= t11 && t00 <= t11) return true;
else return false;
}