diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl b/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl index 81d5baa9..29bf4644 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl @@ -103,21 +103,28 @@ namespace PolyVox BaseVolume::template Sampler< LargeVolume >::setPosition(xPos, yPos, zPos); // Then we update the voxel pointer - const int32_t uXBlock = this->mXPosInVolume >> this->mVolume->m_uBlockSideLengthPower; - const int32_t uYBlock = this->mYPosInVolume >> this->mVolume->m_uBlockSideLengthPower; - const int32_t uZBlock = this->mZPosInVolume >> this->mVolume->m_uBlockSideLengthPower; + if(this->isCurrentPositionValid()) + { + const int32_t uXBlock = this->mXPosInVolume >> this->mVolume->m_uBlockSideLengthPower; + const int32_t uYBlock = this->mYPosInVolume >> this->mVolume->m_uBlockSideLengthPower; + const int32_t uZBlock = this->mZPosInVolume >> this->mVolume->m_uBlockSideLengthPower; - const uint16_t uXPosInBlock = static_cast(this->mXPosInVolume - (uXBlock << this->mVolume->m_uBlockSideLengthPower)); - const uint16_t uYPosInBlock = static_cast(this->mYPosInVolume - (uYBlock << this->mVolume->m_uBlockSideLengthPower)); - const uint16_t uZPosInBlock = static_cast(this->mZPosInVolume - (uZBlock << this->mVolume->m_uBlockSideLengthPower)); + const uint16_t uXPosInBlock = static_cast(this->mXPosInVolume - (uXBlock << this->mVolume->m_uBlockSideLengthPower)); + const uint16_t uYPosInBlock = static_cast(this->mYPosInVolume - (uYBlock << this->mVolume->m_uBlockSideLengthPower)); + const uint16_t uZPosInBlock = static_cast(this->mZPosInVolume - (uZBlock << this->mVolume->m_uBlockSideLengthPower)); - const uint32_t uVoxelIndexInBlock = uXPosInBlock + - uYPosInBlock * this->mVolume->m_uBlockSideLength + - uZPosInBlock * this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength; + const uint32_t uVoxelIndexInBlock = uXPosInBlock + + uYPosInBlock * this->mVolume->m_uBlockSideLength + + uZPosInBlock * this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength; - Block* pUncompressedCurrentBlock = this->mVolume->getUncompressedBlock(uXBlock, uYBlock, uZBlock); + Block* pUncompressedCurrentBlock = this->mVolume->getUncompressedBlock(uXBlock, uYBlock, uZBlock); - mCurrentVoxel = pUncompressedCurrentBlock->m_tUncompressedData + uVoxelIndexInBlock; + mCurrentVoxel = pUncompressedCurrentBlock->m_tUncompressedData + uVoxelIndexInBlock; + } + else + { + mCurrentVoxel = 0; + } } template @@ -145,7 +152,7 @@ namespace PolyVox BaseVolume::template Sampler< LargeVolume >::movePositiveX(); // Then we update the voxel pointer - if((this->isCurrentPositionValid()) && ((this->mXPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) + if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mXPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) { //No need to compute new block. ++mCurrentVoxel; @@ -164,7 +171,7 @@ namespace PolyVox BaseVolume::template Sampler< LargeVolume >::movePositiveY(); // Then we update the voxel pointer - if((this->isCurrentPositionValid()) && ((this->mYPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) + if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mYPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) { //No need to compute new block. mCurrentVoxel += this->mVolume->m_uBlockSideLength; @@ -183,7 +190,7 @@ namespace PolyVox BaseVolume::template Sampler< LargeVolume >::movePositiveZ(); // Then we update the voxel pointer - if((this->isCurrentPositionValid()) && ((this->mZPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) + if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mZPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) { //No need to compute new block. mCurrentVoxel += this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength; @@ -202,7 +209,7 @@ namespace PolyVox BaseVolume::template Sampler< LargeVolume >::moveNegativeX(); // Then we update the voxel pointer - if((this->isCurrentPositionValid()) && ((this->mXPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) + if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mXPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) { //No need to compute new block. --mCurrentVoxel; @@ -221,7 +228,7 @@ namespace PolyVox BaseVolume::template Sampler< LargeVolume >::moveNegativeY(); // Then we update the voxel pointer - if((this->isCurrentPositionValid()) && ((this->mYPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) + if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mYPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) { //No need to compute new block. mCurrentVoxel -= this->mVolume->m_uBlockSideLength; @@ -240,7 +247,7 @@ namespace PolyVox BaseVolume::template Sampler< LargeVolume >::moveNegativeZ(); // Then we update the voxel pointer - if((this->isCurrentPositionValid()) && ((this->mZPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) + if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mZPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) { //No need to compute new block. mCurrentVoxel -= this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength; diff --git a/library/PolyVoxCore/include/PolyVoxCore/RawVolumeSampler.inl b/library/PolyVoxCore/include/PolyVoxCore/RawVolumeSampler.inl index 165a1505..ee39efe6 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/RawVolumeSampler.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/RawVolumeSampler.inl @@ -68,16 +68,23 @@ namespace PolyVox BaseVolume::template Sampler< RawVolume >::setPosition(xPos, yPos, zPos); // Then we update the voxel pointer - const Vector3DInt32& v3dLowerCorner = this->mVolume->m_regValidRegion.getLowerCorner(); - int32_t iLocalXPos = xPos - v3dLowerCorner.getX(); - int32_t iLocalYPos = yPos - v3dLowerCorner.getY(); - int32_t iLocalZPos = zPos - v3dLowerCorner.getZ(); + if(this->isCurrentPositionValid()) + { + const Vector3DInt32& v3dLowerCorner = this->mVolume->m_regValidRegion.getLowerCorner(); + int32_t iLocalXPos = xPos - v3dLowerCorner.getX(); + int32_t iLocalYPos = yPos - v3dLowerCorner.getY(); + int32_t iLocalZPos = zPos - v3dLowerCorner.getZ(); - const int32_t uVoxelIndex = iLocalXPos + - iLocalYPos * this->mVolume->getWidth() + - iLocalZPos * this->mVolume->getWidth() * this->mVolume->getHeight(); + const int32_t uVoxelIndex = iLocalXPos + + iLocalYPos * this->mVolume->getWidth() + + iLocalZPos * this->mVolume->getWidth() * this->mVolume->getHeight(); - mCurrentVoxel = this->mVolume->m_pData + uVoxelIndex; + mCurrentVoxel = this->mVolume->m_pData + uVoxelIndex; + } + else + { + mCurrentVoxel = 0; + } } template @@ -102,7 +109,7 @@ namespace PolyVox BaseVolume::template Sampler< RawVolume >::movePositiveX(); // Then we update the voxel pointer - if(this->isCurrentPositionValid()) + if(this->isCurrentPositionValid() && mCurrentVoxel ) { ++mCurrentVoxel; } @@ -119,7 +126,7 @@ namespace PolyVox BaseVolume::template Sampler< RawVolume >::movePositiveY(); // Then we update the voxel pointer - if(this->isCurrentPositionValid()) + if(this->isCurrentPositionValid() && mCurrentVoxel ) { mCurrentVoxel += this->mVolume->getWidth(); } @@ -136,7 +143,7 @@ namespace PolyVox BaseVolume::template Sampler< RawVolume >::movePositiveZ(); // Then we update the voxel pointer - if(this->isCurrentPositionValid()) + if(this->isCurrentPositionValid() && mCurrentVoxel ) { mCurrentVoxel += this->mVolume->getWidth() * this->mVolume->getHeight(); } @@ -153,7 +160,7 @@ namespace PolyVox BaseVolume::template Sampler< RawVolume >::moveNegativeX(); // Then we update the voxel pointer - if(this->isCurrentPositionValid()) + if(this->isCurrentPositionValid() && mCurrentVoxel ) { --mCurrentVoxel; } @@ -170,7 +177,7 @@ namespace PolyVox BaseVolume::template Sampler< RawVolume >::moveNegativeY(); // Then we update the voxel pointer - if(this->isCurrentPositionValid()) + if(this->isCurrentPositionValid() && mCurrentVoxel ) { mCurrentVoxel -= this->mVolume->getWidth(); } @@ -187,7 +194,7 @@ namespace PolyVox BaseVolume::template Sampler< RawVolume >::moveNegativeZ(); // Then we update the voxel pointer - if(this->isCurrentPositionValid()) + if(this->isCurrentPositionValid() && mCurrentVoxel ) { mCurrentVoxel -= this->mVolume->getWidth() * this->mVolume->getHeight(); } diff --git a/library/PolyVoxCore/include/PolyVoxCore/SimpleVolume.inl b/library/PolyVoxCore/include/PolyVoxCore/SimpleVolume.inl index ae90c549..62b48b4a 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/SimpleVolume.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/SimpleVolume.inl @@ -306,6 +306,10 @@ namespace PolyVox uBlockY -= m_regValidRegionInBlocks.getLowerCorner().getY(); uBlockZ -= m_regValidRegionInBlocks.getLowerCorner().getZ(); + assert(uBlockX >= 0); + assert(uBlockY >= 0); + assert(uBlockZ >= 0); + //Compute the block index uint32_t uBlockIndex = uBlockX + diff --git a/library/PolyVoxCore/include/PolyVoxCore/SimpleVolumeSampler.inl b/library/PolyVoxCore/include/PolyVoxCore/SimpleVolumeSampler.inl index 395f104b..651ef074 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/SimpleVolumeSampler.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/SimpleVolumeSampler.inl @@ -117,21 +117,28 @@ namespace PolyVox BaseVolume::template Sampler< SimpleVolume >::setPosition(xPos, yPos, zPos); // Then we update the voxel pointer - const int32_t uXBlock = this->mXPosInVolume >> this->mVolume->m_uBlockSideLengthPower; - const int32_t uYBlock = this->mYPosInVolume >> this->mVolume->m_uBlockSideLengthPower; - const int32_t uZBlock = this->mZPosInVolume >> this->mVolume->m_uBlockSideLengthPower; + if(this->isCurrentPositionValid()) + { + const int32_t uXBlock = this->mXPosInVolume >> this->mVolume->m_uBlockSideLengthPower; + const int32_t uYBlock = this->mYPosInVolume >> this->mVolume->m_uBlockSideLengthPower; + const int32_t uZBlock = this->mZPosInVolume >> this->mVolume->m_uBlockSideLengthPower; - const uint16_t uXPosInBlock = static_cast(this->mXPosInVolume - (uXBlock << this->mVolume->m_uBlockSideLengthPower)); - const uint16_t uYPosInBlock = static_cast(this->mYPosInVolume - (uYBlock << this->mVolume->m_uBlockSideLengthPower)); - const uint16_t uZPosInBlock = static_cast(this->mZPosInVolume - (uZBlock << this->mVolume->m_uBlockSideLengthPower)); + const uint16_t uXPosInBlock = static_cast(this->mXPosInVolume - (uXBlock << this->mVolume->m_uBlockSideLengthPower)); + const uint16_t uYPosInBlock = static_cast(this->mYPosInVolume - (uYBlock << this->mVolume->m_uBlockSideLengthPower)); + const uint16_t uZPosInBlock = static_cast(this->mZPosInVolume - (uZBlock << this->mVolume->m_uBlockSideLengthPower)); - const uint32_t uVoxelIndexInBlock = uXPosInBlock + - uYPosInBlock * this->mVolume->m_uBlockSideLength + - uZPosInBlock * this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength; + const uint32_t uVoxelIndexInBlock = uXPosInBlock + + uYPosInBlock * this->mVolume->m_uBlockSideLength + + uZPosInBlock * this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength; - Block* pUncompressedCurrentBlock = this->mVolume->getUncompressedBlock(uXBlock, uYBlock, uZBlock); + Block* pUncompressedCurrentBlock = this->mVolume->getUncompressedBlock(uXBlock, uYBlock, uZBlock); - mCurrentVoxel = pUncompressedCurrentBlock->m_tUncompressedData + uVoxelIndexInBlock; + mCurrentVoxel = pUncompressedCurrentBlock->m_tUncompressedData + uVoxelIndexInBlock; + } + else + { + mCurrentVoxel = 0; + } } /** @@ -164,7 +171,7 @@ namespace PolyVox BaseVolume::template Sampler< SimpleVolume >::movePositiveX(); // Then we update the voxel pointer - if((this->isCurrentPositionValid()) && ((this->mXPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) + if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mXPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) { //No need to compute new block. ++mCurrentVoxel; @@ -183,7 +190,7 @@ namespace PolyVox BaseVolume::template Sampler< SimpleVolume >::movePositiveY(); // Then we update the voxel pointer - if((this->isCurrentPositionValid()) && ((this->mYPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) + if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mYPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) { //No need to compute new block. mCurrentVoxel += this->mVolume->m_uBlockSideLength; @@ -202,7 +209,7 @@ namespace PolyVox BaseVolume::template Sampler< SimpleVolume >::movePositiveZ(); // Then we update the voxel pointer - if((this->isCurrentPositionValid()) && ((this->mZPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) + if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mZPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) { //No need to compute new block. mCurrentVoxel += this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength; @@ -221,7 +228,7 @@ namespace PolyVox BaseVolume::template Sampler< SimpleVolume >::moveNegativeX(); // Then we update the voxel pointer - if((this->isCurrentPositionValid()) && ((this->mXPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) + if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mXPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) { //No need to compute new block. --mCurrentVoxel; @@ -240,7 +247,7 @@ namespace PolyVox BaseVolume::template Sampler< SimpleVolume >::moveNegativeY(); // Then we update the voxel pointer - if((this->isCurrentPositionValid()) && ((this->mYPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) + if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mYPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) { //No need to compute new block. mCurrentVoxel -= this->mVolume->m_uBlockSideLength; @@ -259,7 +266,7 @@ namespace PolyVox BaseVolume::template Sampler< SimpleVolume >::moveNegativeZ(); // Then we update the voxel pointer - if((this->isCurrentPositionValid()) && ((this->mZPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) + if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mZPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) { //No need to compute new block. mCurrentVoxel -= this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength;