无法加载带有 assimp 的模型,访问冲突

Failed to load model with assimp, access violation

本文关键字:模型 访问冲突 assimp 加载      更新时间:2023-10-16

每当我编译它抛出的代码时,我想使用 assimp 导入一个简单的模型时,我遇到了问题:

0xC0000005: Access violation reading location 0x00000000.

我知道这是关于空指针的东西,但我只是找不到它,代码如下:

Model::Model(GLchar* path)
{
    loadModel(path);
}
void Model::loadModel(std::string path)
{
    Assimp::Importer import;
    const aiScene* scene = import.ReadFile(
        path, 
        aiProcess_Triangulate | 
        aiProcess_FlipUVs);
    if (!scene || scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode){
        std::cout << "ERROR::ASSIMP::" << import.GetErrorString() << std::endl;
        return;
    }
    directory = path.substr(0, path.find_last_of('/'));
    aiNode* node = scene->mRootNode;
    for (GLuint i = 0; i < node->mNumChildren; i++){
        aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
        meshes.push_back(processMesh(mesh, scene));
    }
    for (GLuint i = 0; i < node->mNumChildren; i++){
        processNode(node->mChildren[i], scene);
    }
}

我使用此模型类作为全局变量:

//include stuff
//other global variable
Model mymodel("D:/Project/xxx/xxx.obj");
int main(){
//...
return 0;
}

问题是错误发生在行directory = path.substr(0, path.find_last_of('/'));和行aiNode* node = scene->mRootNode;之间,所以我不知道如何调试它,谁能告诉我如何解决这个问题?我使用 Visual Studio 2013-64 和 assimp-3.1.1。

谢谢。

我认为问题可能出在代码的这一部分:

Model::Model(GLchar* path)
{    
    loadModel(path); // path is declared a string in loadModel function - type conversion might not be happening as expected.
}

检查,您是否在行上的路径变量中获得正确/有效的值:

directory = path.substr(0, path.find_last_of('/'));

此链接可能会有所帮助:无法解析 GLchar

注意:我不熟悉OpenGL,但是查看您遇到的错误是我要检查的第一个地方。