Search code examples
c++assimp

Copy pointer to aiScene


I do skeletal animations using Assimp. Accordingly, I always need the data loaded by it. But for some reason, this data becomes inaccessible and results in a Segmentation fault. Below is the problem code:

static Mesh processMesh(const aiMesh *aimesh, const aiScene *scene) {
    Mesh mesh;
    mesh.ai_scene = scene;
    std::cout << mesh.ai_scene->mRootNode->mName.data; // OK

    ...
}

But if you do it inside the Mesh itself, it will lead to an error. For example:

void boneTransform(float TimeInSeconds) {
    glm::mat4 identity = glm::mat4();
    std::cout << ai_scene->mRootNode->mName.data; // SEG FAULT
    ...
}

EDIT: ai_scene is in the Mesh structure:

private:
const aiScene *ai_scene = nullptr;

The scene itself is created in the Model structure as follows:

void init(const std::string &path, const GLuint params) {
    // Create an instance of the Importer class
    Assimp::Importer importer;
    const aiScene *scene = importer.ReadFile(path, params);

    // If the import failed, report it
    if (!scene) {
        std::cout << importer.GetErrorString() << "\n";
        return;
    }

    processNode(scene->mRootNode, scene);
}

processNode code:

void processNode(const aiNode *node, const aiScene *scene) {
    for (size_t i = 0; i < node->mNumMeshes; i++) {
        aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
        meshes.push_back(Mesh::processMesh(mesh, scene));       
    }

    for(size_t i = 0; i < node->mNumChildren; i++) {
        processNode(node->mChildren[i], scene);
    }
}

What is this problem? And how to solve it?


Solution

  • Problem has been solved by expanding the scope of Assimp::Importer importer:

    // Create an instance of the Importer class
    Assimp::Importer importer;
    void init(const std::string &path, const GLuint params) {
        const aiScene *scene = importer.ReadFile(path, params);
        ...
    }