diff --git a/library/PolyVoxCore/include/PolyVoxImpl/Block.h b/library/PolyVoxCore/include/PolyVoxImpl/Block.h index 617f631d..924210d2 100644 --- a/library/PolyVoxCore/include/PolyVoxImpl/Block.h +++ b/library/PolyVoxCore/include/PolyVoxImpl/Block.h @@ -34,7 +34,6 @@ namespace PolyVox { public: POLYVOX_SHARED_PTR< BlockData > m_pBlockData; - VoxelType m_pHomogenousValue; bool m_bIsShared; bool m_bIsPotentiallySharable; }; diff --git a/library/PolyVoxCore/include/PolyVoxImpl/BlockData.inl b/library/PolyVoxCore/include/PolyVoxImpl/BlockData.inl index 5317747d..b5fe1913 100644 --- a/library/PolyVoxCore/include/PolyVoxImpl/BlockData.inl +++ b/library/PolyVoxCore/include/PolyVoxImpl/BlockData.inl @@ -77,9 +77,14 @@ namespace PolyVox return *this; } + //If this fails an exception will be thrown. Memory is not + //allocated and there is nothing else in this class to clean up + m_tData = new VoxelType[rhs.m_uSideLength * rhs.m_uSideLength * rhs.m_uSideLength]; + + //Copy the data m_uSideLength = rhs.m_uSideLength; - m_uSideLengthPower = rhs.m_uSideLengthPower; - memcpy(m_tData, rhs.m_tData, m_uSideLength * m_uSideLength * m_uSideLength); + m_uSideLengthPower = rhs.m_uSideLengthPower; + memcpy(m_tData, rhs.m_tData, m_uSideLength * m_uSideLength * m_uSideLength * sizeof(VoxelType)); return *this; } diff --git a/library/PolyVoxCore/include/Volume.inl b/library/PolyVoxCore/include/Volume.inl index 8e0dbe22..b7b72a67 100644 --- a/library/PolyVoxCore/include/Volume.inl +++ b/library/PolyVoxCore/include/Volume.inl @@ -78,7 +78,6 @@ namespace PolyVox m_pBlocks[i].m_pBlockData = getHomogenousBlockData(0); m_pBlocks[i].m_bIsShared = true; m_pBlocks[i].m_bIsPotentiallySharable = false; - m_pBlocks[i].m_pHomogenousValue = 0; } } @@ -171,26 +170,27 @@ namespace PolyVox blockZ * m_uSideLengthInBlocks * m_uSideLengthInBlocks ]; - if(block.m_bIsShared) + //It's quite possible that the user might attempt to set a voxel to it's current value. + //We test for this case firstly because it could help performance, but more importantly + //because it lets us avoid unsharing blocks unnecessarily. + if(block.m_pBlockData->getVoxelAt(xOffset, yOffset, zOffset) != tValue) { - const VoxelType tHomogenousValue = block.m_pHomogenousValue; - if(tHomogenousValue != tValue) + if(block.m_bIsShared) { - POLYVOX_SHARED_PTR< BlockData > pNewBlockData(new BlockData(m_uBlockSideLength)); + POLYVOX_SHARED_PTR< BlockData > pNewBlockData(new BlockData(*(block.m_pBlockData))); block.m_pBlockData = pNewBlockData; block.m_bIsShared = false; block.m_bIsPotentiallySharable = false; - block.m_pBlockData->fill(tHomogenousValue); block.m_pBlockData->setVoxelAt(xOffset,yOffset,zOffset, tValue); } + else + { + block.m_pBlockData->setVoxelAt(xOffset,yOffset,zOffset, tValue); + //There is a chance that setting this voxel makes the block homogenous and therefore shareable. But checking + //this will take some time, so for now just set a flag. + block.m_bIsPotentiallySharable = true; + } } - else - { - block.m_pBlockData->setVoxelAt(xOffset,yOffset,zOffset, tValue); - //There is a chance that setting this voxel makes the block homogenous and therefore shareable. But checking - //this will take some time, so for now just set a flag. - block.m_bIsPotentiallySharable = true; - } } template @@ -221,7 +221,6 @@ namespace PolyVox delete block.m_pBlockData; block.m_pBlockData = getHomogenousBlockData(homogeneousValue); - block.m_pHomogenousValue = homogeneousValue; block.m_bIsShared = true; }