From 9dc09de75eb99107ad84b3ea549c2fa35ee5e128 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Fri, 10 May 2013 09:32:56 -0700 Subject: [PATCH 1/2] make VoxelSystem::deleteVoxelAt() work properly --- interface/src/VoxelSystem.cpp | 27 +++++++++++++++++++++++---- libraries/voxels/src/VoxelNode.cpp | 1 + libraries/voxels/src/VoxelNode.h | 7 +++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index b5dab29029..592a70ca26 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -232,7 +232,7 @@ int VoxelSystem::newTreeToArrays(VoxelNode* node) { bool inChildBoundary = (distanceToNode <= childBoundary); shouldRender = (node->isLeaf() && inChildBoundary) || (inBoundary && !inChildBoundary); } - node->setShouldRender(shouldRender); + node->setShouldRender(shouldRender && !node->deleteMe()); // let children figure out their renderness for (int i = 0; i < NUMBER_OF_CHILDREN; i++) { if (node->getChildAtIndex(i)) { @@ -244,6 +244,15 @@ int VoxelSystem::newTreeToArrays(VoxelNode* node) { } else { voxelsUpdated += updateNodeInArraysAsPartialVBO(node); } + + // If the node has been asked to be deleted, but we've gotten to here, after updateNodeInArraysXXX() + // then it means our VBOs are "clean" and our vertices have been removed or not added. So we can now + // safely remove the node from the tree and actually delete it. + // otherwise honor our calculated shouldRender + if (node->deleteMe()) { + _tree->deleteVoxelCodeFromTree(node->getOctalCode()); + } + node->clearDirtyBit(); // always clear the dirty bit, even if it doesn't need to be rendered return voxelsUpdated; } @@ -901,9 +910,19 @@ void VoxelSystem::collectStatsForTreesAndVBOs() { void VoxelSystem::deleteVoxelAt(float x, float y, float z, float s) { - //printLog("VoxelSystem::deleteVoxelAt(%f,%f,%f,%f)\n",x,y,z,s); - _tree->deleteVoxelAt(x, y, z, s); - setupNewVoxelsForDrawing(); + printLog("VoxelSystem::deleteVoxelAt(%f,%f,%f,%f)\n",x,y,z,s); + + VoxelNode* node = _tree->getVoxelAt(x, y, z, s); + if (node) { + // tell the node we want it deleted + node->pleaseDeleteMe(); + + // tree is now dirty + _tree->setDirtyBit(); + + // redraw! + setupNewVoxelsForDrawing(); // do we even need to do this? Or will the next network receive kick in? + } }; VoxelNode* VoxelSystem::getVoxelAt(float x, float y, float z, float s) const { diff --git a/libraries/voxels/src/VoxelNode.cpp b/libraries/voxels/src/VoxelNode.cpp index 5af7f09ea2..5070a6442d 100644 --- a/libraries/voxels/src/VoxelNode.cpp +++ b/libraries/voxels/src/VoxelNode.cpp @@ -44,6 +44,7 @@ void VoxelNode::init(unsigned char * octalCode) { _glBufferIndex = GLBUFFER_INDEX_UNKNOWN; _isDirty = true; _shouldRender = false; + _deleteMe = false; calculateAABox(); } diff --git a/libraries/voxels/src/VoxelNode.h b/libraries/voxels/src/VoxelNode.h index 51af8f6379..4ac083bd0f 100644 --- a/libraries/voxels/src/VoxelNode.h +++ b/libraries/voxels/src/VoxelNode.h @@ -27,6 +27,7 @@ private: glBufferIndex _glBufferIndex; bool _isDirty; bool _shouldRender; + bool _deleteMe; AABox _box; unsigned char* _octalCode; VoxelNode* _children[8]; @@ -66,9 +67,15 @@ public: glBufferIndex getBufferIndex() const { return _glBufferIndex; }; bool isKnownBufferIndex() const { return (_glBufferIndex != GLBUFFER_INDEX_UNKNOWN); }; void setBufferIndex(glBufferIndex index) { _glBufferIndex = index; }; + + // Used by VoxelSystem for rendering in/out of view and LOD void setShouldRender(bool shouldRender); bool getShouldRender() const { return _shouldRender; } + // Used by VoxelSystem to mark a node as to be deleted on next render pass + void pleaseDeleteMe() { _deleteMe = true; }; + bool deleteMe() const { return _deleteMe; } + #ifndef NO_FALSE_COLOR // !NO_FALSE_COLOR means, does have false color void setFalseColor(colorPart red, colorPart green, colorPart blue); void setFalseColored(bool isFalseColored); From 3af8a1b8f443555093697562f4b1043bf95dba94 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Fri, 10 May 2013 10:03:28 -0700 Subject: [PATCH 2/2] code review fixes --- interface/src/VoxelSystem.cpp | 6 +++--- libraries/voxels/src/VoxelNode.cpp | 2 +- libraries/voxels/src/VoxelNode.h | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index 592a70ca26..e473961c8e 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -232,7 +232,7 @@ int VoxelSystem::newTreeToArrays(VoxelNode* node) { bool inChildBoundary = (distanceToNode <= childBoundary); shouldRender = (node->isLeaf() && inChildBoundary) || (inBoundary && !inChildBoundary); } - node->setShouldRender(shouldRender && !node->deleteMe()); + node->setShouldRender(shouldRender && !node->isStagedForDeletion()); // let children figure out their renderness for (int i = 0; i < NUMBER_OF_CHILDREN; i++) { if (node->getChildAtIndex(i)) { @@ -249,7 +249,7 @@ int VoxelSystem::newTreeToArrays(VoxelNode* node) { // then it means our VBOs are "clean" and our vertices have been removed or not added. So we can now // safely remove the node from the tree and actually delete it. // otherwise honor our calculated shouldRender - if (node->deleteMe()) { + if (node->isStagedForDeletion()) { _tree->deleteVoxelCodeFromTree(node->getOctalCode()); } @@ -915,7 +915,7 @@ void VoxelSystem::deleteVoxelAt(float x, float y, float z, float s) { VoxelNode* node = _tree->getVoxelAt(x, y, z, s); if (node) { // tell the node we want it deleted - node->pleaseDeleteMe(); + node->stageForDeletion(); // tree is now dirty _tree->setDirtyBit(); diff --git a/libraries/voxels/src/VoxelNode.cpp b/libraries/voxels/src/VoxelNode.cpp index 5070a6442d..19e6a12aa5 100644 --- a/libraries/voxels/src/VoxelNode.cpp +++ b/libraries/voxels/src/VoxelNode.cpp @@ -44,7 +44,7 @@ void VoxelNode::init(unsigned char * octalCode) { _glBufferIndex = GLBUFFER_INDEX_UNKNOWN; _isDirty = true; _shouldRender = false; - _deleteMe = false; + _isStagedForDeletion = false; calculateAABox(); } diff --git a/libraries/voxels/src/VoxelNode.h b/libraries/voxels/src/VoxelNode.h index 4ac083bd0f..c07cb528e6 100644 --- a/libraries/voxels/src/VoxelNode.h +++ b/libraries/voxels/src/VoxelNode.h @@ -27,7 +27,7 @@ private: glBufferIndex _glBufferIndex; bool _isDirty; bool _shouldRender; - bool _deleteMe; + bool _isStagedForDeletion; AABox _box; unsigned char* _octalCode; VoxelNode* _children[8]; @@ -73,8 +73,8 @@ public: bool getShouldRender() const { return _shouldRender; } // Used by VoxelSystem to mark a node as to be deleted on next render pass - void pleaseDeleteMe() { _deleteMe = true; }; - bool deleteMe() const { return _deleteMe; } + void stageForDeletion() { _isStagedForDeletion = true; }; + bool isStagedForDeletion() const { return _isStagedForDeletion; } #ifndef NO_FALSE_COLOR // !NO_FALSE_COLOR means, does have false color void setFalseColor(colorPart red, colorPart green, colorPart blue);