Assimp: Failed to load texture filename using GetTexture in assimp v5.0.0

Created on 8 Nov 2019  路  9Comments  路  Source: assimp/assimp

I am able to load my model with texture with absolute path, but the model presented black as it failed to retrieve the filename using GetTexture()

The .obj, .mtl file and all the images or textures have been placed in a same directory (I have used blender try to import or load the .obj file, it works fine with texture)

IDE vscode linux

After many tests, I made the code work by downgrading the version of the assimp
The following code works fine on assimp v4.1.0 but does not work in assimp v5.0.0

I want to know if GetTexture is deprecated, and if so, we should consider removing it...

Source code from : learnopengl.com/Model-Loading/Model

 vector<Texture> loadMaterialTextures(aiMaterial *mat, aiTextureType type, string typeName, const aiScene *scene)
    {
        vector<Texture> textures;
        for(unsigned int i = 0; i < mat->GetTextureCount(type); i++)
        {
            aiString str;

            mat->GetTexture(type, i, &str);
            std::cout << str.C_Str() << std::endl;   // ---> emtyp string
            std::cout << string(str.C_Str()) << std::endl;   // ---> emtyp string
            // check if texture was loaded before and if so, continue to next iteration: skip loading a new texture
            bool skip = false;
            for(unsigned int j = 0; j < textures_loaded.size(); j++)
            {
                if(std::strcmp(textures_loaded[j].path.data(), str.C_Str()) == 0)
                {
                    textures.push_back(textures_loaded[j]);
                    skip = true; // a texture with the same filepath has already been loaded, continue to next one. (optimization)
                    break;
                }
            }
            if(!skip)
            {   // if texture hasn't been loaded already, load it
                Texture texture;
                texture.id = TextureFromFile(str.C_Str(), this->directory);
                texture.type = typeName;
                texture.path = str.C_Str();   // ---> emtyp string
                textures.push_back(texture);
                textures_loaded.push_back(texture);  // store it as texture loaded for entire model, to ensure we won't unnecesery load duplicate textures.
            }
        }
        return textures;
    }
unsigned int TextureFromFile(const char *path, const string &directory, bool gamma)
{
    string filename = string(path);  // ---> emtyp string
    filename = directory + '/' + filename;
    std::cout << filename << std::endl;   // ---> only show directory path
    unsigned int textureID;
    glGenTextures(1, &textureID);

    int width, height, nrComponents;
    unsigned char *data = stbi_load(filename.c_str(), &width, &height, &nrComponents, 0);
    if (data)
    {
        GLenum format;
        if (nrComponents == 1)
            format = GL_RED;
        else if (nrComponents == 3)
            format = GL_RGB;
        else if (nrComponents == 4)
            format = GL_RGBA;

        glBindTexture(GL_TEXTURE_2D, textureID);
        glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
        glGenerateMipmap(GL_TEXTURE_2D);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        stbi_image_free(data);
    }
    else
    {
        std::cout << "Texture failed to load at path: " << path << std::endl;
        stbi_image_free(data);
    }

    return textureID;
}
Bug

Most helpful comment

Observed similar issue with assimp: stable 5.0.1 . Looks like the texture name is not correctly loaded from:

// ---------------------------------------------------------------------------
inline aiReturn aiMaterial::GetTexture( aiTextureType type,
   unsigned int  index,
   C_STRUCT aiString* path,
   aiTextureMapping* mapping    /*= NULL*/,
   unsigned int* uvindex        /*= NULL*/,
   float* blend                /*= NULL*/,
   aiTextureOp* op              /*= NULL*/,
   aiTextureMapMode* mapmode    /*= NULL*/) const
{
    return ::aiGetMaterialTexture(this,type,index,path,mapping,uvindex,blend,op,mapmode);
}

What's in mtl file:

newmtl Glass
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
map_Bump glass_ddn.png
map_Ka glass_refl.png
map_Kd glass_dif.png

the output texture file name :

s_dif.png
s_ddn.png
s_refl.png

Also the length of the aiString is incorrect.
Please check kindly : )

All 9 comments

What status of this bug? I have the same problem with this bug.

What status of this bug? I have the same problem with this bug.

I think you should use assimp v4.1.0 instead, if you want that feature.

Observed similar issue with assimp: stable 5.0.1 . Looks like the texture name is not correctly loaded from:

// ---------------------------------------------------------------------------
inline aiReturn aiMaterial::GetTexture( aiTextureType type,
   unsigned int  index,
   C_STRUCT aiString* path,
   aiTextureMapping* mapping    /*= NULL*/,
   unsigned int* uvindex        /*= NULL*/,
   float* blend                /*= NULL*/,
   aiTextureOp* op              /*= NULL*/,
   aiTextureMapMode* mapmode    /*= NULL*/) const
{
    return ::aiGetMaterialTexture(this,type,index,path,mapping,uvindex,blend,op,mapmode);
}

What's in mtl file:

newmtl Glass
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
map_Bump glass_ddn.png
map_Ka glass_refl.png
map_Kd glass_dif.png

the output texture file name :

s_dif.png
s_ddn.png
s_refl.png

Also the length of the aiString is incorrect.
Please check kindly : )

I encountered the same problem so that I once suspected a problem with my code!

I'm currently having the same problem with Assimp 5.0.1.
The first four characters of the texture name referenced in the MTL file of an OBJ file always get stripped (e.g. "diffuse.jpg" becomes "use.jpg").

This line of code looks particularly suspicious:
memcpy(pOut->data, prop->mData + 4, pOut->length + 1);

This happened to me as well. I spent a good 3 hours before realising there isnt anything wrong with my code. Hope that doesnt happen to someone else and this is fixed soon.

assimp 4.1.0 works
Had to downgrade brew to that specific commit for assimp and then install it.

I'm currently having the same problem with Assimp 5.0.1.
The first four characters of the texture name referenced in the MTL file of an OBJ file always get stripped (e.g. "diffuse.jpg" becomes "use.jpg").

This line of code looks particularly suspicious:
memcpy(pOut->data, prop->mData + 4, pOut->length + 1);

I didnt see this line in the assimp code. This might not be the issue

Observed similar issue with assimp: stable 5.0.1 . Looks like the texture name is not correctly loaded from:

// ---------------------------------------------------------------------------
inline aiReturn aiMaterial::GetTexture( aiTextureType type,
   unsigned int  index,
   C_STRUCT aiString* path,
   aiTextureMapping* mapping  /*= NULL*/,
   unsigned int* uvindex      /*= NULL*/,
   float* blend                  /*= NULL*/,
   aiTextureOp* op                /*= NULL*/,
   aiTextureMapMode* mapmode  /*= NULL*/) const
{
  return ::aiGetMaterialTexture(this,type,index,path,mapping,uvindex,blend,op,mapmode);
}

What's in mtl file:

newmtl Glass
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
map_Bump glass_ddn.png
map_Ka glass_refl.png
map_Kd glass_dif.png

the output texture file name :

s_dif.png
s_ddn.png
s_refl.png

Also the length of the aiString is incorrect.
Please check kindly : )

did you happen to find a solution to your problem? i am encountering the same exact thing

same problem.

just change to:

map_Kd ....diffuse.jpg
map_Bump ....normal.png
map_Ks ....specular.jpg

Was this page helpful?
0 / 5 - 0 ratings