🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Using tinyobjloader library - material file is not loaded when loading object

Started by
3 comments, last by VanillaSnake21 4 years, 1 month ago

I'm using tinyobjloader (https://github.com/tinyobjloader/tinyobjloader)​ library to load my Wavefront Object files, however there is a bit of an issue when it comes to loading the “material” .mtl file that contains the texture file paths for the textures that the object uses. I'm not exactly sure what the problem is, but I seem to be using everything correctly. Can anyone find the issue here, or recommend a different library?

In the code below I give the library the name of the object I'm loading and I also give it the base directory where all the material files are located (it's the same directory as my .obj files). After the LoadObj function returns, I have a valid “shapes” node, meaning the object was loaded but the “materials” is at zero. I also don't have any errors or warnings returned.

	//load a test object
	std::string inputfile = "C:/Users/Tim/Documents/Visual Studio 2017/Projects/HardwareRasterizer/HardwareRasterizer/Objects/boxobj.obj";
	
	std::string base_dir = "C:/Users/Tim/Documents/Visual Studio 2017/Projects/HardwareRasterizer/HardwareRasterizer/Objects/"
	
	tinyobj::attrib_t attrib;
	std::vector<tinyobj::shape_t> shapes;
	std::vector<tinyobj::material_t> materials;

	std::string warn;
	std::string err;

	struct object
	{
		Vertex::PosNormTex*		  vertexList;
		int numVertices;
		std::vector<unsigned int> indexList;
	};


	bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, inputfile.c_str(), base_dir.c_str());

	if (!warn.empty()) {

	}

	if (!err.empty()) {

	}

	if (!ret) {

	}

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Advertisement

Funny, i've just dabbled with tinyobj but decided to not pursue that further and in favour of assimp. I only loaded obj files without separate materials so can't help with thet specific problem. But i realize that you're using tinyobj 0.9 (because of the seperate warn and err strings). If you try tinyobj 1.0, maybe the problem then disappears ?

Sorry if i can't help more specific …

Green_Baron said:

Funny, i've just dabbled with tinyobj but decided to not pursue that further and in favour of assimp. I only loaded obj files without separate materials so can't help with thet specific problem. But i realize that you're using tinyobj 0.9 (because of the seperate warn and err strings). If you try tinyobj 1.0, maybe the problem then disappears ?

Sorry if i can't help more specific …

I just re-downloaded the repo from GitHub just to be sure, but the LoadObject function still has separate warn, err strings that I have to pass in, maybe I'm cloning the wrong repo? I'm pulling the master.

After more digging I see that the LoadObj function is listed under “legacy API” however it's confusing because all the samples still use LoadObj files, also the example on the main GitHub page also uses LoadObj the same way that I use it in my code. I'm not sure what to do. I'm going to try the assimp library you suggested in the meantime.

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

I found the issue. After going through the source of the library I noticed it's looking for “mtllib” token to be specified in the .obj file before loading the .mtl file. After checking my .obj file it seems like there was no such token. It turns out that that particular object I was using wasn't exported from Maya with materials. Re-exported it properly and it worked. Thanks.

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

This topic is closed to new replies.

Advertisement