mtl Texture refuses to load

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Atraitus
Posts: 16
Joined: Sun Jan 02, 2011 11:44 pm

mtl Texture refuses to load

Post by Atraitus »

First of all, let me say I'm loving Irrlicht so far. I discovered it a few weeks ago, and just had time to start using it yesterday. I've gone through the tutorials, and I have a reasonable grasp on C++, but I'm still fairly new at this whole thing :oops: Anyway, I'm attempting to load into my scene a .obj model, with textures defined by a .mtl. Here's my loading code:

Code: Select all

        scene::IMeshSceneNode* node = smgr->addMeshSceneNode(smgr->getMesh("tree.obj"),0,-1,core::vector3df(1922.f,360.f,450.f),
                                                             core::vector3df(0.f,0.f,0.f),core::vector3df(75.0f,75.0f,75.0f));

        node->setMaterialFlag(video::EMF_LIGHTING, false);
        node->setMaterialTexture(0,driver->getTexture("tree.mtl"));
Compiles fine, runs fine. Loads the two textures defined by "tree.mtl", loads "tree.obj", informs me that "tree.mtl" failed to load, and continues happily on, leaving me with a monochrome, greyish tree standing proudly on a majestic terrain.

I'm sure I've done something wrong, and it's probably ridiculously simple, too. Anyone have any ideas what's going on, or need more information?

I've searched these forums, I've Googled it, and I've been fiddling with this for two hours, before I finally broke down and posted this. Any help would be much appreciated.
Mag-got
Posts: 42
Joined: Tue Dec 04, 2007 5:53 pm

Post by Mag-got »

The .mtl file is the material file, not the texture. If the .obj file is linked to the .mtl file, irrlicht should load the texture for you when you load the .obj file.

So try exporting with materials and remove the setMaterialTexture line, or just load the texture.
Atraitus
Posts: 16
Joined: Sun Jan 02, 2011 11:44 pm

Post by Atraitus »

Edit: It seems I was hopelessly misinformed. I have changed my code to the following:

Code: Select all

        node->setMaterialTexture(0,driver->getTexture("bark.jpg"));
        node->setMaterialTexture(1,driver->getTexture("leaf.png"));
Now the model will get one texture, whichever is the texture layer 0. I'm not quite sure why it's only getting one, but I'm no longer calling the mtl, and I've tried it with the texture layer alternated (not that I thought that would work, but hey).
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

