Rather ugly split of some code into a separate function, to keep the main loop as small and simple as possible. To be tidied up shortly.
This commit is contained in:
parent
e0ce93acb1
commit
13be35aac9
@ -159,7 +159,7 @@ namespace PolyVox
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
//Compute the cell bitmask for a particular slice in z.
|
//Compute the cell bitmask for a particular slice in z.
|
||||||
void computeBitmaskForSlice();
|
void generateMeshForCell(uint32_t uXRegSpace, uint32_t uYRegSpace, uint32_t uZRegSpace, typename VolumeType::Sampler& sampler, typename VolumeType::VoxelType v111, uint8_t iCubeIndex, Array3DInt32& pIndicesX, Array3DInt32& pIndicesY, Array3DInt32& pIndicesZ, int32_t iXVolSpace, int32_t iYVolSpace, int32_t iZVolSpace);
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// NOTE: These two functions are in the .h file rather than the .inl due to an apparent bug in VC2010.
|
// NOTE: These two functions are in the .h file rather than the .inl due to an apparent bug in VC2010.
|
||||||
|
@ -42,18 +42,6 @@ namespace PolyVox
|
|||||||
Timer timer;
|
Timer timer;
|
||||||
m_meshCurrent->clear();
|
m_meshCurrent->clear();
|
||||||
|
|
||||||
computeBitmaskForSlice();
|
|
||||||
|
|
||||||
m_meshCurrent->setOffset(m_regSizeInVoxels.getLowerCorner());
|
|
||||||
|
|
||||||
POLYVOX_LOG_TRACE("Marching cubes surface extraction took ", timer.elapsedTimeInMilliSeconds(),
|
|
||||||
"ms (Region size = ", m_regSizeInVoxels.getWidthInVoxels(), "x", m_regSizeInVoxels.getHeightInVoxels(),
|
|
||||||
"x", m_regSizeInVoxels.getDepthInVoxels(), ")");
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename VolumeType, typename MeshType, typename ControllerType>
|
|
||||||
void MarchingCubesSurfaceExtractor<VolumeType, MeshType, ControllerType>::computeBitmaskForSlice()
|
|
||||||
{
|
|
||||||
const uint32_t uArrayWidth = m_regSizeInVoxels.getUpperX() - m_regSizeInVoxels.getLowerX() + 2;
|
const uint32_t uArrayWidth = m_regSizeInVoxels.getUpperX() - m_regSizeInVoxels.getLowerX() + 2;
|
||||||
const uint32_t uArrayHeight = m_regSizeInVoxels.getUpperY() - m_regSizeInVoxels.getLowerY() + 2;
|
const uint32_t uArrayHeight = m_regSizeInVoxels.getUpperY() - m_regSizeInVoxels.getLowerY() + 2;
|
||||||
const uint32_t uArrayDepth = m_regSizeInVoxels.getUpperZ() - m_regSizeInVoxels.getLowerZ() + 2;
|
const uint32_t uArrayDepth = m_regSizeInVoxels.getUpperZ() - m_regSizeInVoxels.getLowerZ() + 2;
|
||||||
@ -130,196 +118,208 @@ namespace PolyVox
|
|||||||
/* Cube is entirely in/out of the surface */
|
/* Cube is entirely in/out of the surface */
|
||||||
if (edgeTable[iCubeIndex] != 0)
|
if (edgeTable[iCubeIndex] != 0)
|
||||||
{
|
{
|
||||||
|
generateMeshForCell(uXRegSpace, uYRegSpace, uZRegSpace, sampler, v111, iCubeIndex, pIndicesX, pIndicesY, pIndicesZ, iXVolSpace, iYVolSpace, iZVolSpace);
|
||||||
|
}
|
||||||
|
|
||||||
// These three might not have been sampled, as v111 is the only one we sample every iteration.
|
|
||||||
typename VolumeType::VoxelType v110 = sampler.peekVoxel0px0py1nz();
|
|
||||||
typename VolumeType::VoxelType v101 = sampler.peekVoxel0px1ny0pz();
|
|
||||||
typename VolumeType::VoxelType v011 = sampler.peekVoxel1nx0py0pz();
|
|
||||||
|
|
||||||
const Vector3DFloat n000 = computeCentralDifferenceGradient(sampler);
|
|
||||||
|
|
||||||
/* Find the vertices where the surface intersects the cube */
|
|
||||||
if ((edgeTable[iCubeIndex] & 64) && (uXRegSpace > 0))
|
|
||||||
{
|
|
||||||
sampler.moveNegativeX();
|
|
||||||
POLYVOX_ASSERT(v011 != v111, "Attempting to insert vertex between two voxels with the same value");
|
|
||||||
const Vector3DFloat n100 = computeCentralDifferenceGradient(sampler);
|
|
||||||
|
|
||||||
const float fInterp = static_cast<float>(m_tThreshold - m_controller.convertToDensity(v011)) / static_cast<float>(m_controller.convertToDensity(v111) - m_controller.convertToDensity(v011));
|
|
||||||
|
|
||||||
const Vector3DFloat v3dPosition(static_cast<float>(uXRegSpace - 1) + fInterp, static_cast<float>(uYRegSpace), static_cast<float>(uZRegSpace));
|
|
||||||
const Vector3DUint16 v3dScaledPosition(static_cast<uint16_t>(v3dPosition.getX() * 256.0f), static_cast<uint16_t>(v3dPosition.getY() * 256.0f), static_cast<uint16_t>(v3dPosition.getZ() * 256.0f));
|
|
||||||
|
|
||||||
Vector3DFloat v3dNormal = (n100*fInterp) + (n000*(1 - fInterp));
|
|
||||||
|
|
||||||
// The gradient for a voxel can be zero (e.g. solid voxel surrounded by empty ones) and so
|
|
||||||
// the interpolated normal can also be zero (e.g. a grid of alternating solid and empty voxels).
|
|
||||||
if (v3dNormal.lengthSquared() > 0.000001f)
|
|
||||||
{
|
|
||||||
v3dNormal.normalise();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Allow the controller to decide how the material should be derived from the voxels.
|
|
||||||
const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v011, v111, fInterp);
|
|
||||||
|
|
||||||
MarchingCubesVertex<typename VolumeType::VoxelType> surfaceVertex;
|
|
||||||
surfaceVertex.encodedPosition = v3dScaledPosition;
|
|
||||||
surfaceVertex.encodedNormal = encodeNormal(v3dNormal);
|
|
||||||
surfaceVertex.data = uMaterial;
|
|
||||||
|
|
||||||
const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex);
|
|
||||||
pIndicesX(uXRegSpace, uYRegSpace, uZRegSpace) = uLastVertexIndex;
|
|
||||||
|
|
||||||
sampler.movePositiveX();
|
|
||||||
}
|
|
||||||
if ((edgeTable[iCubeIndex] & 32) && (uYRegSpace > 0))
|
|
||||||
{
|
|
||||||
sampler.moveNegativeY();
|
|
||||||
POLYVOX_ASSERT(v101 != v111, "Attempting to insert vertex between two voxels with the same value");
|
|
||||||
const Vector3DFloat n010 = computeCentralDifferenceGradient(sampler);
|
|
||||||
|
|
||||||
const float fInterp = static_cast<float>(m_tThreshold - m_controller.convertToDensity(v101)) / static_cast<float>(m_controller.convertToDensity(v111) - m_controller.convertToDensity(v101));
|
|
||||||
|
|
||||||
const Vector3DFloat v3dPosition(static_cast<float>(uXRegSpace), static_cast<float>(uYRegSpace - 1) + fInterp, static_cast<float>(uZRegSpace));
|
|
||||||
const Vector3DUint16 v3dScaledPosition(static_cast<uint16_t>(v3dPosition.getX() * 256.0f), static_cast<uint16_t>(v3dPosition.getY() * 256.0f), static_cast<uint16_t>(v3dPosition.getZ() * 256.0f));
|
|
||||||
|
|
||||||
Vector3DFloat v3dNormal = (n010*fInterp) + (n000*(1 - fInterp));
|
|
||||||
|
|
||||||
// The gradient for a voxel can be zero (e.g. solid voxel surrounded by empty ones) and so
|
|
||||||
// the interpolated normal can also be zero (e.g. a grid of alternating solid and empty voxels).
|
|
||||||
if (v3dNormal.lengthSquared() > 0.000001f)
|
|
||||||
{
|
|
||||||
v3dNormal.normalise();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Allow the controller to decide how the material should be derived from the voxels.
|
|
||||||
const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v101, v111, fInterp);
|
|
||||||
|
|
||||||
MarchingCubesVertex<typename VolumeType::VoxelType> surfaceVertex;
|
|
||||||
surfaceVertex.encodedPosition = v3dScaledPosition;
|
|
||||||
surfaceVertex.encodedNormal = encodeNormal(v3dNormal);
|
|
||||||
surfaceVertex.data = uMaterial;
|
|
||||||
|
|
||||||
uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex);
|
|
||||||
pIndicesY(uXRegSpace, uYRegSpace, uZRegSpace) = uLastVertexIndex;
|
|
||||||
|
|
||||||
sampler.movePositiveY();
|
|
||||||
}
|
|
||||||
if ((edgeTable[iCubeIndex] & 1024) && (uZRegSpace > 0))
|
|
||||||
{
|
|
||||||
sampler.moveNegativeZ();
|
|
||||||
POLYVOX_ASSERT(v110 != v111, "Attempting to insert vertex between two voxels with the same value");
|
|
||||||
const Vector3DFloat n001 = computeCentralDifferenceGradient(sampler);
|
|
||||||
|
|
||||||
const float fInterp = static_cast<float>(m_tThreshold - m_controller.convertToDensity(v110)) / static_cast<float>(m_controller.convertToDensity(v111) - m_controller.convertToDensity(v110));
|
|
||||||
|
|
||||||
const Vector3DFloat v3dPosition(static_cast<float>(uXRegSpace), static_cast<float>(uYRegSpace), static_cast<float>(uZRegSpace - 1) + fInterp);
|
|
||||||
const Vector3DUint16 v3dScaledPosition(static_cast<uint16_t>(v3dPosition.getX() * 256.0f), static_cast<uint16_t>(v3dPosition.getY() * 256.0f), static_cast<uint16_t>(v3dPosition.getZ() * 256.0f));
|
|
||||||
|
|
||||||
Vector3DFloat v3dNormal = (n001*fInterp) + (n000*(1 - fInterp));
|
|
||||||
// The gradient for a voxel can be zero (e.g. solid voxel surrounded by empty ones) and so
|
|
||||||
// the interpolated normal can also be zero (e.g. a grid of alternating solid and empty voxels).
|
|
||||||
if (v3dNormal.lengthSquared() > 0.000001f)
|
|
||||||
{
|
|
||||||
v3dNormal.normalise();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Allow the controller to decide how the material should be derived from the voxels.
|
|
||||||
const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v110, v111, fInterp);
|
|
||||||
|
|
||||||
MarchingCubesVertex<typename VolumeType::VoxelType> surfaceVertex;
|
|
||||||
surfaceVertex.encodedPosition = v3dScaledPosition;
|
|
||||||
surfaceVertex.encodedNormal = encodeNormal(v3dNormal);
|
|
||||||
surfaceVertex.data = uMaterial;
|
|
||||||
|
|
||||||
const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex);
|
|
||||||
pIndicesZ(uXRegSpace, uYRegSpace, uZRegSpace) = uLastVertexIndex;
|
|
||||||
|
|
||||||
sampler.movePositiveZ();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now output the indices. For the first row, column or slice there aren't
|
|
||||||
// any (the region size in cells is one less than the region size in voxels)
|
|
||||||
if ((uXRegSpace != 0) && (uYRegSpace != 0) && (uZRegSpace != 0))
|
|
||||||
{
|
|
||||||
|
|
||||||
int32_t indlist[12];
|
|
||||||
|
|
||||||
sampler.setPosition(iXVolSpace, iYVolSpace, iZVolSpace);
|
|
||||||
|
|
||||||
/* Cube is entirely in/out of the surface */
|
|
||||||
if (edgeTable[iCubeIndex] != 0)
|
|
||||||
{
|
|
||||||
|
|
||||||
/* Find the vertices where the surface intersects the cube */
|
|
||||||
if (edgeTable[iCubeIndex] & 1)
|
|
||||||
{
|
|
||||||
indlist[0] = pIndicesX(uXRegSpace, uYRegSpace - 1, uZRegSpace - 1);
|
|
||||||
}
|
|
||||||
if (edgeTable[iCubeIndex] & 2)
|
|
||||||
{
|
|
||||||
indlist[1] = pIndicesY(uXRegSpace, uYRegSpace, uZRegSpace - 1);
|
|
||||||
}
|
|
||||||
if (edgeTable[iCubeIndex] & 4)
|
|
||||||
{
|
|
||||||
indlist[2] = pIndicesX(uXRegSpace, uYRegSpace, uZRegSpace - 1);
|
|
||||||
}
|
|
||||||
if (edgeTable[iCubeIndex] & 8)
|
|
||||||
{
|
|
||||||
indlist[3] = pIndicesY(uXRegSpace - 1, uYRegSpace, uZRegSpace - 1);
|
|
||||||
}
|
|
||||||
if (edgeTable[iCubeIndex] & 16)
|
|
||||||
{
|
|
||||||
indlist[4] = pIndicesX(uXRegSpace, uYRegSpace - 1, uZRegSpace);
|
|
||||||
}
|
|
||||||
if (edgeTable[iCubeIndex] & 32)
|
|
||||||
{
|
|
||||||
indlist[5] = pIndicesY(uXRegSpace, uYRegSpace, uZRegSpace);
|
|
||||||
}
|
|
||||||
if (edgeTable[iCubeIndex] & 64)
|
|
||||||
{
|
|
||||||
indlist[6] = pIndicesX(uXRegSpace, uYRegSpace, uZRegSpace);
|
|
||||||
}
|
|
||||||
if (edgeTable[iCubeIndex] & 128)
|
|
||||||
{
|
|
||||||
indlist[7] = pIndicesY(uXRegSpace - 1, uYRegSpace, uZRegSpace);
|
|
||||||
}
|
|
||||||
if (edgeTable[iCubeIndex] & 256)
|
|
||||||
{
|
|
||||||
indlist[8] = pIndicesZ(uXRegSpace - 1, uYRegSpace - 1, uZRegSpace);
|
|
||||||
}
|
|
||||||
if (edgeTable[iCubeIndex] & 512)
|
|
||||||
{
|
|
||||||
indlist[9] = pIndicesZ(uXRegSpace, uYRegSpace - 1, uZRegSpace);
|
|
||||||
}
|
|
||||||
if (edgeTable[iCubeIndex] & 1024)
|
|
||||||
{
|
|
||||||
indlist[10] = pIndicesZ(uXRegSpace, uYRegSpace, uZRegSpace);
|
|
||||||
}
|
|
||||||
if (edgeTable[iCubeIndex] & 2048)
|
|
||||||
{
|
|
||||||
indlist[11] = pIndicesZ(uXRegSpace - 1, uYRegSpace, uZRegSpace);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; triTable[iCubeIndex][i] != -1; i += 3)
|
|
||||||
{
|
|
||||||
const int32_t ind0 = indlist[triTable[iCubeIndex][i]];
|
|
||||||
const int32_t ind1 = indlist[triTable[iCubeIndex][i + 1]];
|
|
||||||
const int32_t ind2 = indlist[triTable[iCubeIndex][i + 2]];
|
|
||||||
|
|
||||||
if ((ind0 != -1) && (ind1 != -1) && (ind2 != -1))
|
|
||||||
{
|
|
||||||
m_meshCurrent->addTriangle(ind0, ind1, ind2);
|
|
||||||
}
|
|
||||||
} // For each triangle
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} // For each cell
|
|
||||||
sampler.movePositiveX();
|
sampler.movePositiveX();
|
||||||
} // For X
|
} // For X
|
||||||
startOfRow.movePositiveY();
|
startOfRow.movePositiveY();
|
||||||
} // For Y
|
} // For Y
|
||||||
startOfSlice.movePositiveZ();
|
startOfSlice.movePositiveZ();
|
||||||
} // For Z
|
} // For Z
|
||||||
|
|
||||||
|
m_meshCurrent->setOffset(m_regSizeInVoxels.getLowerCorner());
|
||||||
|
|
||||||
|
POLYVOX_LOG_TRACE("Marching cubes surface extraction took ", timer.elapsedTimeInMilliSeconds(),
|
||||||
|
"ms (Region size = ", m_regSizeInVoxels.getWidthInVoxels(), "x", m_regSizeInVoxels.getHeightInVoxels(),
|
||||||
|
"x", m_regSizeInVoxels.getDepthInVoxels(), ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename VolumeType, typename MeshType, typename ControllerType>
|
||||||
|
void MarchingCubesSurfaceExtractor<VolumeType, MeshType, ControllerType>::generateMeshForCell(uint32_t uXRegSpace, uint32_t uYRegSpace, uint32_t uZRegSpace, typename VolumeType::Sampler& sampler, typename VolumeType::VoxelType v111, uint8_t iCubeIndex, Array3DInt32& pIndicesX, Array3DInt32& pIndicesY, Array3DInt32& pIndicesZ, int32_t iXVolSpace, int32_t iYVolSpace, int32_t iZVolSpace)
|
||||||
|
{
|
||||||
|
// These three might not have been sampled, as v111 is the only one we sample every iteration.
|
||||||
|
typename VolumeType::VoxelType v110 = sampler.peekVoxel0px0py1nz();
|
||||||
|
typename VolumeType::VoxelType v101 = sampler.peekVoxel0px1ny0pz();
|
||||||
|
typename VolumeType::VoxelType v011 = sampler.peekVoxel1nx0py0pz();
|
||||||
|
|
||||||
|
const Vector3DFloat n000 = computeCentralDifferenceGradient(sampler);
|
||||||
|
|
||||||
|
/* Find the vertices where the surface intersects the cube */
|
||||||
|
if ((edgeTable[iCubeIndex] & 64) && (uXRegSpace > 0))
|
||||||
|
{
|
||||||
|
sampler.moveNegativeX();
|
||||||
|
POLYVOX_ASSERT(v011 != v111, "Attempting to insert vertex between two voxels with the same value");
|
||||||
|
const Vector3DFloat n100 = computeCentralDifferenceGradient(sampler);
|
||||||
|
|
||||||
|
const float fInterp = static_cast<float>(m_tThreshold - m_controller.convertToDensity(v011)) / static_cast<float>(m_controller.convertToDensity(v111) - m_controller.convertToDensity(v011));
|
||||||
|
|
||||||
|
const Vector3DFloat v3dPosition(static_cast<float>(uXRegSpace - 1) + fInterp, static_cast<float>(uYRegSpace), static_cast<float>(uZRegSpace));
|
||||||
|
const Vector3DUint16 v3dScaledPosition(static_cast<uint16_t>(v3dPosition.getX() * 256.0f), static_cast<uint16_t>(v3dPosition.getY() * 256.0f), static_cast<uint16_t>(v3dPosition.getZ() * 256.0f));
|
||||||
|
|
||||||
|
Vector3DFloat v3dNormal = (n100*fInterp) + (n000*(1 - fInterp));
|
||||||
|
|
||||||
|
// The gradient for a voxel can be zero (e.g. solid voxel surrounded by empty ones) and so
|
||||||
|
// the interpolated normal can also be zero (e.g. a grid of alternating solid and empty voxels).
|
||||||
|
if (v3dNormal.lengthSquared() > 0.000001f)
|
||||||
|
{
|
||||||
|
v3dNormal.normalise();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allow the controller to decide how the material should be derived from the voxels.
|
||||||
|
const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v011, v111, fInterp);
|
||||||
|
|
||||||
|
MarchingCubesVertex<typename VolumeType::VoxelType> surfaceVertex;
|
||||||
|
surfaceVertex.encodedPosition = v3dScaledPosition;
|
||||||
|
surfaceVertex.encodedNormal = encodeNormal(v3dNormal);
|
||||||
|
surfaceVertex.data = uMaterial;
|
||||||
|
|
||||||
|
const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex);
|
||||||
|
pIndicesX(uXRegSpace, uYRegSpace, uZRegSpace) = uLastVertexIndex;
|
||||||
|
|
||||||
|
sampler.movePositiveX();
|
||||||
|
}
|
||||||
|
if ((edgeTable[iCubeIndex] & 32) && (uYRegSpace > 0))
|
||||||
|
{
|
||||||
|
sampler.moveNegativeY();
|
||||||
|
POLYVOX_ASSERT(v101 != v111, "Attempting to insert vertex between two voxels with the same value");
|
||||||
|
const Vector3DFloat n010 = computeCentralDifferenceGradient(sampler);
|
||||||
|
|
||||||
|
const float fInterp = static_cast<float>(m_tThreshold - m_controller.convertToDensity(v101)) / static_cast<float>(m_controller.convertToDensity(v111) - m_controller.convertToDensity(v101));
|
||||||
|
|
||||||
|
const Vector3DFloat v3dPosition(static_cast<float>(uXRegSpace), static_cast<float>(uYRegSpace - 1) + fInterp, static_cast<float>(uZRegSpace));
|
||||||
|
const Vector3DUint16 v3dScaledPosition(static_cast<uint16_t>(v3dPosition.getX() * 256.0f), static_cast<uint16_t>(v3dPosition.getY() * 256.0f), static_cast<uint16_t>(v3dPosition.getZ() * 256.0f));
|
||||||
|
|
||||||
|
Vector3DFloat v3dNormal = (n010*fInterp) + (n000*(1 - fInterp));
|
||||||
|
|
||||||
|
// The gradient for a voxel can be zero (e.g. solid voxel surrounded by empty ones) and so
|
||||||
|
// the interpolated normal can also be zero (e.g. a grid of alternating solid and empty voxels).
|
||||||
|
if (v3dNormal.lengthSquared() > 0.000001f)
|
||||||
|
{
|
||||||
|
v3dNormal.normalise();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allow the controller to decide how the material should be derived from the voxels.
|
||||||
|
const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v101, v111, fInterp);
|
||||||
|
|
||||||
|
MarchingCubesVertex<typename VolumeType::VoxelType> surfaceVertex;
|
||||||
|
surfaceVertex.encodedPosition = v3dScaledPosition;
|
||||||
|
surfaceVertex.encodedNormal = encodeNormal(v3dNormal);
|
||||||
|
surfaceVertex.data = uMaterial;
|
||||||
|
|
||||||
|
uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex);
|
||||||
|
pIndicesY(uXRegSpace, uYRegSpace, uZRegSpace) = uLastVertexIndex;
|
||||||
|
|
||||||
|
sampler.movePositiveY();
|
||||||
|
}
|
||||||
|
if ((edgeTable[iCubeIndex] & 1024) && (uZRegSpace > 0))
|
||||||
|
{
|
||||||
|
sampler.moveNegativeZ();
|
||||||
|
POLYVOX_ASSERT(v110 != v111, "Attempting to insert vertex between two voxels with the same value");
|
||||||
|
const Vector3DFloat n001 = computeCentralDifferenceGradient(sampler);
|
||||||
|
|
||||||
|
const float fInterp = static_cast<float>(m_tThreshold - m_controller.convertToDensity(v110)) / static_cast<float>(m_controller.convertToDensity(v111) - m_controller.convertToDensity(v110));
|
||||||
|
|
||||||
|
const Vector3DFloat v3dPosition(static_cast<float>(uXRegSpace), static_cast<float>(uYRegSpace), static_cast<float>(uZRegSpace - 1) + fInterp);
|
||||||
|
const Vector3DUint16 v3dScaledPosition(static_cast<uint16_t>(v3dPosition.getX() * 256.0f), static_cast<uint16_t>(v3dPosition.getY() * 256.0f), static_cast<uint16_t>(v3dPosition.getZ() * 256.0f));
|
||||||
|
|
||||||
|
Vector3DFloat v3dNormal = (n001*fInterp) + (n000*(1 - fInterp));
|
||||||
|
// The gradient for a voxel can be zero (e.g. solid voxel surrounded by empty ones) and so
|
||||||
|
// the interpolated normal can also be zero (e.g. a grid of alternating solid and empty voxels).
|
||||||
|
if (v3dNormal.lengthSquared() > 0.000001f)
|
||||||
|
{
|
||||||
|
v3dNormal.normalise();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allow the controller to decide how the material should be derived from the voxels.
|
||||||
|
const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v110, v111, fInterp);
|
||||||
|
|
||||||
|
MarchingCubesVertex<typename VolumeType::VoxelType> surfaceVertex;
|
||||||
|
surfaceVertex.encodedPosition = v3dScaledPosition;
|
||||||
|
surfaceVertex.encodedNormal = encodeNormal(v3dNormal);
|
||||||
|
surfaceVertex.data = uMaterial;
|
||||||
|
|
||||||
|
const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex);
|
||||||
|
pIndicesZ(uXRegSpace, uYRegSpace, uZRegSpace) = uLastVertexIndex;
|
||||||
|
|
||||||
|
sampler.movePositiveZ();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now output the indices. For the first row, column or slice there aren't
|
||||||
|
// any (the region size in cells is one less than the region size in voxels)
|
||||||
|
if ((uXRegSpace != 0) && (uYRegSpace != 0) && (uZRegSpace != 0))
|
||||||
|
{
|
||||||
|
|
||||||
|
int32_t indlist[12];
|
||||||
|
|
||||||
|
sampler.setPosition(iXVolSpace, iYVolSpace, iZVolSpace);
|
||||||
|
|
||||||
|
/* Cube is entirely in/out of the surface */
|
||||||
|
if (edgeTable[iCubeIndex] != 0)
|
||||||
|
{
|
||||||
|
|
||||||
|
/* Find the vertices where the surface intersects the cube */
|
||||||
|
if (edgeTable[iCubeIndex] & 1)
|
||||||
|
{
|
||||||
|
indlist[0] = pIndicesX(uXRegSpace, uYRegSpace - 1, uZRegSpace - 1);
|
||||||
|
}
|
||||||
|
if (edgeTable[iCubeIndex] & 2)
|
||||||
|
{
|
||||||
|
indlist[1] = pIndicesY(uXRegSpace, uYRegSpace, uZRegSpace - 1);
|
||||||
|
}
|
||||||
|
if (edgeTable[iCubeIndex] & 4)
|
||||||
|
{
|
||||||
|
indlist[2] = pIndicesX(uXRegSpace, uYRegSpace, uZRegSpace - 1);
|
||||||
|
}
|
||||||
|
if (edgeTable[iCubeIndex] & 8)
|
||||||
|
{
|
||||||
|
indlist[3] = pIndicesY(uXRegSpace - 1, uYRegSpace, uZRegSpace - 1);
|
||||||
|
}
|
||||||
|
if (edgeTable[iCubeIndex] & 16)
|
||||||
|
{
|
||||||
|
indlist[4] = pIndicesX(uXRegSpace, uYRegSpace - 1, uZRegSpace);
|
||||||
|
}
|
||||||
|
if (edgeTable[iCubeIndex] & 32)
|
||||||
|
{
|
||||||
|
indlist[5] = pIndicesY(uXRegSpace, uYRegSpace, uZRegSpace);
|
||||||
|
}
|
||||||
|
if (edgeTable[iCubeIndex] & 64)
|
||||||
|
{
|
||||||
|
indlist[6] = pIndicesX(uXRegSpace, uYRegSpace, uZRegSpace);
|
||||||
|
}
|
||||||
|
if (edgeTable[iCubeIndex] & 128)
|
||||||
|
{
|
||||||
|
indlist[7] = pIndicesY(uXRegSpace - 1, uYRegSpace, uZRegSpace);
|
||||||
|
}
|
||||||
|
if (edgeTable[iCubeIndex] & 256)
|
||||||
|
{
|
||||||
|
indlist[8] = pIndicesZ(uXRegSpace - 1, uYRegSpace - 1, uZRegSpace);
|
||||||
|
}
|
||||||
|
if (edgeTable[iCubeIndex] & 512)
|
||||||
|
{
|
||||||
|
indlist[9] = pIndicesZ(uXRegSpace, uYRegSpace - 1, uZRegSpace);
|
||||||
|
}
|
||||||
|
if (edgeTable[iCubeIndex] & 1024)
|
||||||
|
{
|
||||||
|
indlist[10] = pIndicesZ(uXRegSpace, uYRegSpace, uZRegSpace);
|
||||||
|
}
|
||||||
|
if (edgeTable[iCubeIndex] & 2048)
|
||||||
|
{
|
||||||
|
indlist[11] = pIndicesZ(uXRegSpace - 1, uYRegSpace, uZRegSpace);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; triTable[iCubeIndex][i] != -1; i += 3)
|
||||||
|
{
|
||||||
|
const int32_t ind0 = indlist[triTable[iCubeIndex][i]];
|
||||||
|
const int32_t ind1 = indlist[triTable[iCubeIndex][i + 1]];
|
||||||
|
const int32_t ind2 = indlist[triTable[iCubeIndex][i + 2]];
|
||||||
|
|
||||||
|
if ((ind0 != -1) && (ind1 != -1) && (ind2 != -1))
|
||||||
|
{
|
||||||
|
m_meshCurrent->addTriangle(ind0, ind1, ind2);
|
||||||
|
}
|
||||||
|
} // For each triangle
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user