diff --git a/PolyVoxCore/include/BlockVolumeIterator.h b/PolyVoxCore/include/BlockVolumeIterator.h index 6c3af7df..7ac934ad 100644 --- a/PolyVoxCore/include/BlockVolumeIterator.h +++ b/PolyVoxCore/include/BlockVolumeIterator.h @@ -44,6 +44,7 @@ namespace PolyVox bool operator>=(const BlockVolumeIterator& rhs); float getAveragedVoxel(boost::uint16_t size) const; + VoxelType getMaxedVoxel(void) const; boost::uint16_t getPosX(void) const; boost::uint16_t getPosY(void) const; boost::uint16_t getPosZ(void) const; diff --git a/PolyVoxCore/include/BlockVolumeIterator.inl b/PolyVoxCore/include/BlockVolumeIterator.inl index cabdaa5a..4a5054b8 100644 --- a/PolyVoxCore/include/BlockVolumeIterator.inl +++ b/PolyVoxCore/include/BlockVolumeIterator.inl @@ -136,6 +136,21 @@ namespace PolyVox return sum; } + template + VoxelType BlockVolumeIterator::getMaxedVoxel(void) const + { + VoxelType tValue = getVoxel(); + tValue = (std::max)(tValue, peekVoxel1px0py0pz()); + tValue = (std::max)(tValue, peekVoxel0px1py0pz()); + tValue = (std::max)(tValue, peekVoxel1px1py0pz()); + tValue = (std::max)(tValue, peekVoxel0px0py1pz()); + tValue = (std::max)(tValue, peekVoxel1px0py1pz()); + tValue = (std::max)(tValue, peekVoxel0px1py1pz()); + tValue = (std::max)(tValue, peekVoxel1px1py1pz()); + return tValue; + + } + template boost::uint16_t BlockVolumeIterator::getPosX(void) const { diff --git a/PolyVoxCore/include/GradientEstimators.h b/PolyVoxCore/include/GradientEstimators.h index 65dec040..8b65f543 100644 --- a/PolyVoxCore/include/GradientEstimators.h +++ b/PolyVoxCore/include/GradientEstimators.h @@ -32,6 +32,9 @@ namespace PolyVox template Vector3DFloat computeSmoothCentralDifferenceGradient(BlockVolumeIterator& volIter); + template + Vector3DFloat computeDecimatedCentralDifferenceGradient(BlockVolumeIterator& volIter); + template Vector3DFloat computeSobelGradient(const BlockVolumeIterator& volIter); } diff --git a/PolyVoxCore/include/GradientEstimators.inl b/PolyVoxCore/include/GradientEstimators.inl index b26c25c2..2f44cfbc 100644 --- a/PolyVoxCore/include/GradientEstimators.inl +++ b/PolyVoxCore/include/GradientEstimators.inl @@ -42,6 +42,31 @@ namespace PolyVox ); } + template + Vector3DFloat computeDecimatedCentralDifferenceGradient(const BlockVolumeIterator& volIter) + { + const uint16_t x = volIter.getPosX(); + const uint16_t y = volIter.getPosY(); + const uint16_t z = volIter.getPosZ(); + + //FIXME - bitwise way of doing this? + VoxelType voxel1nx = volIter.getVoxelAt(x-2, y ,z ) > 0 ? 1: 0; + VoxelType voxel1px = volIter.getVoxelAt(x-2, y ,z ) > 0 ? 1: 0; + + VoxelType voxel1ny = volIter.getVoxelAt(x , y-2,z ) > 0 ? 1: 0; + VoxelType voxel1py = volIter.getVoxelAt(x , y-2,z ) > 0 ? 1: 0; + + VoxelType voxel1nz = volIter.getVoxelAt(x , y ,z-2) > 0 ? 1: 0; + VoxelType voxel1pz = volIter.getVoxelAt(x , y ,z-2) > 0 ? 1: 0; + + return Vector3DFloat + ( + static_cast(voxel1px) - static_cast(voxel1nx), + static_cast(voxel1py) - static_cast(voxel1ny), + static_cast(voxel1pz) - static_cast(voxel1nz) + ); + } + template Vector3DFloat computeSmoothCentralDifferenceGradient(BlockVolumeIterator& volIter) { diff --git a/PolyVoxCore/include/SurfaceExtractors.h b/PolyVoxCore/include/SurfaceExtractors.h index 722bb6d5..604c4d05 100644 --- a/PolyVoxCore/include/SurfaceExtractors.h +++ b/PolyVoxCore/include/SurfaceExtractors.h @@ -49,6 +49,8 @@ namespace PolyVox POLYVOX_API void generateSmoothMeshDataForRegion(BlockVolume* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch); POLYVOX_API Vector3DFloat computeSmoothNormal(BlockVolume* volumeData, const Vector3DFloat& position, NormalGenerationMethod normalGenerationMethod); + + POLYVOX_API void generateDecimatedMeshDataForRegion(BlockVolume* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch); } #endif diff --git a/PolyVoxCore/source/SurfaceExtractors.cpp b/PolyVoxCore/source/SurfaceExtractors.cpp index b13123c6..347da802 100644 --- a/PolyVoxCore/source/SurfaceExtractors.cpp +++ b/PolyVoxCore/source/SurfaceExtractors.cpp @@ -1167,4 +1167,279 @@ namespace PolyVox } return result; } + + void generateDecimatedMeshDataForRegion(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_t 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_t z = region.getLowerCorner().getZ(); z <= region.getUpperCorner().getZ(); z += 2) + { + for(uint16_t y = region.getLowerCorner().getY(); y <= region.getUpperCorner().getY(); y += 2) + { + for(uint16_t x = region.getLowerCorner().getX(); x <= region.getUpperCorner().getX(); x += 2) + { + //while(volIter.moveForwardInRegionXYZ()) + //{ + volIter.setPosition(x,y,z); + const uint8_t v000 = volIter.getMaxedVoxel(); + volIter.setPosition(x+2,y,z); + const uint8_t v100 = volIter.getMaxedVoxel(); + volIter.setPosition(x,y+2,z); + const uint8_t v010 = volIter.getMaxedVoxel(); + volIter.setPosition(x+2,y+2,z); + const uint8_t v110 = volIter.getMaxedVoxel(); + volIter.setPosition(x,y,z+2); + const uint8_t v001 = volIter.getMaxedVoxel(); + volIter.setPosition(x+2,y,z+2); + const uint8_t v101 = volIter.getMaxedVoxel(); + volIter.setPosition(x,y+2,z+2); + const uint8_t v011 = volIter.getMaxedVoxel(); + volIter.setPosition(x+2,y+2,z+2); + const uint8_t v111 = volIter.getMaxedVoxel(); + + //Determine the index into the edge table which tells us which vertices are inside of the surface + uint8_t 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_t material0 = vertMaterials[triTable[iCubeIndex][i ]]; + const uint8_t material1 = vertMaterials[triTable[iCubeIndex][i+1]]; + const uint8_t 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,1.0f); + surfaceVertex0Alpha1.setNormal(normal0); + SurfaceVertex surfaceVertex1Alpha1(vertex1,material1 + 0.1f,1.0f); + surfaceVertex1Alpha1.setNormal(normal1); + SurfaceVertex surfaceVertex2Alpha1(vertex2,material2 + 0.1f,1.0f); + 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; + }*/ + } + } + +#ifdef BLAH + 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_t floorX = static_cast(posX) - 1; + const uint16_t floorY = static_cast(posY) - 1; + const uint16_t floorZ = static_cast(posZ) - 1; + + const uint16_t ceilX = static_cast(posX) + 1; + const uint16_t ceilY = static_cast(posY) + 1; + const uint16_t ceilZ = static_cast(posZ) + 1; + + //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 == SIMPLE) + { + volIter.setPosition(static_cast(posX),static_cast(posY),static_cast(posZ)); + const uint8_t uFloor = volIter.getVoxel() > 0 ? 1 : 0; + if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 + { + uint8_t 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_t 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_t uCeil = volIter.peekVoxel0px0py1pz() > 0 ? 1 : 0; + result = Vector3DFloat(0.0, 0.0,static_cast(uFloor - uCeil)); + } + } + return result; + } +#endif } diff --git a/PolyVoxCore/source/VolumeChangeTracker.cpp b/PolyVoxCore/source/VolumeChangeTracker.cpp index 6b933d15..e033d8bf 100644 --- a/PolyVoxCore/source/VolumeChangeTracker.cpp +++ b/PolyVoxCore/source/VolumeChangeTracker.cpp @@ -64,13 +64,13 @@ namespace PolyVox listToFill.clear(); //Regenerate meshes. - for(uint16_t regionZ = 0; regionZ < POLYVOX_VOLUME_SIDE_LENGTH_IN_REGIONS; ++regionZ) + for(uint16_t regionZ = 0; regionZ < POLYVOX_VOLUME_SIDE_LENGTH_IN_REGIONS-1; ++regionZ) //for(uint16_t regionZ = 0; regionZ < 1; ++regionZ) { - for(uint16_t regionY = 0; regionY < POLYVOX_VOLUME_SIDE_LENGTH_IN_REGIONS; ++regionY) + for(uint16_t regionY = 0; regionY < POLYVOX_VOLUME_SIDE_LENGTH_IN_REGIONS-1; ++regionY) //for(uint16_t regionY = 0; regionY < 2; ++regionY) { - for(uint16_t regionX = 0; regionX < POLYVOX_VOLUME_SIDE_LENGTH_IN_REGIONS; ++regionX) + for(uint16_t regionX = 0; regionX < POLYVOX_VOLUME_SIDE_LENGTH_IN_REGIONS-1; ++regionX) //for(uint16_t regionX = 0; regionX < 2; ++regionX) { if(volRegionUpToDate->getVoxelAt(regionX, regionY, regionZ) == false)