将指针复制到 aiScene

Copy pointer to aiScene

我使用 Assimp 制作骨骼动画。因此,我总是需要它加载的数据。但由于某种原因,此数据变得不可访问并导致分段错误。下面是问题代码:

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

    ...
}

但是如果你在Mesh里面做,会导致错误。例如:

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

编辑: ai_scene在Mesh结构中:

private:
const aiScene *ai_scene = nullptr;

场景本身在模型结构中创建如下:

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代码:

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

这是什么问题?以及如何解决?

问题已通过扩大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);
    ...
}