Skip to content

Commit 07153e6

Browse files
author
Lin
committed
Naive stitching before redoing the watcher to be managed. Stitches currently flicker and in the rare case, the program crashes.
1 parent 9103044 commit 07153e6

20 files changed

+859
-77
lines changed

BinaryMeshFitting.sln

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,7 @@ Global
7373
GlobalSection(Performance) = preSolution
7474
HasPerformanceSessions = true
7575
EndGlobalSection
76+
GlobalSection(Performance) = preSolution
77+
HasPerformanceSessions = true
78+
EndGlobalSection
7679
EndGlobal

BinaryMeshFitting/BinaryMeshFitting.vcxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
<ClCompile Include="CubicChunk.cpp" />
158158
<ClCompile Include="DebugScene.cpp" />
159159
<ClCompile Include="DualChunk.cpp" />
160+
<ClCompile Include="DynamicGLChunk.cpp" />
160161
<ClCompile Include="Entry.cpp" />
161162
<ClCompile Include="FPSCamera.cpp" />
162163
<ClCompile Include="GLChunk.cpp" />
@@ -185,6 +186,7 @@
185186
</ClCompile>
186187
<ClCompile Include="WorldOctree.cpp" />
187188
<ClCompile Include="WorldOctreeNode.cpp" />
189+
<ClCompile Include="WorldStitcher.cpp" />
188190
<ClCompile Include="WorldWatcher.cpp" />
189191
<ClInclude Include="BinaryChunk.hpp" />
190192
<ClInclude Include="Chunk.hpp" />
@@ -195,6 +197,7 @@
195197
<ClInclude Include="Debug.h" />
196198
<ClInclude Include="DefaultOptions.h" />
197199
<ClInclude Include="DualChunk.hpp" />
200+
<ClInclude Include="DynamicGLChunk.hpp" />
198201
<ClInclude Include="ResourceAllocator.hpp" />
199202
<ClInclude Include="GLChunk.hpp" />
200203
<ClInclude Include="GUI\imconfig.h" />
@@ -227,6 +230,7 @@
227230
</ClInclude>
228231
<ClInclude Include="WorldOctree.hpp" />
229232
<ClInclude Include="WorldOctreeNode.hpp" />
233+
<ClInclude Include="WorldStitcher.hpp" />
230234
<ClInclude Include="WorldWatcher.hpp" />
231235
</ItemGroup>
232236
<ItemGroup>

BinaryMeshFitting/BinaryMeshFitting.vcxproj.filters

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@
8181
<ClCompile Include="ChunkGenerator.cpp">
8282
<Filter>Source Files</Filter>
8383
</ClCompile>
84+
<ClCompile Include="WorldStitcher.cpp">
85+
<Filter>Source Files</Filter>
86+
</ClCompile>
87+
<ClCompile Include="DynamicGLChunk.cpp">
88+
<Filter>Source Files</Filter>
89+
</ClCompile>
8490
</ItemGroup>
8591
<ItemGroup>
8692
<ClInclude Include="Core.hpp">
@@ -203,6 +209,12 @@
203209
<ClInclude Include="ChunkBlocks.hpp">
204210
<Filter>Header Files</Filter>
205211
</ClInclude>
212+
<ClInclude Include="WorldStitcher.hpp">
213+
<Filter>Header Files</Filter>
214+
</ClInclude>
215+
<ClInclude Include="DynamicGLChunk.hpp">
216+
<Filter>Header Files</Filter>
217+
</ClInclude>
206218
</ItemGroup>
207219
<ItemGroup>
208220
<None Include="MemoryPool.tcc">

BinaryMeshFitting/ChunkGenerator.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ ChunkGenerator::~ChunkGenerator()
1919
void ChunkGenerator::init(WorldOctree* _world)
2020
{
2121
this->world = _world;
22+
this->stitcher.init();
23+
this->generating = false;
2224
_thread = std::thread(std::bind(&ChunkGenerator::update, this));
2325
}
2426

@@ -35,7 +37,8 @@ void ChunkGenerator::update()
3537
{
3638
auto now = std::chrono::system_clock::now();
3739

38-
process_queue();
40+
//if (!generating)
41+
process_queue();
3942

4043
End:
4144
auto elapsed = std::chrono::system_clock::now() - now;
@@ -68,6 +71,10 @@ void ChunkGenerator::process_queue()
6871
local_queue = queue;
6972
queue.clear();
7073
}
74+
if (local_queue.size() == 0)
75+
{
76+
return;
77+
}
7178

