I am able to load a Trimesh as a softbody and render it using plain OpenGL. The rendering is done as follows but the simulation runs very slow for some reason. I know that it is partly because of the brute force rendering I use but the spheres are only 162 vertices each and I am not even calculating vertex normals. I may also be adding some duplicate links which I am working on removing.
Please see attached video for illustration.
Is there any tips & tricks to increase the rendering speed?
Any help will be really appreciated.
Thanks
V
Here is the softbody code:
Code: Select all
btSoftBody* BulletWrap::addSoftBodyMesh(int numAllVertices, float *allVerts, int numTriangles, int *indices,
btVector3 &position, float mass)
{
int i,j,ni;
btAlignedObjectArray<btVector3> vtx;
vtx.resize(numTriangles * 3 );
for(i=0,j=0,ni=numTriangles*3;i<ni;++j,i+=3)
{
vtx[j]=btVector3(allVerts[i],allVerts[i+1],allVerts[i+2]);
}
btSoftBody* psb = new btSoftBody(&m_softBodyWorldInfo, vtx.size(),&vtx[0],0);
for (int i=0; i< numTriangles; i++)
{
psb->appendLink(indices[3*i+2],indices[3*i+0]);
psb->appendLink(indices[3*i+0],indices[3*i+1]);
psb->appendLink(indices[3*i+1],indices[3*i+2]);
psb->appendFace(indices[3*i+0], indices[3*i+1], indices[3*i+2]);
}
btSoftBody::Material* pm=psb->appendMaterial();
pm->m_kLST = 0.55;
pm->m_kAST = 0.2;
pm->m_kVST = 0.55;
psb->generateBendingConstraints(2,pm);
psb->m_cfg.piterations = 2;
psb->m_cfg.collisions = btSoftBody::fCollision::SDF_RS;
psb->randomizeConstraints();
btTransform trans;
trans.setIdentity();
trans.setOrigin( position );
psb->transform(trans);
psb->setTotalMass(1.0,true);
psb->generateClusters(64);
m_world->addSoftBody(psb);
return psb;
}
Code: Select all
void renderSoftbodyMeshes(btSoftBody* sBody)
{
btTransform meshTransform = sBody->getWorldTransform();
GLfloat tempForm[16];
meshTransform.getOpenGLMatrix(tempForm);
glPushMatrix();
glMultMatrixf(tempForm);
glBegin(GL_TRIANGLES);
int numFaces = sBody->m_faces.size();
for (int i=0; i< numFaces; i++)
{
btSoftBody::Node* node_0=sBody->m_faces[i].m_n[0];
btSoftBody::Node* node_1=sBody->m_faces[i].m_n[1];
btSoftBody::Node* node_2=sBody->m_faces[i].m_n[2];
btVector3 p0;
p0 = node_0->m_x;
btVector3 p1;
p1 = node_1->m_x;
btVector3 p2;
p2 = node_2->m_x;
//Calculate the normals for the 2 triangles and add on
btVector3 tpt1, tpt2;
tpt1 = p1-p0;
tpt2 = p2-p0;
btVector3 normal= tpt1.cross(tpt2);
glNormal3fv(normal);
glVertex3fv(p0);
glVertex3fv(p1);
glVertex3fv(p2);
}
glEnd();
glPopMatrix();
}