Skip to content

Commit 454631a

Browse files
author
Lin
committed
Big change switching from manifold dual contouring to dual marching cubes for fixing problems. Currently uses densities for smooth vertex placement. Vertex valences aren't calculated, so meshes can't be processed yet
1 parent 4e4cf48 commit 454631a

35 files changed

+1931
-459
lines changed

BinaryMeshFitting.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,16 @@ Global
8282
GlobalSection(Performance) = preSolution
8383
HasPerformanceSessions = true
8484
EndGlobalSection
85+
GlobalSection(Performance) = preSolution
86+
HasPerformanceSessions = true
87+
EndGlobalSection
88+
GlobalSection(Performance) = preSolution
89+
HasPerformanceSessions = true
90+
EndGlobalSection
91+
GlobalSection(Performance) = preSolution
92+
HasPerformanceSessions = true
93+
EndGlobalSection
94+
GlobalSection(Performance) = preSolution
95+
HasPerformanceSessions = true
96+
EndGlobalSection
8597
EndGlobal

BinaryMeshFitting/BinaryChunk.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void BinaryChunk::generate_samples()
7878

7979
float* block_data = 0;
8080
FastNoiseVectorSet vector_set;
81-
sampler.block(res, pos, ivec3(dim, dim, dim), delta * scale, &block_data, &vector_set, 0);
81+
sampler.block(res, pos, ivec3(dim, dim, dim), delta * scale, (void**)&block_data, &vector_set, 0, 0, sizeof(float), 0);
8282

8383
vec3 dxyz;
8484
auto f = sampler.value;

BinaryMeshFitting/BinaryMeshFitting.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
<ClCompile Include="Core.cpp" />
157157
<ClCompile Include="CubicChunk.cpp" />
158158
<ClCompile Include="DebugScene.cpp" />
159+
<ClCompile Include="DMCChunk.cpp" />
159160
<ClCompile Include="DualChunk.cpp" />
160161
<ClCompile Include="DynamicGLChunk.cpp" />
161162
<ClCompile Include="Entry.cpp" />
@@ -196,8 +197,10 @@
196197
<ClInclude Include="CubicChunk.hpp" />
197198
<ClInclude Include="Debug.h" />
198199
<ClInclude Include="DefaultOptions.h" />
200+
<ClInclude Include="DMCChunk.hpp" />
199201
<ClInclude Include="DualChunk.hpp" />
200202
<ClInclude Include="DynamicGLChunk.hpp" />
203+
<ClInclude Include="MCTable.h" />
201204
<ClInclude Include="ResourceAllocator.hpp" />
202205
<ClInclude Include="GLChunk.hpp" />
203206
<ClInclude Include="GUI\imconfig.h" />

BinaryMeshFitting/BinaryMeshFitting.vcxproj.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@
8787
<ClCompile Include="DynamicGLChunk.cpp">
8888
<Filter>Source Files</Filter>
8989
</ClCompile>
90+
<ClCompile Include="DMCChunk.cpp">
91+
<Filter>Source Files</Filter>
92+
</ClCompile>
9093
</ItemGroup>
9194
<ItemGroup>
9295
<ClInclude Include="Core.hpp">
@@ -215,6 +218,12 @@
215218
<ClInclude Include="DynamicGLChunk.hpp">
216219
<Filter>Header Files</Filter>
217220
</ClInclude>
221+
<ClInclude Include="DMCChunk.hpp">
222+
<Filter>Header Files</Filter>
223+
</ClInclude>
224+
<ClInclude Include="MCTable.h">
225+
<Filter>Header Files</Filter>
226+
</ClInclude>
218227
</ItemGroup>
219228
<ItemGroup>
220229
<None Include="MemoryPool.tcc">

BinaryMeshFitting/Chunk.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,11 @@ class Chunk
3939
virtual void init(glm::vec3 pos, float size, int level, Sampler& sampler, bool produce_quads);
4040
virtual void generate_samples(ResourceAllocator<BinaryBlock>* binary_allocator, ResourceAllocator<FloatBlock>* float_allocator) = 0;
4141
virtual void generate_dual_vertices(ResourceAllocator<VerticesIndicesBlock>* vi_allocator, ResourceAllocator<CellsBlock>* cell_allocator, ResourceAllocator<IndexesBlock>* inds_allocator, ResourceAllocator<FloatBlock>* float_allocator) = 0;
42-
virtual bool calculate_dual_vertex(glm::uvec3 xyz, uint32_t next_index, DualVertex* result, bool force, uint8_t mask, glm::vec3 pos_override) = 0;
4342

4443
virtual uint32_t encode_vertex(glm::uvec3 xyz);
4544
virtual uint32_t encode_cell(glm::uvec3 xyz);
4645