7279
// Trim the local queue
7380
int count = (int)local_queue.size();
@@ -118,12 +125,24 @@ void ChunkGenerator::process_queue()
118125
{
119126
local_queue[i]->generation_stage = GENERATION_STAGES_NEEDS_UPLOAD;
120127
}
128+
129+
if (stitcher.stage == STITCHING_STAGES_READY)
130+
{
131+
std::unique_lock<std::mutex> stitcher_lock(stitcher._mutex);
132+
stitcher.stitch_all(&world->octree);
133+
stitcher.format();
134+
stitcher.stage = STITCHING_STAGES_NEEDS_UPLOAD;
135+
}
136+
137+
//assert(stitcher.stage == STITCHING_STAGES_READY);
121138
}
122139

123140
void ChunkGenerator::add_batch(SmartContainer<WorldOctreeNode*>& batch)
124141
{
125142
std::unique_lock<std::mutex> lock(_mutex);
126143

144+
generating = true;
145+
127146
int count = (int)batch.count;
128147
for (int i = 0; i < count; i++)
129148
{

BinaryMeshFitting/ChunkGenerator.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "SmartContainer.hpp"
1111
#include "ResourceAllocator.hpp"
1212
#include "ChunkBlocks.hpp"
13+
#include "WorldStitcher.hpp"
1314

1415
class ChunkGenerator : public ThreadDebug
1516
{
@@ -32,6 +33,9 @@ class ChunkGenerator : public ThreadDebug
3233
ResourceAllocator<CellsBlock> cell_allocator;
3334
ResourceAllocator<IndexesBlock> inds_allocator;
3435

36+
WorldStitcher stitcher;
37+
std::atomic<bool> generating;
38+
3539
private:
3640
class WorldOctree* world;
3741
std::thread _thread;

BinaryMeshFitting/CubicChunk.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void CubicChunk::generate_samples(ResourceAllocator<BinaryBlock>* binary_allocat
7171
float delta = size / (float)dim;
7272
const float res = sampler.world_size;
7373
FloatBlock* float_block = float_allocator->new_element();
74-
float_block->init(dimp1 * dimp1 * dimp1, dimp1 * dimp1 * dimp1);
74+
float_block->init(dimp1 * dimp1 * dimp1, dimp1 * dimp1);
7575

7676
sampler.block(res, pos, ivec3(dimp1, dimp1, dimp1), delta * scale, &float_block->data, &float_block->vectorset, float_block->dest_noise);
7777

@@ -151,6 +151,7 @@ void CubicChunk::generate_dual_vertices(ResourceAllocator<VerticesIndicesBlock>*
151151
}
152152

153153
uint32_t dimp1 = dim + 1;
154+
154155
uint32_t count = dim * dim * dim;
155156
uint32_t countp1 = dimp1 * dimp1 * dimp1;
156157
uint32_t z_per_y = ((dim + 32)) / 32;
@@ -755,7 +756,7 @@ void CubicChunk::generate_base_mesh(ResourceAllocator<VerticesIndicesBlock>* vi_
755756
Cell& c = cells[i];
756757
if (c.xyz.x == 27 && c.xyz.y == 5 && c.xyz.z == 0)
757758
{
758-
printf("F");
759+
//printf("F");
759760
}
760761
// TODO: Get xyz from cell vertices
761762
uvec3 xyz = c.xyz;

BinaryMeshFitting/CubicChunk.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class CubicChunk : public Chunk
1111
BinaryBlock* binary_block;
1212
CellsBlock* cell_block;
1313
DualNode octree;
14-
MemoryPool<DualNode, 8192> node_pool;
14+
MemoryPool<DualNode> node_pool;
1515

1616
CubicChunk();
1717
CubicChunk(glm::vec3 pos, float size, int level, Sampler& sampler, bool produce_quads);

BinaryMeshFitting/DebugScene.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ int DebugScene::update(RenderInput* input)
385385
if(update_focus)
386386
{
387387
std::unique_lock<std::mutex> l(world.watcher._mutex);
388-
world.watcher.focus_pos = camera.v_position;
388+
world.watcher.focus_pos = camera.v_position + camera.v_velocity * 0.0f;
389389
}
390390

391391
return 0;
@@ -509,6 +509,12 @@ void DebugScene::render_world()
509509
n = n->renderable_next;
510510
}
511511

512+
glBindVertexArray(world.watcher.generator.stitcher.gl_chunk.vao);
513+
if (!flat_quads || !QUADS)
514+
glDrawElements((QUADS ? GL_QUADS : GL_TRIANGLES), world.watcher.generator.stitcher.gl_chunk.p_count, GL_UNSIGNED_INT, 0);
515+
else
516+
glDrawArrays(GL_QUADS, 0, world.watcher.generator.stitcher.gl_chunk.p_count);
517+
512518
//glDisable(GL_POLYGON_OFFSET_FILL);
513519
}
514520
if (fillmode == FILL_MODE_BOTH)
@@ -542,6 +548,12 @@ void DebugScene::render_world()
542548
}
543549
n = n->renderable_next;
544550
}
551+
552+
glBindVertexArray(world.watcher.generator.stitcher.gl_chunk.vao);
553+
if (!flat_quads || !QUADS)
554+
glDrawElements((QUADS ? GL_QUADS : GL_TRIANGLES), world.watcher.generator.stitcher.gl_chunk.p_count, GL_UNSIGNED_INT, 0);
555+
else
556+
glDrawArrays(GL_QUADS, 0, world.watcher.generator.stitcher.gl_chunk.p_count);
545557
}
546558

