Jump to content
  • Advertisement

Recommended Posts

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
        ...
    }

What is this problem? And how to solve it?

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);
        }
    }

 

Share this post


Link to post
Share on other sites
Advertisement

I haven't read through the Assimp internals or documentation in a while, but from what you're describing, I'm guessing that your aiScene pointer becomes invalid when the Assimp::Importer goes out of scope. If that's the case, you'll need to store the Assimp::Importer somewhere until you're done using it.

Share this post


Link to post
Share on other sites
Posted (edited)

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);
    ...
}

 

14 minutes ago, joe_chip said:

Importer will delete the scene as soon as it gets destroyed itself. If you want to keep the scene, you can use Importer::GetOrphanedScene() method - then deleting the scene is your responsibility.

Thanks I'll know!

Edited by congard

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!