4746
virtual void generate_base_mesh(ResourceAllocator<VerticesIndicesBlock>* vi_allocator) = 0;
48-
virtual void calculate_valences() = 0;
49-
virtual uint32_t collapse_bad_cells() = 0;
5047
virtual void copy_verts_and_inds(SmartContainer<DualVertex>& v_out, SmartContainer<uint32_t>& i_out);
5148

5249
double extract(SmartContainer<DualVertex>& v_out, SmartContainer<uint32_t>& i_out, bool silent);

BinaryMeshFitting/ChunkBlocks.hpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,68 @@ struct FloatBlock : public LinkedNode<FloatBlock>
7373
}
7474
};
7575

76+
struct IsoVertexBlock : public LinkedNode<IsoVertexBlock>
77+
{
78+
uint32_t size;
79+
DMC_Isovertex* data;
80+
bool initialized;
81+
82+
inline IsoVertexBlock()
83+
{
84+
initialized = false;
85+
size = 0;
86+
data = 0;
87+
}
88+
89+
inline ~IsoVertexBlock()
90+
{
91+
_aligned_free(data);
92+
size = 0;
93+
initialized = false;
94+
}
95+
96+
void init(uint32_t _size)
97+
{
98+
if (initialized)
99+
return;
100+
_aligned_free(data);
101+
data = (DMC_Isovertex*)_aligned_malloc(sizeof(DMC_Isovertex) * _size, 16);
102+
initialized = true;
103+
}
104+
};
105+
106+
struct NoiseBlock : public LinkedNode<NoiseBlock>
107+
{
108+
uint32_t size;
109+
float* dest_noise;
110+
FastNoiseVectorSet vectorset;
111+
bool initialized;
112+
113+
inline NoiseBlock()
114+
{
115+
initialized = false;
116+
size = 0;
117+
dest_noise = 0;
118+
}
119+
120+
inline ~NoiseBlock()
121+
{
122+
_aligned_free(dest_noise);
123+
size = 0;
124+
initialized = false;
125+
}
126+
127+
void init(uint32_t noise_size)
128+
{
129+
if (initialized)
130+
return;
131+
_aligned_free(dest_noise);
132+
dest_noise = (float*)_aligned_malloc(sizeof(float) * noise_size, 16);
133+
vectorset.SetSize(noise_size);
134+
initialized = true;
135+
}
136+
};
137+
76138
struct VerticesIndicesBlock : public LinkedNode<VerticesIndicesBlock>
77139
{
78140
SmartContainer<DualVertex> vertices;
@@ -140,3 +202,21 @@ struct IndexesBlock : public LinkedNode<IndexesBlock>
140202
initialized = true;
141203
}
142204
};
205+
206+
struct DMC_CellsBlock : public LinkedNode<DMC_CellsBlock>
207+
{
208+
SmartContainer<DMC_Cell> cells;
209+
210+
inline DMC_CellsBlock()
211+
{
212+
}
213+
214+
inline ~DMC_CellsBlock()
215+
{
216+
}
217+
218+
void init()
219+
{
220+
cells.count = 0;
221+
}
222+
};

BinaryMeshFitting/ChunkGenerator.cpp

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "WorldOctreeNode.hpp"
55
#include "MeshProcessor.hpp"
66
#include "DefaultOptions.h"
7+
#include "DMCChunk.hpp"
78
#include <iostream>
89

910
ChunkGenerator::ChunkGenerator() : ThreadDebug("ChunkGenerator")
@@ -34,16 +35,17 @@ void ChunkGenerator::process_queue(SmartContainer<WorldOctreeNode*>& batch)
3435
}
3536
}
3637

37-
extract_samples(batch, &binary_allocator, &float_allocator);
38+
extract_chunk(batch);
39+
/*extract_samples(batch);
3840
extract_dual_vertices(batch);
3941
extract_octrees(batch);
4042
extract_base_meshes(batch);
41-
extract_format_meshes(batch);
43+
extract_format_meshes(batch);*/
4244

43-
for (int i = 0; i < count; i++)
45+
/*for (int i = 0; i < count; i++)
4446
{
4547
batch[i]->generation_stage = GENERATION_STAGES_NEEDS_UPLOAD;
46-
}
48+
}*/
4749

