diff --git a/PolyVoxCore/include/SurfaceExtractors.h b/PolyVoxCore/include/SurfaceExtractors.h index 722bb6d5..56327815 100644 --- a/PolyVoxCore/include/SurfaceExtractors.h +++ b/PolyVoxCore/include/SurfaceExtractors.h @@ -42,7 +42,7 @@ namespace PolyVox POLYVOX_API boost::uint32_t computeInitialRoughBitmaskForSlice(BlockVolumeIterator& volIter, const Region& regSlice, const Vector3DFloat& offset, boost::uint8_t *bitmask); POLYVOX_API boost::uint32_t computeRoughBitmaskForSliceFromPrevious(BlockVolumeIterator& volIter, const Region& regSlice, const Vector3DFloat& offset, boost::uint8_t *bitmask, boost::uint8_t *previousBitmask); POLYVOX_API void generateRoughIndicesForSlice(BlockVolumeIterator& volIter, const Region& regSlice, IndexedSurfacePatch* singleMaterialPatch, const Vector3DFloat& offset, boost::uint8_t* bitmask0, boost::uint8_t* bitmask1, boost::int32_t vertexIndicesX0[],boost::int32_t vertexIndicesY0[],boost::int32_t vertexIndicesZ0[], boost::int32_t vertexIndicesX1[],boost::int32_t vertexIndicesY1[],boost::int32_t vertexIndicesZ1[]); - POLYVOX_API void generateRoughVerticesForSlice(BlockVolumeIterator& volIter, Region& regSlice, const Vector3DFloat& offset, boost::uint8_t* bitmask, IndexedSurfacePatch* singleMaterialPatch,boost::int32_t vertexIndicesX[],boost::int32_t vertexIndicesY[],boost::int32_t vertexIndicesZ[], Vector3DFloat vertlist[], boost::uint8_t vertMaterials[]); + POLYVOX_API void generateRoughVerticesForSlice(BlockVolumeIterator& 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* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch); POLYVOX_API Vector3DFloat computeNormal(BlockVolume* volumeData, const Vector3DFloat& position, NormalGenerationMethod normalGenerationMethod); diff --git a/PolyVoxCore/include/SurfaceExtractorsDecimated.h b/PolyVoxCore/include/SurfaceExtractorsDecimated.h index 310fa9b4..3271159e 100644 --- a/PolyVoxCore/include/SurfaceExtractorsDecimated.h +++ b/PolyVoxCore/include/SurfaceExtractorsDecimated.h @@ -40,7 +40,7 @@ namespace PolyVox POLYVOX_API boost::uint32_t computeInitialDecimatedBitmaskForSlice(BlockVolumeIterator& volIter, const Region& regSlice, const Vector3DFloat& offset, boost::uint8_t *bitmask); POLYVOX_API boost::uint32_t computeDecimatedBitmaskForSliceFromPrevious(BlockVolumeIterator& volIter, const Region& regSlice, const Vector3DFloat& offset, boost::uint8_t *bitmask, boost::uint8_t *previousBitmask); POLYVOX_API void generateDecimatedIndicesForSlice(BlockVolumeIterator& volIter, const Region& regSlice, IndexedSurfacePatch* singleMaterialPatch, const Vector3DFloat& offset, boost::uint8_t* bitmask0, boost::uint8_t* bitmask1, boost::int32_t vertexIndicesX0[],boost::int32_t vertexIndicesY0[],boost::int32_t vertexIndicesZ0[], boost::int32_t vertexIndicesX1[],boost::int32_t vertexIndicesY1[],boost::int32_t vertexIndicesZ1[]); - POLYVOX_API void generateDecimatedVerticesForSlice(BlockVolumeIterator& volIter, Region& regSlice, const Vector3DFloat& offset, boost::uint8_t* bitmask, IndexedSurfacePatch* singleMaterialPatch,boost::int32_t vertexIndicesX[],boost::int32_t vertexIndicesY[],boost::int32_t vertexIndicesZ[], Vector3DFloat vertlist[], boost::uint8_t vertMaterials[]); + POLYVOX_API void generateDecimatedVerticesForSlice(BlockVolumeIterator& 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 generateDecimatedMeshDataForRegionSlow(BlockVolume* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch); diff --git a/PolyVoxCore/include/SurfaceVertex.h b/PolyVoxCore/include/SurfaceVertex.h index 5be6b722..1f387d90 100644 --- a/PolyVoxCore/include/SurfaceVertex.h +++ b/PolyVoxCore/include/SurfaceVertex.h @@ -32,7 +32,7 @@ namespace PolyVox public: SurfaceVertex(); SurfaceVertex(Vector3DFloat positionToSet, float materialToSet, float alphaToSet); - SurfaceVertex(Vector3DFloat positionToSet, Vector3DFloat normalToSet); + SurfaceVertex(Vector3DFloat positionToSet, Vector3DFloat normalToSet, float materialToSet, float alphaToSet); friend bool operator==(const SurfaceVertex& lhs, const SurfaceVertex& rhs); friend bool operator < (const SurfaceVertex& lhs, const SurfaceVertex& rhs); diff --git a/PolyVoxCore/source/SurfaceExtractors.cpp b/PolyVoxCore/source/SurfaceExtractors.cpp index a4052397..a8351c05 100644 --- a/PolyVoxCore/source/SurfaceExtractors.cpp +++ b/PolyVoxCore/source/SurfaceExtractors.cpp @@ -29,7 +29,7 @@ namespace PolyVox regionGeometry.m_patchSingleMaterial = new IndexedSurfacePatch(false); regionGeometry.m_v3dRegionPosition = iterChangedRegions->getLowerCorner(); - generateRoughMeshDataForRegion(volume.getVolumeData(), *iterChangedRegions, regionGeometry.m_patchSingleMaterial); + generateDecimatedMeshDataForRegion(volume.getVolumeData(), *iterChangedRegions, regionGeometry.m_patchSingleMaterial); //genMultiFromSingle(regionGeometry.m_patchSingleMaterial, regionGeometry.m_patchMultiMaterial); @@ -73,10 +73,6 @@ namespace PolyVox //Offset from volume corner const Vector3DFloat offset = static_cast(region.getLowerCorner()); - //Temporary space use to store the vertices - Vector3DFloat vertlist[12]; - uint8_t vertMaterials[12]; - //Create a region corresponding to the first slice Region regSlice0(region); regSlice0.setUpperCorner(Vector3DInt32(regSlice0.getUpperCorner().getX(),regSlice0.getUpperCorner().getY(),regSlice0.getLowerCorner().getZ())); @@ -89,7 +85,7 @@ namespace PolyVox if(uNoOfNonEmptyCellsForSlice0 != 0) { //If there were some non-empty cells then generate initial slice vertices for them - generateRoughVerticesForSlice(volIter,regSlice0, offset, bitmask0, singleMaterialPatch, vertexIndicesX0, vertexIndicesY0, vertexIndicesZ0, /*regTwoSlice.getUpperCorner(),*/ vertlist, vertMaterials); + generateRoughVerticesForSlice(volIter,regSlice0, offset, bitmask0, singleMaterialPatch, vertexIndicesX0, vertexIndicesY0, vertexIndicesZ0); } for(boost::uint32_t uSlice = 0; ((uSlice <= POLYVOX_REGION_SIDE_LENGTH-1) && (uSlice + offset.getZ() < region.getUpperCorner().getZ())); ++uSlice) @@ -101,7 +97,7 @@ namespace PolyVox if(uNoOfNonEmptyCellsForSlice1 != 0) { - generateRoughVerticesForSlice(volIter,regSlice1, offset, bitmask1, singleMaterialPatch, vertexIndicesX1, vertexIndicesY1, vertexIndicesZ1, vertlist, vertMaterials); + generateRoughVerticesForSlice(volIter,regSlice1, offset, bitmask1, singleMaterialPatch, vertexIndicesX1, vertexIndicesY1, vertexIndicesZ1); } if((uNoOfNonEmptyCellsForSlice0 != 0) || (uNoOfNonEmptyCellsForSlice1 != 0)) @@ -403,7 +399,7 @@ namespace PolyVox return uNoOfNonEmptyCells; } - void generateRoughVerticesForSlice(BlockVolumeIterator& volIter, Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask, IndexedSurfacePatch* singleMaterialPatch,boost::int32_t vertexIndicesX[],boost::int32_t vertexIndicesY[],boost::int32_t vertexIndicesZ[], Vector3DFloat vertlist[], uint8_t vertMaterials[]) + void generateRoughVerticesForSlice(BlockVolumeIterator& volIter, Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask, IndexedSurfacePatch* singleMaterialPatch,boost::int32_t vertexIndicesX[],boost::int32_t vertexIndicesY[],boost::int32_t vertexIndicesZ[]) { //Iterate over each cell in the region volIter.setPosition(regSlice.getLowerCorner().getX(),regSlice.getLowerCorner().getY(), regSlice.getLowerCorner().getZ()); @@ -432,11 +428,10 @@ namespace PolyVox { if((x + offset.getX()) != regSlice.getUpperCorner().getX()) { - vertlist[0].setX(x + 0.5f); - vertlist[0].setY(y); - vertlist[0].setZ(z); - vertMaterials[0] = v000 | volIter.peekVoxel1px0py0pz(); //Because one of these is 0, the or operation takes the max. - SurfaceVertex surfaceVertex(vertlist[0],vertMaterials[0], 1.0); + const Vector3DFloat v3dPosition(x + 0.5f, y, z); + const Vector3DFloat v3dNormal(1.0f, 0.0f, 0.0f); + const uint8_t uMaterial = v000 | volIter.peekVoxel1px0py0pz(); //Because one of these is 0, the or operation takes the max. + const SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); vertexIndicesX[getIndex(x,y)] = singleMaterialPatch->m_vecVertices.size()-1; } @@ -445,11 +440,10 @@ namespace PolyVox { if((y + offset.getY()) != regSlice.getUpperCorner().getY()) { - vertlist[3].setX(x); - vertlist[3].setY(y + 0.5f); - vertlist[3].setZ(z); - vertMaterials[3] = v000 | volIter.peekVoxel0px1py0pz(); - SurfaceVertex surfaceVertex(vertlist[3],vertMaterials[3], 1.0); + const Vector3DFloat v3dPosition(x, y + 0.5f, z); + const Vector3DFloat v3dNormal(0.0f, 1.0f, 0.0f); + const uint8_t uMaterial = v000 | volIter.peekVoxel0px1py0pz(); + SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); vertexIndicesY[getIndex(x,y)] = singleMaterialPatch->m_vecVertices.size()-1; } @@ -458,11 +452,10 @@ namespace PolyVox { //if((z + offset.getZ()) != upperCorner.getZ()) { - vertlist[8].setX(x); - vertlist[8].setY(y); - vertlist[8].setZ(z + 0.5f); - vertMaterials[8] = v000 | volIter.peekVoxel0px0py1pz(); - SurfaceVertex surfaceVertex(vertlist[8],vertMaterials[8], 1.0); + const Vector3DFloat v3dPosition(x, y, z + 0.5f); + const Vector3DFloat v3dNormal(0.0f, 0.0f, 1.0f); + const uint8_t uMaterial = v000 | volIter.peekVoxel0px0py1pz(); + SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); vertexIndicesZ[getIndex(x,y)] = singleMaterialPatch->m_vecVertices.size()-1; } diff --git a/PolyVoxCore/source/SurfaceExtractorsDecimated.cpp b/PolyVoxCore/source/SurfaceExtractorsDecimated.cpp index de3e4e71..12c64cc0 100644 --- a/PolyVoxCore/source/SurfaceExtractorsDecimated.cpp +++ b/PolyVoxCore/source/SurfaceExtractorsDecimated.cpp @@ -46,10 +46,6 @@ namespace PolyVox //Offset from volume corner const Vector3DFloat offset = static_cast(region.getLowerCorner()); - //Temporary space use to store the vertices - Vector3DFloat vertlist[12]; - uint8_t vertMaterials[12]; - //Create a region corresponding to the first slice Region regSlice0(region); regSlice0.setUpperCorner(Vector3DInt32(regSlice0.getUpperCorner().getX(),regSlice0.getUpperCorner().getY(),regSlice0.getLowerCorner().getZ())); @@ -62,7 +58,7 @@ namespace PolyVox if(uNoOfNonEmptyCellsForSlice0 != 0) { //If there were some non-empty cells then generate initial slice vertices for them - generateDecimatedVerticesForSlice(volIter,regSlice0, offset, bitmask0, singleMaterialPatch, vertexIndicesX0, vertexIndicesY0, vertexIndicesZ0, /*regTwoSlice.getUpperCorner(),*/ vertlist, vertMaterials); + generateDecimatedVerticesForSlice(volIter,regSlice0, offset, bitmask0, singleMaterialPatch, vertexIndicesX0, vertexIndicesY0, vertexIndicesZ0); } for(boost::uint32_t uSlice = 0; ((uSlice <= POLYVOX_REGION_SIDE_LENGTH-1) && (uSlice + offset.getZ() < region.getUpperCorner().getZ())); uSlice += 2) @@ -74,7 +70,7 @@ namespace PolyVox if(uNoOfNonEmptyCellsForSlice1 != 0) { - generateDecimatedVerticesForSlice(volIter,regSlice1, offset, bitmask1, singleMaterialPatch, vertexIndicesX1, vertexIndicesY1, vertexIndicesZ1, vertlist, vertMaterials); + generateDecimatedVerticesForSlice(volIter,regSlice1, offset, bitmask1, singleMaterialPatch, vertexIndicesX1, vertexIndicesY1, vertexIndicesZ1); } if((uNoOfNonEmptyCellsForSlice0 != 0) || (uNoOfNonEmptyCellsForSlice1 != 0)) @@ -101,13 +97,13 @@ namespace PolyVox delete[] vertexIndicesZ1; - std::vector::iterator iterSurfaceVertex = singleMaterialPatch->getVertices().begin(); + /*std::vector::iterator iterSurfaceVertex = singleMaterialPatch->getVertices().begin(); while(iterSurfaceVertex != singleMaterialPatch->getVertices().end()) { Vector3DFloat tempNormal = computeDecimatedNormal(volumeData, static_cast(iterSurfaceVertex->getPosition() + offset), CENTRAL_DIFFERENCE); const_cast(*iterSurfaceVertex).setNormal(tempNormal); ++iterSurfaceVertex; - } + }*/ } boost::uint32_t computeInitialDecimatedBitmaskForSlice(BlockVolumeIterator& volIter, const Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask) @@ -118,9 +114,9 @@ namespace PolyVox //volIter.setPosition(regSlice.getLowerCorner().getX(),regSlice.getLowerCorner().getY(), regSlice.getLowerCorner().getZ()); //volIter.setValidRegion(regSlice); //do - for(uint16_t y = regSlice.getLowerCorner().getY(); y < regSlice.getUpperCorner().getY(); y += 2) + for(uint16_t y = regSlice.getLowerCorner().getY(); y <= regSlice.getUpperCorner().getY(); y += 2) { - for(uint16_t x = regSlice.getLowerCorner().getX(); x < regSlice.getUpperCorner().getX(); x += 2) + for(uint16_t x = regSlice.getLowerCorner().getX(); x <= regSlice.getUpperCorner().getX(); x += 2) { //Current position //const uint16_t x = volIter.getPosX() - offset.getX(); @@ -291,9 +287,9 @@ namespace PolyVox //volIter.setPosition(regSlice.getLowerCorner().getX(),regSlice.getLowerCorner().getY(), regSlice.getLowerCorner().getZ()); //volIter.setValidRegion(regSlice); //do - for(uint16_t y = regSlice.getLowerCorner().getY(); y < regSlice.getUpperCorner().getY(); y += 2) + for(uint16_t y = regSlice.getLowerCorner().getY(); y <= regSlice.getUpperCorner().getY()+1; y += 2) { - for(uint16_t x = regSlice.getLowerCorner().getX(); x < regSlice.getUpperCorner().getX(); x += 2) + for(uint16_t x = regSlice.getLowerCorner().getX(); x <= regSlice.getUpperCorner().getX()+1; x += 2) { //Current position //const uint16_t x = volIter.getPosX() - offset.getX(); @@ -413,16 +409,16 @@ namespace PolyVox return uNoOfNonEmptyCells; } - void generateDecimatedVerticesForSlice(BlockVolumeIterator& volIter, Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask, IndexedSurfacePatch* singleMaterialPatch,boost::int32_t vertexIndicesX[],boost::int32_t vertexIndicesY[],boost::int32_t vertexIndicesZ[], Vector3DFloat vertlist[], uint8_t vertMaterials[]) + void generateDecimatedVerticesForSlice(BlockVolumeIterator& volIter, Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask, IndexedSurfacePatch* singleMaterialPatch,boost::int32_t vertexIndicesX[],boost::int32_t vertexIndicesY[],boost::int32_t vertexIndicesZ[]) { //Iterate over each cell in the region //volIter.setPosition(regSlice.getLowerCorner().getX(),regSlice.getLowerCorner().getY(), regSlice.getLowerCorner().getZ()); //volIter.setValidRegion(regSlice); //while(volIter.moveForwardInRegionXYZ()) //do - for(uint16_t y = regSlice.getLowerCorner().getY(); y < regSlice.getUpperCorner().getY(); y += 2) + for(uint16_t y = regSlice.getLowerCorner().getY(); y < regSlice.getUpperCorner().getY()+1; y += 2) { - for(uint16_t x = regSlice.getLowerCorner().getX(); x < regSlice.getUpperCorner().getX(); x += 2) + for(uint16_t x = regSlice.getLowerCorner().getX(); x < regSlice.getUpperCorner().getX()+1; x += 2) { //Current position //const uint16_t x = volIter.getPosX() - offset.getX(); @@ -446,12 +442,11 @@ namespace PolyVox { if((x) != regSlice.getUpperCorner().getX()) { - vertlist[0].setX(x - offset.getX() + 0.5f * 2.0f); - vertlist[0].setY(y - offset.getY()); - vertlist[0].setZ(z - offset.getZ()); + const Vector3DFloat v3dPosition(x - offset.getX() + 0.5f * 2.0f, y - offset.getY(), z - offset.getZ()); + const Vector3DFloat v3dNormal(1.0,0.0,0.0); volIter.setPosition(x+2,y,z); - vertMaterials[0] = v000 | volIter.getMaxedVoxel(); //Because one of these is 0, the or operation takes the max. - SurfaceVertex surfaceVertex(vertlist[0],vertMaterials[0], 1.0); + const uint8_t uMaterial = v000 | volIter.getMaxedVoxel(); //Because one of these is 0, the or operation takes the max. + SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); vertexIndicesX[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = singleMaterialPatch->m_vecVertices.size()-1; } @@ -460,12 +455,11 @@ namespace PolyVox { if((y) != regSlice.getUpperCorner().getY()) { - vertlist[3].setX(x - offset.getX()); - vertlist[3].setY(y - offset.getY() + 0.5f * 2.0f); - vertlist[3].setZ(z - offset.getZ()); + const Vector3DFloat v3dPosition(x - offset.getX(), y - offset.getY() + 0.5f * 2.0f, z - offset.getZ()); + const Vector3DFloat v3dNormal(0.0,1.0,0.0); volIter.setPosition(x,y+2,z); - vertMaterials[3] = v000 | volIter.getMaxedVoxel(); - SurfaceVertex surfaceVertex(vertlist[3],vertMaterials[3], 1.0); + const uint8_t uMaterial = v000 | volIter.getMaxedVoxel(); //Because one of these is 0, the or operation takes the max. + SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); vertexIndicesY[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = singleMaterialPatch->m_vecVertices.size()-1; } @@ -474,12 +468,11 @@ namespace PolyVox { //if((z + offset.getZ()) != upperCorner.getZ()) { - vertlist[8].setX(x - offset.getX()); - vertlist[8].setY(y - offset.getY()); - vertlist[8].setZ(z - offset.getZ() + 0.5f * 2.0f); + const Vector3DFloat v3dPosition(x - offset.getX(), y - offset.getY(), z - offset.getZ() + 0.5f * 2.0f); + const Vector3DFloat v3dNormal(0.0,0.0,1.0); volIter.setPosition(x,y,z+2); - vertMaterials[8] = v000 | volIter.getMaxedVoxel(); - SurfaceVertex surfaceVertex(vertlist[8],vertMaterials[8], 1.0); + const uint8_t uMaterial = v000 | volIter.getMaxedVoxel(); //Because one of these is 0, the or operation takes the max. + const SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); vertexIndicesZ[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = singleMaterialPatch->m_vecVertices.size()-1; } @@ -498,9 +491,9 @@ namespace PolyVox //volIter.setPosition(regCroppedSlice.getLowerCorner().getX(),regCroppedSlice.getLowerCorner().getY(), regCroppedSlice.getLowerCorner().getZ()); //volIter.setValidRegion(regCroppedSlice); //do - for(uint16_t y = regCroppedSlice.getLowerCorner().getY() - offset.getY(); y < regCroppedSlice.getUpperCorner().getY() - offset.getY(); y += 2) + for(uint16_t y = regCroppedSlice.getLowerCorner().getY() - offset.getY(); y <= regCroppedSlice.getUpperCorner().getY() - offset.getY(); y += 2) { - for(uint16_t x = regCroppedSlice.getLowerCorner().getX() - offset.getX(); x < regCroppedSlice.getUpperCorner().getX() - offset.getX(); x += 2) + for(uint16_t x = regCroppedSlice.getLowerCorner().getX() - offset.getX(); x <= regCroppedSlice.getUpperCorner().getX() - offset.getX(); x += 2) { //Current position //const uint16_t x = volIter.getPosX() - offset.getX(); diff --git a/PolyVoxCore/source/SurfaceVertex.cpp b/PolyVoxCore/source/SurfaceVertex.cpp index 25887a4d..a74e2f38 100644 --- a/PolyVoxCore/source/SurfaceVertex.cpp +++ b/PolyVoxCore/source/SurfaceVertex.cpp @@ -38,9 +38,11 @@ namespace PolyVox } - SurfaceVertex::SurfaceVertex(Vector3DFloat positionToSet, Vector3DFloat normalToSet) + SurfaceVertex::SurfaceVertex(Vector3DFloat positionToSet, Vector3DFloat normalToSet, float materialToSet, float alphaToSet) :position(positionToSet) ,normal(normalToSet) + ,material(materialToSet) + ,alpha(alphaToSet) { }