Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Presumably broken hashmap for material listing #37

Closed
iyenal opened this issue Apr 26, 2020 · 6 comments
Closed

Presumably broken hashmap for material listing #37

iyenal opened this issue Apr 26, 2020 · 6 comments

Comments

@iyenal
Copy link

iyenal commented Apr 26, 2020

Hi,

Well firstly I am very grateful to the developers of the C version of Tinyobjloader, and Tinyobjloader itself being a real lifesaver.
So I've been experimenting the library for my own usage, and recently started to add materials support to my program. However, it looks like the Hashmap implementation that handles the material IDs is broken, as it firstly work well with like 1500 triangles, but then returns a large random number that doesn't seem correct.

It happened on 2 different models, with relatively low numbers of materials (2 and 6), and so by printing printf("Texture mat ID = %d\n", attrib.material_ids[face_offset + 1]); on every triangle, and at some (random) point in a middle of a shape, the material_id starts to behave strangely:
issue.png

I hope this can be fixed, and I would to thank you very much for all what have been already done :)

@syoyo
Copy link
Owner

syoyo commented Apr 26, 2020

Hashmap implementation that handles the material IDs is broken, as it firstly work well with like 1500 triangles,

Could you post reproducible .obj(and .mtl) file?

@iyenal
Copy link
Author

iyenal commented Apr 26, 2020

Hi,
For sure, you'll find both of them in this archive:
https://mega.nz/file/HQslFYrb#SnVobW74RlGAlqWzHeyw4sM6KGGi57KZPkxgiUWcqds
The models are exported from Blender, and it happens with the model without materials too. Thanks so much for looking in that :)

@syoyo
Copy link
Owner

syoyo commented Apr 27, 2020

I have tested with your .obj, but material_ids works well.

printf("Texture mat ID = %d\n", attrib.material_ids[face_offset + 1])

You should not use face_offset to access material_ids since material id is provided per-face manner. With address sanitizer, you'll get an out-of-bounds access error.

I have added an comment how to access material_ids

/* You can access per-face material through attrib.material_ids[i] */

@iyenal
Copy link
Author

iyenal commented Apr 29, 2020

I have checked that solution, but I get weird results.
I splitted the vertex buffer on multiple ones for each shape, and each shape have its own material. However inside 1 shape, I could get multiple Material IDs, which shouldn't be possible.

@syoyo
Copy link
Owner

syoyo commented May 4, 2020

I could get multiple Material IDs

.obj can have multiple material ids(per-face material). If you use tinyobjloader-c for OpenGL/DirectX application, you first need to re-arrange vertex data by iterating material ids.

@syoyo syoyo closed this as completed May 4, 2020
@iyenal
Copy link
Author

iyenal commented May 8, 2020

Got it, thanks. For now so using a workaround to instead to get per-face material IDs, use per-object materials by merging them before-hand.
Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants