btTriangleMesh::btTriangleMesh (bool use32bitIndices,bool use4componentVertices)
:m_use32bitIndices(use32bitIndices),
-m_use4componentVertices(use4componentVertices)
+m_use4componentVertices(use4componentVertices),
+m_weldingThreshold(0.0)
{
btIndexedMesh meshIndex;
meshIndex.m_numTriangles = 0;
}
-
-void btTriangleMesh::addTriangle(const btVector3& vertex0,const btVector3& vertex1,const btVector3& vertex2)
+void btTriangleMesh::addIndex(int index)
{
- m_indexedMeshes[0].m_numTriangles++;
- m_indexedMeshes[0].m_numVertices+=3;
-
- if (m_use4componentVertices)
+ if (m_use32bitIndices)
{
- m_4componentVertices.push_back(vertex0);
- m_4componentVertices.push_back(vertex1);
- m_4componentVertices.push_back(vertex2);
- m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_4componentVertices[0];
+ m_32bitIndices.push_back(index);
+ m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_32bitIndices[0];
} else
{
- m_3componentVertices.push_back(vertex0.getX());
- m_3componentVertices.push_back(vertex0.getY());
- m_3componentVertices.push_back(vertex0.getZ());
-
- m_3componentVertices.push_back(vertex1.getX());
- m_3componentVertices.push_back(vertex1.getY());
- m_3componentVertices.push_back(vertex1.getZ());
-
- m_3componentVertices.push_back(vertex2.getX());
- m_3componentVertices.push_back(vertex2.getY());
- m_3componentVertices.push_back(vertex2.getZ());
- m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_3componentVertices[0];
+ m_16bitIndices.push_back(index);
+ m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_16bitIndices[0];
}
+}
- if (m_use32bitIndices)
+int btTriangleMesh::findOrAddVertex(const btVector3& vertex)
+{
+ //return index of new/existing vertex
+ //todo: could use acceleration structure for this
+ if (m_use4componentVertices)
{
- int curIndex = m_32bitIndices.size();
- m_32bitIndices.push_back(curIndex++);
- m_32bitIndices.push_back(curIndex++);
- m_32bitIndices.push_back(curIndex++);
- m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_32bitIndices[0];
+ for (int i=0;i< m_4componentVertices.size();i++)
+ {
+ if ((m_4componentVertices[i]-vertex).length2() <= m_weldingThreshold)
+ {
+ return i;
+ }
+ }
+ m_indexedMeshes[0].m_numVertices++;
+ m_4componentVertices.push_back(vertex);
+ m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_4componentVertices[0];
+
+ return m_4componentVertices.size()-1;
+
} else
{
- short curIndex = static_cast<short>(m_16bitIndices.size());
- m_16bitIndices.push_back(curIndex++);
- m_16bitIndices.push_back(curIndex++);
- m_16bitIndices.push_back(curIndex++);
- m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_16bitIndices[0];
+
+ for (int i=0;i< m_3componentVertices.size();i+=3)
+ {
+ btVector3 vtx(m_3componentVertices[i],m_3componentVertices[i+1],m_3componentVertices[i+2]);
+ if ((vtx-vertex).length2() <= m_weldingThreshold)
+ {
+ return i/3;
+ }
+ }
+ m_3componentVertices.push_back(vertex.getX());
+ m_3componentVertices.push_back(vertex.getY());
+ m_3componentVertices.push_back(vertex.getZ());
+ m_indexedMeshes[0].m_numVertices++;
+ m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_3componentVertices[0];
+ return (m_3componentVertices.size()/3)-1;
}
+
+}
+
+void btTriangleMesh::addTriangle(const btVector3& vertex0,const btVector3& vertex1,const btVector3& vertex2)
+{
+ m_indexedMeshes[0].m_numTriangles++;
+
+ addIndex(findOrAddVertex(vertex0));
+ addIndex(findOrAddVertex(vertex1));
+ addIndex(findOrAddVertex(vertex2));
}
int btTriangleMesh::getNumTriangles() const
//disable soft body until first sneak preview is ready
if (m_cci.m_bSoft && m_cci.m_collisionShape &&
- (shapeType == CONVEX_HULL_SHAPE_PROXYTYPE))
- //(shapeType == TRIANGLE_MESH_SHAPE_PROXYTYPE) |
- //(shapeType == SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE)))
+ (shapeType == CONVEX_HULL_SHAPE_PROXYTYPE)|
+ (shapeType == TRIANGLE_MESH_SHAPE_PROXYTYPE) |
+ (shapeType == SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE))
{
btRigidBody::btRigidBodyConstructionInfo rbci(m_cci.m_mass,m_bulletMotionState,m_collisionShape,m_cci.m_localInertiaTensor * m_cci.m_inertiaFactor);
rbci.m_linearDamping = m_cci.m_linearDamping;
psb->appendFace(idx[0],idx[1],idx[2]);
}
- ///create a mapping between graphics mesh vertices and soft body vertices
- {
- RAS_MeshObject* rasMesh= GetShapeInfo()->GetMesh();
-
- if (rasMesh && !m_softbodyMappingDone)
- {
-
- //printf("apply\n");
- RAS_MeshSlot::iterator it;
- RAS_MeshMaterial *mmat;
- RAS_MeshSlot *slot;
- size_t i;
-
- //for each material
- for (int m=0;m<rasMesh->NumMaterials();m++)
- {
- // The vertex cache can only be updated for this deformer:
- // Duplicated objects with more than one ploymaterial (=multiple mesh slot per object)
- // share the same mesh (=the same cache). As the rendering is done per polymaterial
- // cycling through the objects, the entire mesh cache cannot be updated in one shot.
- mmat = rasMesh->GetMeshMaterial(m);
-
- slot = mmat->m_baseslot;
- for(slot->begin(it); !slot->end(it); slot->next(it))
- {
- int index = 0;
- for(i=it.startvertex; i<it.endvertex; i++,index++)
- {
- RAS_TexVert* vertex = &it.vertex[i];
-
-
- //search closest index, and store it in vertex
- vertex->setSoftBodyIndex(0);
- btScalar maxDistSqr = 1e30;
- btSoftBody::tNodeArray& nodes(psb->m_nodes);
- btVector3 xyz = btVector3(vertex->getXYZ()[0],vertex->getXYZ()[1],vertex->getXYZ()[2]);
- for (int n=0;n<nodes.size();n++)
- {
- btScalar distSqr = (nodes[n].m_x - xyz).length2();
- if (distSqr<maxDistSqr)
- {
- maxDistSqr = distSqr;
-
- vertex->setSoftBodyIndex(n);
- }
- }
- }
- }
- }
- }
- }
+
hlib.ReleaseResult(hres);
} else
{
- /*
+
+ btSoftBodyWorldInfo& sbi= softDynaWorld->getWorldInfo();
+
if (m_cci.m_collisionShape->getShapeType() ==SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE)
{
btScaledBvhTriangleMeshShape* scaledtrimeshshape = (btScaledBvhTriangleMeshShape*) m_cci.m_collisionShape;
//psb = btSoftBodyHelpers::CreateFromTriMesh(sbi,&pts[0].getX(),triangles,numtriangles);
}
- */
}
-
- m_softbodyMappingDone = true;
+
+
m_object = psb;
//psb->m_cfg.collisions = btSoftBody::fCollision::SDF_RS;//btSoftBody::fCollision::CL_SS+ btSoftBody::fCollision::CL_RS;
- psb->m_cfg.collisions = btSoftBody::fCollision::SDF_RS + btSoftBody::fCollision::CL_SS;
+ psb->m_cfg.collisions = btSoftBody::fCollision::SDF_RS + btSoftBody::fCollision::VF_SS;//CL_SS;
//psb->m_cfg.collisions = btSoftBody::fCollision::CL_SS + btSoftBody::fCollision::CL_RS;
//btSoftBody::Material* pm=psb->appendMaterial();
//pm->m_kAST = 0.01f;
//pm->m_kVST = 0.001f;
psb->generateBendingConstraints(2,pm);
- //psb->m_cfg.piterations = 4;
- //psb->m_cfg.viterations = 4;
- //psb->m_cfg.diterations = 4;
- //psb->m_cfg.citerations = 4;
+ psb->m_cfg.piterations = 4;
+ psb->m_cfg.viterations = 4;
+ psb->m_cfg.diterations = 4;
+ psb->m_cfg.citerations = 4;
if (m_cci.m_gamesoftFlag & 2)//OB_SB_GOAL)
{
psb->setPose(false,true);//
}
psb->m_cfg.kDF = 0.5;
- psb->m_cfg.kMT = 0.05;
+ //psb->m_cfg.kMT = 0.05;
psb->m_cfg.piterations = 5;
psb->m_cfg.piterations = 5;
//psb->m_materials[0]->m_kLST = 0.1+(i/(btScalar)(n-1))*0.9;
psb->setTotalMass(m_cci.m_mass);
- psb->generateClusters(8);//(64);
+ psb->generateClusters(64);
psb->setCollisionFlags(0);
+
+
+
+
+
+ ///create a mapping between graphics mesh vertices and soft body vertices
+ {
+ RAS_MeshObject* rasMesh= GetShapeInfo()->GetMesh();
+
+ if (rasMesh && !m_softbodyMappingDone)
+ {
+
+ //printf("apply\n");
+ RAS_MeshSlot::iterator it;
+ RAS_MeshMaterial *mmat;
+ RAS_MeshSlot *slot;
+ size_t i;
+
+ //for each material
+ for (int m=0;m<rasMesh->NumMaterials();m++)
+ {
+ // The vertex cache can only be updated for this deformer:
+ // Duplicated objects with more than one ploymaterial (=multiple mesh slot per object)
+ // share the same mesh (=the same cache). As the rendering is done per polymaterial
+ // cycling through the objects, the entire mesh cache cannot be updated in one shot.
+ mmat = rasMesh->GetMeshMaterial(m);
+
+ slot = mmat->m_baseslot;
+ for(slot->begin(it); !slot->end(it); slot->next(it))
+ {
+ int index = 0;
+ for(i=it.startvertex; i<it.endvertex; i++,index++)
+ {
+ RAS_TexVert* vertex = &it.vertex[i];
+
+
+ //search closest index, and store it in vertex
+ vertex->setSoftBodyIndex(0);
+ btScalar maxDistSqr = 1e30;
+ btSoftBody::tNodeArray& nodes(psb->m_nodes);
+ btVector3 xyz = btVector3(vertex->getXYZ()[0],vertex->getXYZ()[1],vertex->getXYZ()[2]);
+ for (int n=0;n<nodes.size();n++)
+ {
+ btScalar distSqr = (nodes[n].m_x - xyz).length2();
+ if (distSqr<maxDistSqr)
+ {
+ maxDistSqr = distSqr;
+
+ vertex->setSoftBodyIndex(n);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ m_softbodyMappingDone = true;
+
+
+
+
+
+
// m_object->setCollisionShape(rbci.m_collisionShape);
btTransform startTrans;
if (m_useGimpact)
{
collisionMeshData = new btTriangleMesh();
+
+
// m_vertexArray is necessarily a multiple of 3
for (std::vector<btPoint3>::iterator it=m_vertexArray.begin(); it != m_vertexArray.end(); )
{
{
if (!m_unscaledShape)
{
- collisionMeshData = new btTriangleMesh();
+ collisionMeshData = new btTriangleMesh(true,false);
+ collisionMeshData->m_weldingThreshold = m_weldingThreshold;
+
// m_vertexArray is necessarily a multiple of 3
for (std::vector<btPoint3>::iterator it=m_vertexArray.begin(); it != m_vertexArray.end(); )
{