Summit
Summit

Reputation: 2268

How to get the position of the imported Mesh from Assimp

I am importing FBX files using assimp and also want to get the Transformation of the mesh from the model.

This is the function that gets the position of the mesh.

This works but sometimes the application crashes while importing certain models and if i remove the transformation part than model imports successfully.

void processNode(aiNode* node, const aiScene *scene)
{
    // process each mesh located at the current node
    
    for (unsigned int i = 0; i < node->mNumMeshes; i++)
    {
        aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];    
        ai_real x = scene->mRootNode->mChildren[iModel]->mTransformation.a4;
        ai_real y = scene->mRootNode->mChildren[iModel]->mTransformation.b4;
        ai_real z = scene->mRootNode->mChildren[iModel]->mTransformation.c4;        
        std::cout << x << " " << y << " " << z; // if i comment this than no crash    
     }
}

Upvotes: 0

Views: 1638

Answers (1)

KimKulling
KimKulling

Reputation: 2843

You need to use the instance of aiNode where the mesh is referenced and perform the whole transformation chain from your node up to the root node. The you can extract the position out of it. So in short:

  1. Check if your node has any parent node.
  2. If yes, get the transformation node from the parent, get its transformation and multiply this with your child transformation
  3. Repeat this until you have reached the root node (parent pointer will be nullptr to find it)

With this global transformation you have the information where your mesh is located in the World-Space.

Upvotes: 2

Related Questions