Skip to content

Commit 1c2ee98

Browse files
author
Lin
committed
-Basic use of the density field to position vertices
-Space key to toggle updating -Unsuccessful attempts at solving manifold stitching problems
1 parent 40e616b commit 1c2ee98

12 files changed

+113
-39
lines changed

BinaryMeshFitting/Chunk.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Chunk
3838
virtual ~Chunk() = 0;
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;
41-
virtual void generate_dual_vertices(ResourceAllocator<VerticesIndicesBlock>* vi_allocator, ResourceAllocator<CellsBlock>* cell_allocator, ResourceAllocator<IndexesBlock>* inds_allocator) = 0;
41+
virtual void generate_dual_vertices(ResourceAllocator<VerticesIndicesBlock>* vi_allocator, ResourceAllocator<CellsBlock>* cell_allocator, ResourceAllocator<IndexesBlock>* inds_allocator, ResourceAllocator<FloatBlock>* float_allocator) = 0;
4242
virtual bool calculate_dual_vertex(glm::uvec3 xyz, uint32_t next_index, DualVertex* result, bool force, uint8_t mask, glm::vec3 pos_override) = 0;
4343

4444
virtual uint32_t encode_vertex(glm::uvec3 xyz);

BinaryMeshFitting/ChunkGenerator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ void ChunkGenerator::extract_dual_vertices(SmartContainer<WorldOctreeNode*>& bat
132132
for (int i = 0; i < count; i++)
133133
{
134134
if (batch[i]->generation_stage == GENERATION_STAGES_GENERATING)
135-
batch[i]->chunk->generate_dual_vertices(&vi_allocator, &cell_allocator, &inds_allocator);
135+
batch[i]->chunk->generate_dual_vertices(&vi_allocator, &cell_allocator, &inds_allocator, &float_allocator);
136136
}
137137
}
138138

BinaryMeshFitting/CubicChunk.cpp

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ using namespace glm;
99

1010
#define MORTON_CODING 0
1111

12+
#define USE_DENSITIES true
13+
1214
#define DC_LINE(ox,oy,oz,m) \
1315
s0 = encode_vertex(xyz.x + (ox), xyz.y + (oy), xyz.z + (oz), dimp1, z_per_y, y_per_x, s0_mask); \
1416
if (samples[s0] & s0_mask) \
@@ -29,12 +31,14 @@ else if (z_block > 0) \
2931
CubicChunk::CubicChunk() : Chunk()
3032
{
3133
cell_block = 0;
34+
float_block = 0;
3235
}
3336

3437
CubicChunk::CubicChunk(glm::vec3 pos, float size, int level, Sampler& sampler,
3538
bool produce_quads) : Chunk(pos, size, level, sampler, produce_quads)
3639
{
3740
cell_block = 0;
41+
float_block = 0;
3842
}
3943

