jkuhlmann / cgltf

:diamond_shape_with_a_dot_inside: Single-file glTF 2.0 loader and writer written in C99
MIT License
1.42k stars 135 forks source link

Incorrect parsing of numeric values from glb file #250

Closed native-engine closed 4 months ago

native-engine commented 4 months ago

When importing models from a glb file, properties cgltf_float of class cgltf_accessor such as 0.9999997019767761 and 0.9999993443489075 are parsed as 0, and -0.9999990463256836 as -0:

Screenshot_20240329_145055

I already wrote to Khronos developers about why their plugin exports 1 and -1 values from Blender 3D so incorrectly, but they replied that this is a flaw in the JSON parser, not in their plugin.

I'm attaching some of my code:

if (data->accessors[j + iBufferView].has_min) ParsingMesh->vMin = glm::vec3(data->accessors[j + iBufferView].min[0], data->accessors[j + iBufferView].min[1], data->accessors[j + iBufferView].min[2]);
if (data->accessors[j + iBufferView].has_max) ParsingMesh->vMax = glm::vec3(data->accessors[j + iBufferView].max[0], data->accessors[j + iBufferView].max[1], data->accessors[j + iBufferView].max[2]);

and the textual analog of its glb file: model.gltf.json.

P.S. Parser TinyGLTF exports all numeric values correctly, but: https://github.com/syoyo/tinygltf/issues/483...

zeux commented 4 months ago

My guess would be that you're not using the English locale; cgltf uses stdlib functions like strtod for parsing, which are locale sensitive. Normally the locale would default to C but maybe something in your application is setting it to system; using setlocale(LC_ALL, "C"); before calling cgltf_parse could fix this.

native-engine commented 4 months ago

Thank you very much!

Screenshot_20240404_105851