Skip to content

Commit

Permalink
[rmodels] Improve OBJ vertex data precision and lower memory usage by…
Browse files Browse the repository at this point in the history
… ExportMesh() (raysan5#4496)

* increased vertex data precision to match blender exports

* modified memory allocation to better fit higher precision data

* now accounting for newline characters during allocation

* now accounting for potentially higher vertex counts

* removed unnecessary final newline
  • Loading branch information
mikeemm authored Nov 19, 2024
1 parent bd706df commit 95e7664
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/rmodels.c
Original file line number Diff line number Diff line change
Expand Up @@ -1943,13 +1943,14 @@ bool ExportMesh(Mesh mesh, const char *fileName)
if (IsFileExtension(fileName, ".obj"))
{
// Estimated data size, it should be enough...
int dataSize = mesh.vertexCount*(int)strlen("v 0000.00f 0000.00f 0000.00f") +
mesh.vertexCount*(int)strlen("vt 0.000f 0.00f") +
mesh.vertexCount*(int)strlen("vn 0.000f 0.00f 0.00f") +
mesh.triangleCount*(int)strlen("f 00000/00000/00000 00000/00000/00000 00000/00000/00000");
int vc = mesh.vertexCount;
int dataSize = vc*(int)strlen("v -0000.000000f -0000.000000f -0000.000000f\n") +
vc*(int)strlen("vt -0.000000f -0.000000f\n") +
vc*(int)strlen("vn -0.0000f -0.0000f -0.0000f\n") +
mesh.triangleCount*snprintf(NULL, 0, "f %i/%i/%i %i/%i/%i %i/%i/%i\n", vc, vc, vc, vc, vc, vc, vc, vc, vc);

// NOTE: Text data buffer size is estimated considering mesh data size
char *txtData = (char *)RL_CALLOC(dataSize*2 + 2000, sizeof(char));
char *txtData = (char *)RL_CALLOC(dataSize + 1000, sizeof(char));

int byteCount = 0;
byteCount += sprintf(txtData + byteCount, "# //////////////////////////////////////////////////////////////////////////////////\n");
Expand All @@ -1969,17 +1970,17 @@ bool ExportMesh(Mesh mesh, const char *fileName)

for (int i = 0, v = 0; i < mesh.vertexCount; i++, v += 3)
{
byteCount += sprintf(txtData + byteCount, "v %.2f %.2f %.2f\n", mesh.vertices[v], mesh.vertices[v + 1], mesh.vertices[v + 2]);
byteCount += sprintf(txtData + byteCount, "v %.6f %.6f %.6f\n", mesh.vertices[v], mesh.vertices[v + 1], mesh.vertices[v + 2]);
}

for (int i = 0, v = 0; i < mesh.vertexCount; i++, v += 2)
{
byteCount += sprintf(txtData + byteCount, "vt %.3f %.3f\n", mesh.texcoords[v], mesh.texcoords[v + 1]);
byteCount += sprintf(txtData + byteCount, "vt %.6f %.6f\n", mesh.texcoords[v], mesh.texcoords[v + 1]);
}

for (int i = 0, v = 0; i < mesh.vertexCount; i++, v += 3)
{
byteCount += sprintf(txtData + byteCount, "vn %.3f %.3f %.3f\n", mesh.normals[v], mesh.normals[v + 1], mesh.normals[v + 2]);
byteCount += sprintf(txtData + byteCount, "vn %.4f %.4f %.4f\n", mesh.normals[v], mesh.normals[v + 1], mesh.normals[v + 2]);
}

if (mesh.indices != NULL)
Expand All @@ -2000,8 +2001,6 @@ bool ExportMesh(Mesh mesh, const char *fileName)
}
}

byteCount += sprintf(txtData + byteCount, "\n");

// NOTE: Text data length exported is determined by '\0' (NULL) character
success = SaveFileText(fileName, txtData);

Expand Down

0 comments on commit 95e7664

Please sign in to comment.