Code: Select all
void Mesh::CreateMeshShape(const std::vector<Vertex>& targetVertices, const std::vector<UINT>& targetIndices)
{
mTriangleVertexArray = new btTriangleIndexVertexArray();
btIndexedMesh tempMesh;
mTriangleVertexArray->addIndexedMesh(tempMesh, PHY_FLOAT);
btIndexedMesh& mesh = mTriangleVertexArray->getIndexedMeshArray()[0];
const int32_t VERTICES_PER_TRIANGLE = 3;
size_t numIndices = targetIndices.size();
mesh.m_numTriangles = numIndices / VERTICES_PER_TRIANGLE;
if (numIndices < std::numeric_limits<int16_t>::max())
{
mesh.m_triangleIndexBase = new unsigned char[sizeof(int16_t) * (size_t)numIndices];
mesh.m_indexType = PHY_SHORT;
mesh.m_triangleIndexStride = VERTICES_PER_TRIANGLE * sizeof(int16_t);
}
else
{
mesh.m_triangleIndexBase = new unsigned char[sizeof(int32_t) * (size_t)numIndices];
mesh.m_indexType = PHY_INTEGER;
mesh.m_triangleIndexStride = VERTICES_PER_TRIANGLE * sizeof(int32_t);
}
mesh.m_numVertices = targetVertices.size();
mesh.m_vertexBase = new unsigned char[VERTICES_PER_TRIANGLE * sizeof(btScalar) * (size_t)mesh.m_numVertices];
mesh.m_vertexStride = VERTICES_PER_TRIANGLE * sizeof(btScalar);
btScalar* vertexData = static_cast<btScalar*>((void*)(mesh.m_vertexBase));
for (int32_t i = 0; i < mesh.m_numVertices; ++i)
{
int32_t j = i * VERTICES_PER_TRIANGLE;
const XMFLOAT3& point = targetVertices[i].Position;
vertexData[j] = point.x;
vertexData[j + 1] = point.y;
vertexData[j + 2] = point.z;
}
if (numIndices < std::numeric_limits<int16_t>::max())
{
int16_t* indices = static_cast<int16_t*>((void*)(mesh.m_triangleIndexBase));
for (int32_t i = 0; i < numIndices; ++i) {
indices[i] = (int16_t)targetIndices[i];
}
}
else
{
int32_t* indices = static_cast<int32_t*>((void*)(mesh.m_triangleIndexBase));
for (int32_t i = 0; i < numIndices; ++i)
{
indices[i] = targetIndices[i];
}
}
const bool USE_QUANTIZED_AABB_COMPRESSION = true;
mMeshShape = std::make_shared<btBvhTriangleMeshShape>(mTriangleVertexArray, USE_QUANTIZED_AABB_COMPRESSION);
}
//Create RigidBody
while(*mesh read end*)
{
for (int i = 0; i < obj->GetMeshCount(); ++i)
{
if (meshes[i]->GetMeshShape())
{
btTransform btLocalTransform;
btLocalTransform.setIdentity();
btLocalTransform.setOrigin(btVector3(pos.x, pos.y, pos.z));
compound->addChildShape(btLocalTransform, meshes[i]->GetMeshShape().get());
}
}
}
btTransform btObjectTransform;
btObjectTransform.setIdentity();
btObjectTransform.setOrigin(btVector3(0, 0, 0));
mTrackRigidBody = physics->CreateRigidBody(0.0f, btObjectTransform, compound);
There seems to be no problem with the code, but is there anything wrong?