Yes, I wanted a smooth terrain using marching cubes. Which function should I use to smooth it? I'm also running into some trouble using the Ogre mesh code in the Wiki. Mainly around here - beginIndex and endIndex are 0 so the for loop never goes anywhere
Code:
// Begin writing to manualObject
unsigned int uLodLevel = 0;
int beginIndex = mesh2.m_vecLodRecords[uLodLevel].beginIndex;
int endIndex = mesh2.m_vecLodRecords[uLodLevel].endIndex;
for(int index = beginIndex; index < endIndex; ++index) {
const PolyVox::PositionMaterialNormal& vertex = vecVertices[vecIndices[index]];
const PolyVox::Vector3DFloat& v3dVertexPos = vertex.getPosition();
const PolyVox::Vector3DFloat& v3dVertexNormal = vertex.getNormal();
//const Vector3DFloat v3dRegionOffset(uRegionX * g_uRegionSideLength, uRegionY * g_uRegionSideLength, uRegionZ * g_uRegionSideLength);
const PolyVox::Vector3DFloat v3dFinalVertexPos = v3dVertexPos + static_cast<PolyVox::Vector3DFloat>(mesh2.m_Region.getLowerCorner());
ogreMesh->position(v3dFinalVertexPos.getX(), v3dFinalVertexPos.getY(), v3dFinalVertexPos.getZ());
ogreMesh->normal(v3dVertexNormal.getX(), v3dVertexNormal.getY(), v3dVertexNormal.getZ());
uint8_t mat = vertex.getMaterial() + 0.5;
uint8_t red = mat & 0xF0;
uint8_t green = mat & 0x03;
uint8_t blue = mat & 0x0C;
ogreMesh->colour(red*2, green*4, blue*4);// just some random colors, I'm too lazy for hsv
}
UPDATE:
I changed the code to look like so. I'm not sure if changing to a simpleVolume instead of a largeVolume makes any difference, but I still don't know why my mesh isn't getting any vertices or indices from the surfaceExtraction. My for loop seems to set the density ok, but maybe it's not?
Code:
// Heightmap to use for voxel filling
Ogre::Image myTerrainHM;
myTerrainHM.load("terrain.png","General");
//Create a volume
SimpleVolume<Density8> volData(PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(myTerrainHM.getWidth(),256,myTerrainHM.getHeight())));
// Set voxel type
Density8 voxel;
voxel.setMaterial(0);
//Clear volume to zeros.
//FIXME - Add function to PolyVox for this.
/*
for(unsigned int z = 0; z < volData.getDepth(); ++z)
{
for(unsigned int y = 0; y < volData.getHeight(); ++y)
{
for(unsigned int x = 0; x < volData.getWidth(); ++x)
{
volData.setVoxelAt(x,y,z,voxel);
}
}
}
*/
// loop through all voxels and set density for each based on heightmap
for(int z = 1; z < volData.getDepth()-1; ++z)
{
for(int y = 1; y < volData.getHeight()-1; ++y)
{
for(int x = 1; x < volData.getWidth()-1; ++x)
{
//We check the red channel, but in a greyscale image they should all be the same.
ColourValue color = myTerrainHM.getColourAt(x,y,0);
//RGBA colorRGBA = color.getAsRGBA();
//Set the voxel based on the height.
if(y <= color.r * 256)
{
voxel.setMaterial(1);
volData.setVoxelAt(x,y,z,voxel);
}
else if(y >= color.r * 256)
{
voxel.setMaterial(2);
volData.setVoxelAt(x,y,z,voxel);
}
//Zero the faces
//FIXME - looks like a bug - shouldn't be -2?
//if((x == 0) || (y == 0) || (z == 0) || (x == volumeSideLength-2) || (y == volumeSideLength-2) || (z == volumeSideLength-2))
//{
// volIter.setVoxel(0);
//}
}
}
}
//PolyVox::Volume<Material8> volume(128,128,128);
// now add some data to it
//PolyVox::smoothRegion<MaterialDensityPair44>(volData, volData.getEnclosingRegion());
// Make a surfaceMesh from the voxelData
SurfaceMesh<PositionMaterialNormal> mesh;
SurfaceMesh<PositionMaterialNormal> mesh2;
// Extract the surface
PolyVox::SurfaceExtractor<SimpleVolume, Density8> suf(&volData, volData.getEnclosingRegion(), &mesh);
suf.execute(); // bug - mesh contains no vertices after this line!
UPDATE 2 : Setting the density rather than setting the material worked for getting the verts and indices
UPDATE 3 : For some reason the decimation wasn't passing values from mesh to mesh2, so I just used mesh to loop through it's indices and add to the manualObject, but for some reason I'm getting the same value across the x or z plane so it looks like the screenshot below, instead of a terrain like it should