无法使用assimp加载模型,访问冲突

时间:2016-01-22 03:56:27

标签: c++ opengl access-violation assimp

当我想使用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);
    }
}

我将此Model类用作全局变量:

//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。

非常感谢。

1 个答案:

答案 0 :(得分:0)

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

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 could not be resolved

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