@@ -9,6 +9,8 @@ using namespace glm;
9
9
10
10
#define MORTON_CODING 0
11
11
12
+ #define USE_DENSITIES true
13
+
12
14
#define DC_LINE (ox,oy,oz,m ) \
13
15
s0 = encode_vertex(xyz.x + (ox), xyz.y + (oy), xyz.z + (oz), dimp1, z_per_y, y_per_x, s0_mask); \
14
16
if (samples[s0] & s0_mask) \
@@ -29,12 +31,14 @@ else if (z_block > 0) \
29
31
CubicChunk::CubicChunk () : Chunk()
30
32
{
31
33
cell_block = 0 ;
34
+ float_block = 0 ;
32
35
}
33
36
34
37
CubicChunk::CubicChunk (glm::vec3 pos, float size, int level, Sampler& sampler,
35
38
bool produce_quads) : Chunk(pos, size, level, sampler, produce_quads)
36
39
{
37
40
cell_block = 0 ;
41
+ float_block = 0 ;
38
42
}
39
43
40
44
CubicChunk::~CubicChunk ()
@@ -70,7 +74,7 @@ void CubicChunk::generate_samples(ResourceAllocator<BinaryBlock>* binary_allocat
70
74
// memset(samples, 0, sizeof(uint32_t) * real_count);
71
75
float delta = size / (float )dim;
72
76
const float res = sampler.world_size ;
73
- FloatBlock* float_block = float_allocator->new_element ();
77
+ float_block = float_allocator->new_element ();
74
78
float_block->init (dimp1 * dimp1 * dimp1, dimp1 * dimp1 * dimp1);
75
79
76
80
sampler.block (res, pos, ivec3 (dimp1, dimp1, dimp1), delta * scale, &float_block->data , &float_block->vectorset , float_block->dest_noise );
@@ -131,10 +135,14 @@ void CubicChunk::generate_samples(ResourceAllocator<BinaryBlock>* binary_allocat
131
135
else
132
136
contains_mesh = mesh;
133
137
134
- float_allocator->free_element (float_block);
138
+ if (!USE_DENSITIES || !contains_mesh)
139
+ {
140
+ float_allocator->free_element (float_block);
141
+ float_block = 0 ;
142
+ }
135
143
}
136
144
137
- void CubicChunk::generate_dual_vertices (ResourceAllocator<VerticesIndicesBlock>* vi_allocator, ResourceAllocator<CellsBlock>* cell_allocator, ResourceAllocator<IndexesBlock>* inds_allocator)
145
+ void CubicChunk::generate_dual_vertices (ResourceAllocator<VerticesIndicesBlock>* vi_allocator, ResourceAllocator<CellsBlock>* cell_allocator, ResourceAllocator<IndexesBlock>* inds_allocator, ResourceAllocator<FloatBlock>* float_allocator )
138
146
{
139
147
if (!vi)
140
148
{
@@ -412,7 +420,7 @@ void CubicChunk::generate_dual_vertices(ResourceAllocator<VerticesIndicesBlock>*
412
420
line_masks[0 ] |= local_masks[0 ];
413
421
if (local_masks[1 ] != 0 )
414
422
line_masks[1 ] |= local_masks[1 ];
415
- if (local_masks[2 ] != 0 )
423
+ if (local_masks[2 ] != 0 )
416
424
line_masks[2 ] |= local_masks[2 ];
417
425
if (local_masks[3 ] != 0 )
418
426
line_masks[3 ] |= local_masks[3 ];
@@ -474,6 +482,18 @@ void CubicChunk::generate_dual_vertices(ResourceAllocator<VerticesIndicesBlock>*
474
482
// cells.shrink();
475
483
476
484
free (masks);
485
+ if (USE_DENSITIES)
486
+ {
487
+ float_allocator->free_element (float_block);
488
+ float_block = 0 ;
489
+ }
490
+ }
491
+
492
+ __forceinline void _get_intersection (vec3& v0, vec3& v1, float s0, float s1, float isolevel, vec3& out)
493
+ {
494
+ float mu = (isolevel - s0) / (s1 - s0);
495
+ vec3 delta_v = (v1 - v0) * mu;
496
+ out = delta_v + v0;
477
497
}
478
498
479
499
__forceinline void CubicChunk::calculate_cell (glm::uvec3 xyz, uint32_t next_index, Cell* result, bool force, uint8_t mask)
@@ -490,6 +510,19 @@ __forceinline void CubicChunk::calculate_cell(glm::uvec3 xyz, uint32_t next_inde
490
510
result->v_map [3 ] = -1 ;
491
511
492
512
uint16_t edge_mask = 0 ;
513
+ glm::vec3 edge_crossings[12 ];
514
+ float corners[8 ];
515
+ float dv = size / (float )dim;
516
+
517
+ if (USE_DENSITIES)
518
+ {
519
+ int dimp1 = dim + 1 ;
520
+ for (int i = 0 ; i < 8 ; i++)
521
+ {
522
+ corners[i] = float_block->data [(xyz.x + Tables::TDX[i]) * dimp1 * dimp1 + (xyz.y + Tables::TDY[i]) * dimp1 + xyz.z + Tables::TDZ[i]];
523
+ }
524
+ }
525
+
493
526
for (int i = 0 ; i < 12 ; i++)
494
527
{
495
528
int v0 = Tables::TEdgePairs[i][0 ];
@@ -501,40 +534,61 @@ __forceinline void CubicChunk::calculate_cell(glm::uvec3 xyz, uint32_t next_inde
501
534
continue ;
502
535
503
536
edge_mask |= 1 << i;
537
+
538
+ if (USE_DENSITIES)
539
+ {
540
+ vec3 e0 (dv * (Tables::TDX[v0] + xyz.x ), dv * (Tables::TDY[v0] + xyz.y ), dv * (Tables::TDZ[v0] + xyz.z ));
541
+ vec3 e1 (dv * (Tables::TDX[v1] + xyz.x ), dv * (Tables::TDY[v1] + xyz.y ), dv * (Tables::TDZ[v1] + xyz.z ));
542
+ _get_intersection (e0 , e1 , corners[v0], corners[v1], 0 , edge_crossings[i]);
543
+ }
504
544
}
545
+
505
546
result->edge_mask = edge_mask;
506
547
507
548
int v_index = 0 ;
508
549
uint32_t edge_map = 0 ;
509
550
bool boundary = force || xyz.x <= 0 || xyz.y <= 0 || xyz.z <= 0 || xyz.x >= dim - 1 || xyz.y >= dim - 1 || xyz.z >= dim - 1 ;
510
- for (int i = 0 ; i < 22 ; i++)
551
+ int v_count = 0 ;
552
+ glm::vec3 v_pos[4 ] = { vec3 (0 ,0 ,0 ), vec3 (0 ,0 ,0 ), vec3 (0 ,0 ,0 ), vec3 (0 ,0 ,0 ) };
553
+
554
+ for (int i = 0 ; i < 16 ; i++)
511
555
{
512
556
int e = Tables::EdgeTable[mask][i];
513
- if (e == -1 )
557
+ if (e == -1 || e == - 2 )
514
558
{
559
+ if (v_count > 0 )
560
+ {
561
+ v_pos[v_index] /= (float )v_count;
562
+ v_pos[v_index] += pos;
563
+ v_count = 0 ;
564
+ }
515
565
v_index++;
516
- if (boundary )
566
+ if (e == - 2 )
517
567
break ;
518
568
continue ;
519
569
}
520
- if (e == -2 )
521
- {
522
- v_index++;
523
- // assert(v_index == n_verts);
524
- break ;
525
- }
526
570
assert (((edge_map >> (e * 2 )) & 3 ) == 0 );
527
571
if (((edge_map >> (e * 2 )) & 3 ) != 0 )
528
572
printf (" Hmm\n " );
529
573
edge_map |= (v_index) << (e * 2 );
574
+ assert ((edge_mask & (1 << e)) != 0 );
575
+ if (USE_DENSITIES)
576
+ {
577
+ if (!v_count)
578
+ v_pos[v_index] = edge_crossings[e];
579
+ else
580
+ v_pos[v_index] += edge_crossings[e];
581
+ v_count++;
582
+
583
+ }
530
584
}
531
585
532
586
// assert(v_index == n_verts);
533
587
534
588
for (int i = 0 ; i < v_index; i++)
535
589
{
536
590
DualVertex v;
537
- calculate_dual_vertex (xyz, (uint32_t )vi->vertices .count , &v, false , mask, glm::vec3 ( 0 , 0 , 0 ) );
591
+ calculate_dual_vertex (xyz, (uint32_t )vi->vertices .count , &v, USE_DENSITIES , mask, v_pos[i] );
538
592
result->v_map [i] = (uint32_t )vi->vertices .count ;
539
593
vi->vertices .push_back (v);
540
594
}
0 commit comments