vkoskiv / c-ray

c-ray is a small, simple path tracer written in C
MIT License
797 stars 44 forks source link

Crash with OBJ files from OneShape #112

Closed mattrust closed 1 year ago

mattrust commented 1 year ago

The CAD software OnShape has the option to export OBJ files. An attempt to use it causes a crash: INFO [2023-04-18 21:40:29] c-ray v0.6.3D [821a6746], © 2015-2023 Valtteri Koskivuori INFO [2023-04-18 21:40:29] 1156 bytes of input JSON loaded from file, parsing. INFO [2023-04-18 21:40:29] Loading mesh file 1/1 ASSERTION FAILED: In /home/mazze/projects/c-ray/src/utils/loaders/formats/wavefront/wavefront.c in function parseVertex on line 64, expression "line->amountOf.tokens == 4"

Here's my test file. It contains only a simple quad. onshape.zip

vkoskiv commented 1 year ago

Thanks for reporting, Matthias!

I took a look, and the parser expects a vertices to be 3-dimensional. Could you paste the wavefront text here so I could see what's going on? I could just remove that assertion, and it would just grab the first three values to get the vertex

mattrust commented 1 year ago

What do you mean with "wavefront text"? The OBJ file?

vkoskiv commented 1 year ago

Yeah, just the contents of the OBJ file. Should be just a text file if you open it in a text editor.

mattrust commented 1 year ago

File units = meters

mtllib onshape.mtl g Part 1 v 0.03 0.005 0.005 v 0 0.005 0.005 v 0 -0.005 0.005 v 0.03 -0.005 0.005 v 0 -0.005 -0.005 v 0.03 -0.005 -0.005 v 0 0.005 -0.005 v 0.03 0.005 -0.005 vn 0 0 1 vn 0 -1 -8.67362e-17 vn 0 0 -1 vn 0 1 0 vn 1 0 0 vn -1 0 0 usemtl 1.000000_0.000000_0.000000_0.000000_0.000000 o mesh0 f 1//1 2//1 3//1 f 3//1 4//1 1//1 o mesh1 f 4//2 3//2 5//2 f 5//2 6//2 4//2 o mesh2 f 6//3 5//3 7//3 f 7//3 8//3 6//3 o mesh3 f 8//4 7//4 2//4 f 2//4 1//4 8//4 o mesh4 f 8//5 1//5 4//5 f 4//5 6//5 8//5 o mesh5 f 5//6 3//6 2//6 f 2//6 7//6 5//6

vkoskiv commented 1 year ago

Just saw you included the whole zip, investigating now.

vkoskiv commented 1 year ago

I found the bug. I saw this before as well, the tokenizer gets confused when there is a trailing separator, in this case a space at the end of your v, vn and f lines. I'll try and come up with a fix.

vkoskiv commented 1 year ago

@mattrust I pushed some fixes that resolve the crashing. I also messed around with your stage.json file and set it up to show your mesh on top of a grid plane, as it's a bit hard to orient oneself when trying to set up a scene with just the JSON.

I observed that your mesh consists of several single-quad meshes, it's at a bit of a weird scale, and the origin is at the bottom of the shape instead of at the geometric center.

Here's the modified JSON, feel free to use it: https://pastebin.com/95p7e366

Thanks for reporting this! I'll work to make the wavefront test suite a bit more robust. I already added one test that now passes with these fixes, but it still needs more work. Asset loading is one of the weaker aspects of this project for the time being, as I usually am more interested in working on other things.