Search code examples
c++importmeshassimp

Assimp - How do you import a mesh with textures using any file format?


When I import meshes, I get the material, but can't access the file name of the texture. The .mtl file explicitly shows the filename for the texture. In the code, it shows a texture count of 1 but the filename field shows an empty string and fullPath outputs "*0". In the mTexture it does show the texture file extension ".png" but not the filename of the texture itself. Thanks for any help.

    if (scene->HasMaterials())
    {
        for (unsigned int i = 0; i < scene->mNumMaterials; ++i)
        {
            aiMaterial* material = scene->mMaterials[i];
            aiString name;
            material->Get(AI_MATKEY_NAME, name);
            aiReturn texFound = scene->mMaterials[i]->GetTexture(aiTextureType_DIFFUSE, i, &name);

            if (material->GetTextureCount(aiTextureType_DIFFUSE) > 0)
            {
                aiString path;
                if (material->GetTexture(aiTextureType_DIFFUSE, 0, &path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS)
                {
                    std::string fullPath = path.data;


                }
            }
        }
    }

Solution

  • If anyone is curious to how I solved my problem, it is shown below. In the .mtl file I had to add map_Kd diffusefile.png to add a diffuse file so then assimp could pick up the texture file.

            if (scene->HasMaterials())//True when number of materials is greater than 0
            {
                for (unsigned int m = 0; m < scene->mNumMaterials; ++m)
                {
                    aiMaterial* material = scene->mMaterials[m];//Get the current material
                    aiString materialName;//The name of the material found in mesh file
                    aiReturn ret;//Code which says whether loading something has been successful of not
    
                    ret = material->Get(AI_MATKEY_NAME, materialName);//Get the material name (pass by reference)
                    if (ret != AI_SUCCESS) materialName = "";//Failed to find material name so makes var empty
    
                    //Diffuse maps
                    int numTextures = material->GetTextureCount(aiTextureType_DIFFUSE);//Amount of diffuse textures
                    aiString textureName;//Filename of the texture using the aiString assimp structure
    
                    if (numTextures > 0)
                    {
                        //Get the file name of the texture by passing the variable by reference again
                        //Second param is 0, which is the first diffuse texture
                        //There can be more diffuse textures but for now we are only interested in the first one
                        ret = material->Get(AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0), textureName);
    
                        std::string textureType = "diff_";
                        std::string textureFileName = textureType + textureName.data;//The actual name of the texture file
                    }
                }