4044
CubicChunk::~CubicChunk()
@@ -70,7 +74,7 @@ void CubicChunk::generate_samples(ResourceAllocator<BinaryBlock>* binary_allocat
7074
//memset(samples, 0, sizeof(uint32_t) * real_count);
7175
float delta = size / (float)dim;
7276
const float res = sampler.world_size;
73-
FloatBlock* float_block = float_allocator->new_element();
77+
float_block = float_allocator->new_element();
7478
float_block->init(dimp1 * dimp1 * dimp1, dimp1 * dimp1 * dimp1);
7579

7680
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
131135
else
132136
contains_mesh = mesh;
133137

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+
}
135143
}
136144

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)
138146
{
139147
if (!vi)
140148
{
@@ -412,7 +420,7 @@ void CubicChunk::generate_dual_vertices(ResourceAllocator<VerticesIndicesBlock>*
412420
line_masks[0] |= local_masks[0];
413421
if (local_masks[1] != 0)
414422
line_masks[1] |= local_masks[1];
415-
if (local_masks[2] != 0)
423+
if (local_masks[2] != 0)
416424
line_masks[2] |= local_masks[2];
417425
if (local_masks[3] != 0)
418426
line_masks[3] |= local_masks[3];
@@ -474,6 +482,18 @@ void CubicChunk::generate_dual_vertices(ResourceAllocator<VerticesIndicesBlock>*
474482
//cells.shrink();
475483

476484
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;
477497
}
478498

479499
__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
490510
result->v_map[3] = -1;
491511

492512
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+
493526
for (int i = 0; i < 12; i++)
494527
{
495528
int v0 = Tables::TEdgePairs[i][0];
@@ -501,40 +534,61 @@ __forceinline void CubicChunk::calculate_cell(glm::uvec3 xyz, uint32_t next_inde
501534
continue;
502535

503536
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+
}
504544
}
545+
505546
result->edge_mask = edge_mask;
506547

507548
int v_index = 0;
508549
uint32_t edge_map = 0;
509550
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++)
511555
{
512556
int e = Tables::EdgeTable[mask][i];
513-
if (e == -1)
557+
if (e == -1 || e == -2)
514558
{
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+
}
515565
v_index++;
516-
if (boundary)
566+
if (e == -2)
517567
break;
518568
continue;
519569
}
520-
if (e == -2)
521-
{
522-
v_index++;
523-
//assert(v_index == n_verts);
524-
break;
525-
}
526570
assert(((edge_map >> (e * 2)) & 3) == 0);
527571
if (((edge_map >> (e * 2)) & 3) != 0)
528572
printf("Hmm\n");
529573
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+
}
530584
}
531585

532586
//assert(v_index == n_verts);
533587

534588
for (int i = 0; i < v_index; i++)
535589
{
536590
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]);
538592
result->v_map[i] = (uint32_t)vi->vertices.count;
539593
vi->vertices.push_back(v);
540594
}

