From 0d3a215637e8afbe144a6ebc0d5619ee48a5c64a Mon Sep 17 00:00:00 2001 From: David Williams Date: Sat, 5 Jul 2008 22:11:09 +0000 Subject: [PATCH] Work on IndexedSurfacePatch --- .../include/PolyVoxCore/IndexedSurfacePatch.h | 25 +- .../PolyVoxImpl/DecimatedSurfaceExtractor.h | 4 - .../source/PolyVoxCore/GradientEstimators.cpp | 2 +- .../PolyVoxCore/IndexedSurfacePatch.cpp | 102 +++--- .../PolyVoxImpl/DecimatedSurfaceExtractor.cpp | 337 +----------------- .../PolyVoxImpl/FastSurfaceExtractor.cpp | 19 +- .../PolyVoxImpl/ReferenceSurfaceExtractor.cpp | 43 +-- .../source/PolyVoxCore/SurfaceAdjusters.cpp | 4 +- 8 files changed, 93 insertions(+), 443 deletions(-) diff --git a/library/include/PolyVoxCore/IndexedSurfacePatch.h b/library/include/PolyVoxCore/IndexedSurfacePatch.h index ca7848e6..b40ad4ae 100644 --- a/library/include/PolyVoxCore/IndexedSurfacePatch.h +++ b/library/include/PolyVoxCore/IndexedSurfacePatch.h @@ -39,25 +39,22 @@ namespace PolyVox IndexedSurfacePatch(); ~IndexedSurfacePatch(); - void addTriangle(const SurfaceVertex& v0,const SurfaceVertex& v1,const SurfaceVertex& v2); - void fillVertexAndIndexData(std::vector& vecVertices, std::vector& vecIndices); - - const std::vector& getVertices(void) const; - std::vector& getVertices(void); //FIXME - non const version should be removed. const std::vector& getIndices(void) const; + uint32 getNoOfIndices(void) const; + uint32 getNoOfNonUniformTrianges(void) const; + uint32 getNoOfUniformTrianges(void) const; + uint32 getNoOfVertices(void) const; + std::vector& getRawVertexData(void); //FIXME - this shoudl be removed + const std::vector& getVertices(void) const; - const uint32 getNoOfIndices(void) const; - const uint32 getNoOfVertices(void) const; - + void addTriangle(uint32 index0, uint32 index1, uint32 index2); + uint32 addVertex(const SurfaceVertex& vertex); + void clear(void); const bool isEmpty(void) const; - - unsigned short getNoNonUniformTrianges(void); - unsigned short getNoUniformTrianges(void); - - Vector3DInt32 m_v3dRegionPosition; + Vector3DInt32 m_v3dRegionPosition; //FIXME - remove this? - public: + private: std::vector m_vecTriangleIndices; std::vector m_vecVertices; }; diff --git a/library/include/PolyVoxCore/PolyVoxImpl/DecimatedSurfaceExtractor.h b/library/include/PolyVoxCore/PolyVoxImpl/DecimatedSurfaceExtractor.h index a70663ab..61c4c1a9 100644 --- a/library/include/PolyVoxCore/PolyVoxImpl/DecimatedSurfaceExtractor.h +++ b/library/include/PolyVoxCore/PolyVoxImpl/DecimatedSurfaceExtractor.h @@ -39,10 +39,6 @@ namespace PolyVox uint32 computeDecimatedBitmaskForSliceFromPrevious(BlockVolumeIterator& volIter, uint8 uLevel, const Region& regSlice, const Vector3DFloat& offset, uint8 *bitmask, uint8 *previousBitmask); void generateDecimatedIndicesForSlice(BlockVolumeIterator& volIter, uint8 uLevel, const Region& regSlice, IndexedSurfacePatch* singleMaterialPatch, const Vector3DFloat& offset, uint8* bitmask0, uint8* bitmask1, int32 vertexIndicesX0[],int32 vertexIndicesY0[],int32 vertexIndicesZ0[], int32 vertexIndicesX1[],int32 vertexIndicesY1[],int32 vertexIndicesZ1[]); void generateDecimatedVerticesForSlice(BlockVolumeIterator& volIter, uint8 uLevel, Region& regSlice, const Vector3DFloat& offset, uint8* bitmask, IndexedSurfacePatch* singleMaterialPatch,int32 vertexIndicesX[],int32 vertexIndicesY[],int32 vertexIndicesZ[]); - - void generateDecimatedMeshDataForRegionSlow(BlockVolume* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch); - - Vector3DFloat computeDecimatedNormal(BlockVolume* volumeData, const Vector3DFloat& position, NormalGenerationMethod normalGenerationMethod); } #endif diff --git a/library/source/PolyVoxCore/GradientEstimators.cpp b/library/source/PolyVoxCore/GradientEstimators.cpp index 794dd7da..d5b1631a 100644 --- a/library/source/PolyVoxCore/GradientEstimators.cpp +++ b/library/source/PolyVoxCore/GradientEstimators.cpp @@ -10,7 +10,7 @@ namespace PolyVox { POLYVOX_API void computeNormalsForVertices(BlockVolume* volumeData, IndexedSurfacePatch& isp, NormalGenerationMethod normalGenerationMethod) { - std::vector& vecVertices = isp.m_vecVertices; + std::vector& vecVertices = isp.getRawVertexData(); std::vector::iterator iterSurfaceVertex = vecVertices.begin(); while(iterSurfaceVertex != vecVertices.end()) { diff --git a/library/source/PolyVoxCore/IndexedSurfacePatch.cpp b/library/source/PolyVoxCore/IndexedSurfacePatch.cpp index 1408f9a5..d901e616 100644 --- a/library/source/PolyVoxCore/IndexedSurfacePatch.cpp +++ b/library/source/PolyVoxCore/IndexedSurfacePatch.cpp @@ -33,65 +33,20 @@ namespace PolyVox { } - void IndexedSurfacePatch::addTriangle(const SurfaceVertex& v0,const SurfaceVertex& v1,const SurfaceVertex& v2) - { - m_vecVertices.push_back(v0); - m_vecTriangleIndices.push_back(m_vecVertices.size()-1); - m_vecVertices.push_back(v1); - m_vecTriangleIndices.push_back(m_vecVertices.size()-1); - m_vecVertices.push_back(v2); - m_vecTriangleIndices.push_back(m_vecVertices.size()-1); - } - - void IndexedSurfacePatch::fillVertexAndIndexData(std::vector& vecVertices, std::vector& vecIndices) - { - vecVertices.resize(m_vecVertices.size()); - std::copy(m_vecVertices.begin(), m_vecVertices.end(), vecVertices.begin()); - - vecIndices.resize(m_vecTriangleIndices.size()); - std::copy(m_vecTriangleIndices.begin(), m_vecTriangleIndices.end(), vecIndices.begin()); - - /*for(std::vector::iterator iterVertices = m_vecTriangleIndices.begin(); iterVertices != m_vecTriangleIndices.end(); ++iterVertices) - { - std::vector::iterator iterVertex = lower_bound(vecVertices.begin(), vecVertices.end(), **iterVertices); - vecIndices.push_back(iterVertex - vecVertices.begin()); - }*/ - } - - const std::vector& IndexedSurfacePatch::getVertices(void) const - { - return m_vecVertices; - } - - std::vector& IndexedSurfacePatch::getVertices(void) - { - return m_vecVertices; - } - const std::vector& IndexedSurfacePatch::getIndices(void) const { return m_vecTriangleIndices; } - const uint32 IndexedSurfacePatch::getNoOfIndices(void) const + uint32 IndexedSurfacePatch::getNoOfIndices(void) const { return m_vecTriangleIndices.size(); - } + } - const uint32 IndexedSurfacePatch::getNoOfVertices(void) const + uint32 IndexedSurfacePatch::getNoOfNonUniformTrianges(void) const { - return m_vecVertices.size(); - } - - const bool IndexedSurfacePatch::isEmpty(void) const - { - return (getNoOfVertices() == 0) || (getNoOfIndices() == 0); - } - - unsigned short IndexedSurfacePatch::getNoNonUniformTrianges(void) - { - unsigned short result = 0; - for(int i = 0; i < m_vecTriangleIndices.size() - 2; i += 3) + uint32 result = 0; + for(uint32 i = 0; i < m_vecTriangleIndices.size() - 2; i += 3) { if((m_vecVertices[m_vecTriangleIndices[i]].getMaterial() == m_vecVertices[m_vecTriangleIndices[i+1]].getMaterial()) && (m_vecVertices[m_vecTriangleIndices[i]].getMaterial() == m_vecVertices[m_vecTriangleIndices[i+2]].getMaterial())) @@ -105,10 +60,10 @@ namespace PolyVox return result; } - unsigned short IndexedSurfacePatch::getNoUniformTrianges(void) + uint32 IndexedSurfacePatch::getNoOfUniformTrianges(void) const { - unsigned short result = 0; - for(int i = 0; i < m_vecTriangleIndices.size() - 2; i += 3) + uint32 result = 0; + for(uint32 i = 0; i < m_vecTriangleIndices.size() - 2; i += 3) { if((m_vecVertices[m_vecTriangleIndices[i]].getMaterial() == m_vecVertices[m_vecTriangleIndices[i+1]].getMaterial()) && (m_vecVertices[m_vecTriangleIndices[i]].getMaterial() == m_vecVertices[m_vecTriangleIndices[i+2]].getMaterial())) @@ -118,4 +73,45 @@ namespace PolyVox } return result; } + + uint32 IndexedSurfacePatch::getNoOfVertices(void) const + { + return m_vecVertices.size(); + } + + std::vector& IndexedSurfacePatch::getRawVertexData(void) + { + return m_vecVertices; + } + + const std::vector& IndexedSurfacePatch::getVertices(void) const + { + return m_vecVertices; + } + + void IndexedSurfacePatch::addTriangle(uint32 index0, uint32 index1, uint32 index2) + { + m_vecTriangleIndices.push_back(index0); + m_vecTriangleIndices.push_back(index1); + m_vecTriangleIndices.push_back(index2); + } + + uint32 IndexedSurfacePatch::addVertex(const SurfaceVertex& vertex) + { + m_vecVertices.push_back(vertex); + return m_vecVertices.size() - 1; + } + + void IndexedSurfacePatch::clear(void) + { + m_vecVertices.clear(); + m_vecTriangleIndices.clear(); + } + + const bool IndexedSurfacePatch::isEmpty(void) const + { + return (getNoOfVertices() == 0) || (getNoOfIndices() == 0); + } + + } diff --git a/library/source/PolyVoxCore/PolyVoxImpl/DecimatedSurfaceExtractor.cpp b/library/source/PolyVoxCore/PolyVoxImpl/DecimatedSurfaceExtractor.cpp index cf66b07c..322aa3a9 100644 --- a/library/source/PolyVoxCore/PolyVoxImpl/DecimatedSurfaceExtractor.cpp +++ b/library/source/PolyVoxCore/PolyVoxImpl/DecimatedSurfaceExtractor.cpp @@ -41,8 +41,7 @@ namespace PolyVox void extractDecimatedSurfaceImpl(BlockVolume* volumeData, uint8 uLevel, Region region, IndexedSurfacePatch* singleMaterialPatch) { - singleMaterialPatch->m_vecVertices.clear(); - singleMaterialPatch->m_vecTriangleIndices.clear(); + singleMaterialPatch->clear(); //For edge indices int32* vertexIndicesX0 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)]; @@ -459,8 +458,8 @@ namespace PolyVox const Vector3DFloat v3dNormal(v000 > v100 ? 1.0f : -1.0f,0.0,0.0); const uint8 uMaterial = v000 | v100; //Because one of these is 0, the or operation takes the max. SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial); - singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); - vertexIndicesX[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = singleMaterialPatch->m_vecVertices.size()-1; + uint32 uLastVertexIndex = singleMaterialPatch->addVertex(surfaceVertex); + vertexIndicesX[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = uLastVertexIndex; } } if (edgeTable[iCubeIndex] & 8) @@ -473,8 +472,8 @@ namespace PolyVox const Vector3DFloat v3dNormal(0.0,v000 > v010 ? 1.0f : -1.0f,0.0); const uint8 uMaterial = v000 | v010; //Because one of these is 0, the or operation takes the max. SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial); - singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); - vertexIndicesY[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = singleMaterialPatch->m_vecVertices.size()-1; + uint32 uLastVertexIndex = singleMaterialPatch->addVertex(surfaceVertex); + vertexIndicesY[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = uLastVertexIndex; } } if (edgeTable[iCubeIndex] & 256) @@ -487,8 +486,8 @@ namespace PolyVox const Vector3DFloat v3dNormal(0.0,0.0,v000 > v001 ? 1.0f : -1.0f); const uint8 uMaterial = v000 | v001; //Because one of these is 0, the or operation takes the max. const SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial); - singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); - vertexIndicesZ[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = singleMaterialPatch->m_vecVertices.size()-1; + uint32 uLastVertexIndex = singleMaterialPatch->addVertex(surfaceVertex); + vertexIndicesZ[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = uLastVertexIndex; } } }//For each cell @@ -584,329 +583,9 @@ namespace PolyVox uint32 ind1 = indlist[triTable[iCubeIndex][i+1]]; uint32 ind2 = indlist[triTable[iCubeIndex][i+2]]; - singleMaterialPatch->m_vecTriangleIndices.push_back(ind0); - singleMaterialPatch->m_vecTriangleIndices.push_back(ind1); - singleMaterialPatch->m_vecTriangleIndices.push_back(ind2); + singleMaterialPatch->addTriangle(ind0, ind1, ind2); }//For each triangle }//For each cell } } - - void generateDecimatedMeshDataForRegionSlow(BlockVolume* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch) - { - //When generating the mesh for a region we actually look one voxel outside it in the - // back, bottom, right direction. Protect against access violations by cropping region here - Region regVolume = volumeData->getEnclosingRegion(); - //regVolume.setUpperCorner(regVolume.getUpperCorner() - Vector3DInt32(1,1,1)); - region.cropTo(regVolume); - region.setUpperCorner(region.getUpperCorner() - Vector3DInt32(1,1,1)); - - //Offset from lower block corner - const Vector3DFloat offset = static_cast(region.getLowerCorner()); - - Vector3DFloat vertlist[12]; - Vector3DFloat normlist[12]; - uint8 vertMaterials[12]; - BlockVolumeIterator volIter(*volumeData); - volIter.setValidRegion(region); - - ////////////////////////////////////////////////////////////////////////// - //Get mesh data - ////////////////////////////////////////////////////////////////////////// - - //Iterate over each cell in the region - //volIter.setPosition(region.getLowerCorner().getX(),region.getLowerCorner().getY(), region.getLowerCorner().getZ()); - for(uint16 z = region.getLowerCorner().getZ(); z <= region.getUpperCorner().getZ(); z += 2) - { - for(uint16 y = region.getLowerCorner().getY(); y <= region.getUpperCorner().getY(); y += 2) - { - for(uint16 x = region.getLowerCorner().getX(); x <= region.getUpperCorner().getX(); x += 2) - { - //while(volIter.moveForwardInRegionXYZ()) - //{ - volIter.setPosition(x,y,z); - const uint8 v000 = volIter.getSubSampledVoxel(1); - volIter.setPosition(x+2,y,z); - const uint8 v100 = volIter.getSubSampledVoxel(1); - volIter.setPosition(x,y+2,z); - const uint8 v010 = volIter.getSubSampledVoxel(1); - volIter.setPosition(x+2,y+2,z); - const uint8 v110 = volIter.getSubSampledVoxel(1); - volIter.setPosition(x,y,z+2); - const uint8 v001 = volIter.getSubSampledVoxel(1); - volIter.setPosition(x+2,y,z+2); - const uint8 v101 = volIter.getSubSampledVoxel(1); - volIter.setPosition(x,y+2,z+2); - const uint8 v011 = volIter.getSubSampledVoxel(1); - volIter.setPosition(x+2,y+2,z+2); - const uint8 v111 = volIter.getSubSampledVoxel(1); - - //Determine the index into the edge table which tells us which vertices are inside of the surface - uint8 iCubeIndex = 0; - - if (v000 == 0) iCubeIndex |= 1; - if (v100 == 0) iCubeIndex |= 2; - if (v110 == 0) iCubeIndex |= 4; - if (v010 == 0) iCubeIndex |= 8; - if (v001 == 0) iCubeIndex |= 16; - if (v101 == 0) iCubeIndex |= 32; - if (v111 == 0) iCubeIndex |= 64; - if (v011 == 0) iCubeIndex |= 128; - - /* Cube is entirely in/out of the surface */ - if (edgeTable[iCubeIndex] == 0) - { - continue; - } - - /* Find the vertices where the surface intersects the cube */ - if (edgeTable[iCubeIndex] & 1) - { - vertlist[0].setX(x + 0.5f * 2.0f); - vertlist[0].setY(y); - vertlist[0].setZ(z); - normlist[0] = Vector3DFloat(v000 - v100,0.0,0.0); - vertMaterials[0] = v000 | v100; //Because one of these is 0, the or operation takes the max. - } - if (edgeTable[iCubeIndex] & 2) - { - vertlist[1].setX(x + 1.0f * 2.0f); - vertlist[1].setY(y + 0.5f * 2.0f); - vertlist[1].setZ(z); - vertMaterials[1] = v100 | v110; - normlist[1] = Vector3DFloat(0.0,v100 - v110,0.0); - } - if (edgeTable[iCubeIndex] & 4) - { - vertlist[2].setX(x + 0.5f * 2.0f); - vertlist[2].setY(y + 1.0f * 2.0f); - vertlist[2].setZ(z); - vertMaterials[2] = v010 | v110; - normlist[2] = Vector3DFloat(v010 - v110,0.0,0.0); - } - if (edgeTable[iCubeIndex] & 8) - { - vertlist[3].setX(x); - vertlist[3].setY(y + 0.5f * 2.0f); - vertlist[3].setZ(z); - vertMaterials[3] = v000 | v010; - normlist[3] = Vector3DFloat(0.0,v000 - v010,0.0); - } - if (edgeTable[iCubeIndex] & 16) - { - vertlist[4].setX(x + 0.5f * 2.0f); - vertlist[4].setY(y); - vertlist[4].setZ(z + 1.0f * 2.0f); - vertMaterials[4] = v001 | v101; - normlist[4] = Vector3DFloat(v001 - v101,0.0,0.0); - } - if (edgeTable[iCubeIndex] & 32) - { - vertlist[5].setX(x + 1.0f * 2.0f); - vertlist[5].setY(y + 0.5f * 2.0f); - vertlist[5].setZ(z + 1.0f * 2.0f); - vertMaterials[5] = v101 | v111; - normlist[5] = Vector3DFloat(0.0,v101 - v111,0.0); - } - if (edgeTable[iCubeIndex] & 64) - { - vertlist[6].setX(x + 0.5f * 2.0f); - vertlist[6].setY(y + 1.0f * 2.0f); - vertlist[6].setZ(z + 1.0f * 2.0f); - vertMaterials[6] = v011 | v111; - normlist[6] = Vector3DFloat(v011 - v111,0.0,0.0); - } - if (edgeTable[iCubeIndex] & 128) - { - vertlist[7].setX(x); - vertlist[7].setY(y + 0.5f * 2.0f); - vertlist[7].setZ(z + 1.0f * 2.0f); - vertMaterials[7] = v001 | v011; - normlist[7] = Vector3DFloat(0.0,v001 - v011,0.0); - } - if (edgeTable[iCubeIndex] & 256) - { - vertlist[8].setX(x); - vertlist[8].setY(y); - vertlist[8].setZ(z + 0.5f * 2.0f); - vertMaterials[8] = v000 | v001; - normlist[8] = Vector3DFloat(0.0,0.0,v000 - v001); - } - if (edgeTable[iCubeIndex] & 512) - { - vertlist[9].setX(x + 1.0f * 2.0f); - vertlist[9].setY(y); - vertlist[9].setZ(z + 0.5f * 2.0f); - vertMaterials[9] = v100 | v101; - normlist[9] = Vector3DFloat(0.0,0.0,v100 - v101); - } - if (edgeTable[iCubeIndex] & 1024) - { - vertlist[10].setX(x + 1.0f * 2.0f); - vertlist[10].setY(y + 1.0f * 2.0f); - vertlist[10].setZ(z + 0.5f * 2.0f); - vertMaterials[10] = v110 | v111; - normlist[10] = Vector3DFloat(0.0,0.0,v110 - v111); - } - if (edgeTable[iCubeIndex] & 2048) - { - vertlist[11].setX(x); - vertlist[11].setY(y + 1.0f * 2.0f); - vertlist[11].setZ(z + 0.5f * 2.0f); - vertMaterials[11] = v010 | v011; - normlist[11] = Vector3DFloat(0.0,0.0,v010 - v011); - } - - for (int i=0;triTable[iCubeIndex][i]!=-1;i+=3) - { - //The three vertices forming a triangle - Vector3DFloat vertex0 = vertlist[triTable[iCubeIndex][i ]] - offset; - Vector3DFloat vertex1 = vertlist[triTable[iCubeIndex][i+1]] - offset; - Vector3DFloat vertex2 = vertlist[triTable[iCubeIndex][i+2]] - offset; - - Vector3DFloat normal0 = normlist[triTable[iCubeIndex][i ]]; - Vector3DFloat normal1 = normlist[triTable[iCubeIndex][i+1]]; - Vector3DFloat normal2 = normlist[triTable[iCubeIndex][i+2]]; - - normal0.normalise(); - normal1.normalise(); - normal2.normalise(); - - vertex0 += (normal0); - vertex1 += (normal1); - vertex2 += (normal2); - - //Cast to floats and divide by two. - //const Vector3DFloat vertex0AsFloat = (static_cast(vertex0) / 2.0f) - offset; - //const Vector3DFloat vertex1AsFloat = (static_cast(vertex1) / 2.0f) - offset; - //const Vector3DFloat vertex2AsFloat = (static_cast(vertex2) / 2.0f) - offset; - - const uint8 material0 = vertMaterials[triTable[iCubeIndex][i ]]; - const uint8 material1 = vertMaterials[triTable[iCubeIndex][i+1]]; - const uint8 material2 = vertMaterials[triTable[iCubeIndex][i+2]]; - - - //If all the materials are the same, we just need one triangle for that material with all the alphas set high. - SurfaceVertex surfaceVertex0Alpha1(vertex0,material0 + 0.1f); - surfaceVertex0Alpha1.setNormal(normal0); - SurfaceVertex surfaceVertex1Alpha1(vertex1,material1 + 0.1f); - surfaceVertex1Alpha1.setNormal(normal1); - SurfaceVertex surfaceVertex2Alpha1(vertex2,material2 + 0.1f); - surfaceVertex2Alpha1.setNormal(normal2); - singleMaterialPatch->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha1); - }//For each triangle - //}//For each cell - } - } - } - - //FIXME - can it happen that we have no vertices or triangles? Should exit early? - - - //for(std::map::iterator iterPatch = surfacePatchMapResult.begin(); iterPatch != surfacePatchMapResult.end(); ++iterPatch) - { - - /*std::vector::iterator iterSurfaceVertex = singleMaterialPatch->getVertices().begin(); - while(iterSurfaceVertex != singleMaterialPatch->getVertices().end()) - { - Vector3DFloat tempNormal = computeNormal(volumeData, static_cast(iterSurfaceVertex->getPosition() + offset), SIMPLE); - const_cast(*iterSurfaceVertex).setNormal(tempNormal); - ++iterSurfaceVertex; - }*/ - } - } - - Vector3DFloat computeDecimatedNormal(BlockVolume* volumeData, const Vector3DFloat& position, NormalGenerationMethod normalGenerationMethod) - { - const float posX = position.getX(); - const float posY = position.getY(); - const float posZ = position.getZ(); - - const uint16 floorX = static_cast(posX); - const uint16 floorY = static_cast(posY); - const uint16 floorZ = static_cast(posZ); - - //Check all corners are within the volume, allowing a boundary for gradient estimation - bool lowerCornerInside = volumeData->containsPoint(Vector3DInt32(floorX, floorY, floorZ),1); - bool upperCornerInside = volumeData->containsPoint(Vector3DInt32(floorX+1, floorY+1, floorZ+1),1); - if((!lowerCornerInside) || (!upperCornerInside)) - { - normalGenerationMethod = SIMPLE; - } - - Vector3DFloat result; - - BlockVolumeIterator volIter(*volumeData); //FIXME - save this somewhere - could be expensive to create? - - - if(normalGenerationMethod == SOBEL) - { - volIter.setPosition(static_cast(posX),static_cast(posY),static_cast(posZ)); - const Vector3DFloat gradFloor = computeSobelGradient(volIter); - if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 - { - volIter.setPosition(static_cast(posX+1.0),static_cast(posY),static_cast(posZ)); - } - if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5 - { - volIter.setPosition(static_cast(posX),static_cast(posY+1.0),static_cast(posZ)); - } - if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5 - { - volIter.setPosition(static_cast(posX),static_cast(posY),static_cast(posZ+1.0)); - } - const Vector3DFloat gradCeil = computeSobelGradient(volIter); - result = ((gradFloor + gradCeil) * -1.0f); - if(result.lengthSquared() < 0.0001) - { - //Operation failed - fall back on simple gradient estimation - normalGenerationMethod = SIMPLE; - } - } - if(normalGenerationMethod == CENTRAL_DIFFERENCE) - { - volIter.setPosition(static_cast(posX),static_cast(posY),static_cast(posZ)); - const Vector3DFloat gradFloor = computeCentralDifferenceGradient(volIter); - if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 - { - volIter.setPosition(static_cast(posX+1.0),static_cast(posY),static_cast(posZ)); - } - if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5 - { - volIter.setPosition(static_cast(posX),static_cast(posY+1.0),static_cast(posZ)); - } - if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5 - { - volIter.setPosition(static_cast(posX),static_cast(posY),static_cast(posZ+1.0)); - } - const Vector3DFloat gradCeil = computeCentralDifferenceGradient(volIter); - result = ((gradFloor + gradCeil) * -1.0f); - if(result.lengthSquared() < 0.0001) - { - //Operation failed - fall back on simple gradient estimation - normalGenerationMethod = SIMPLE; - } - } - if(normalGenerationMethod == SIMPLE) - { - volIter.setPosition(static_cast(posX),static_cast(posY),static_cast(posZ)); - const uint8 uFloor = volIter.getVoxel() > 0 ? 1 : 0; - if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 - { - uint8 uCeil = volIter.peekVoxel1px0py0pz() > 0 ? 1 : 0; - result = Vector3DFloat(static_cast(uFloor - uCeil),0.0,0.0); - } - else if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5 - { - uint8 uCeil = volIter.peekVoxel0px1py0pz() > 0 ? 1 : 0; - result = Vector3DFloat(0.0,static_cast(uFloor - uCeil),0.0); - } - else if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5 - { - uint8 uCeil = volIter.peekVoxel0px0py1pz() > 0 ? 1 : 0; - result = Vector3DFloat(0.0, 0.0,static_cast(uFloor - uCeil)); - } - } - return result; - } } diff --git a/library/source/PolyVoxCore/PolyVoxImpl/FastSurfaceExtractor.cpp b/library/source/PolyVoxCore/PolyVoxImpl/FastSurfaceExtractor.cpp index 7531c1cc..f4203db5 100644 --- a/library/source/PolyVoxCore/PolyVoxImpl/FastSurfaceExtractor.cpp +++ b/library/source/PolyVoxCore/PolyVoxImpl/FastSurfaceExtractor.cpp @@ -31,8 +31,7 @@ namespace PolyVox void extractFastSurfaceImpl(BlockVolume* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch) { - singleMaterialPatch->m_vecVertices.clear(); - singleMaterialPatch->m_vecTriangleIndices.clear(); + singleMaterialPatch->clear(); //For edge indices int32* vertexIndicesX0 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)]; @@ -411,8 +410,8 @@ namespace PolyVox const Vector3DFloat v3dNormal(v000 > v100 ? 1.0f : -1.0f, 0.0f, 0.0f); const uint8 uMaterial = v000 | v100; //Because one of these is 0, the or operation takes the max. const SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial); - singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); - vertexIndicesX[getIndex(x,y)] = singleMaterialPatch->m_vecVertices.size()-1; + uint32 uLastVertexIndex = singleMaterialPatch->addVertex(surfaceVertex); + vertexIndicesX[getIndex(x,y)] = uLastVertexIndex; } } if (edgeTable[iCubeIndex] & 8) @@ -424,8 +423,8 @@ namespace PolyVox const Vector3DFloat v3dNormal(0.0f, v000 > v010 ? 1.0f : -1.0f, 0.0f); const uint8 uMaterial = v000 | v010; SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial); - singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); - vertexIndicesY[getIndex(x,y)] = singleMaterialPatch->m_vecVertices.size()-1; + uint32 uLastVertexIndex = singleMaterialPatch->addVertex(surfaceVertex); + vertexIndicesY[getIndex(x,y)] = uLastVertexIndex; } } if (edgeTable[iCubeIndex] & 256) @@ -437,8 +436,8 @@ namespace PolyVox const Vector3DFloat v3dNormal(0.0f, 0.0f, v000 > v001 ? 1.0f : -1.0f); const uint8 uMaterial = v000 | v001; SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial); - singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); - vertexIndicesZ[getIndex(x,y)] = singleMaterialPatch->m_vecVertices.size()-1; + uint32 uLastVertexIndex = singleMaterialPatch->addVertex(surfaceVertex); + vertexIndicesZ[getIndex(x,y)] = uLastVertexIndex; } } }while(volIter.moveForwardInRegionXYZ());//For each cell @@ -537,9 +536,7 @@ namespace PolyVox uint32 ind1 = indlist[triTable[iCubeIndex][i+1]]; uint32 ind2 = indlist[triTable[iCubeIndex][i+2]]; - singleMaterialPatch->m_vecTriangleIndices.push_back(ind0); - singleMaterialPatch->m_vecTriangleIndices.push_back(ind1); - singleMaterialPatch->m_vecTriangleIndices.push_back(ind2); + singleMaterialPatch->addTriangle(ind0, ind1, ind2); }//For each triangle }while(volIter.moveForwardInRegionXYZ());//For each cell } diff --git a/library/source/PolyVoxCore/PolyVoxImpl/ReferenceSurfaceExtractor.cpp b/library/source/PolyVoxCore/PolyVoxImpl/ReferenceSurfaceExtractor.cpp index 4c2c8c81..95218764 100644 --- a/library/source/PolyVoxCore/PolyVoxImpl/ReferenceSurfaceExtractor.cpp +++ b/library/source/PolyVoxCore/PolyVoxImpl/ReferenceSurfaceExtractor.cpp @@ -245,43 +245,28 @@ namespace PolyVox SurfaceVertex v1(vertex1, normal1, material1 + 0.1f); SurfaceVertex v2(vertex2, normal2, material2 + 0.1f); - //singleMaterialPatch->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha1); - - int32 index = getIndexFor(v0.getPosition(), vertexIndicesX, vertexIndicesY, vertexIndicesZ); - if(index == -1) + int32 index0 = getIndexFor(v0.getPosition(), vertexIndicesX, vertexIndicesY, vertexIndicesZ); + if(index0 == -1) { - singleMaterialPatch->m_vecVertices.push_back(v0); - singleMaterialPatch->m_vecTriangleIndices.push_back(singleMaterialPatch->m_vecVertices.size()-1); - setIndexFor(v0.getPosition(), singleMaterialPatch->m_vecVertices.size()-1, vertexIndicesX, vertexIndicesY, vertexIndicesZ); - } - else - { - singleMaterialPatch->m_vecTriangleIndices.push_back(index); + index0 = singleMaterialPatch->addVertex(v0); + setIndexFor(v0.getPosition(), index0, vertexIndicesX, vertexIndicesY, vertexIndicesZ); } - index = getIndexFor(v1.getPosition(), vertexIndicesX, vertexIndicesY, vertexIndicesZ); - if(index == -1) + int32 index1 = getIndexFor(v1.getPosition(), vertexIndicesX, vertexIndicesY, vertexIndicesZ); + if(index1 == -1) { - singleMaterialPatch->m_vecVertices.push_back(v1); - singleMaterialPatch->m_vecTriangleIndices.push_back(singleMaterialPatch->m_vecVertices.size()-1); - setIndexFor(v1.getPosition(), singleMaterialPatch->m_vecVertices.size()-1, vertexIndicesX, vertexIndicesY, vertexIndicesZ); - } - else - { - singleMaterialPatch->m_vecTriangleIndices.push_back(index); + index1 = singleMaterialPatch->addVertex(v1); + setIndexFor(v1.getPosition(), index1, vertexIndicesX, vertexIndicesY, vertexIndicesZ); } - index = getIndexFor(v2.getPosition(), vertexIndicesX, vertexIndicesY, vertexIndicesZ); - if(index == -1) + int32 index2 = getIndexFor(v2.getPosition(), vertexIndicesX, vertexIndicesY, vertexIndicesZ); + if(index2 == -1) { - singleMaterialPatch->m_vecVertices.push_back(v2); - singleMaterialPatch->m_vecTriangleIndices.push_back(singleMaterialPatch->m_vecVertices.size()-1); - setIndexFor(v2.getPosition(), singleMaterialPatch->m_vecVertices.size()-1, vertexIndicesX, vertexIndicesY, vertexIndicesZ); - } - else - { - singleMaterialPatch->m_vecTriangleIndices.push_back(index); + index2 = singleMaterialPatch->addVertex(v2); + setIndexFor(v2.getPosition(), index2, vertexIndicesX, vertexIndicesY, vertexIndicesZ); } + + singleMaterialPatch->addTriangle(index0, index1, index2); }//For each triangle }//For each cell } diff --git a/library/source/PolyVoxCore/SurfaceAdjusters.cpp b/library/source/PolyVoxCore/SurfaceAdjusters.cpp index cda8accf..b6dc7746 100644 --- a/library/source/PolyVoxCore/SurfaceAdjusters.cpp +++ b/library/source/PolyVoxCore/SurfaceAdjusters.cpp @@ -19,7 +19,7 @@ namespace PolyVox BlockVolumeIterator volIter(*volumeData); - std::vector& vecVertices = isp.m_vecVertices; + std::vector& vecVertices = isp.getRawVertexData(); std::vector::iterator iterSurfaceVertex = vecVertices.begin(); while(iterSurfaceVertex != vecVertices.end()) { @@ -80,7 +80,7 @@ namespace PolyVox { BlockVolumeIterator volIter(*volumeData); - std::vector& vecVertices = isp.m_vecVertices; + std::vector& vecVertices = isp.getRawVertexData(); std::vector::iterator iterSurfaceVertex = vecVertices.begin(); while(iterSurfaceVertex != vecVertices.end()) {