tinyobjloader / tinyobjloader

Tiny but powerful single file wavefront obj loader
Other
3.3k stars 608 forks source link

Normal indices with a value of zero #348

Closed Trider12 closed 1 year ago

Trider12 commented 1 year ago

Describe the issue I know that this a strange request, but is it possible to support invalid (i.e. zero) normal indices? Currently I get the following error:

Failed parse `f' line(e.g. zero value for face index. line 70537.)

The problematic line looks like this:

f 22979/21114/0 22978/21108/3845 23110/21108/3853 23345/21115/3852 23346/21324/4037 23108/21323/3901

And it's not the only such line. In fact, it seems all the hexagons in this file have this issue. The file is generated by Cinema 4D, so it seems that their exporter is bugged (I couldn't find any easy way of reporting this issue to them).

To Reproduce Unfortunately, I can't provide a sample .obj file due to the licensing.

Expected behavior It would be nice if instead of aborting, the library provided invalid normal indices for such vertices, so I could handle these values in my code. Thanks.

struct index_t {
  int vertex_index;
  int normal_index; // -1 here, for example
  int texcoord_index;
};
syoyo commented 1 year ago

http://paulbourke.net/dataformats/obj/

The spec say nothing when normal/texcoord index is zero(even vertex, but at least it should be an error having zero for vertex index)

How Blender and meshlab handles such .obj data?

Trider12 commented 1 year ago

How Blender and meshlab handles such .obj data?

I'll test that and come back with the results.

Trider12 commented 1 year ago

I tested the model in Blender and CAD Assistant (which uses Open Cascade) and both of them imported the model fine, but the faces with the zeroed indices had incorrect shading (like the normals for that face or that particular vertex were inverted).

syoyo commented 1 year ago

@Trider12 Thanks! So Blender and other tools seems use zero normal value(0, 0, 0) or maybe 0, 0, 1 for invalid normal index.

TinyObjLoader's solution would be

PR is much appreciated!