How to render soft bodies efficiently ???

winspear
Posts: 77
Joined: Thu Nov 26, 2009 6:32 pm

How to render soft bodies efficiently ???

Post by winspear »

Hi,
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;
}	
Here is the rendering code:

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(); 
}
You do not have the required permissions to view the files attached to this post.
Last edited by winspear on Sun Mar 13, 2011 4:01 pm, edited 1 time in total.
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Optimized rendering of soft bodies

Post by Flix »

I didn't download your attached file, but from a quick look of the code you posted:

1) numAllVertices is not used (for some reason you used numTriangles instead of it... why?).
2) when you append links you should check if they are already present, if you don't want to add edges more than once. There's an overload version of appendLink that checks it (I don't remember if the default version you used does this check or not).
3) in second part of the code you recalculate the normals, that the Bullet soft body engine already calculates for you (they should be somewhere inside the btSoftBody::Node struct).

Hope this helps.

P.S. The title of your post is a bit misleading, since you should probably use a (cloned) vertex array (or buffer object) on the GPU synchronized with Bullet to render the soft body in an optimized way. (That way you can even decide not to update the array every frame if the CPU/GPU bandwidth is the bottleneck).
winspear
Posts: 77
Joined: Thu Nov 26, 2009 6:32 pm

Re: How to render soft bodies efficiently ??

Post by winspear »

Thanks for the reply.
I did not know that Bullet already had a method to check if a link is already present. Good that you mentioned it since I was working on a binary tree structure for my mesh loader which will save connectivity info.
I also did not know that bullet calculates normals.
Can you please point me to some example which checks for the above methods ?

Thanks
V
winspear
Posts: 77
Joined: Thu Nov 26, 2009 6:32 pm

Re: How to render soft bodies efficiently ???

Post by winspear »

Thanks for all the help. I got it working.
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: How to render soft bodies efficiently ???

Post by Flix »

winspear wrote:I got it working
You're welcome. Glad it worked :wink:

For others having the same problems:
From btSoftBody.h:

Code: Select all

	void btSoftBody::appendLink(	
		int node0,
		int node1,
		Material* mat=0,
		bool bcheckexist=false	<==============
	);
	void btSoftBody::appendLink(
		Node* node0,
		Node* node1,
		Material* mat=0,
		bool bcheckexist=false	<==============
	);

Code: Select all

	struct btSoftBody::Node : Feature
	{
		btVector3 m_x;	// Position (hence the arg named "x" in the ctr ?)
		btVector3 m_q;	// Previous step position
		btVector3 m_v;	// Velocity
		btVector3 m_f;	// Force accumulator
		btVector3 m_n;	// Vertex Normal	<==============
		btScalar m_im;	// 1/mass
		btScalar m_area;// Area
		btDbvtNode* m_leaf;// Leaf data
		int m_battach:1;// Attached
	};
winspear
Posts: 77
Joined: Thu Nov 26, 2009 6:32 pm

Re: How to render soft bodies efficiently ???

Post by winspear »

Do you have any idea on my followup question about self collisions for soft bodies here.

http://bulletphysics.org/Bullet/phpBB3/ ... f=9&t=6495
rookie
Posts: 12
Joined: Mon Dec 05, 2011 8:36 pm

Re: How to render soft bodies efficiently ???

Post by rookie »

I want to attach texture to the soft body rendered. I am using the normals & vertices in the node structure but I can't find anything to specify texture coordinates. When I use the coordinates without using the soft bodys node structure, it renders the soft body but doesn't do anything about the physics. Please help me.