-
Notifications
You must be signed in to change notification settings - Fork 3
/
HermiteFieldToMesh.cpp
1954 lines (1731 loc) · 57.1 KB
/
HermiteFieldToMesh.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Geometry/HermiteField.h"
#include "Geometry/DebugDraw.h"
#include "Core/Defer.h"
#include "OS/ThreadLocal.h"
// Two other axes of a given one.
static const Vec2i OTHER_AXES_TABLE[] = {
{1, 2},
{0, 2},
{0, 1},
};
static const Vec4i VINDEX_EDGES_TABLE_E[] = {
Vec4i(3, 2, 1, 0),
Vec4i(7, 6, 5, 4),
Vec4i(11, 10, 9, 8),
};
static const Vec4i VINDEX_EDGES_TABLE_F[3][3] = {
{Vec4i(-1), Vec4i(2,3,1,0), Vec4i(1,3,2,0)},
{Vec4i(6,7,5,4), Vec4i(-1), Vec4i(5,7,6,4)},
{Vec4i(10,11,9,8), Vec4i(9,11,10,8), Vec4i(-1)},
};
// Bit array of non-manifold cases: 1 - non-manifold, 0 - manifold
/*
const uint8_t NON_MANIFOLD_CASES[32] = {
64, 2, 84, 87, 114, 51, 80, 115, 78, 15, 68, 79, 255, 255, 64, 127, 254, 2,
255, 255, 242, 34, 240, 114, 206, 10, 204, 78, 234, 42, 64, 2
};
*/
// excludes 3a and 6a cases
/*
const uint8_t NON_MANIFOLD_CASES[32] = {
64, 2, 84, 87, 114, 51, 80, 19, 78, 15, 68, 7, 127, 63, 0, 67, 254, 2, 255,
87, 114, 2, 80, 32, 78, 2, 68, 8, 130, 2, 0, 0
};
static inline bool IsNonManifoldConfig(uint8_t c)
{
const uint8_t n = c / 8;
const uint8_t b = 1 << (c % 8);
return (NON_MANIFOLD_CASES[n] & b) != 0;
}
*/
const uint32_t VCONFIGS[256] = {
16777216, 16777216, 16777216, 16777216, 16777216, 16777216, 33817601, 16777216,
16777216, 33620225, 16777216, 16777216, 16777216, 16777216, 16777216, 16777216,
16777216, 16777216, 33624080, 16777216, 33624080, 16777216, 52498968, 16777216,
37749764, 37749764, 33624080, 16777216, 33624080, 16777216, 39059713, 16777216,
16777216, 33832976, 16777216, 16777216, 33832976, 33832976, 34603268, 16777216,
33832976, 55084068, 16777216, 16777216, 33832976, 38863873, 16777216, 16777216,
16777216, 16777216, 16777216, 16777216, 34603268, 16777216, 33641473, 16777216,
37749764, 33837313, 16777216, 16777216, 33902592, 16777216, 16777216, 16777216,
16777216, 33620225, 33817601, 34607168, 16777216, 16777216, 33817601, 16777216,
37749764, 52502913, 34607168, 38076676, 16777216, 16777216, 16777216, 16777216,
16777216, 16777216, 33817601, 16777216, 16777216, 16777216, 33620308, 16777216,
37749764, 34603345, 38010885, 16777216, 16777216, 16777216, 16777216, 16777216,
34607168, 51515970, 34607168, 33637648, 33832976, 33624133, 33571857, 16777216,
59000856, 81304684, 37765141, 37830932, 37754176, 37819457, 16777216, 16777216,
16777216, 16777216, 16777216, 16777216, 16777216, 16777216, 16777216, 16777216,
34947136, 34881857, 16777216, 16777216, 16777216, 16777216, 37765184, 16777216,
16777216, 37765184, 37765184, 37765184, 34603268, 37765184, 55068738, 34931716,
16777216, 33620225, 16777216, 16777216, 16777216, 16777216, 16777216, 16777216,
37765184, 37765184, 59016321, 33821968, 51519780, 34607125, 80337106, 34870292,
33624080, 33571908, 33833029, 16777216, 34620736, 16777216, 34881857, 16777216,
16777216, 33620225, 16777216, 16777216, 34603268, 38010960, 37749841, 16777216,
16777216, 33817684, 16777216, 16777216, 16777216, 16777216, 16777216, 16777216,
16777216, 16777216, 16777216, 16777216, 38080576, 16777216, 37819457, 16777216,
16777216, 16777216, 16777216, 16777216, 16777216, 33817601, 16777216, 16777216,
16777216, 33620225, 33817601, 38817792, 16777216, 16777216, 37769476, 16777216,
16777216, 34624516, 16777216, 16777216, 16777216, 16777216, 16777216, 16777216,
16777216, 16777216, 38879248, 16777216, 16777216, 16777216, 37830932, 16777216,
16777216, 16777216, 16777216, 33832976, 16777216, 16777216, 16777216, 16777216,
16777216, 39063568, 16777216, 16777216, 16777216, 16777216, 16777216, 37749764,
16777216, 34870292, 16777216, 16777216, 16777216, 16777216, 16777216, 16777216,
16777216, 16777216, 16777216, 16777216, 16777216, 16777216, 16777216, 16777216,
16777216, 16777216, 16777216, 16777216, 16777216, 16777216, 16777216, 16777216
};
static inline int vconfig_n_vertices(uint32_t vconfig)
{
return vconfig >> 24;
}
static inline int vconfig_vertex_index(uint32_t vconfig, int edge)
{
return (vconfig >> (edge * 2)) & 3;
}
constexpr uint32_t VF_VIRTUAL = 1 << 31;
constexpr uint32_t INDEX_MASK = ~VF_VIRTUAL;
static inline bool is_virtual(uint32_t idx)
{
return (idx & VF_VIRTUAL) != 0;
}
static inline uint32_t index(uint32_t idx)
{
return idx & INDEX_MASK;
}
// For each field a set of closest neighbours is defined along each axis
// on negative direction.
const Vec3i NEIGHBOUR_FIELDS[8] = {
Vec3i(-1, -1, -1),
Vec3i( 0, -1, -1),
Vec3i(-1, 0, -1),
Vec3i( 2, 1, -1),
Vec3i(-1, -1, 0),
Vec3i( 4, -1, 1),
Vec3i(-1, 4, 2),
Vec3i( 6, 5, 3),
};
// A table of edges for each chunk, it points to EDGES_TABLE below. Each chunk
// has 3 edges on the corresponding axes. We also store offsets here, it's from
// EDGES_TABLE.
const struct {
Vec3i edges;
Vec3i offsets[3];
} CHUNK_EDGES[8] = {
{{1, 3, 5}, {{0,1,1}, {1,0,1}, {1,1,0}}},
{{0, 3, 5}, {{0,1,1}, {0,0,1}, {0,1,0}}},
{{1, 2, 5}, {{0,0,1}, {1,0,1}, {1,0,0}}},
{{0, 2, 5}, {{0,0,1}, {0,0,1}, {0,0,0}}},
{{1, 3, 4}, {{0,1,0}, {1,0,0}, {1,1,0}}},
{{0, 3, 4}, {{0,1,0}, {0,0,0}, {0,1,0}}},
{{1, 2, 4}, {{0,0,0}, {1,0,0}, {1,0,0}}},
{{0, 2, 4}, {{0,0,0}, {0,0,0}, {0,0,0}}},
};
// Lists all 6 edges of 8-field group. The chunk specified all 4 chunks which
// share the same edge. Axis is the axis on which the edge lies. And offsets is
// where you can find that edge in a given chunk, same convention as above.
// 0 is 0 and 1 is size-1.
const struct {
Vec4i chunks;
int axis;
Vec3i offsets[4];
} EDGES_TABLE[6] = {
{{1,3,5,7}, 0, {{0,1,1}, {0,0,1}, {0,1,0}, {0,0,0}}}, // +x (0)
{{0,2,4,6}, 0, {{0,1,1}, {0,0,1}, {0,1,0}, {0,0,0}}}, // -x (1)
{{2,3,6,7}, 1, {{1,0,1}, {0,0,1}, {1,0,0}, {0,0,0}}}, // +y (2)
{{0,1,4,5}, 1, {{1,0,1}, {0,0,1}, {1,0,0}, {0,0,0}}}, // -y (3)
{{4,5,6,7}, 2, {{1,1,0}, {0,1,0}, {1,0,0}, {0,0,0}}}, // +z (4)
{{0,1,2,3}, 2, {{1,1,0}, {0,1,0}, {1,0,0}, {0,0,0}}}, // -z (5)
};
// A table of faces for each chunk, it points to FACES_TABLE below. Each chunk
// has 3 faces on the corresponding axes. We also store offsets here, it's from
// FACES_TABLE. Offset is an position on a corresponding axis as usual 0 for 0
// and 1 for size-1. Can be a cube size or voxel size, depending on what you
// want.
const struct {
Vec3i faces;
Vec3i offsets;
} CHUNK_FACES[8] = {
{{0,1,8 }, {1,1,1}},
{{0,2,9 }, {0,1,1}},
{{3,1,10}, {1,0,1}},
{{3,2,11}, {0,0,1}},
{{4,5,8 }, {1,1,0}},
{{4,6,9 }, {0,1,0}},
{{7,5,10}, {1,0,0}},
{{7,6,11}, {0,0,0}},
};
// Lists all 12 faces of 8-field group.
const struct {
Vec2i chunks;
Vec2i axes;
Vec3i offsets[2];
} FACES_TABLE[12] = {
{{0,1}, {1,2}, {{1,0,0}, {0,0,0}}}, // 0
{{0,2}, {0,2}, {{0,1,0}, {0,0,0}}}, // 1
{{1,3}, {0,2}, {{0,1,0}, {0,0,0}}}, // 2
{{2,3}, {1,2}, {{1,0,0}, {0,0,0}}}, // 3
{{4,5}, {1,2}, {{1,0,0}, {0,0,0}}}, // 4
{{4,6}, {0,2}, {{0,1,0}, {0,0,0}}}, // 5
{{5,7}, {0,2}, {{0,1,0}, {0,0,0}}}, // 6
{{6,7}, {1,2}, {{1,0,0}, {0,0,0}}}, // 7
{{0,4}, {0,1}, {{0,0,1}, {0,0,0}}}, // 8
{{1,5}, {0,1}, {{0,0,1}, {0,0,0}}}, // 9
{{2,6}, {0,1}, {{0,0,1}, {0,0,0}}}, // 10
{{3,7}, {0,1}, {{0,0,1}, {0,0,0}}}, // 11
};
struct FieldAccessHelper {
Slice<const int> lods;
int local_largest_lod;
int local_smallest_lod;
int largest_lod;
Vec3i m_csize; // local largest lod's size of a field in cubes
int m_offset; // local largest lod's offset
// Each edge has an authoritative representation, which is a given edge
// with the lowest LOD amongst 4 overlapping edges. Offset is in voxels in
// work area notation.
struct {
int chunk;
int axis;
Vec3i offset;
int lod;
} auth_edges[6];
// Each face has an authoritative representation, which is a given face
// with the lowest LOD amongst 2 overlapping faces. Offset is in voxels in
// work area notation.
struct {
int chunk;
Vec2i axes;
Vec3i offset;
int lod;
} auth_faces[12];
// Defines positions of the cubes (in a work area notation) touching the
// edges. Basically a copy of CHUNK_EDGES table with proper position values.
struct {
Vec3i position[3];
} edge_positions[8];
// Same for faces. Except here we define just a position along a
// corresponding axis of a face.
struct {
Vec3i position;
} face_positions[8];
// Each chunk contains a certain set of faces which are duplicated when
// building neighbour chunks. So, we avoid creating them along one of the
// directions.
Vec3i dup_faces[8];
FieldAccessHelper(Slice<const int> lods, int largest_lod)
{
this->lods = lods;
this->largest_lod = largest_lod;
local_largest_lod = -1;
local_smallest_lod = 999;
for (int lod : lods) {
if (lod > local_largest_lod)
local_largest_lod = lod;
if (lod >= 0 && lod < local_smallest_lod)
local_smallest_lod = lod;
}
m_csize = CHUNK_SIZE / Vec3i(lod_factor(local_largest_lod));
m_offset = offset_for_lod(local_largest_lod, largest_lod);
// setup auth edges
for (int i = 0; i < 6; i++) {
int smallest_lod = 999;
int idx = -1;
for (int j = 0; j < 4; j++) {
const int lod = lods[EDGES_TABLE[i].chunks[j]];
if (lod != -1 && lod < smallest_lod) {
smallest_lod = lod;
idx = j;
}
}
if (idx == -1) {
auth_edges[i].lod = -1;
continue;
}
const int c = EDGES_TABLE[i].chunks[idx];
const Vec3i dcsize = vsize(c) - Vec3i(1);
auth_edges[i].axis = EDGES_TABLE[i].axis;
auth_edges[i].chunk = c;
auth_edges[i].offset = EDGES_TABLE[i].offsets[idx] * dcsize;
auth_edges[i].lod = lods[auth_edges[i].chunk];
}
// setup auth faces
for (int i = 0; i < 12; i++) {
int smallest_lod = 999;
int idx = -1;
for (int j = 0; j < 2; j++) {
const int lod = lods[FACES_TABLE[i].chunks[j]];
if (lod != -1 && lod < smallest_lod) {
smallest_lod = lod;
idx = j;
}
}
if (idx == -1) {
auth_faces[i].lod = -1;
continue;
}
const int c = FACES_TABLE[i].chunks[idx];
const Vec3i dcsize = vsize(c) - Vec3i(1);
auth_faces[i].axes = FACES_TABLE[i].axes;
auth_faces[i].chunk = c;
auth_faces[i].offset = FACES_TABLE[i].offsets[idx] * dcsize;
auth_faces[i].lod = lods[auth_faces[i].chunk];
}
// edge and face positions, dup faces
for (int i = 0; i < 8; i++) {
const Vec3i dcsize = vsize(i) - Vec3i(1);
for (int j = 0; j < 3; j++) {
edge_positions[i].position[j] =
CHUNK_EDGES[i].offsets[j] * dcsize;
}
face_positions[i].position =
CHUNK_FACES[i].offsets * dcsize;
const Vec3i csize = non_virtual_csize(i);
const Vec3i vadd = (dcsize - csize + Vec3i(1)) * rel22(i^7);
dup_faces[i] = Vec3i(-1) + vadd;
}
}
// LOD factor for two LODs in 8-field set
inline int lodf(int lod) const { return lod < local_largest_lod ? 2 : 1; }
// size of a field in cubes for a given lod
inline Vec3i csize(int lod) const { return m_csize * Vec3i(lodf(lod)); }
// The following sizes are based on the largest lod, therefore for smaller
// lods they are larger than the actual need, I do that for simplicity.
// Irrelevant stuff is simply ignored later when generating the actual
// vertices.
// positive size in cubes
inline Vec3i pcsize(int lod) const
{
return (m_csize - Vec3i(m_offset)) * Vec3i(lodf(lod));
}
// positive size in cubes (+ dependencies)
inline Vec3i pdcsize(int lod) const
{
return (m_csize - Vec3i(m_offset - 1)) * Vec3i(lodf(lod));
}
// negative size in cubes
inline Vec3i ncsize(int lod) const
{
return Vec3i(m_offset + 1) * Vec3i(lodf(lod));
}
// negative size in cubes (+ dependencies)
inline Vec3i ndcsize(int lod) const
{
return Vec3i(m_offset + 2) * Vec3i(lodf(lod));
}
// voxel size of a work area of a given chunk, according to largest lod
Vec3i vsize(int chunk) const
{
const int lod = lods[chunk];
const Vec3i pdvsize = pdcsize(lod) + Vec3i(1);
const Vec3i ndvsize = ndcsize(lod) + Vec3i(1);
const Vec3i rel = rel22(chunk);
const Vec3i irel = rel^Vec3i(1);
return ndvsize * irel + pdvsize * rel;
}
// voxel offset to a work area of a given chunk, according to largest lod
Vec3i voffset(int chunk) const
{
const int lod = lods[chunk];
const Vec3i vsize = csize(lod) + Vec3i(1);
const Vec3i ndvsize = ndcsize(lod) + Vec3i(1);
return (vsize - ndvsize) * rel22(chunk^7);
}
// real size of a chunk in cubes (excluding virtual cubes)
Vec3i non_virtual_csize(int chunk) const
{
const int lod = lods[chunk];
const Vec3i csize = CHUNK_SIZE / Vec3i(lod_factor(lod));
const int offset = offset_for_lod(lod, largest_lod);
const Vec3i pcsize = csize - Vec3i(offset);
const Vec3i ncsize = Vec3i(offset + 1);
const Vec3i rel = rel22(chunk);
const Vec3i irel = rel22(chunk^7);
return ncsize * irel + pcsize * rel;
}
};
struct CubeContext {
Vec3i accumulator[4] = {Vec3i(0), Vec3i(0), Vec3i(0), Vec3i(0)};
int accumulated_n[4] = {0, 0, 0, 0};
uint8_t materials[20];
int materials_n = 0;
inline void add_vector(const Vec3i &v, int i = 0)
{
accumulator[i] += v;
accumulated_n[i]++;
}
inline Vec3 average_vector(int i = 0) const
{
const Vec3 v = ToVec3(accumulator[i] / Vec3i(accumulated_n[i])) /
Vec3(HermiteData_FullEdgeF());
return v;
}
inline void add_material(uint8_t m)
{
materials[materials_n++] = m;
}
inline uint8_t average_material()
{
uint8_t m = 0;
int n = 0;
while (materials_n > 0) {
uint8_t cur_m = materials[--materials_n];
int cur_n = 1;
int i = 0;
while (i < materials_n) {
if (materials[i] != cur_m) {
i++;
continue;
}
cur_n++;
std::swap(materials[i], materials[--materials_n]);
}
if (cur_m != 0 && cur_n > n) {
n = cur_n;
m = cur_m;
}
}
return m;
}
};
struct IndexVConfigPair {
uint32_t m_index;
uint32_t m_vconfig;
// basically adds vertex index preserving the virtual flag
uint32_t index(int edge) const
{
const int vindex = vconfig_vertex_index(m_vconfig, edge);
const uint32_t vflag = m_index & VF_VIRTUAL;
return (::index(m_index) + vindex) | vflag;
}
};
struct TemporaryData {
Vector<IndexVConfigPair> idxbuf;
Vector<Vec3> normals;
Vector<Vec3> vertices;
HermiteField fs[8];
Vector<IndexVConfigPair> idxbufs[8];
};
static ThreadLocal<TemporaryData> temporary_data;
static void hermite_rle_fields_to_mesh_same_lod(Vector<V3N3M1_terrain> &vertices,
Vector<uint32_t> &indices, Slice<const HermiteRLEField*> fields,
int lod, int largest_lod, const Vec3 &base)
{
// Size of the chunk in cubes, according to the given LOD
const Vec3i csize = CHUNK_SIZE / Vec3i(lod_factor(lod));
// Size of the chunk in voxels, according to the given LOD
const Vec3i vsize = csize + Vec3i(1);
// Offset to align chunks of all the lods at the same position and to
// include the dependencies for smooth normals calculation. For largest lod
// it's 1, for largest-1 it's 2, then 4, and so on.
const int offset = offset_for_lod(lod, largest_lod);
// Positive size in cubes (size of the 7th chunk), according to offset.
const Vec3i pcsize = csize - Vec3i(offset);
// Same as above, but also including dependencies.
const Vec3i pdcsize = pcsize + Vec3i(1);
// Negative size in cubes (size of the 0 chunk), according to offset.
// +1 because we also include the glue layer
const Vec3i ncsize = Vec3i(offset + 1);
// Same as above, but also including dependencies.
const Vec3i ndcsize = ncsize + Vec3i(1);
// Offset for negative chunks in voxels to the beginning of the data.
const Vec3i voffset = vsize - (ndcsize + Vec3i(1));
// Total size of the work area in cubes.
const Vec3i dcsize = pdcsize + ndcsize;
// Size of the cube according to given LOD.
const Vec3 cube_size_lod = CUBE_SIZE * Vec3(lod_factor(lod));
// Non-virtual cube boundaries, relative to work area of course
const Vec3i nonv_min(1, 1, 1);
const Vec3i nonv_max = dcsize - Vec3i(2);
// On chunk boundaries we need to know if the neighbour is available, if
// true, then it's ok to generate a stitching face.
struct { bool x; bool y; bool z; } neighbours[8];
for (int i = 0; i < 8; i++) {
neighbours[i].x = NEIGHBOUR_FIELDS[i].x != -1 &&
fields[NEIGHBOUR_FIELDS[i].x] != nullptr;
neighbours[i].y = NEIGHBOUR_FIELDS[i].y != -1 &&
fields[NEIGHBOUR_FIELDS[i].y] != nullptr;
neighbours[i].z = NEIGHBOUR_FIELDS[i].z != -1 &&
fields[NEIGHBOUR_FIELDS[i].z] != nullptr;
}
const int base_vertex = vertices.length();
TemporaryData *tmp = temporary_data.get();
Vector<IndexVConfigPair> &idxbuf = tmp->idxbuf;
idxbuf.resize(dcsize.x * dcsize.y * 2);
Vector<Vec3> &tmp_normals = tmp->normals;
tmp_normals.clear();
Vector<Vec3> &virt_vertices = tmp->vertices;
virt_vertices.resize(1);
auto quad = [&](bool flip, uint32_t ia, uint32_t ib, uint32_t ic, uint32_t id, bool ignore)
{
if (flip)
std::swap(ib, id);
Vec3 a, b, c, d, nop;
Vec3 *na = &nop;
Vec3 *nb = &nop;
Vec3 *nc = &nop;
Vec3 *nd = &nop;
auto i_to_v = [&](uint32_t &ix, Vec3 &x, Vec3 *&nx) {
if (is_virtual(ix)) {
x = virt_vertices[index(ix)];
} else {
x = vertices[base_vertex+index(ix)].position;
nx = &tmp_normals[index(ix)];
}
};
i_to_v(ia, a, na);
i_to_v(ib, b, nb);
i_to_v(ic, c, nc);
i_to_v(id, d, nd);
const Vec3 ab = a - b;
const Vec3 cb = c - b;
const Vec3 n1 = cross(cb, ab);
*na += n1;
*nb += n1;
*nc += n1;
const Vec3 ac = a - c;
const Vec3 dc = d - c;
const Vec3 n2 = cross(dc, ac);
*na += n2;
*nc += n2;
*nd += n2;
if (ignore)
return;
if (!is_virtual(ia) && !is_virtual(ib) && !is_virtual(ic)) {
indices.append(index(ia));
indices.append(index(ib));
indices.append(index(ic));
}
if (!is_virtual(ia) && !is_virtual(ic) && !is_virtual(id)) {
indices.append(index(ia));
indices.append(index(ic));
indices.append(index(id));
}
};
HermiteData hd[8];
auto do_line = [&](const Vec3i coffset,
HermiteRLEIterator its[4], bool continuation,
int length, int fi)
{
const Vec3i rel = rel22(fi);
// offset in cubes to the current field
const Vec3i field_coffset = rel * csize;
for (int i = 0; i < length; i++) {
if (continuation || i != 0) {
hd[0] = hd[1];
hd[2] = hd[3];
hd[4] = hd[5];
hd[6] = hd[7];
} else {
hd[0] = *its[0]++;
hd[2] = *its[1]++;
hd[4] = *its[2]++;
hd[6] = *its[3]++;
}
hd[1] = *its[0]++;
hd[3] = *its[1]++;
hd[5] = *its[2]++;
hd[7] = *its[3]++;
const uint8_t config =
((hd[0].material != 0) << 0) |
((hd[1].material != 0) << 1) |
((hd[2].material != 0) << 2) |
((hd[3].material != 0) << 3) |
((hd[4].material != 0) << 4) |
((hd[5].material != 0) << 5) |
((hd[6].material != 0) << 6) |
((hd[7].material != 0) << 7);
if (config == 0 || config == 255) {
int tmp;
int canskip = its[0].can_skip();
if (canskip == 0) continue;
tmp = its[1].can_skip();
if (tmp == 0) continue;
if (tmp < canskip) canskip = tmp;
tmp = its[2].can_skip();
if (tmp == 0) continue;
if (tmp < canskip) canskip = tmp;
tmp = its[3].can_skip();
if (tmp == 0) continue;
if (tmp < canskip) canskip = tmp;
int willskip = min(canskip, length-1-i);
if (willskip <= 0) continue;
i += willskip;
its[0].skip(willskip);
its[1].skip(willskip);
its[2].skip(willskip);
its[3].skip(willskip);
continue;
}
const uint32_t vconfig = VCONFIGS[config];
CubeContext cc;
const uint8_t M = HermiteData_FullEdge();
auto do_edge = [&](int edge_n, int axis, const Vec2i &value,
const HermiteData &hd0, const HermiteData &hd1)
{
const Vec2i o_axes = OTHER_AXES_TABLE[axis];
if (!edge_has_intersection(hd0, hd1))
return;
Vec3i v(0);
v[axis] = M - hd1.edges[axis];
v[o_axes[0]] = value[0];
v[o_axes[1]] = value[1];
cc.add_vector(v, vconfig_vertex_index(vconfig, edge_n));
};
do_edge(0, 0, {0, 0}, hd[0], hd[1]);
do_edge(1, 0, {M, 0}, hd[2], hd[3]);
do_edge(2, 0, {0, M}, hd[4], hd[5]);
do_edge(3, 0, {M, M}, hd[6], hd[7]);
do_edge(4, 1, {0, 0}, hd[0], hd[2]);
do_edge(5, 1, {M, 0}, hd[1], hd[3]);
do_edge(6, 1, {0, M}, hd[4], hd[6]);
do_edge(7, 1, {M, M}, hd[5], hd[7]);
do_edge(8, 2, {0, 0}, hd[0], hd[4]);
do_edge(9, 2, {M, 0}, hd[1], hd[5]);
do_edge(10, 2, {0, M}, hd[2], hd[6]);
do_edge(11, 2, {M, M}, hd[3], hd[7]);
// position of the cube relative to fields
const Vec3i p = coffset + Vec3i_X(i);
// position of the cube relative to work area
const Vec3i wp = p - voffset;
uint8_t average_material = 0;
const bool is_virtual = !(nonv_min <= wp && wp <= nonv_max);
const int idxoffset = offset_3d_slab(wp, dcsize);
IndexVConfigPair &pair = idxbuf[idxoffset];
pair.m_vconfig = vconfig;
if (!is_virtual) {
for (int i = 0; i < 8; i++)
cc.add_material(hd[i].material);
average_material = cc.average_material() - 1;
pair.m_index = vertices.length() - base_vertex;
} else {
pair.m_index = virt_vertices.length() | VF_VIRTUAL;
}
for (int j = 0, n = vconfig_n_vertices(vconfig); j < n; j++) {
// vertex position is relative to 7th (1;1;1) field
const Vec3 v = cube_size_lod *
(ToVec3(p-csize) + cc.average_vector(j));
if (!is_virtual) {
vertices.append({base+v, 0, average_material});
tmp_normals.append(Vec3(0));
} else {
virt_vertices.append(base+v);
}
}
// position of the cube relative to the current field
const Vec3i lp = p - field_coffset;
const bool flip = hd[0].material != 0;
if (
(lp.y > 1 || neighbours[fi].y) &&
(lp.z > 1 || neighbours[fi].z) &&
wp.y > 0 &&
wp.z > 0 &&
edge_has_intersection(hd[0], hd[1])
) {
quad(flip,
idxbuf[offset_3d_slab(Vec3i(wp.x, wp.y, wp.z), dcsize)].index(0),
idxbuf[offset_3d_slab(Vec3i(wp.x, wp.y, wp.z-1), dcsize)].index(2),
idxbuf[offset_3d_slab(Vec3i(wp.x, wp.y-1, wp.z-1), dcsize)].index(3),
idxbuf[offset_3d_slab(Vec3i(wp.x, wp.y-1, wp.z), dcsize)].index(1),
1 == wp.x
);
}
if (
(lp.x > 1 || neighbours[fi].x) &&
(lp.z > 1 || neighbours[fi].z) &&
wp.x > 0 &&
wp.z > 0 &&
edge_has_intersection(hd[0], hd[2])
) {
quad(flip,
idxbuf[offset_3d_slab(Vec3i(wp.x, wp.y, wp.z), dcsize)].index(4),
idxbuf[offset_3d_slab(Vec3i(wp.x-1, wp.y, wp.z), dcsize)].index(5),
idxbuf[offset_3d_slab(Vec3i(wp.x-1, wp.y, wp.z-1), dcsize)].index(7),
idxbuf[offset_3d_slab(Vec3i(wp.x, wp.y, wp.z-1), dcsize)].index(6),
1 == wp.y
);
}
if (
(lp.x > 1 || neighbours[fi].x) &&
(lp.y > 1 || neighbours[fi].y) &&
wp.x > 0 &&
wp.y > 0 &&
edge_has_intersection(hd[0], hd[4])
) {
quad(flip,
idxbuf[offset_3d_slab(Vec3i(wp.x, wp.y, wp.z), dcsize)].index(8),
idxbuf[offset_3d_slab(Vec3i(wp.x, wp.y-1, wp.z), dcsize)].index(10),
idxbuf[offset_3d_slab(Vec3i(wp.x-1, wp.y-1, wp.z), dcsize)].index(11),
idxbuf[offset_3d_slab(Vec3i(wp.x-1, wp.y, wp.z), dcsize)].index(9),
1 == wp.z
);
}
}
};
// true when the line is a continuation of the previous line
bool continuation = false;
for (int z = 0; z < dcsize.z; z++) {
for (int y = 0; y < dcsize.y; y++) {
Vec3i line_starts[4] = {
voffset + Vec3i(0, y, z),
voffset + Vec3i(0, y+1, z),
voffset + Vec3i(0, y, z+1),
voffset + Vec3i(0, y+1, z+1),
};
// field offsets in [0; 1] range, 1 per each of 4 iterators
Vec3i foffsets[4];
// voxel offsets local to fields in [0; CHUNK_SIZE] range
Vec3i voffsets[4];
for (int i = 0; i < 4; i++) {
for (int j = 1; j < 3; j++) {
foffsets[i][j] = line_starts[i][j] > csize[j] ? 1 : 0;
voffsets[i][j] = line_starts[i][j] > csize[j] ?
line_starts[i][j] - csize[j] : line_starts[i][j];
}
}
continuation = false;
for (int x = 0; x < 2; x++) {
// actual fields where we will take the iterators from
const HermiteRLEField *fs[4];
for (int i = 0; i < 4; i++) {
foffsets[i].x = x;
voffsets[i].x = x ? 0 : voffset.x;
fs[i] = fields[offset_3d(foffsets[i], Vec3i(2))];
}
if (!fs[0] or !fs[1] or !fs[2] or !fs[3])
continue;
// the iterators
HermiteRLEIterator its[4];
const int add = continuation ? 1 : 0; // skip 1 more on continuations
for (int i = 0; i < 4; i++)
its[i] = fs[i]->iterator(voffsets[i] + Vec3i_X(add));
const Vec3i lcdsize = foffsets[0] * pdcsize +
(foffsets[0]^Vec3i(1)) * ndcsize;
const int length = lcdsize.x;
const Vec3i coffset =
foffsets[0] * csize + voffsets[0];
do_line(coffset, its, continuation, length,
offset_3d(foffsets[0], Vec3i(2)));
continuation = true;
}}}
for (int i = base_vertex; i < vertices.length(); i++)
vertices[i].normal = pack_normal(normalize(tmp_normals[i-base_vertex]));
}
static bool has_valid_lod(Slice<const int> lods)
{
for (int lod : lods) {
if (lod >= 0)
return true;
}
return false;
}
static bool same_lod(Slice<const int> lods, int *thelod)
{
*thelod = -1;
for (int lod : lods) {
if (lod >= 0) {
*thelod = lod;
break;
}
}
for (int lod : lods) {
if (lod >= 0 && lod != *thelod)
return false;
}
return true;
}
static void unpack_fields(Slice<HermiteField> out,
Slice<const HermiteRLEField*> fields, const FieldAccessHelper &fah)
{
for (int i = 0; i < 8; i++) {
// skip non-existent chunk
if (fah.lods[i] == -1)
continue;
const Vec3i offset = fah.voffset(i);
const Vec3i size = fah.vsize(i);
out[i].resize(size);
fields[i]->partial_decompress(out[i].data, offset, offset + size);
}
}
// TODO
static void sync_fields(Slice<HermiteField> fields, const FieldAccessHelper &fah)
{
// sync edges
for (int i = 0; i < 6; i++) {
const int axis = EDGES_TABLE[i].axis;
const auto &auth_edge = fah.auth_edges[i];
// no edges here
if (auth_edge.lod == -1)
continue;
const int auth_lod = auth_edge.lod;
const int auth_c = auth_edge.chunk;
const HermiteField &auth_field = fields[auth_c];
const Vec3i auth_offset = auth_edge.offset;
for (int j = 0; j < 4; j++) {
const int c = EDGES_TABLE[i].chunks[j];
// no need to sync with ourselves
if (c == auth_c)
continue;
// not a field
const int lod = fah.lods[c];
if (lod == -1)
continue;
const Vec3i vsize = fah.vsize(c);
const Vec3i dcsize = vsize - Vec3i(1);
const Vec3i offset = EDGES_TABLE[i].offsets[j] * dcsize;
HermiteField &field = fields[c];
for (int a = 0; a < vsize[axis]; a++) {
Vec3i pos = offset;
pos[axis] = a;
Vec3i opos = auth_offset;
opos[axis] = auth_lod < lod ? a*2 : a;
field.get(pos).material = auth_field.get(opos).material;
}
}
}
// sync faces
for (int i = 0; i < 12; i++) {
const Vec2i axes = FACES_TABLE[i].axes;
const auto &auth_face = fah.auth_faces[i];
// no faces here
if (auth_face.lod == -1)
continue;
const int auth_lod = auth_face.lod;
const int auth_c = auth_face.chunk;
const HermiteField &auth_field = fields[auth_c];
const Vec3i auth_offset = auth_face.offset;
for (int j = 0; j < 2; j++) {
const int c = FACES_TABLE[i].chunks[j];
if (c == auth_c)
continue;
const int lod = fah.lods[c];
if (lod == -1)
continue;
const Vec3i vsize = fah.vsize(c);
const Vec3i dcsize = vsize - Vec3i(1);
const Vec3i offset = FACES_TABLE[i].offsets[j] * dcsize;
HermiteField &field = fields[c];
for (int a0 = 0; a0 < vsize[axes[0]]; a0++) {
for (int a1 = 0; a1 < vsize[axes[1]]; a1++) {
Vec3i pos = offset;
pos[axes[0]] = a0;
pos[axes[1]] = a1;
Vec3i opos = auth_offset;
opos[axes[0]] = auth_lod < lod ? a0*2 : a0;
opos[axes[1]] = auth_lod < lod ? a1*2 : a1;
field.get(pos).material = auth_field.get(opos).material;
}}
}
}
}
static inline void collect_additional_face_edges(CubeContext *ctx, const Vec3i &pos,
int axis, int value, const HermiteField &field)
{
// a1
// +----+----+
// | | |
// | | |
// +----+----+ b
// | | |
// | | |
// +----+----+
// a lpos0
// a0
const Vec2i other_axes = OTHER_AXES_TABLE[axis];
Vec3i lpos0a = pos;
lpos0a[other_axes[0]]++;
Vec3i lpos1a = lpos0a;
Vec3i lpos2a = lpos0a;
lpos1a[other_axes[1]] += 1;
lpos2a[other_axes[1]] += 2;
Vec3i lpos0b = pos;
lpos0b[other_axes[1]]++;
Vec3i lpos2b = lpos0b;
lpos2b[other_axes[0]] += 2;
const HermiteData &hd0a = field.get(lpos0a);
const HermiteData &hd1a = field.get(lpos1a);
const HermiteData &hd2a = field.get(lpos2a);
const HermiteData &hd0b = field.get(lpos0b);
const HermiteData &hd1b = hd1a;
const HermiteData &hd2b = field.get(lpos2b);
if (edge_has_intersection(hd2b, hd1b)) {
Vec3i v(0);
v[axis] = value;
v[other_axes[1]] = HermiteData_HalfEdge();
v[other_axes[0]] = HermiteData_HalfEdge() +
(HermiteData_FullEdge() - hd2b.edges[other_axes[0]]) / 2;
ctx->add_vector(v);
}
if (edge_has_intersection(hd1b, hd0b)) {
Vec3i v(0);
v[axis] = value;
v[other_axes[1]] = HermiteData_HalfEdge();
v[other_axes[0]] = (HermiteData_FullEdge() - hd1b.edges[other_axes[0]]) / 2;
ctx->add_vector(v);
}
if (edge_has_intersection(hd2a, hd1a)) {
Vec3i v(0);
v[axis] = value;
v[other_axes[0]] = HermiteData_HalfEdge();
v[other_axes[1]] = HermiteData_HalfEdge() +
(HermiteData_FullEdge() - hd2a.edges[other_axes[1]]) / 2;
ctx->add_vector(v);
}
if (edge_has_intersection(hd1a, hd0a)) {
Vec3i v(0);
v[axis] = value;
v[other_axes[0]] = HermiteData_HalfEdge();
v[other_axes[1]] = (HermiteData_FullEdge() - hd1a.edges[other_axes[1]]) / 2;
ctx->add_vector(v);
}
ctx->add_material(hd1a.material);
}
static inline void collect_edges(CubeContext *ctx, const Vec3i &pos,
int eaxis, const Vec2i &value, const HermiteField &field)
{