diff --git a/library/PolyVoxCore/include/PolyVoxCore/DefaultMarchingCubesController.h b/library/PolyVoxCore/include/PolyVoxCore/DefaultMarchingCubesController.h index 55eef19e..bd7adf5a 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/DefaultMarchingCubesController.h +++ b/library/PolyVoxCore/include/PolyVoxCore/DefaultMarchingCubesController.h @@ -108,18 +108,18 @@ namespace PolyVox * * The Marching Cubes algotithm generates vertices which lie between voxels, and ideally the material of the vertex should be interpolated from the materials * of the voxels. In practice, that material type is often an integer identifier (e.g. 1 = rock, 2 = soil, 3 = grass) and an interpolation doean't make sense - * (e.g. soil is not a combination or rock and grass). Therefore this default interpolation just return one material or the other, but if more advanced voxel - * types do support interpolation then it can be implemented in this function. + * (e.g. soil is not a combination or rock and grass). Therefore this default interpolation just returns whichever material is associated with a voxel of the + * higher density, but if more advanced voxel types do support interpolation then it can be implemented in this function. */ - MaterialType blendMaterials(MaterialType a, MaterialType b, float weight) + MaterialType blendMaterials(VoxelType a, VoxelType b, float /*weight*/) { - if(weight < 0.5f) + if(convertToDensity(a) > convertToDensity(b)) { - return a; + return convertToMaterial(a); } else { - return b; + return convertToMaterial(b); } } diff --git a/library/PolyVoxCore/include/PolyVoxCore/Density.h b/library/PolyVoxCore/include/PolyVoxCore/Density.h index 72248fda..5eb75e26 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Density.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Density.h @@ -168,7 +168,7 @@ namespace PolyVox return 1; } - MaterialType blendMaterials(MaterialType /*a*/, MaterialType /*b*/, float /*weight*/) + MaterialType blendMaterials(Density /*a*/, Density /*b*/, float /*weight*/) { return 1; } diff --git a/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl b/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl index 4a4db219..0ddc75f7 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl @@ -456,12 +456,8 @@ namespace PolyVox v3dNormal.normalise(); } - //Choose one of the two materials to use for the vertex (we don't interpolate as interpolation of - //material IDs does not make sense). We take the largest, so that if we are working on a material-only - //volume we get the one which is non-zero. Both materials can be non-zero if our volume has a density component. - const typename Controller::MaterialType uMaterial000 = m_controller.convertToMaterial(v000); - const typename Controller::MaterialType uMaterial100 = m_controller.convertToMaterial(v100); - const typename Controller::MaterialType uMaterial = m_controller.blendMaterials(uMaterial000, uMaterial100, fInterp); + // Allow the controller to decide how the material should be derived from the voxels. + const typename Controller::MaterialType uMaterial = m_controller.blendMaterials(v000, v100, fInterp); const PositionMaterialNormal surfaceVertex(v3dPosition, v3dNormal, static_cast(uMaterial)); const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); @@ -489,12 +485,8 @@ namespace PolyVox v3dNormal.normalise(); } - //Choose one of the two materials to use for the vertex (we don't interpolate as interpolation of - //material IDs does not make sense). We take the largest, so that if we are working on a material-only - //volume we get the one which is non-zero. Both materials can be non-zero if our volume has a density component. - const typename Controller::MaterialType uMaterial000 = m_controller.convertToMaterial(v000); - const typename Controller::MaterialType uMaterial010 = m_controller.convertToMaterial(v010); - const typename Controller::MaterialType uMaterial = m_controller.blendMaterials(uMaterial000, uMaterial010, fInterp); + // Allow the controller to decide how the material should be derived from the voxels. + const typename Controller::MaterialType uMaterial = m_controller.blendMaterials(v000, v010, fInterp); const PositionMaterialNormal surfaceVertex(v3dPosition, v3dNormal, static_cast(uMaterial)); const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); @@ -521,12 +513,8 @@ namespace PolyVox v3dNormal.normalise(); } - //Choose one of the two materials to use for the vertex (we don't interpolate as interpolation of - //material IDs does not make sense). We take the largest, so that if we are working on a material-only - //volume we get the one which is non-zero. Both materials can be non-zero if our volume has a density component. - const typename Controller::MaterialType uMaterial000 = m_controller.convertToMaterial(v000); - const typename Controller::MaterialType uMaterial001 = m_controller.convertToMaterial(v001); - const typename Controller::MaterialType uMaterial = m_controller.blendMaterials(uMaterial000, uMaterial001, fInterp); + // Allow the controller to decide how the material should be derived from the voxels. + const typename Controller::MaterialType uMaterial = m_controller.blendMaterials(v000, v001, fInterp); const PositionMaterialNormal surfaceVertex(v3dPosition, v3dNormal, static_cast(uMaterial)); const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); diff --git a/library/PolyVoxCore/include/PolyVoxCore/MaterialDensityPair.h b/library/PolyVoxCore/include/PolyVoxCore/MaterialDensityPair.h index 6f9e2d11..0e5131fd 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/MaterialDensityPair.h +++ b/library/PolyVoxCore/include/PolyVoxCore/MaterialDensityPair.h @@ -132,15 +132,15 @@ namespace PolyVox return voxel.getMaterial(); } - MaterialType blendMaterials(MaterialType a, MaterialType b, float weight) + MaterialType blendMaterials(MaterialDensityPair a, MaterialDensityPair b, float /*weight*/) { - if(weight < 0.5f) + if(convertToDensity(a) > convertToDensity(b)) { - return a; + return convertToMaterial(a); } else { - return b; + return convertToMaterial(b); } }