Softbody: texture coordinates for rendering?

TheKing
Posts: 13
Joined: Sun Sep 11, 2011 2:02 pm

Softbody: texture coordinates for rendering?

Post by TheKing »

Hi,

I recently decided to try out bullet, mainly the soft-body part of the libary. I managed to asign a piece of clothing using btSoftBodyHelpers::CreatePatch(), and render its faces with Direct3D. However, I've got a question: How do I calculate the texture coordinates of every face? I'm reading the face-data like this:

Code: Select all

for(int i=0;i<Faces.size();++i)
	{
		btSoftBody::Node*   node_0=Faces[i].m_n[0];
		btSoftBody::Node*   node_1=Faces[i].m_n[1];
		btSoftBody::Node*   node_2=Faces[i].m_n[2];
		Data[j].fX = node_0->m_x.x();
		Data[j].fY = node_0->m_x.y();
		Data[j].fZ = node_0->m_x.z();
		Data[j].tex0 = ???;
		Data[j].tex1 = ???;
		j+=1;
		Data[j].fX = node_1->m_x.x();
		Data[j].fY = node_1->m_x.y();
		Data[j].fZ = node_1->m_x.z();
		Data[j].tex0 = ???;
		Data[j].tex1 = ???;
		j+=1;
		Data[j].fX = node_2->m_x.x();
		Data[j].fY = node_2->m_x.y();
		Data[j].fZ = node_2->m_x.z();
		Data[j].tex0 = ???;
		Data[j].tex1 = ???;
		j+=1;
	}
Is there any method or variable where texture coordinates are stored? Or do I have to calculate them myself? If so, can somebody tell me or give me some advice how to do so? I tried figuring it out myself, but can't really get it to work.. Thanks in Advance!
Skuzz
Posts: 11
Joined: Wed Aug 25, 2010 12:57 am

Re: Softbody: texture coordinates for rendering?

Post by Skuzz »

You'll have to create your own, for a patch it's easy, just use the vertex positions from the major axis's in which the patch is created e.t.c.

Woops my bad, try this:

btSoftBodyHelpers.h
/* Create a patch with UV Texture Coordinates */
static btSoftBody* CreatePatchUV(btSoftBodyWorldInfo& worldInfo,
const btVector3& corner00,
const btVector3& corner10,
const btVector3& corner01,
const btVector3& corner11,
int resx,
int resy,
int fixeds,
bool gendiags,
float* tex_coords=0);
shomy2k11
Posts: 24
Joined: Thu Sep 08, 2011 1:59 am

Re: Softbody: texture coordinates for rendering?

Post by shomy2k11 »

I've tried using this to create a cloth but it renders nothing but a black screen any ideas?
TheKing
Posts: 13
Joined: Sun Sep 11, 2011 2:02 pm

Re: Softbody: texture coordinates for rendering?

Post by TheKing »

@Skuzz: Thanks for telling me this function, but I already managed to calculate the coordinates myself! As you said it isn't really hard. But for more complicated tasks, I'll keep the CreatePatchUV in mind.

@shomy2k11: There could be multiple issues. Despite your graphics-api, try disabling face culling, thats the first issue I had. Otherwise, I can't over you much help except that you should check how you render the faces of your soft bodies... maybe there is some issue in your rendering cycle anyway!
rookie
Posts: 12
Joined: Mon Dec 05, 2011 8:36 pm

Re: Softbody: texture coordinates for rendering?

Post by rookie »

@TheKing Can you let me know how you did it?? I am trying to do it but I am getting a weird texture. I am rendering a .obj file from Maya. It falls down due o gravity but the texture is weird. I think it is because of the texture coordinates. Please let me know. My code for rendering is as follows. I am using OpenGL

void ViewManager::renderSoftbodyMeshes(btSoftBody* sBody)
{
btTransform meshTransform = sBody->getWorldTransform();

int j = 0;

static btScalar tempForm[16];
meshTransform.getOpenGLMatrix(tempForm);

glBindTexture(GL_TEXTURE_2D, GLuint(imageData));
glTexImage2D(GL_TEXTURE_2D, 0, 3, imageCols, imageRows, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

glPushMatrix();
glMultMatrixf(tempForm);
glBegin(GL_TRIANGLES);
int numFaces = sBody->m_faces.size();
for (int i=0; i< numFaces; i++)
{
glNormal3fv(sBody->m_faces.m_n[0]->m_n);
glTexCoord2fv(&objmodel->texcoords[objmodel->triangles.tindices[0]]);
glVertex3fv(sBody->m_faces.m_n[0]->m_q);

glNormal3fv(sBody->m_faces.m_n[1]->m_n);
glTexCoord2fv(&objmodel->texcoords[objmodel->triangles.tindices[1]]);
glVertex3fv(sBody->m_faces.m_n[1]->m_q);

glNormal3fv(sBody->m_faces.m_n[2]->m_n);
glTexCoord2fv(&objmodel->texcoords[objmodel->triangles.tindices[2]]);
glVertex3fv(sBody->m_faces.m_n[2]->m_q);
}

glEnd();
glPopMatrix();

glDisable(GL_TEXTURE_2D);
}
rookie
Posts: 12
Joined: Mon Dec 05, 2011 8:36 pm

Re: Softbody: texture coordinates for rendering?

Post by rookie »

I am adding the soft body as follows

btSoftBody* PhysicsManager::addSoftBodyMesh(int numAllVertices, float *allVerts , int numTriangles, int *indices, btVector3 position, float mass)
{

int maxidx=0;
int i,j,ni;

for(i=0,ni=numTriangles * 3;i<ni;++i)
{
maxidx=btMax((int)indices,maxidx);
}/*qDebug() << "2";*/
++maxidx;
btAlignedObjectArray<bool> chks;
btAlignedObjectArray<btVector3> vtx;
chks.resize(maxidx*maxidx,false);
vtx.resize(maxidx);
for(i=0,j=0,ni=maxidx*3;i<ni; ++j,i+=3)
{
vtx[j]=btVector3(allVerts,allVerts[i+1],allVerts[i+2]);
btSoftBody* psb = new btSoftBody(&m_softBodyWorldInfo,vtx.size(),&vtx[0],0);
for( i=0,ni=/*numIndices*/ numTriangles * 3;i<ni;i+=3)
{
const int idx[]={indices,indices[i+1],indices[i+2]};
#define IDX(_x_,_y_) ((_y_)*maxidx+(_x_))
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]);
}
}
#undef IDX
psb->appendFace(idx[0],idx[1],idx[2]);
}

// psb->setMass( 100, 0); psb->setMass( 120, 0); psb->setMass( 200, 0); psb->setMass( 300, 0);
btSoftBody::Material* pm=psb->appendMaterial();
pm->m_kLST = 0.3;
pm->m_kAST = 0.2;
pm->m_kVST = 0.5;

psb->generateBendingConstraints(2,pm);
psb->m_cfg.piterations = 7;
psb->m_cfg.collisions = btSoftBody::fCollision::SDF_RS+btSoftBody::fCollision::CL_SS + btSoftBody::fCollision::CL_SELF;

psb->m_cfg.kDF = 1.0;
psb->randomizeConstraints();
btTransform trans;
trans.setIdentity();
trans.setOrigin( position );
psb->transform(trans);
psb->setTotalMass(1.5,true);
psb->generateClusters(8);

m_world->addSoftBody(psb);

return psb;
}