We load in a .fbx file which is split up into multiple child mesh objects. First we must calculate a transform based on the child mesh translation/rotation/scale which will later be applied to offset our vertices from local to global positions:

Code: Select all

```
FbxAMatrix matrixGeo;
matrixGeo.SetIdentity();
if (Node->GetNodeAttribute())
{
const fbxsdk::FbxVector4 lT = Node->GetGeometricTranslation(FbxNode::eSourcePivot);
const fbxsdk::FbxVector4 lR = Node->GetGeometricRotation(FbxNode::eSourcePivot);
const fbxsdk::FbxVector4 lS = Node->GetGeometricScaling(FbxNode::eSourcePivot);
matrixGeo.SetT(lT);
matrixGeo.SetR(lR);
matrixGeo.SetS(lS);
}
FbxAMatrix globalMatrix = Node->EvaluateGlobalTransform();
FbxAMatrix matrix = globalMatrix * matrixGeo;
```

Code: Select all

```
const FbxMesh* Mesh = FbxCast<const FbxMesh>(Node->GetNodeAttribute());
fbxsdk::FbxVector4* Vertices = Mesh->GetControlPoints();
uint32_t IndexCount = 0;
for (unsigned int j = 0; j < Mesh->GetPolygonCount(); j++) {
const unsigned int NumVerts = Mesh->GetPolygonSize(j);
if (NumVerts != 3) { continue; }
for (unsigned int k = 0; k < NumVerts; k++) {
Vertex* NewVertex = new Vertex;
const unsigned int Index = Mesh->GetPolygonVertex(j, k);
//NewVertex->pos.x = (float)(Vertices[Index].mData[0]);
//NewVertex->pos.y = (float)(Vertices[Index].mData[1]);
//NewVertex->pos.z = (float)(Vertices[Index].mData[2]);
fbxsdk::FbxVector4 result = matrix.MultT(Vertices[Index]);
NewVertex->pos.x = (float)result.mData[0];
NewVertex->pos.y = (float)result.mData[1];
NewVertex->pos.z = (float)result.mData[2];
NewFBX->Meshes.back()->Indices.push_back(IndexCount++);
NewFBX->Meshes.back()->Vertices.push_back(*NewVertex);
}
}
```

After constructing array of vertices we pass this array over to create a collision shape with bullet:

Code: Select all

```
TriangleMesh* Mesh = new TriangleMesh(_Driver, Pipe, FBXMesh, DiffuseTex);
btCollisionShape* ColShape;
btTriangleMesh* trimesh = new btTriangleMesh();
for (unsigned int i = 0; i < FBXMesh->Indices.size() / 3; i++) {
auto V1 = FBXMesh->Vertices[i * 3].pos;
auto V2 = FBXMesh->Vertices[i * 3 + 1].pos;
auto V3 = FBXMesh->Vertices[i * 3 + 2].pos;
trimesh->addTriangle(btVector3(V1.x, V1.y, V1.z), btVector3(V2.x, V2.y, V2.z), btVector3(V3.x, V3.y, V3.z));
}
ColShape = new btBvhTriangleMeshShape(trimesh, true);
```

Code: Select all

```
btTransform Transform;
Transform.setIdentity();
//Transform.setOrigin(btVector3(FBXMesh->translation[0], FBXMesh->translation[1], FBXMesh->translation[2]));
//Transform.setRotation(btQuaternion(glm::radians(FBXMesh->rotation[1]), glm::radians(FBXMesh->rotation[0]), glm::radians(FBXMesh->rotation[2])));
```

Code: Select all

```
SceneNodeMotionState* MotionState = new SceneNodeMotionState(MeshNode, Transform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(Mass, MotionState, MeshNode->_CollisionShape, localInertia);
MeshNode->_RigidBody = new btRigidBody(rbInfo);
MeshNode->_RigidBody->setUserPointer(MeshNode);
dynamicsWorld->addRigidBody(MeshNode->_RigidBody);
```

I would like to draw your attention to this section of code:

Code: Select all

```
//NewVertex->pos.x = (float)(Vertices[Index].mData[0]);
//NewVertex->pos.y = (float)(Vertices[Index].mData[1]);
//NewVertex->pos.z = (float)(Vertices[Index].mData[2]);
fbxsdk::FbxVector4 result = matrix.MultT(Vertices[Index]);
NewVertex->pos.x = (float)result.mData[0];
NewVertex->pos.y = (float)result.mData[1];
NewVertex->pos.z = (float)result.mData[2];
```

Any ideas?