I've been using the supplied ConvexDecompositionDemo however the code feels like a mess to me and I'm having a hard time deciphering out the bare bones code that's needed to get the job done.
Function to retrieve raw Vertex and Index data from Irrlicht:
Code: Select all
std::vector<HACD::Vec3<HACD::Real>> v_Vertices;
std::vector<HACD::Vec3<long>> v_Indices;
for (irr::u32 m = 0; m < MBCount; m++)
{
irr::scene::IMeshBuffer* MB = GetMesh()->getMeshBuffer(m);
irr::video::S3DVertex* Vertices = (irr::video::S3DVertex*)MB->getVertices();
irr::u16* Indices = MB->getIndices();
for (irr::u32 i = 0; i < MB->getIndexCount(); i += 3)
{
irr::u32 Idx1 = Indices[i];
irr::u32 Idx2 = Indices[i + 1];
irr::u32 Idx3 = Indices[i + 2];
v_Indices.push_back(HACD::Vec3<long>(Idx1, Idx2, Idx3));
irr::core::vector3df Tri1Pos = Vertices[Indices[Idx1]].Pos;
irr::core::vector3df Tri2Pos = Vertices[Indices[Idx2]].Pos;
irr::core::vector3df Tri3Pos = Vertices[Indices[Idx3]].Pos;
v_Vertices.push_back(HACD::Vec3<HACD::Real>(Tri1Pos.X, Tri1Pos.Y, Tri1Pos.Z));
v_Vertices.push_back(HACD::Vec3<HACD::Real>(Tri2Pos.X, Tri2Pos.Y, Tri2Pos.Z));
v_Vertices.push_back(HACD::Vec3<HACD::Real>(Tri3Pos.X, Tri3Pos.Y, Tri3Pos.Z));
}
}
Code: Select all
btCompoundShape* CreateShapeHACD(std::vector<HACD::Vec3<HACD::Real>> i_Vertices, std::vector<HACD::Vec3<long>> i_Indices)
{
HACD::HACD myHACD;
myHACD.SetPoints(&i_Vertices[0]);
myHACD.SetNPoints(i_Vertices.size());
myHACD.SetTriangles(&i_Indices[0]);
myHACD.SetNTriangles(i_Indices.size());
myHACD.SetCompacityWeight(0.1);
myHACD.SetVolumeWeight(0.0);
myHACD.SetNClusters(1); // minimum number of clusters
myHACD.SetNVerticesPerCH(100); // max of 100 vertices per convex-hull
myHACD.SetConcavity(100); // maximum concavity
myHACD.SetAddExtraDistPoints(false);
myHACD.SetAddNeighboursDistPoints(false);
myHACD.SetAddFacesPoints(false);
myHACD.Compute();
//
// Process the HACD data into individual Convex Hull Shapes
// And add them into one main Compound Shape
//
btAlignedObjectArray<btConvexHullShape*> m_convexShapes;
btAlignedObjectArray<btVector3> m_convexCentroids;
btCompoundShape* Compound = new btCompoundShape();
btVector3 Centroid(0, 0, 0);
btTransform Trans;
Trans.setIdentity();
//
// Create the individual pieces of the btCompoundShape
//
for (unsigned int c = 0; c < myHACD.GetNClusters(); c++)
{
size_t nPoints = myHACD.GetNPointsCH(c);
size_t nTriangles = myHACD.GetNTrianglesCH(c);
HACD::Vec3<HACD::Real>* PointsCH = new HACD::Vec3<HACD::Real>[nPoints];
HACD::Vec3<long>* TrianglesCH = new HACD::Vec3<long>[nTriangles];
myHACD.GetCH(c, PointsCH, TrianglesCH);
btAlignedObjectArray<btVector3> h_Vertices;
Centroid.setValue(0, 0, 0);
for (size_t v = 0; v < nPoints; v++)
{
btVector3 Vertex(PointsCH[v].X(), PointsCH[v].Y(), PointsCH[v].Z());
Centroid += Vertex;
h_Vertices.push_back(Vertex);
}
Centroid /= (float(nPoints));
m_convexCentroids.push_back(Centroid);
delete[] PointsCH;
delete[] TrianglesCH;
btConvexHullShape* ConvexShape = new btConvexHullShape(&(h_Vertices[0].getX()), h_Vertices.size());
ConvexShape->setMargin(0.01F);
m_convexShapes.push_back(ConvexShape);
}
//
// Finally add the Convex Hull Shapes into the Compound Shape
//
for (unsigned int i = 0; i < m_convexShapes.size(); i++)
{
Trans.setOrigin(m_convexCentroids[i]);
Compound->addChildShape(Trans, m_convexShapes[i]);
}
//
// Return our HACD Compound Shape
//
return Compound;
}