Moderators: OGRE Team, OgreBullet Moderators
void BulletFrameListener::updateOgreMeshFromBulletMesh(btSoftBody *softBody)
{
//retrieve the vertex data from the softbody.
SoftBody *ogreSoftBody= static_cast<SoftBody*>(softBody->getUserPointer());
MeshPtr mesh= ogreSoftBody->getEntity()->getMesh();
Ogre::SubMesh* submesh = mesh->getSubMesh(0);
Ogre::VertexData* vertex_data = submesh->useSharedVertices ? mesh->sharedVertexData : submesh->vertexData;
if (!submesh->useSharedVertices)
{
const Ogre::VertexElement* posElem =vertex_data->vertexDeclaration->findElementBySemantic(Ogre::VES_POSITION);
Ogre::HardwareVertexBufferSharedPtr vbuf= vertex_data->vertexBufferBinding->getBuffer(posElem->getSource());
const unsigned int vSize = (unsigned int)vbuf->getVertexSize();
unsigned char* vertex =static_cast<unsigned char*>(vbuf->lock(Ogre::HardwareBuffer::HBL_DISCARD));
float* pReal;
//get the vertex data from bullet.
btSoftBody::tNodeArray& nodes(softBody->m_nodes);
//now we simply update the coords of the vertices. Of course this only works
//properly when the Ogre data and the Bullet data have the same number of vertices
//and when they are in the same order. It seems that bullet doesn't shuffle the
//vertices, but this is only an observation!
for (int j=0;j<nodes.size();++j)
{
posElem->baseVertexPointerToElement(vertex, &pReal);
vertex += vSize;
*pReal++= nodes[j].m_x.x();
*pReal++= nodes[j].m_x.y();
*pReal++= nodes[j].m_x.z();
}
vbuf->unlock();
}
}
So, can I safely assume that these vertex buffers will stay valid throughout application runtime, so if I store pointers to mesh vertices as opposed to their value, these pointers will point to a valid data?
Better keep a complete vertex data buffer on you side and upload it after each modification from simulation.
// 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);
// 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);
// _softBody->m_cfg.kDP = 0.001; // Damping coefficient
_softBody->m_cfg.kDF = 0.5; // Dynamic friction coefficient
// _softBody->m_cfg.kMT = 0.01; // Pose matching coefficient
// _softBody->m_cfg.kPR = 2500;
// _softBody->m_cfg.kCHR = 1; // Rigid contacts hardness
// _softBody->m_cfg.kKHR = 0.8; // Kinetic contacts hardness
// _softBody->m_cfg.kSHR = 1; // Soft contacts hardness
_softBody->m_cfg.piterations = 2; // Positions solver iterations
_softBody->m_materials[0]->m_kLST = 0.45;
btSoftBody::Material* softBodyMaterial = _softBody->appendMaterial();
softBodyMaterial->m_kLST = 0.5; // Linear stiffness coefficient
softBodyMaterial->m_kAST = 0.1; // Area/Angular stiffness coefficient
softBodyMaterial->m_kVST = 0.5; // Volume stiffness coefficient
btScalar mass(WorldMetricsHelper::kg(60));
_softBody->setTotalMass(mass, true);
// _softBody->setPose(false, true); // XXX Set current state as a pose
// _softBody->generateBendingConstraints(2, softBodyMaterial);
_softBody->randomizeConstraints();
Users browsing this forum: No registered users and 1 guest