BinaryMeshFitting/CubicChunk.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class CubicChunk : public Chunk
99
{
1010
public:
11+
FloatBlock* float_block;
1112
BinaryBlock* binary_block;
1213
CellsBlock* cell_block;
1314
DualNode octree;
@@ -18,7 +19,7 @@ class CubicChunk : public Chunk
1819
~CubicChunk();
1920
void init(glm::vec3 pos, float size, int level, Sampler& sampler, bool produce_quads) override;
2021
void generate_samples(ResourceAllocator<BinaryBlock>* binary_allocator, ResourceAllocator<FloatBlock>* float_allocator) final override;
21-
void generate_dual_vertices(ResourceAllocator<VerticesIndicesBlock>* vi_allocator, ResourceAllocator<CellsBlock>* cell_allocator, ResourceAllocator<IndexesBlock>* inds_allocator) final override;
22+
void generate_dual_vertices(ResourceAllocator<VerticesIndicesBlock>* vi_allocator, ResourceAllocator<CellsBlock>* cell_allocator, ResourceAllocator<IndexesBlock>* inds_allocator, ResourceAllocator<FloatBlock>* float_allocator) final override;
2223
__forceinline void calculate_cell(glm::uvec3 xyz, uint32_t next_index, Cell* result, bool force, uint8_t mask);
2324
__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;
2425

BinaryMeshFitting/DebugScene.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ DebugScene::DebugScene(RenderInput* render_input)
5151
this->flat_quads = FLAT_QUADS;
5252
this->cull = true;
5353
this->gui_visible = true;
54-
this->update_focus = true;
54+
this->update_focus = false;
5555
this->line_width = 1.0f;
5656
this->specular_power = SPECULAR_POWER;
5757

@@ -606,6 +606,10 @@ void DebugScene::key_callback(int key, int scancode, int action, int mods)
606606
{
607607
camera.v_position = world.focus_point;
608608
}
609+
if (key == GLFW_KEY_SPACE)
610+
{
611+
update_focus = !update_focus;
612+
}
609613

610614
if (key == GLFW_KEY_PAGE_UP)
611615
{
@@ -742,7 +746,7 @@ void DebugScene::render_gui()
742746

743747
ImGui::Text("Group mult.:");
744748
ImGui::NextColumn();
745-
ImGui::SliderFloat("##lbl_octree_group", &world.properties.group_multiplier, 1.0f, 4.0f, 0, 0.5f);
749+
ImGui::SliderFloat("##lbl_octree_group", &world.properties.group_multiplier, 1.0f, 4.0f);
746750
ImGui::NextColumn();
747751

748752
ImGui::Text("Threads:");

BinaryMeshFitting/FPSCamera.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void FPSCamera::init(uint32_t width, uint32_t height, RenderInput* render_input)
1818
this->speed = 0.1f;
1919
this->smoothness = DEFAULT_SMOOTHNESS;
2020
this->rot_sensitivity = DEFAULT_ROTATION_SENSITIVITY;
21-
this->v_position = vec3(0, 0, 0);
21+
this->v_position = vec3(-3.12f, 306.58f, -412.99f);
2222
this->v_rot = vec3(0, 0, 0);
2323
this->v_velocity = vec3(0, 0, 0);
2424
this->v_turn_velocity = vec3(0, 0, 0);

BinaryMeshFitting/ImplicitSampler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace ImplicitFunctions
4444

4545
const float sphere(const float resolution, const glm::vec3& p)
4646
{
47-
const float r = resolution * 0.35f;
47+
const float r = resolution * 0.25f;
4848
return -(glm::length(p) - r);
4949
}
5050

@@ -60,7 +60,7 @@ namespace ImplicitFunctions
6060

6161
const float plane_y(const float resolution, const glm::vec3 & p)
6262
{
63-
return -p.y;
63+
return -p.y + p.x + p.z;
6464
}
6565

6666
glm::vec3 get_intersection(glm::vec3 v0, glm::vec3 v1, float s0, float s1)

BinaryMeshFitting/NoiseSampler.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,13 @@ const void NoiseSamplers::terrain2d_block(const Sampler & sampler, const float r
140140
const void NoiseSamplers::terrain2d_pert_block(const Sampler & sampler, const float resolution, const glm::vec3& p, const glm::ivec3& size, const float scale, float ** out, FastNoiseVectorSet * vectorset_out, float* dest_noise)
141141
{
142142
const float g_scale = 1.0f;
143-
const float ym = 0.5f;
143+
const float nm = 128.0f;
144144
NOISE_BLOCK(size.x, 1, size.z, p.x * g_scale, 0, p.z * g_scale, scale * g_scale, &dest_noise, vectorset_out);
145145

146-
sampler.noise_sampler->SetNoiseType(FastNoiseSIMD::NoiseType::ValueFractal);
147-
sampler.noise_sampler->SetPerturbType(FastNoiseSIMD::PerturbType::GradientFractal);
148-
sampler.noise_sampler->SetPerturbFractalOctaves(20);
149-
sampler.noise_sampler->SetPerturbAmp(0.707f);
146+
sampler.noise_sampler->SetNoiseType(FastNoiseSIMD::NoiseType::PerlinFractal);
147+
sampler.noise_sampler->SetPerturbType(FastNoiseSIMD::PerturbType::None);
148+
sampler.noise_sampler->SetPerturbFractalOctaves(4);
149+
sampler.noise_sampler->SetPerturbAmp(1.0f);
150150
sampler.noise_sampler->SetPerturbFrequency(0.5f);
151151
sampler.noise_sampler->SetFractalType(FastNoiseSIMD::FractalType::RigidMulti);
152152
sampler.noise_sampler->FillNoiseSet(dest_noise, vectorset_out);
@@ -159,11 +159,11 @@ const void NoiseSamplers::terrain2d_pert_block(const Sampler & sampler, const fl
159159
{
160160
for (int iy = 0; iy < size.y; iy++)
161161
{
162-
dy = ((float)iy * scale + p.y) * g_scale * ym;
162+
dy = ((float)iy * scale + p.y) * g_scale;
163163
for (int iz = 0; iz < size.z; iz++)
164164
{
165-
float n = dest_noise[ix * size.z + iz] * resolution * 0.125f;
166-
(*out)[ix * size.y * size.z + iy * size.z + iz] = -dy - n;
165+
float n = dest_noise[ix * size.z + iz];
166+
(*out)[ix * size.y * size.z + iy * size.z + iz] = -dy - n * nm;
167167
}
168168
}
169169
}
@@ -210,10 +210,10 @@ const void NoiseSamplers::terrain3d_pert_block(const Sampler & sampler, const fl
210210

211211
sampler.noise_sampler->SetNoiseType(FastNoiseSIMD::NoiseType::ValueFractal);
212212
sampler.noise_sampler->SetPerturbType(FastNoiseSIMD::PerturbType::GradientFractal);
213-
sampler.noise_sampler->SetFractalOctaves(8);
214-
sampler.noise_sampler->SetPerturbAmp(0.707f);
215-
sampler.noise_sampler->SetPerturbFrequency(0.25f);
216-
sampler.noise_sampler->SetFractalType(FastNoiseSIMD::FractalType::RigidMulti);
213+
sampler.noise_sampler->SetFractalOctaves(6);
214+
sampler.noise_sampler->SetPerturbAmp(1.0f);
215+
sampler.noise_sampler->SetPerturbFrequency(0.5f);
216+
sampler.noise_sampler->SetFractalType(FastNoiseSIMD::FractalType::FBM);
217217
sampler.noise_sampler->FillNoiseSet(dest_noise, vectorset_out);
218218

219219
if (!(*out))

BinaryMeshFitting/WorldOctree.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ __declspec(noinline) WorldProperties::WorldProperties()
3434
WorldOctree::WorldOctree()
3535
{
3636
using namespace std;
37-
//sampler = ImplicitFunctions::create_sampler(ImplicitFunctions::cuboid);
37+
//sampler = ImplicitFunctions::create_sampler(ImplicitFunctions::sphere);
3838
//sampler.block = ImplicitFunctions::cuboid_block;
3939
NoiseSamplers::create_sampler_terrain_pert_3d(&sampler);
4040
sampler.world_size = 256;
41-
focus_point = glm::vec3(0, 0, 0);
41+
focus_point = glm::vec3(-5.88f, 357.70f, -465.41f);
4242
generator_shutdown = false;
4343

4444
this->properties = WorldProperties();

BinaryMeshFitting/WorldStitcher.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,18 +272,28 @@ void WorldStitcher::stitch_indexes(OctreeNode* nodes[4], int direction, SmartCon
272272
int m1 = (n->cell.mask >> c1) & 1;
273273
int m2 = (n->cell.mask >> c2) & 1;
274274

275-
if (d_nodes[i]->size < min_size)
275+
if (n->size < min_size)
276276
{
277-
min_size = d_nodes[i]->size;
277+
min_size = n->size;
278278
min_index = i;
279279
flip = m1 == 1;
280280
sign_changed = (n->cell.edge_mask >> edge) & 1; //((m1 == 0 && m2 != 0) || (m1 != 0 && m2 == 0));
281281
}
282+
else if (n->size == min_size && n != d_nodes[i-1])
283+
{
284+
/*assert(((n->cell.edge_mask >> edge) & 1) == sign_changed);
285+
if (((n->cell.edge_mask >> edge) & 1) != sign_changed)
286+
{
287+
return;
288+
}*/
289+
}
282290
}
283291

284292
if (!sign_changed)
285293
return;
286294

295+
int indx = 0;
296+
bool connected[4] = { false, false, false, false };
287297

288298
for (int i = 0; i < 4; i++)
289299
{
@@ -305,9 +315,11 @@ void WorldStitcher::stitch_indexes(OctreeNode* nodes[4], int direction, SmartCon
305315
inds[i] = n->root->vi->vertices[ind + n->root->mesh_offset];
306316
else
307317
inds[i] = n->root->vi->vertices[ind & 0x3FFFFFFF];
318+
connected[i] = true;
308319
}
309320
else
310321
{
322+
connected[i] = false;
311323
int new_index = -1;
312324
for (int k = 0; k < 4; k++)
313325
{
@@ -337,6 +349,7 @@ void WorldStitcher::stitch_indexes(OctreeNode* nodes[4], int direction, SmartCon
337349
n->cell.edge_map |= new_index << (2 * edge);
338350
n->cell.edge_mask |= 1 << edge;
339351
}
352+
340353
}
341354

342355
if (QUADS)

0 commit comments

Comments
 (0)