Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assimp does not import textures

Tags:

c++

opengl

assimp

I am using assimp to import 3d models in my game engine. For some reason, no matter what model or model format I use, assimp does not report any textures whatsoever. Why is this?

The following is the very simple setup with assimp:

const aiScene* scene = importer.ReadFile(asset, aiProcessPreset_TargetRealtime_Quality);

if (scene->HasMaterials())
        {
            for (unsigned int i = 0; i < scene->mNumMaterials; i++)
            {
                const aiMaterial* material = scene->mMaterials[i];
                aiString texturePath;

                unsigned int numTextures= material->GetTextureCount(aiTextureType_DIFFUSE);   // always 0

                if (material->GetTextureCount(aiTextureType_DIFFUSE) > 0 && material->GetTexture(aiTextureType_DIFFUSE, 0, &texturePath) == AI_SUCCESS) 
                {
                   // never happens..
                   // scene->mNumTextures is always 0 aswell.
                }
            }
        }

I'm using various models that I am positive has textures, for example models from tutorials that are designed for the very specific reason of loading textured models using assimp. (for example, http://www.lighthouse3d.com/cg-topics/code-samples/importing-3d-models-with-assimp)

What could be the reason for this?

like image 319
KaiserJohaan Avatar asked Dec 08 '22 16:12

KaiserJohaan


1 Answers

I know this is quite old, but I was having this exact problem the past couple days and managed to figure it out, so here's a solution for whoever is looking.

There are two possible reasons for why your textures are not loading:

  1. The textures are embedded.

or

  1. Something is wrong with your materials file for the model.

If it is case #1, then it is not working simply because GetTexture and GetTextureCount do not apply to embedded materials. Embedded materials are usually just simple colours. If you would like to know how to import embedded materials, take a look at the ASSIMP sample codes, or the source code for AssimpView.

Chances are your issue is in case #2: something is wrong with your materials file. .OBJ model files, for example, come with a corresponding .MTL file, if they are textured. If the .MTL file is missing, the textures will not load. If there is something wrong with the .MTL file, the textures also will not load.

So, make sure all of your .mtl files are there and any other material files (including the images for the textures themselves). If they are there, then the next thing you will want to do is open the model using the AssimpView tool. Make sure that the model loads in properly with all of its textures. If it doesn't, either something is wrong with the materials file (solution below), or something is wrong with the model file and Assimp simply can't load it (or something is fundamentally wrong with Assimp). If it loads properly, You should see the names of the diffuse textures on the right side of AssimpView.

So, if all of this checks out and the Assimp importer in your program is still not recognizing textures from your model (even though AssimpView does), then the next thing you want to do is actually open the .MTL file using something like notepad. You will see something like this:

# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
# File Created: 06.10.2013 19:40:35

newmtl 01___Default
Ns 10.0000
illum 2
Ka 0.0000 0.0000 0.0000
Kd 0.5882 0.5882 0.5882
Ks 0.0000 0.0000 0.0000
Ni 1
d 0
map_Kd MoonMap2_2500x1250.jpg

There are various things that can be wrong with these files. Notice that map_Kd defines the image file to be used for the diffuse texture. Sometimes, instead of having the correct path or just the filename there, you will instead see an entire directory path relative to the computer that the model was made on (i.e. it'll say something like C:/Users/blah blah blah/MoonMap2...). You can easily fix this by removing the directory path and only keeping the image file name (and keeping the image in the same folder as the model), or you can put your own custom path and put the textures there.

Another kind of issue that you may see is variables being listed in the .MTL file without actually being given values.

For example:

# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
# File Created: 06.10.2013 19:40:35

newmtl 01___Default
Ns 10.0000
illum 2
Ka 0.0000 0.0000 0.0000
Kd 0.5882 0.5882 0.5882
Ks 0.0000 0.0000 0.0000
Ni 1
d 0
map_Kd MoonMap2_2500x1250.jpg
map_Ka 

Notice the "map_Ka" at the end there. It has nothing next to it. Not only does this prevent textures from being recognized properly, it actually prevents AssimpView from being able to load models at all. Simply remove any properties like this that you see when they have no values next to them.

Finally, if none of this works, then you may just have to experiment with the .MTL file - there might be a property/variable listed in it that's causing trouble for some reason, so try removing lines that you're absolutely sure you don't need (Note that Kd represents the diffuse texture, Ka would represent the ambient texture, Ks the specular, etc.). The .MTL file that I have been using as an example here contains what I believe is the bare minimum lines that an MTL file should have in order to be able to load in a diffuse texture.

Hope this helps. If none of this works, I would suspect that something is fundamentally wrong with your ASSIMP library. If your model file has no materials file well... there's your problem.

like image 159
Matt Avatar answered Dec 14 '22 10:12

Matt