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

[rmodels] Improve OBJ vertex data precision and lower memory usage by ExportMesh() #4496

Merged
merged 5 commits into from
Nov 19, 2024

Conversation

mikeemm
Copy link
Contributor

@mikeemm mikeemm commented Nov 15, 2024

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:

20241107_195444

And here's the same model, first imported into the program, then exported using ExportMesh(), then imported back:

20241107_195546

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.

@raysan5
Copy link
Owner

raysan5 commented Nov 16, 2024

@mikeemm I prefer to keep the same function structure as other similar functions and avoid that TEXT_APPEND macro. I the problem is the scale of the buffer, incrementing its size to fit more decimals would be enough, no need to redesign all the function.

@mikeemm mikeemm force-pushed the obj_export_revisited branch from 3788d6f to 7d46047 Compare November 16, 2024 13:57
@mikeemm
Copy link
Contributor Author

mikeemm commented Nov 16, 2024

@raysan5 I understand. I have reverted the macro changes and modified the initial allocation instead, please take a look whenever you'd like.

@raysan5 raysan5 merged commit 95e7664 into raysan5:master Nov 19, 2024
14 checks passed
@raysan5
Copy link
Owner

raysan5 commented Nov 19, 2024

@mikeemm Nice! Thanks for the review!

@mikeemm mikeemm deleted the obj_export_revisited branch November 19, 2024 10:17
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

Successfully merging this pull request may close these issues.

2 participants