From edfd5cd5439324febd7b5802330d50091e02a0ad Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 5 Aug 2013 09:45:00 -0700 Subject: [PATCH] fixed disappearing voxel bug --- interface/src/VoxelSystem.cpp | 29 +++++++++++++++++++++++------ interface/src/VoxelSystem.h | 1 + 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index 2631e255fb..fbc6760665 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -73,10 +73,29 @@ void VoxelSystem::nodeDeleted(VoxelNode* node) { } } +// returns an available index, starts by reusing a previously freed index, but if there isn't one available +// it will use the end of the VBO array and grow our accounting of that array. +// and makes the index available for some other node to use +glBufferIndex VoxelSystem::getNextBufferIndex() { + glBufferIndex output = GLBUFFER_INDEX_UNKNOWN; + // if there's a free index, use it... + if (_freeIndexes.size() > 0) { + output = _freeIndexes.back(); + _freeIndexes.pop_back(); + } else { + output = _voxelsInWriteArrays; + _voxelsInWriteArrays++; + } + return output; +} + +// Doesn't actually clean up the VBOs for the index, but does release responsibility of the index from the VoxelNode, +// and makes the index available for some other node to use void VoxelSystem::freeBufferIndex(glBufferIndex index) { _freeIndexes.push_back(index); } +// This will run through the list of _freeIndexes and reset their VBO array values to be "invisible". void VoxelSystem::clearFreeBufferIndexes() { for (int i = 0; i < _freeIndexes.size(); i++) { glBufferIndex nodeIndex = _freeIndexes[i]; @@ -246,12 +265,13 @@ void VoxelSystem::setupNewVoxelsForDrawing() { _callsToTreesToArrays++; if (_writeRenderFullVBO) { _voxelsInWriteArrays = 0; // reset our VBO + _freeIndexes.clear(); // reset our free indexes } _voxelsUpdated = newTreeToArrays(_tree->rootNode); _tree->clearDirtyBit(); // after we pull the trees into the array, we can consider the tree clean if (_writeRenderFullVBO) { - _abandonedVBOSlots = 0; // reset the count of our abandoned slots + _abandonedVBOSlots = 0; // reset the count of our abandoned slots, why is this here and not earlier???? } // since we called treeToArrays, we can assume that our VBO is in sync, and so partial updates to the VBOs are @@ -399,15 +419,13 @@ int VoxelSystem::updateNodeInArraysAsFullVBO(VoxelNode* node) { if (node->getShouldRender()) { glm::vec3 startVertex = node->getCorner(); float voxelScale = node->getScale(); - glBufferIndex nodeIndex = _voxelsInWriteArrays; + glBufferIndex nodeIndex = getNextBufferIndex(); // populate the array with points for the 8 vertices // and RGB color for each added vertex updateNodeInArrays(nodeIndex, startVertex, voxelScale, node->getColor()); node->setBufferIndex(nodeIndex); node->setVoxelSystem(this); - _writeVoxelDirtyArray[nodeIndex] = true; // just in case we switch to Partial mode - _voxelsInWriteArrays++; // our know vertices in the arrays return 1; // rendered } else { node->setBufferIndex(GLBUFFER_INDEX_UNKNOWN); @@ -444,10 +462,9 @@ int VoxelSystem::updateNodeInArraysAsPartialVBO(VoxelNode* node) { if (node->isKnownBufferIndex()) { nodeIndex = node->getBufferIndex(); } else { - nodeIndex = _voxelsInWriteArrays; + nodeIndex = getNextBufferIndex(); node->setBufferIndex(nodeIndex); node->setVoxelSystem(this); - _voxelsInWriteArrays++; } _writeVoxelDirtyArray[nodeIndex] = true; diff --git a/interface/src/VoxelSystem.h b/interface/src/VoxelSystem.h index ef7c33ee43..6c6bf261f9 100644 --- a/interface/src/VoxelSystem.h +++ b/interface/src/VoxelSystem.h @@ -200,6 +200,7 @@ private: void freeBufferIndex(glBufferIndex index); void clearFreeBufferIndexes(); + glBufferIndex getNextBufferIndex(); bool _falseColorizeBySource; int _dataSourceID;