Did anyone around already developed some code to import an Ogre Mesh to a btSoftBody?
I have it almost working, but some vertices are not shared on the mesh, so I don't know how to link the triangles - It ends up loading the triangles correctly, but not linked to each other, so the object 'falls apart' on the simulation.
Here's my current code (two versions with the same result):
Code: Select all
// using btSoftBodyHelpers::CreateFromTriMesh
btScalar vertices[mVertexCount * 3];
int i,j;
for(i=0, j=0; i < mVertexCount; i++)
{
Vector3 v = mVertexBuffer[i];
vertices[j++] = v.x;
vertices[j++] = v.y;
vertices[j++] = v.z;
}
int ntriangles = mIndexCount / 3;
int *indexes = (int*) mIndexBuffer;
return btSoftBodyHelpers::CreateFromTriMesh(*worldInfo, vertices, indexes, ntriangles);
Code: Select all
// doing basically what btSoftBodyHelpers::CreateFromTriMesh does
unsigned int ntriangles = mIndexCount / 3;
btAlignedObjectArray<bool> chks;
btAlignedObjectArray<btVector3> vtx;
chks.resize(mVertexCount*mVertexCount,false);
vtx.resize(mVertexCount);
unsigned int maxidx = mVertexCount;
int i, j;
for(i=0; i < mVertexCount; i++)
{
vtx[i] = Convert::toBullet(mVertexBuffer[i]);
}
btSoftBody* psb=new btSoftBody(worldInfo,vtx.size(),&vtx[0],0);
#define IDX(_x_,_y_) ((_y_)*maxidx+(_x_))
for( i=0; i < mIndexCount; i+=3)
{
const int idx[]={mIndexBuffer[i],mIndexBuffer[i+1],mIndexBuffer[i+2]};
for(int j=2,k=0;k<3;j=k++)
{
if(!chks[IDX(idx[j],idx[k])])
{
chks[IDX(idx[j],idx[k])]=true;
chks[IDX(idx[k],idx[j])]=true;
psb->appendLink(idx[j],idx[k]);
}
}
printf("face %d %d %d \n", idx[0],idx[1],idx[2]);
psb->appendFace(idx[0],idx[1],idx[2]);
}
#undef IDX
psb->randomizeConstraints();
return(psb);
-Geovani