[rmodels] Improve OBJ vertex data precision and lower memory usage by ExportMesh() #4496
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Using ExportMesh() to save a mesh as an OBJ currently creates broken models due to very low floating-point precision, both for vertex position, normals and UVs.
Here's a model I made, imported into a raylib program:
And here's the same model, first imported into the program, then exported using ExportMesh(), then imported back:
I guess vertex positions could be scaled x1000 before exporting to account for the precision loss, but neither vertex normals nor UVs can be treated the same way. I don't see any reason why these values cannot be increased; I replaced the relevant numbers with the precision values that Blender's OBJ exporter uses, which seems to give me a visually identical model to the original upon next import.
The exporter also greatly overestimates the amount of memory necessary to be allocated to store the data in, which could potentially lead to issues when attempting to export very big meshes. Wanting to lower the memory allocation to the minimum necessary, while trying to add the least amount of extra code as well as avoiding polluting the codebase with extra functions, I made a macro function declaration inside the exporter and replaced much of the code there with that new function. The code compiles and works fine in both Windows and HTML5; however I don't know if macro functions are acceptable in this project, and perhaps a different approach might be preferable to the one I ended up taking. If so, I totally understand; I'll change it as required.