syoyo / tinyobjloader-c

Header only tiny wavefront .obj loader in pure C99
423 stars 60 forks source link

Loading materials #4

Open latencyhiding opened 7 years ago

latencyhiding commented 7 years ago

Not an issue per se, but I just wanted to get an update on some of the contributors' plans for completing this.

Scanning through the code, it seems like the thing that needs to be done to wrap up the material loading is mapping material IDs for each face. Is it worth implementing a hashmap right in the file, or should it suffice to just keep a list of hashed material names that you search through linearly?

syoyo commented 7 years ago

Thanks for pointing out.

As you noticed,

https://github.com/syoyo/tinyobjloader-c/blob/master/tinyobj_loader_c.h#L1081

Updating material_id is TODO because I was a bit troublesome to implement string-to-int hash function :-)

The best way is to implement (portable) string hash table like this,

http://stackoverflow.com/questions/20462826/hash-function-for-strings-in-c

But for a while, simple linear search should be enough since the number of materials are usually small(~100)

latencyhiding commented 7 years ago

I haven't very fully tested it yet but I made a pull request at https://github.com/syoyo/tinyobjloader-c/pull/5 where I implemented a simple hashtable with quadratic probing and added it to parse_obj and parse_mtl (I renamed parse_mtl to parse_and_index_mtl, which fills a hash table with the material names and their indices and their names, and created parse_mtl which calls parse_and_index without providing a hash table (parameter is NULL)).

latencyhiding commented 7 years ago

Also I forgot to mention this before, but shouldn't tinyobj_parse_obj have the material file's data as a parameter? If tinyobj_parse_obj's intention is to allow the user to load their file however they want (hence the buf and len parameters, but no filename parameter), then perhaps it should also allow the user to feed it the data from the material file, rather than designing the function to not use fopen but then requiring it once a material filename is encountered.

I have a couple of ideas:

Again this is an API decision, would be great to get some feedback!

syoyo commented 7 years ago

Thanks for hashing feature!

For API, yes, you can implement callback based API like tinyobj_parse_obj_with_callbacks

Please see example in C++ version: https://github.com/syoyo/tinyobjloader/blob/master/tiny_obj_loader.h#L316

Callback based API also make it possible to read .mtl data from memory.

syoyo commented 7 years ago

So, possible API design is

tinyobj_parse_obj_with_mtl_callback

I will consider to rewrite tinyobj_parse_and_index_mtl_file so that it could read .mtl data from user supplied callback function and memory.