547559
glBindVertexArray(0);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include "PCH.h"
2+
#include "DynamicGLChunk.hpp"
3+
4+
#define DEFAULT_V_COUNT 524288
5+
6+
DynamicGLChunk::DynamicGLChunk()
7+
{
8+
initialized = false;
9+
normals = true;
10+
11+
vao = 0;
12+
v_vbo = 0;
13+
n_vbo = 0;
14+
ibo = 0;
15+
16+
v_count = 0;
17+
p_count = 0;
18+
vbo_size = 0;
19+
ibo_size = 0;
20+
}
21+
22+
DynamicGLChunk::~DynamicGLChunk()
23+
{
24+
destroy();
25+
}
26+
27+
void DynamicGLChunk::init(bool _normals, bool _colors)
28+
{
29+
if (initialized)
30+
return;
31+
32+
normals = _normals;
33+
colors = _colors;
34+
35+
glGenBuffers(1, &v_vbo);
36+
if (normals)
37+
glGenBuffers(1, &n_vbo);
38+
if (colors)
39+
glGenBuffers(1, &c_vbo);
40+
41+
glGenVertexArrays(1, &vao);
42+
glBindVertexArray(0);
43+
44+
initialized = true;
45+
46+
VertexRegion* base = region_pool.newElement(0, DEFAULT_V_COUNT);
47+
free_regions.push_back(base);
48+
}
49+
50+
void DynamicGLChunk::destroy()
51+
{
52+
if (initialized)
53+
{
54+
glDeleteVertexArrays(1, &vao);
55+
glDeleteBuffers(4, &v_vbo);
56+
}
57+
initialized = 0;
58+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#pragma once
2+
3+
#include <gl/glew.h>
4+
#define GLFW_DLL
5+
#include <GLFW/glfw3.h>
6+
#include <glm/glm.hpp>
7+
#include "SmartContainer.hpp"
8+
#include "Vertices.hpp"
9+
#include "LinkedList.hpp"
10+
#include "MemoryPool.h"
11+
12+
struct VertexRegion : public LinkedNode<VertexRegion>
13+
{
14+
uint32_t start;
15+
uint32_t count;
16+
17+
inline VertexRegion()
18+
{
19+
start = 0;
20+
count = 0;
21+
}
22+
23+
inline VertexRegion(uint32_t _start, uint32_t _count) : start(_start), count(_count)
24+
{
25+
}
26+
};
27+
28+
class DynamicGLChunk
29+
{
30+
public:
31+
bool initialized;
32+
bool normals;
33+
bool colors;
34+
35+
// Important -- Do not change structure!
36+
GLuint vao;
37+
GLuint v_vbo;
38+
GLuint n_vbo;
39+
GLuint c_vbo;
40+
GLuint ibo;
41+
42+
uint32_t v_count;
43+
uint32_t p_count;
44+
uint32_t vbo_size;
45+
uint32_t ibo_size;
46+
47+
SmartContainer<glm::vec3> p_data;
48+
SmartContainer<glm::vec3> n_data;
49+
SmartContainer<glm::vec3> c_data;
50+
51+
DynamicGLChunk();
52+
~DynamicGLChunk();
53+
void init(bool _normals, bool _colors);
54+
void destroy();
55+
56+
57+
58+
MemoryPool<VertexRegion> region_pool;
59+
LinkedList<VertexRegion> used_regions;
60+
LinkedList<VertexRegion> free_regions;
61+
};

0 commit comments

Comments
 (0)