As Mag-got said - the .mtl file should be loaded automatically. Just make sure the .mtl and the textures are in the same folder as the .obj file (it's also possible otherwise, but for a first test just do that).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Atraitus
Posts: 16
Joined: Sun Jan 02, 2011 11:44 pm

Post by Atraitus »

Yes, the .mtl and both textures are in the same folder as the .obj (I'm loading from a zip archive, if that makes any difference). The .mtl definitely isn't loading automatically. I exported the model from Blender, originally in a .blend file.

If I'm reading your advice correctly, it seems I shouldn't have to load textures at all, correct? I tried this, and I get an untextured model. The only way I can get any texture at all is with

Code: Select all

node->setMaterialTexture(0,driver->getTexture("bark.jpg"));
I'm not sure if Blender is incorrectly exporting or what, any information on the Web is from ~2005 and slightly less than still reliable.
Mag-got
Posts: 42
Joined: Tue Dec 04, 2007 5:53 pm

Post by Mag-got »

Open up your .obj file, on the top of it after the comments should be linkage to the .mtl file "mtllib tree.mtl"

If the line isn't there, you haven't exported correctly.

If the line is there, then look at your material file. There should be the texture names in the materials.

For example "map_Kd tree.jpg"
Atraitus
Posts: 16
Joined: Sun Jan 02, 2011 11:44 pm

Post by Atraitus »

Yes, both are there. The .obj starts out with:

Code: Select all

# Blender v2.56 (sub 0) OBJ File: 'trees_0.blend'
# www.blender.org
mtllib tree.mtl
The .mtl is (irrelevant parts cut):

Code: Select all

# Material Count: 2
newmtl tree_brown_trees_bark_002_col
...
illum 1
map_Kd bark.jpg


newmtl browntreeleaves_trees_leaves_003_col
Ns 43.137255
...
illum 1
map_Kd leaf.png
Without loading textures, I get just that: an untextured model. If I manually load textures, I get a model with only one texture.
Mag-got
Posts: 42
Joined: Tue Dec 04, 2007 5:53 pm

Post by Mag-got »

Well damn.. And you've turned off lighting with

Code: Select all

node->setMaterialFlag(EMF_LIGHTING, false);
?

You exported with UV?

Check if the materials are being used in the .obj file, with usemtl <material name>?

And irrlicht finds these textures, so it can't be because of that?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

setMaterialTexture sets the layers of a single material - so using more than one is only useful if you have a material with multiple textures. But from your .mtl it seems that you have rather several materials each with a single texture. Each material will create an own meshbuffer, so you would have to set the texture 0 for the corresponding meshbuffers.

But certainly the point behind .obj and .mtl is that this should work automatically. So please try first if it loads correctly in the meshviewer (example 09). If it works there, then the bug is in your code and you should maybe post more code of what you do. If it doesn't work there as well then we would need to have access to the models/textures so we can reproduce it for debugging.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Atraitus
Posts: 16
Joined: Sun Jan 02, 2011 11:44 pm

Post by Atraitus »

Huh, it loaded up fine in the meshviewer. Must be my code, then. Here's what I've got:

Code: Select all

        scene::IMeshSceneNode* node = smgr->addMeshSceneNode(smgr->getMesh("tree.obj"),0,-1,core::vector3df(1922.f,360.f,450.f),
                                                             core::vector3df(0.f,0.f,0.f),core::vector3df(75.0f,75.0f,75.0f));

        node->setMaterialFlag(video::EMF_LIGHTING, false);
        node->setMaterialType(EMT_TRANSPARENT_ALPHA_CHANNEL);
        node->setMaterialTexture(0,driver->getTexture("leaf.png"));
        node->setMaterialTexture(1,driver->getTexture("bark.jpg"));
That's everything that has anything to do with the loading of the model/materials/textures. If I load without calling setMaterialTexture, I get an untextured model.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Don't set the textures, and don't set the material type. Only disable lighting.
Atraitus
Posts: 16
Joined: Sun Jan 02, 2011 11:44 pm

Post by Atraitus »

I commented out the setMaterialType and setMaterialTexture functions, and it gave me an untextured model. I'm now down to just the following:

Code: Select all

        scene::IMeshSceneNode* node = smgr->addMeshSceneNode(smgr->getMesh("tree.obj"),0,-1,core::vector3df(1922.f,360.f,450.f),
                                                             core::vector3df(0.f,0.f,0.f),core::vector3df(75.0f,75.0f,75.0f));

        node->setMaterialFlag(video::EMF_LIGHTING, false);
Still not displaying any textures, although the console does state that both are loading.
Mag-got
Posts: 42
Joined: Tue Dec 04, 2007 5:53 pm

Post by Mag-got »

Post link to .obj and .mtl file?
Atraitus
Posts: 16
Joined: Sun Jan 02, 2011 11:44 pm

Post by Atraitus »

Sure, here it is:

http://www.mediafire.com/?fqtc905k2v78162

All it is is the first tree from the Yo Frankie pack on OpenGameArt, I just exported pretty much as-is.
Mag-got
Posts: 42
Joined: Tue Dec 04, 2007 5:53 pm

Post by Mag-got »

You have 2 objects in your object file, checked COBJMeshFileLoader.cpp and looks like irrlicht doesn't support multiple objects in one object file.

So try joining your meshes in blender
Post Reply