4850
/*if (stitcher.stage == STITCHING_STAGES_READY)
4951
{
@@ -74,7 +76,50 @@ void ChunkGenerator::generate_chunk(WorldOctreeNode* n)
7476
world->create_chunk(n);
7577
}
7678

77-
void ChunkGenerator::extract_samples(SmartContainer<class WorldOctreeNode*>& batch, ResourceAllocator<BinaryBlock>* binary_allocator, ResourceAllocator<FloatBlock>* float_allocator)
79+
void ChunkGenerator::extract_chunk(SmartContainer<class WorldOctreeNode*>& batch)
80+
{
81+
int count = (int)batch.count;
82+
int i;
83+
#pragma omp parallel for
84+
for (i = 0; i < count; i++)
85+
{
86+
if (batch[i]->generation_stage == GENERATION_STAGES_GENERATING)
87+
{
88+
if (update_still_needed(batch[i]))
89+
{
90+
batch[i]->chunk->label_grid(&binary_allocator, &isovertex_allocator, &noise_allocator);
91+
92+
batch[i]->chunk->label_edges(&vi_allocator, &cell_allocator, &inds_allocator, &isovertex_allocator);
93+
94+
batch[i]->chunk->generate_octree();
95+
if (!batch[i]->chunk->octree.is_leaf())
96+
{
97+
memcpy(batch[i]->children, batch[i]->chunk->octree.children, sizeof(OctreeNode*) * 8);
98+
batch[i]->leaf_flag = false;
99+
}
100+
101+
batch[i]->chunk->polygonize();
102+
103+
binary_allocator.free_element(batch[i]->chunk->binary_block);
104+
batch[i]->chunk->binary_block = 0;
105+
isovertex_allocator.free_element(batch[i]->chunk->density_block);
106+
batch[i]->chunk->density_block = 0;
107+
cell_allocator.free_element(batch[i]->chunk->cell_block);
108+
batch[i]->chunk->cell_block = 0;
109+
inds_allocator.free_element(batch[i]->chunk->indexes_block);
110+
batch[i]->chunk->indexes_block = 0;
111+
}
112+
}
113+
114+
batch[i]->format(&world->gl_allocator);
115+
116+
batch[i]->generation_stage = GENERATION_STAGES_NEEDS_UPLOAD;
117+
}
118+
119+
120+
}
121+
122+
void ChunkGenerator::extract_samples(SmartContainer<class WorldOctreeNode*>& batch)
78123
{
79124
using namespace std;
80125
//cout << "-Generating samples...";
@@ -87,8 +132,10 @@ void ChunkGenerator::extract_samples(SmartContainer<class WorldOctreeNode*>& bat
87132
{
88133
if (batch[i]->generation_stage == GENERATION_STAGES_GENERATING)
89134
{
90-
if(update_still_needed(batch[i]))
91-
batch[i]->chunk->generate_samples(binary_allocator, float_allocator);
135+
if (update_still_needed(batch[i]))
136+
{
137+
batch[i]->chunk->label_grid(&binary_allocator, &isovertex_allocator, &noise_allocator);
138+
}
92139
}
93140
}
94141

@@ -111,8 +158,8 @@ void ChunkGenerator::extract_filter(SmartContainer<WorldOctreeNode*>& batch)
111158
#pragma omp parallel for
112159
for (i = 0; i < count; i++)
113160
{
114-
if (batch[i]->generation_stage == GENERATION_STAGES_GENERATING)
115-
batch[i]->chunk->filter();
161+
//if (batch[i]->generation_stage == GENERATION_STAGES_GENERATING)
162+
//batch[i]->chunk->filter();
116163
}
117164

118165
double ms = clock() - start_clock;
@@ -132,7 +179,7 @@ void ChunkGenerator::extract_dual_vertices(SmartContainer<WorldOctreeNode*>& bat
132179
for (int i = 0; i < count; i++)
133180
{
134181
if (batch[i]->generation_stage == GENERATION_STAGES_GENERATING)
135-
batch[i]->chunk->generate_dual_vertices(&vi_allocator, &cell_allocator, &inds_allocator, &float_allocator);
182+
batch[i]->chunk->label_edges(&vi_allocator, &cell_allocator, &inds_allocator, &isovertex_allocator);
136183
}
137184
}
138185

@@ -160,8 +207,8 @@ void ChunkGenerator::extract_octrees(SmartContainer<WorldOctreeNode*>& batch)
160207
memcpy(batch[i]->children, batch[i]->chunk->octree.children, sizeof(OctreeNode*) * 8);
161208
batch[i]->leaf_flag = false;
162209
}
163-
else
164-
cout << "WARNING: Octree is leaf." << endl;
210+
//else
211+
//cout << "WARNING: Octree is leaf." << endl;
165212
}
166213
}
167214
}
@@ -188,11 +235,11 @@ void ChunkGenerator::extract_base_meshes(SmartContainer<WorldOctreeNode*>& batch
188235
{
189236
if (batch[i]->generation_stage == GENERATION_STAGES_GENERATING)
190237
{
191-
batch[i]->chunk->generate_base_mesh(&vi_allocator);
238+
batch[i]->chunk->polygonize();
192239
if (!iters || !batch[i]->chunk->contains_mesh || !batch[i]->chunk->vi->vertices.count || !batch[i]->chunk->vi->mesh_indexes.count)
193240
continue;
194241

195-
auto& v_out = batch[i]->chunk->vi->vertices;
242+
/*auto& v_out = batch[i]->chunk->vi->vertices;
196243
auto& i_out = batch[i]->chunk->vi->mesh_indexes;
197244
Processing::MeshProcessor<4> mp(true, SMOOTH_NORMALS);
198245
mp.init(batch[i]->chunk->vi->vertices, batch[i]->chunk->vi->mesh_indexes, sampler);
@@ -222,8 +269,8 @@ void ChunkGenerator::extract_base_meshes(SmartContainer<WorldOctreeNode*>& batch
222269
v_out.count = 0;
223270
i_out.count = 0;
224271
mp.flush(v_out, i_out);
225-
}
226-
272+
}*/
273+
227274
}
228275
}
229276
}

