From 8c7e2671bebc2af12a8b593c1f81ce5adeafb620 Mon Sep 17 00:00:00 2001 From: David Williams Date: Fri, 5 Oct 2012 16:38:28 +0200 Subject: [PATCH] Rearranged ambient occlusion code. --- .../PolyVoxCore/AmbientOcclusionCalculator.h | 95 +------------------ .../AmbientOcclusionCalculator.inl | 94 +++++++++++++++++- 2 files changed, 96 insertions(+), 93 deletions(-) diff --git a/library/PolyVoxCore/include/PolyVoxCore/AmbientOcclusionCalculator.h b/library/PolyVoxCore/include/PolyVoxCore/AmbientOcclusionCalculator.h index 5ff8247a..2170ea60 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/AmbientOcclusionCalculator.h +++ b/library/PolyVoxCore/include/PolyVoxCore/AmbientOcclusionCalculator.h @@ -64,99 +64,10 @@ namespace PolyVox IsVoxelTransparentCallback mIsVoxelTransparentCallback; }; + // NOTE: The callback needs to be a functor not a function. I haven't been + // able to work the required template magic to get functions working as well. template - void calculateAmbientOcclusion(VolumeType* volInput, Array<3, uint8_t>* arrayResult, Region region, float fRayLength, uint8_t uNoOfSamplesPerOutputElement, IsVoxelTransparentCallback isVoxelTransparentCallback) - { - typename VolumeType::Sampler m_sampVolume(volInput); - - uint16_t uRandomUnitVectorIndex = 0; - uint16_t uRandomVectorIndex = 0; - uint16_t uIndexIncreament; - - //Make sure that the size of the volume is an exact multiple of the size of the array. - assert(volInput->getWidth() % arrayResult->getDimension(0) == 0); - assert(volInput->getHeight() % arrayResult->getDimension(1) == 0); - assert(volInput->getDepth() % arrayResult->getDimension(2) == 0); - - //Our initial indices. It doesn't matter exactly what we set here, but the code below makes - //sure they are different for different regions which helps reduce tiling patterns in the results. - uRandomUnitVectorIndex += region.getLowerCorner().getX() + region.getLowerCorner().getY() + region.getLowerCorner().getZ(); - uRandomVectorIndex += region.getLowerCorner().getX() + region.getLowerCorner().getY() + region.getLowerCorner().getZ(); - - //This value helps us jump around in the array a bit more, so the - //nth 'random' value isn't always followed by the n+1th 'random' value. - uIndexIncreament = 1; - - const int iRatioX = volInput->getWidth() / arrayResult->getDimension(0); - const int iRatioY = volInput->getHeight() / arrayResult->getDimension(1); - const int iRatioZ = volInput->getDepth() / arrayResult->getDimension(2); - - const float fRatioX = iRatioX; - const float fRatioY = iRatioY; - const float fRatioZ = iRatioZ; - const Vector3DFloat v3dRatio(fRatioX, fRatioY, fRatioZ); - - const float fHalfRatioX = fRatioX * 0.5f; - const float fHalfRatioY = fRatioY * 0.5f; - const float fHalfRatioZ = fRatioZ * 0.5f; - const Vector3DFloat v3dHalfRatio(fHalfRatioX, fHalfRatioY, fHalfRatioZ); - - const Vector3DFloat v3dOffset(0.5f,0.5f,0.5f); - - //This loop iterates over the bottom-lower-left voxel in each of the cells in the output array - for(uint16_t z = region.getLowerCorner().getZ(); z <= region.getUpperCorner().getZ(); z += iRatioZ) - { - for(uint16_t y = region.getLowerCorner().getY(); y <= region.getUpperCorner().getY(); y += iRatioY) - { - for(uint16_t x = region.getLowerCorner().getX(); x <= region.getUpperCorner().getX(); x += iRatioX) - { - //Compute a start position corresponding to - //the centre of the cell in the output array. - Vector3DFloat v3dStart(x, y, z); - v3dStart -= v3dOffset; - v3dStart += v3dHalfRatio; - - //Keep track of how many rays did not hit anything - uint8_t uVisibleDirections = 0; - - for(int ct = 0; ct < uNoOfSamplesPerOutputElement; ct++) - { - //We take a random vector with components going from -1 to 1 and scale it to go from -halfRatio to +halfRatio. - //This jitter value moves our sample point from the center of the array cell to somewhere else in the array cell - Vector3DFloat v3dJitter = randomVectors[(uRandomVectorIndex += (++uIndexIncreament)) % 1019]; //Prime number helps avoid repetition on sucessive loops. - v3dJitter *= v3dHalfRatio; - const Vector3DFloat v3dRayStart = v3dStart + v3dJitter; - - Vector3DFloat v3dRayDirection = randomUnitVectors[(uRandomUnitVectorIndex += (++uIndexIncreament)) % 1021]; //Differenct prime number. - v3dRayDirection *= fRayLength; - - AmbientOcclusionCalculatorRaycastCallback ambientOcclusionCalculatorRaycastCallback(isVoxelTransparentCallback); - MyRaycastResult result = raycastWithDirection(volInput, v3dRayStart, v3dRayDirection, ambientOcclusionCalculatorRaycastCallback); - - if(result == MyRaycastResults::Completed) - { - ++uVisibleDirections; - } - } - - float fVisibility; - if(uNoOfSamplesPerOutputElement == 0) - { - //The user might request zero samples (I've done this in the past while debugging - I don't want to - //wait for ambient occlusion but I do want as valid result for rendering). Avoid the divide by zero. - fVisibility = 1.0f; - } - else - { - fVisibility = static_cast(uVisibleDirections) / static_cast(uNoOfSamplesPerOutputElement); - assert((fVisibility >= 0.0f) && (fVisibility <= 1.0f)); - } - - (*arrayResult)[z / iRatioZ][y / iRatioY][x / iRatioX] = static_cast(255.0f * fVisibility); - } - } - } - } + void calculateAmbientOcclusion(VolumeType* volInput, Array<3, uint8_t>* arrayResult, Region region, float fRayLength, uint8_t uNoOfSamplesPerOutputElement, IsVoxelTransparentCallback isVoxelTransparentCallback); } #include "PolyVoxCore/AmbientOcclusionCalculator.inl" diff --git a/library/PolyVoxCore/include/PolyVoxCore/AmbientOcclusionCalculator.inl b/library/PolyVoxCore/include/PolyVoxCore/AmbientOcclusionCalculator.inl index 461b5a23..f7d40a9c 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/AmbientOcclusionCalculator.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/AmbientOcclusionCalculator.inl @@ -23,5 +23,97 @@ freely, subject to the following restrictions: namespace PolyVox { - + template + void calculateAmbientOcclusion(VolumeType* volInput, Array<3, uint8_t>* arrayResult, Region region, float fRayLength, uint8_t uNoOfSamplesPerOutputElement, IsVoxelTransparentCallback isVoxelTransparentCallback) + { + typename VolumeType::Sampler m_sampVolume(volInput); + + uint16_t uRandomUnitVectorIndex = 0; + uint16_t uRandomVectorIndex = 0; + uint16_t uIndexIncreament; + + //Make sure that the size of the volume is an exact multiple of the size of the array. + assert(volInput->getWidth() % arrayResult->getDimension(0) == 0); + assert(volInput->getHeight() % arrayResult->getDimension(1) == 0); + assert(volInput->getDepth() % arrayResult->getDimension(2) == 0); + + //Our initial indices. It doesn't matter exactly what we set here, but the code below makes + //sure they are different for different regions which helps reduce tiling patterns in the results. + uRandomUnitVectorIndex += region.getLowerCorner().getX() + region.getLowerCorner().getY() + region.getLowerCorner().getZ(); + uRandomVectorIndex += region.getLowerCorner().getX() + region.getLowerCorner().getY() + region.getLowerCorner().getZ(); + + //This value helps us jump around in the array a bit more, so the + //nth 'random' value isn't always followed by the n+1th 'random' value. + uIndexIncreament = 1; + + const int iRatioX = volInput->getWidth() / arrayResult->getDimension(0); + const int iRatioY = volInput->getHeight() / arrayResult->getDimension(1); + const int iRatioZ = volInput->getDepth() / arrayResult->getDimension(2); + + const float fRatioX = iRatioX; + const float fRatioY = iRatioY; + const float fRatioZ = iRatioZ; + const Vector3DFloat v3dRatio(fRatioX, fRatioY, fRatioZ); + + const float fHalfRatioX = fRatioX * 0.5f; + const float fHalfRatioY = fRatioY * 0.5f; + const float fHalfRatioZ = fRatioZ * 0.5f; + const Vector3DFloat v3dHalfRatio(fHalfRatioX, fHalfRatioY, fHalfRatioZ); + + const Vector3DFloat v3dOffset(0.5f,0.5f,0.5f); + + //This loop iterates over the bottom-lower-left voxel in each of the cells in the output array + for(uint16_t z = region.getLowerCorner().getZ(); z <= region.getUpperCorner().getZ(); z += iRatioZ) + { + for(uint16_t y = region.getLowerCorner().getY(); y <= region.getUpperCorner().getY(); y += iRatioY) + { + for(uint16_t x = region.getLowerCorner().getX(); x <= region.getUpperCorner().getX(); x += iRatioX) + { + //Compute a start position corresponding to + //the centre of the cell in the output array. + Vector3DFloat v3dStart(x, y, z); + v3dStart -= v3dOffset; + v3dStart += v3dHalfRatio; + + //Keep track of how many rays did not hit anything + uint8_t uVisibleDirections = 0; + + for(int ct = 0; ct < uNoOfSamplesPerOutputElement; ct++) + { + //We take a random vector with components going from -1 to 1 and scale it to go from -halfRatio to +halfRatio. + //This jitter value moves our sample point from the center of the array cell to somewhere else in the array cell + Vector3DFloat v3dJitter = randomVectors[(uRandomVectorIndex += (++uIndexIncreament)) % 1019]; //Prime number helps avoid repetition on sucessive loops. + v3dJitter *= v3dHalfRatio; + const Vector3DFloat v3dRayStart = v3dStart + v3dJitter; + + Vector3DFloat v3dRayDirection = randomUnitVectors[(uRandomUnitVectorIndex += (++uIndexIncreament)) % 1021]; //Differenct prime number. + v3dRayDirection *= fRayLength; + + AmbientOcclusionCalculatorRaycastCallback ambientOcclusionCalculatorRaycastCallback(isVoxelTransparentCallback); + MyRaycastResult result = raycastWithDirection(volInput, v3dRayStart, v3dRayDirection, ambientOcclusionCalculatorRaycastCallback); + + if(result == MyRaycastResults::Completed) + { + ++uVisibleDirections; + } + } + + float fVisibility; + if(uNoOfSamplesPerOutputElement == 0) + { + //The user might request zero samples (I've done this in the past while debugging - I don't want to + //wait for ambient occlusion but I do want as valid result for rendering). Avoid the divide by zero. + fVisibility = 1.0f; + } + else + { + fVisibility = static_cast(uVisibleDirections) / static_cast(uNoOfSamplesPerOutputElement); + assert((fVisibility >= 0.0f) && (fVisibility <= 1.0f)); + } + + (*arrayResult)[z / iRatioZ][y / iRatioY][x / iRatioX] = static_cast(255.0f * fVisibility); + } + } + } + } }