Jump to content
  • Advertisement
Sign in to follow this  
uzipaz

Need help using assimp library to import 3d models in c++

This topic is 598 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am trying to use assimp library to import 3D models into my c++ program and I am having a memory access violation error with using the library. I am using Visual Studio 2015 on Windows7-64bit.

I am trying to read a model and then render(wireframe only) it using OpenGL. In my program, I first read the model using Assimp::ReadFile function, which seems to be successful and then I iterate through the aiScene data structure to retrieve the lists of vertices and construct corresponding vertex buffer objects for opengl.

My assimp import function

 
const aiScene* readModel(const std::string& File)
{
    Assimp::Importer importer;

    const aiScene* scene = importer.ReadFile(File, aiProcess_Triangulate);

    if (scene == NULL)
        std::cout << "Import error: " << importer.GetErrorString();

    else
    {
        std::cout << "Import successful\n";
        return scene;
    }

    return 0;
}
 

In my program, this function always prints out "Import successful" on the console and returns the scene pointer.

Then I run my processing function to generate vertex buffers from the aiScene object,

 
void genVAOsAndUniformBuffer(const aiScene *sc, GLuint program) {

    MyMesh aMesh;
    GLuint buffer;

    // For each mesh
    for (unsigned int n = 0; n < sc->mNumMeshes; ++n)
    {
        // Program crashes here, in first iteration
        const aiMesh* mesh = sc->mMeshes[n];
 

 

My program crashes on the last line here(memory access violation) when accessing the mesh object from mMeshes array. It crashes in the first iteration of the loop, accessing the mesh object at index 0. I have tried different 3d models and different extensions such obj and 3ds. I have also tried by setting the flag on ReadFile function to 0.

All crash on the same line of code. When debugging the code, I noticed that for no matter the 3d model or extension, the variable sc->mNumMeshes always has the value of 3722304989, which doesn't make sense to me because it can't be the same if I am using the 3d models that don't have the same number of vertices or just completely different.

Thats all I have for now, I tried searching on the internet, I couldn't find anything on it. I would really appreciate any help I can get. If this subforum is not the appropriate place to ask about a 3d model importing library for c++, then I ask the moderators to kindly move this post to the appropriate subform.

Thank you for reading.

Share this post


Link to post
Share on other sites
Advertisement

because of https://www.gamedev.net/topic/686869-assimp-forcing-1-mesh-per-node/

it goes...

aiNode* node = sc->mRootNode;

void ProcessNode(aiNode* node, const aiScene* scene)
{
    for (unsigned int n = 0; n < node->mNumMeshes; ++n)
    {
        const aiMesh* mesh = scene->mMeshes[node->mMeshes[n]];
 
        // process you mesh
    }
    for (unsigned int n = 0; n < node->mNumChildren; ++n)
    {
        ProcessNode(node->mChildren[n], scene);
    }
}

When debugging the code, I noticed that for no matter the 3d model or extension, the variable sc->mNumMeshes always has the value of 3722304989

That can't be right...if it is there are other problems.

Oh...local scope.
Don't return the local scene variable...process from there while the ReadModel function is still active.

Share this post


Link to post
Share on other sites

Thank you for the reference @newtechnology

@Mark please correct me if I am wrong but if I am return a pointer variable from the function, its an address to a dynamic memory, why would local scope would cause any problem here?

Share this post


Link to post
Share on other sites
The summary notes on Assimp::Importer::ReadFile states, "The pointer to the scene remains in possession of the Importer instance. Use GetOrphanedScene() to take ownership of it." Its your importer instance that is in local scope. When your function returns the importer is destroyed along with its data. Or at least that's how I see it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!