Assimp Unhandled Exception when importing .obj

886 views Asked by At

I've been trying to get Assimp to work correctly for the past two weeks, to no avail, so I decided asking here was the best thing to do. I followed thecplusplusguy's tutorial on importing static meshes, and I've copied it word for word in an effort to get it to work.

When I try to import an .obj (a cube, if that matters) it says I have an "Unhandled exception at 0x00EF061B in OpenGL.exe: 0xC0000005: Access violation reading location 0xABABAFFB", it stops the program and tells me it's on line 30 of my code ( "for(int i=0; imNumMeshes; i++)" ).

#include "sceneloader.h"

sceneLoader::sceneLoader(){
    std::cout<<"New scene created."<<std::endl;
}

sceneLoader::sceneLoader(const char* filename){
    std::cout<<"Scene loading hath begun."<<std::endl;
    Assimp::Importer importer;
    const aiScene* scene = importer.ReadFile(filename,aiProcess_GenSmoothNormals | aiProcess_Triangulate |
        aiProcess_CalcTangentSpace | aiProcess_FlipUVs |
        aiProcess_JoinIdenticalVertices);

    if(scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE){
        std::cout<<"MFLAGS - Scene '"<<filename<<"' could not be loaded."<<std::endl;
        return;
    }
    if(!scene->mRootNode){
        std::cout<<"MROOTNODE - Scene '"<<filename<<"' could not be loaded."<<std::endl;
        return;
    }
    std::cout<<"Recursive processing about to begin."<<std::endl;

    recursiveProcess(scene->mRootNode,scene);
    std::cout<<"Recursive processing finished."<<std::endl;
}

void sceneLoader::recursiveProcess(aiNode* node, const aiScene* scene){
    //process
    for(int i = 0; i<node->mNumMeshes;i++){         //HERE IS THE PROBLEM
        aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
            processMesh(mesh,scene);
        }
        //recursion
        for(int i = 0; 0<node->mNumChildren;i++){
            recursiveProcess(node->mChildren[i],scene);
        }
}

When I've added couts to debug it, the "scene->mNumMeshes" returns 1 (which it should, since it's one cube), but "node->mNumMeshes" returns 0.

I understand that an unhandled exception occurs when there's a null pointer, and that the null pointer here is the "node->mNumMeshes", but why is it null? And how would I fix this?

1

There are 1 answers

0
RealityMultiplied On

My bad. There was a typo:

void sceneLoader::recursiveProcess(aiNode* node, const aiScene* scene){
    //process
    for(int i = 0; i<node->mNumMeshes;i++){         
        aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
            processMesh(mesh,scene);
        }
        //recursion
        for(int i = 0; 0<node->mNumChildren;i++){ //IT SHOULD BE AN i INSTEAD OF A ZERO
            recursiveProcess(node->mChildren[i],scene);
        }
}

Finished code looks like this:

void sceneLoader::recursiveProcess(aiNode* node, const aiScene* scene){
    //process

    for(unsigned int i = 0; i<node->mNumMeshes;i++){    
        aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
        processMesh(mesh,scene);
        }
        //recursion
    if(node->mNumChildren > 0){
            for(unsigned int i = 0; i<node->mNumChildren;i++){
                recursiveProcess(node->mChildren[i],scene);
            }
    }
}

Sorry. Won't happen again.