From ae3ac704e401a1516d0597cdc7ff5ffdbd59bc52 Mon Sep 17 00:00:00 2001 From: David Williams Date: Sun, 23 Jan 2011 17:41:06 +0000 Subject: [PATCH] Brought back extractSubset from Thermite. Fixed to work with PositionMaterial vertices. Also made it a free function. --- .../include/AmbientOcclusionCalculator.inl | 6 +- library/PolyVoxCore/include/SurfaceMesh.h | 3 + library/PolyVoxCore/include/SurfaceMesh.inl | 67 +++++++++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/library/PolyVoxCore/include/AmbientOcclusionCalculator.inl b/library/PolyVoxCore/include/AmbientOcclusionCalculator.inl index 854663af..2fffd7e1 100644 --- a/library/PolyVoxCore/include/AmbientOcclusionCalculator.inl +++ b/library/PolyVoxCore/include/AmbientOcclusionCalculator.inl @@ -40,9 +40,9 @@ namespace PolyVox ,m_fRayLength(fRayLength) { //Make sure that the size of the volume is an exact multiple of the size of the array. - assert(m_volInput.getWidth() % arrayResult.getDimension(0) == 0); - assert(m_volInput.getHeight() % arrayResult.getDimension(1) == 0); - assert(m_volInput.getDepth() % arrayResult.getDimension(2) == 0); + assert(m_volInput->getWidth() % arrayResult->getDimension(0) == 0); + assert(m_volInput->getHeight() % arrayResult->getDimension(1) == 0); + assert(m_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 is the results. diff --git a/library/PolyVoxCore/include/SurfaceMesh.h b/library/PolyVoxCore/include/SurfaceMesh.h index 7e94a8ae..82b1c1b7 100644 --- a/library/PolyVoxCore/include/SurfaceMesh.h +++ b/library/PolyVoxCore/include/SurfaceMesh.h @@ -93,6 +93,9 @@ namespace PolyVox //exist on a material boundary do not count. std::set m_mapUsedMaterials; }; + + template + polyvox_shared_ptr< SurfaceMesh > extractSubset(SurfaceMesh& inputMesh, std::set setMaterials); } #include "SurfaceMesh.inl" diff --git a/library/PolyVoxCore/include/SurfaceMesh.inl b/library/PolyVoxCore/include/SurfaceMesh.inl index 833a9f0b..47c39568 100644 --- a/library/PolyVoxCore/include/SurfaceMesh.inl +++ b/library/PolyVoxCore/include/SurfaceMesh.inl @@ -394,4 +394,71 @@ namespace PolyVox m_vecTriangleIndices[triCt] = newPos[m_vecTriangleIndices[triCt]]; } } + + //Currently a free function - think where this needs to go. + template + polyvox_shared_ptr< SurfaceMesh > extractSubset(SurfaceMesh& inputMesh, std::set setMaterials) + { + polyvox_shared_ptr< SurfaceMesh > result(new SurfaceMesh); + + if(inputMesh.m_vecVertices.size() == 0) //FIXME - I don't think we should need this test, but I have seen crashes otherwise... + { + return result; + } + + assert(inputMesh.m_vecLodRecords.size() == 1); + if(inputMesh.m_vecLodRecords.size() != 1) + { + //If we have done progressive LOD then it's too late to split into subsets. + return result; + } + + std::vector indexMap(inputMesh.m_vecVertices.size()); + std::fill(indexMap.begin(), indexMap.end(), -1); + + for(uint32_t triCt = 0; triCt < inputMesh.m_vecTriangleIndices.size(); triCt += 3) + { + + VertexType& v0 = inputMesh.m_vecVertices[inputMesh.m_vecTriangleIndices[triCt]]; + VertexType& v1 = inputMesh.m_vecVertices[inputMesh.m_vecTriangleIndices[triCt + 1]]; + VertexType& v2 = inputMesh.m_vecVertices[inputMesh.m_vecTriangleIndices[triCt + 2]]; + + if( + (setMaterials.find(v0.getMaterial()) != setMaterials.end()) || + (setMaterials.find(v1.getMaterial()) != setMaterials.end()) || + (setMaterials.find(v2.getMaterial()) != setMaterials.end())) + { + uint32_t i0; + if(indexMap[inputMesh.m_vecTriangleIndices[triCt]] == -1) + { + indexMap[inputMesh.m_vecTriangleIndices[triCt]] = result->addVertex(v0); + } + i0 = indexMap[inputMesh.m_vecTriangleIndices[triCt]]; + + uint32_t i1; + if(indexMap[inputMesh.m_vecTriangleIndices[triCt+1]] == -1) + { + indexMap[inputMesh.m_vecTriangleIndices[triCt+1]] = result->addVertex(v1); + } + i1 = indexMap[inputMesh.m_vecTriangleIndices[triCt+1]]; + + uint32_t i2; + if(indexMap[inputMesh.m_vecTriangleIndices[triCt+2]] == -1) + { + indexMap[inputMesh.m_vecTriangleIndices[triCt+2]] = result->addVertex(v2); + } + i2 = indexMap[inputMesh.m_vecTriangleIndices[triCt+2]]; + + result->addTriangle(i0,i1,i2); + } + } + + result->m_vecLodRecords.clear(); + LodRecord lodRecord; + lodRecord.beginIndex = 0; + lodRecord.endIndex = result->getNoOfIndices(); + result->m_vecLodRecords.push_back(lodRecord); + + return result; + } }