Initial version of mesh smoothing code.

This commit is contained in:
David Williams
2008-06-24 21:28:29 +00:00
parent b12bd1ebe2
commit e6a7174b53
10 changed files with 167 additions and 377 deletions

View File

@ -139,13 +139,13 @@ namespace PolyVox
assert(uYPos < getSideLength());
assert(uZPos < getSideLength());
const uint16_t blockX = uXPos >> m_uBlockSideLengthPower;
const uint16_t blockY = uYPos >> m_uBlockSideLengthPower;
const uint16_t blockZ = uZPos >> m_uBlockSideLengthPower;
const boost::uint16_t blockX = uXPos >> m_uBlockSideLengthPower;
const boost::uint16_t blockY = uYPos >> m_uBlockSideLengthPower;
const boost::uint16_t blockZ = uZPos >> m_uBlockSideLengthPower;
const uint16_t xOffset = uXPos - (blockX << m_uBlockSideLengthPower);
const uint16_t yOffset = uYPos - (blockY << m_uBlockSideLengthPower);
const uint16_t zOffset = uZPos - (blockZ << m_uBlockSideLengthPower);
const boost::uint16_t xOffset = uXPos - (blockX << m_uBlockSideLengthPower);
const boost::uint16_t yOffset = uYPos - (blockY << m_uBlockSideLengthPower);
const boost::uint16_t zOffset = uZPos - (blockZ << m_uBlockSideLengthPower);
const Block<VoxelType>* block = m_pBlocks
[

View File

@ -43,11 +43,11 @@ namespace PolyVox
bool operator<=(const BlockVolumeIterator& rhs);
bool operator>=(const BlockVolumeIterator& rhs);
float getAveragedVoxel(boost::uint16_t size) const;
VoxelType getMaxedVoxel(boost::uint8_t uLevel) const;
boost::uint16_t getPosX(void) const;
boost::uint16_t getPosY(void) const;
boost::uint16_t getPosZ(void) const;
const BlockVolume<VoxelType>& getVolume(void) const;
VoxelType getVoxel(void) const;
void setPosition(const Vector3DInt16& v3dNewPos);

View File

@ -104,38 +104,6 @@ namespace PolyVox
#pragma endregion
#pragma region Getters
template <typename VoxelType>
float BlockVolumeIterator<VoxelType>::getAveragedVoxel(boost::uint16_t size) const
{
assert(mXPosInVolume >= size);
assert(mYPosInVolume >= size);
assert(mZPosInVolume >= size);
assert(mXPosInVolume < mVolume.getSideLength() - (size + 1));
assert(mYPosInVolume < mVolume.getSideLength() - (size + 1));
assert(mZPosInVolume < mVolume.getSideLength() - (size + 1));
float sum = 0.0;
for(uint16_t z = mZPosInVolume-size; z <= mZPosInVolume+size; ++z)
{
for(uint16_t y = mYPosInVolume-size; y <= mYPosInVolume+size; ++y)
{
for(uint16_t x = mXPosInVolume-size; x <= mXPosInVolume+size; ++x)
{
if(mVolume.getVoxelAt(x,y,z) != 0)
{
sum += 1.0;
}
}
}
}
uint16_t kernelSideLength = size * 2 + 1;
uint16_t kernelVolume = kernelSideLength * kernelSideLength * kernelSideLength;
sum /= static_cast<float>(kernelVolume);
return sum;
}
template <typename VoxelType>
VoxelType BlockVolumeIterator<VoxelType>::getMaxedVoxel(boost::uint8_t uLevel) const
{
@ -192,6 +160,12 @@ namespace PolyVox
return mZPosInVolume;
}
template <typename VoxelType>
const BlockVolume<VoxelType>& BlockVolumeIterator<VoxelType>::getVolume(void) const
{
return mVolume;
}
template <typename VoxelType>
VoxelType BlockVolumeIterator<VoxelType>::getVoxel(void) const
{

View File

@ -19,6 +19,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
******************************************************************************/
#pragma endregion
#include "SurfaceAdjusters.h"
namespace PolyVox
{
template <typename VoxelType>
@ -76,25 +78,25 @@ namespace PolyVox
//FIXME - bitwise way of doing this?
volIter.setPosition(initialX-1, initialY, initialZ);
float voxel1nx = volIter.getAveragedVoxel(1);
float voxel1nx = computeSmoothedVoxel(volIter);
volIter.setPosition(initialX+1, initialY, initialZ);
float voxel1px = volIter.getAveragedVoxel(1);
float voxel1px = computeSmoothedVoxel(volIter);
volIter.setPosition(initialX, initialY-1, initialZ);
float voxel1ny = volIter.getAveragedVoxel(1);
float voxel1ny = computeSmoothedVoxel(volIter);
volIter.setPosition(initialX, initialY+1, initialZ);
float voxel1py = volIter.getAveragedVoxel(1);
float voxel1py = computeSmoothedVoxel(volIter);
volIter.setPosition(initialX, initialY, initialZ-1);
float voxel1nz = volIter.getAveragedVoxel(1);
float voxel1nz = computeSmoothedVoxel(volIter);
volIter.setPosition(initialX, initialY, initialZ+1);
float voxel1pz = volIter.getAveragedVoxel(1);
float voxel1pz = computeSmoothedVoxel(volIter);
return Vector3DFloat
(
voxel1px - voxel1nx,
voxel1py - voxel1ny,
voxel1pz - voxel1nz
voxel1nx - voxel1px,
voxel1ny - voxel1py,
voxel1nz - voxel1pz
);
}

View File

@ -33,6 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
namespace PolyVox
{
POLYVOX_API void smoothRegionGeometry(BlockVolume<boost::uint8_t>* volumeData, RegionGeometry& regGeom);
float computeSmoothedVoxel(BlockVolumeIterator<boost::uint8_t>& volIter);
}
#endif

View File

@ -45,9 +45,6 @@ namespace PolyVox
POLYVOX_API void generateRoughVerticesForSlice(BlockVolumeIterator<boost::uint8_t>& volIter, Region& regSlice, const Vector3DFloat& offset, boost::uint8_t* bitmask, IndexedSurfacePatch* singleMaterialPatch,boost::int32_t vertexIndicesX[],boost::int32_t vertexIndicesY[],boost::int32_t vertexIndicesZ[]);
POLYVOX_API void generateReferenceMeshDataForRegion(BlockVolume<boost::uint8_t>* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch);
POLYVOX_API void generateSmoothMeshDataForRegion(BlockVolume<boost::uint8_t>* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch);
POLYVOX_API Vector3DFloat computeSmoothNormal(BlockVolume<boost::uint8_t>* volumeData, const Vector3DFloat& position, NormalGenerationMethod normalGenerationMethod);
}
#endif

View File

@ -30,6 +30,31 @@ namespace PolyVox
{
POLYVOX_API boost::uint8_t logBase2(boost::uint32_t uInput);
POLYVOX_API bool isPowerOf2(boost::uint32_t uInput);
template <typename Type>
Type trilinearlyInterpolate(
const Type& v000,const Type& v100,const Type& v010,const Type& v110,
const Type& v001,const Type& v101,const Type& v011,const Type& v111,
const float x, const float y, const float z)
{
assert((x >= 0.0f) && (y >= 0.0f) && (z >= 0.0f) &&
(x <= 1.0f) && (y <= 1.0f) && (z <= 1.0f));
//Interpolate along X
Type v000_v100 = (v100 - v000) * x + v000;
Type v001_v101 = (v101 - v001) * x + v001;
Type v010_v110 = (v110 - v010) * x + v010;
Type v011_v111 = (v111 - v011) * x + v011;
//Interpolate along Y
Type v000_v100__v010_v110 = (v010_v110 - v000_v100) * y + v000_v100;
Type v001_v101__v011_v111 = (v011_v111 - v001_v101) * y + v001_v101;
//Interpolate along Z
Type v000_v100__v010_v110____v001_v101__v011_v111 = (v001_v101__v011_v111 - v000_v100__v010_v110) * z + v000_v100__v010_v110;
return v000_v100__v010_v110____v001_v101__v011_v111;
}
}
#endif