BinaryMeshFitting/ChunkGenerator.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ class ChunkGenerator : public ThreadDebug
2828
ResourceAllocator<BinaryBlock> binary_allocator;
2929
ResourceAllocator<FloatBlock> float_allocator;
3030
ResourceAllocator<VerticesIndicesBlock> vi_allocator;
31-
ResourceAllocator<CellsBlock> cell_allocator;
31+
ResourceAllocator<DMC_CellsBlock> cell_allocator;
3232
ResourceAllocator<IndexesBlock> inds_allocator;
33+
ResourceAllocator<IsoVertexBlock> isovertex_allocator;
34+
ResourceAllocator<NoiseBlock> noise_allocator;
3335

3436
WorldStitcher stitcher;
3537

@@ -41,7 +43,8 @@ class ChunkGenerator : public ThreadDebug
4143
bool update_still_needed(class WorldOctreeNode* n);
4244
void generate_chunk(class WorldOctreeNode* n);
4345

44-
void extract_samples(SmartContainer<class WorldOctreeNode*>& batch, ResourceAllocator<BinaryBlock>* binary_allocator, ResourceAllocator<FloatBlock>* float_allocator);
46+
void extract_chunk(SmartContainer<class WorldOctreeNode*>& batch);
47+
void extract_samples(SmartContainer<class WorldOctreeNode*>& batch);
4548
void extract_filter(SmartContainer<class WorldOctreeNode*>& batch);
4649
void extract_dual_vertices(SmartContainer<class WorldOctreeNode*>& batch);
4750
void extract_octrees(SmartContainer<class WorldOctreeNode*>& batch);

BinaryMeshFitting/CubicChunk.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void CubicChunk::generate_samples(ResourceAllocator<BinaryBlock>* binary_allocat
7777
float_block = float_allocator->new_element();
7878
float_block->init(dimp1 * dimp1 * dimp1, dimp1 * dimp1 * dimp1);
7979

80-
sampler.block(res, pos, ivec3(dimp1, dimp1, dimp1), delta * scale, &float_block->data, &float_block->vectorset, float_block->dest_noise);
80+
sampler.block(res, pos, ivec3(dimp1, dimp1, dimp1), delta * scale, (void**)&float_block->data, &float_block->vectorset, float_block->dest_noise, 0, sizeof(float), 0);
8181

8282
vec3 dxyz;
8383
auto f = sampler.value;

BinaryMeshFitting/CubicChunk.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ class CubicChunk : public Chunk
2121
void generate_samples(ResourceAllocator<BinaryBlock>* binary_allocator, ResourceAllocator<FloatBlock>* float_allocator) final override;
2222
void generate_dual_vertices(ResourceAllocator<VerticesIndicesBlock>* vi_allocator, ResourceAllocator<CellsBlock>* cell_allocator, ResourceAllocator<IndexesBlock>* inds_allocator, ResourceAllocator<FloatBlock>* float_allocator) final override;
2323
__forceinline void calculate_cell(glm::uvec3 xyz, uint32_t next_index, Cell* result, bool force, uint8_t mask);
24-
__forceinline bool calculate_dual_vertex(glm::uvec3 xyz, uint32_t next_index, DualVertex* result, bool force, uint8_t mask, glm::vec3 pos_override) final override;
24+
__forceinline bool calculate_dual_vertex(glm::uvec3 xyz, uint32_t next_index, DualVertex* result, bool force, uint8_t mask, glm::vec3 pos_override);
2525

2626
uint32_t encode_vertex(uint32_t x, uint32_t y, uint32_t z, uint32_t dim, uint32_t z_per_y, uint32_t y_per_x, uint32_t& out_mask);
2727
uint32_t encode_vertex(glm::uvec3 xyz);
2828

29-
void calculate_valences() final override;
30-
uint32_t collapse_bad_cells() final override;
29+
void calculate_valences();
30+
uint32_t collapse_bad_cells();
3131

3232
void generate_base_mesh(ResourceAllocator<VerticesIndicesBlock>* vi_allocator) final override;
3333

0 commit comments

Comments
 (0)