Comments (4)
@syoyo Glad to hear it!
@iyenal I have a folder full of obj
and corresponding mtl
files, so I think the original route of finding the mtl
from the location of the obj
would be best for me. Thanks anyways!
I ended up hacking something together in which the user can supply the filename of the obj
, extract the directory from that, and append the relative path onto it. Since I'll always supply the filename, I didn't bother (yet) having any error checks, so the code is likely brittle to anyone that doesn't have my exact use-case.
I appreciate the decoupling of file reading and .obj
parsing that the current API has, and so I don't think my current solution is fit for integration in the library. This might be what @syoyo is suggesting, but I think a better solution is to have tinyobj_parrse_obj
take a char *(*cb)(const char *rel, void *param)
and a void *param
, where cb
would take the relative filename and a pointer to some user data (param
, for local state) and return a char *
to the full text, or something like this?
If anyone finds this useful, these are my changes:
457c457
< const char *p = (const char *) memchr(s, 0, n);
---
> const char *p = memchr(s, 0, n);
1208,1212d1207
< int tinyobj_parse_obj_with_filename(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes,
< size_t *num_shapes, tinyobj_material_t **materials_out,
< size_t *num_materials_out, const char *buf, size_t len,
< const char *filename, unsigned int flags);
<
1217,1223d1211
< return tinyobj_parse_obj_with_filename(attrib, shapes, num_shapes, materials_out, num_materials_out, buf, len, NULL, flags);
< }
<
< int tinyobj_parse_obj_with_filename(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes,
< size_t *num_shapes, tinyobj_material_t **materials_out,
< size_t *num_materials_out, const char *buf, size_t len,
< const char *filename, unsigned int flags) {
1329c1317
< char *relative_filename = my_strndup(commands[mtllib_line_index].mtllib_name,
---
> char *filename = my_strndup(commands[mtllib_line_index].mtllib_name,
1331,1341d1318
< char buffer[1024];
< int filename_len = strlen(filename);
< memcpy(buffer, filename, filename_len);
< char *sentinel = buffer + filename_len;
< while (buffer < sentinel && *sentinel != '/')
< sentinel--;
< if (*sentinel == '/')
< sentinel++;
< int rel_len = commands[mtllib_line_index].mtllib_name_len;
< memcpy(sentinel, relative_filename, rel_len);
< sentinel[rel_len] = 0;
1343c1320
< int ret = tinyobj_parse_and_index_mtl_file(&materials, &num_materials, buffer, &material_table);
---
> int ret = tinyobj_parse_and_index_mtl_file(&materials, &num_materials, filename, &material_table);
1347c1324
< fprintf(stderr, "TINYOBJ: Failed to parse material file '%s': %d\n", buffer, ret);
---
> fprintf(stderr, "TINYOBJ: Failed to parse material file '%s': %d\n", filename, ret);
1350c1327
< TINYOBJ_FREE(relative_filename);
---
> TINYOBJ_FREE(filename);
1381a1359,1372
> /* @todo
> if (commands[t][i].material_name &&
> commands[t][i].material_name_len > 0) {
> std::string material_name(commands[t][i].material_name,
> commands[t][i].material_name_len);
>
> if (material_map.find(material_name) != material_map.end()) {
> material_id = material_map[material_name];
> } else {
> // Assign invalid material ID
> material_id = -1;
> }
> }
> */
from tinyobjloader-c.
It's just an unimplemented feature.
We need to implement a feature to supply search path to .mtl
file or a callback function for mtllib
to tinyobj_parse_obj
API.
PR is welcome!
from tinyobjloader-c.
@martinhath If you would like a solution to link object's materials to mtllib materials, I created a solution to instead to get the object name, get its material name, and use that material name to then look for its corresponding texture. Let me know if you would like me to upload it here :)
from tinyobjloader-c.
We can implement both.
One is supply both filename(for the use of .mtl search) and its content, something like what libjsonnet's evaluateSnippet does:
And another is supply callback functions for resource access(.obj, .mtl, etc)
from tinyobjloader-c.
Related Issues (20)
- Last face ignored if it's the last line in the file HOT 2
- Clearify num_face and num_face_num_verts variable HOT 3
- carriage return when read diffuse texture name HOT 5
- [TODO] Support reading line larger than 8192 bytes HOT 3
- Windows version of map file HOT 3
- Negative exponents not handled by tryParseDouble
- Heap buffer overflow in my_strndup HOT 1
- Failed to initialize GLEW. (linux) HOT 1
- Material name corruption in tinyobj_parse_obj HOT 1
- Swapped values in returned attribs ? HOT 3
- Presumably broken hashmap for material listing HOT 6
- tinyobj_parse_obj fails to read .mtl file HOT 4
- Maybe use sscanf? HOT 1
- Issue with hashmap algorithm and probably more when loading certain models. HOT 8
- Is this still active?
- Memory leaked after tinyobj_parse_obj do file_reader HOT 5
- obj to gltf? HOT 1
- Memory leak in tinyobj_parse_and_index_mtl_file HOT 2
- Infinite loop in hash table inserting when reading mtl file HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tinyobjloader-c.