Hope this will help you:
Code:
HEADERS:
//Voxel type
struct SVT // Smooth Voxel Type
{
uint16_t density; // Density
uint8_t material; // Material
uint8_t light; // Light
uint32_t color; // Color
bool isSmooth;
SVT();
SVT(uint8_t m, uint8_t d = 255);
SVT &operator +(const SVT& other);
SVT &operator -(const SVT& other);
SVT &operator /(const int& other);
SVT &operator +=(const SVT& other);
SVT &operator -=(const SVT& other);
SVT &operator /=(const int& other);
};
class PGFMarchingCubesController
{
public:
typedef uint8_t DensityType;
typedef uint8_t MaterialType;
PGFMarchingCubesController(void);
DensityType convertToDensity(SVT voxel);
MaterialType convertToMaterial(SVT voxel);
DensityType getThreshold(void);
void setThreshold(DensityType tThreshold);
private:
DensityType m_tThreshold;
};
class PGFIsQuadNeeded
{
public:
bool operator()(SVT back, SVT front, uint32_t& materialToUse);
};
SOURCES:
SVT::SVT(): density(0), material(0), light(0), isSmooth(true){}
SVT& SVT::operator +(const SVT &other)
{
this->density += other.density;
return *this;
}
SVT& SVT::operator -(const SVT &other)
{
this->density -= other.density;
return *this;
}
SVT& SVT::operator /(const int &other)
{
this->density /= other;
return *this;
}
/**/
SVT& SVT::operator +=(const SVT &other)
{
this->density += other.density;
return *this;
}
SVT& SVT::operator -=(const SVT &other)
{
this->density -= other.density;
return *this;
}
SVT& SVT::operator /=(const int &other)
{
this->density /= other;
return *this;
}
...
PGFMarchingCubesController::PGFMarchingCubesController()
:m_tThreshold(127)
{
}
PGFMarchingCubesController::DensityType PGFMarchingCubesController::convertToDensity(SVT voxel)
{
return voxel.density;
}
PGFMarchingCubesController::MaterialType PGFMarchingCubesController::convertToMaterial(SVT voxel)
{
return voxel.material;
}
PGFMarchingCubesController::DensityType PGFMarchingCubesController::getThreshold()
{
return m_tThreshold;
}
void PGFMarchingCubesController::setThreshold(PGFMarchingCubesController::DensityType tThreshold)
{
m_tThreshold = tThreshold;
}
.....
The actual code:
Actually a boolean cloud, setting densities either 0 or 255.
ch->voxdata = new RawVolume<SVT>(Region(-1,-1,-1,ch->sx+1,ch->sy+1,ch->sz+1));
//Generate SOME data into them \paging test!
chunk* c = ch;
for(int xi=-1; xi<=ch->sx+1; ++xi)
for(int yi=-1;yi<=ch->sy+1;++yi)
{
float pval = perlin.Get(float(xi+ch->x*ch->sx)/float(ch->sx), float(yi+ch->y*ch->sy)/float(ch->sy));
pval += 1.0; //0..2;
pval /= 2.0; //0..1
pval *= ch->sz; //reach the z
int zz = pval;
//cout << zz << ", ";
for(int zi=-1;zi<pval;++zi)
{
SVT vx;
vx.density = 255;
vx.material = 1;
vx.color = Ogre::ColourValue(1,1,1,1).getAsRGBA();
vx.light = 255;
ch->voxdata->setVoxelAt(xi,yi,zi,vx);
}
}
.... LATER ....
delete c->newmesh;
c->newmesh =
new SurfaceMesh<PositionMaterialNormal>(); //must DELETE it!
//Smooth chunk
RawVolume<SVT> tempVolume(Region(-1,-1,-1,c->sx+1,c->sy+1,c->sz+1));
LowPassFilter< RawVolume<SVT>, RawVolume<SVT>, SVT >
lpf(c->voxdata, Region(-1,-1,-1,c->sx+1,c->sy+1,c->sz+1),
&tempVolume, Region(-1,-1,-1,c->sx+1,c->sy+1,c->sz+1),
3);
lpf.execute();
MarchingCubesSurfaceExtractor< RawVolume<SVT>, PGFMarchingCubesController >
ext2(&tempVolume, Region(0,0,0,c->sx,c->sy,c->sz), c->newmesh);
ext2.execute();
And that is... the full source code is uploaded here:
https://sourceforge.net/p/kitten-platfo ... ree/trunk/note that this repo is completely.... overused - 2 projects...