aboutsummaryrefslogtreecommitdiff
path: root/src/models.c
diff options
context:
space:
mode:
authorRay <raysan5@gmail.com>2018-09-17 16:56:02 +0200
committerRay <raysan5@gmail.com>2018-09-17 16:56:02 +0200
commitec5c9686b3afef5fb2c147fc9786b6890c025dd9 (patch)
tree3cf23b81c4cf04d54c7c580c8d085c1185572ee7 /src/models.c
parent3a1a4895454f33bea4c33a294050a88cf76d78f4 (diff)
downloadraylib-ec5c9686b3afef5fb2c147fc9786b6890c025dd9.tar.gz
raylib-ec5c9686b3afef5fb2c147fc9786b6890c025dd9.zip
Improved data export capabilities!
REVIEWED: ExportImage() REVIEWED: ExportMesh() ADDED: ExportWave() REMOVED: Internal funcs: SavePNG(), SaveBMP() NOTE: These changes break the API (parameters order)
Diffstat (limited to 'src/models.c')
-rw-r--r--src/models.c77
1 files changed, 43 insertions, 34 deletions
diff --git a/src/models.c b/src/models.c
index d6b93511..8a498139 100644
--- a/src/models.c
+++ b/src/models.c
@@ -664,45 +664,54 @@ void UnloadMesh(Mesh *mesh)
rlUnloadMesh(mesh);
}
-// Export mesh as an OBJ file
-void ExportMesh(const char *fileName, Mesh mesh)
+// Export mesh data to file
+void ExportMesh(Mesh mesh, const char *fileName)
{
- FILE *objFile = fopen(fileName, "wt");
+ bool success = false;
- fprintf(objFile, "# raylib Mesh OBJ exporter v1.0\n\n");
- fprintf(objFile, "# Mesh exported as triangle faces and not optimized.\n");
- fprintf(objFile, "# Vertex Count: %i\n", mesh.vertexCount);
- fprintf(objFile, "# Triangle Count: %i\n\n", mesh.triangleCount);
- fprintf(objFile, "# LICENSE: zlib/libpng\n");
- fprintf(objFile, "# Copyright (c) 2018 Ramon Santamaria (@raysan5)\n\n");
-
- fprintf(objFile, "g mesh\n");
-
- for (int i = 0, v = 0; i < mesh.vertexCount; i++, v += 3)
- {
- fprintf(objFile, "v %.2f %.2f %.2f\n", mesh.vertices[v], mesh.vertices[v + 1], mesh.vertices[v + 2]);
- }
-
- for (int i = 0, v = 0; i < mesh.vertexCount; i++, v += 2)
- {
- fprintf(objFile, "vt %.2f %.2f\n", mesh.texcoords[v], mesh.texcoords[v + 1]);
- }
-
- for (int i = 0, v = 0; i < mesh.vertexCount; i++, v += 3)
+ if (IsFileExtension(fileName, ".obj"))
{
- fprintf(objFile, "vn %.2f %.2f %.2f\n", mesh.normals[v], mesh.normals[v + 1], mesh.normals[v + 2]);
- }
-
- for (int i = 0; i < mesh.triangleCount; i += 3)
- {
- fprintf(objFile, "f %i/%i/%i %i/%i/%i %i/%i/%i\n", i, i, i, i + 1, i + 1, i + 1, i + 2, i + 2, i + 2);
+ FILE *objFile = fopen(fileName, "wt");
+
+ fprintf(objFile, "# raylib Mesh OBJ exporter v1.0\n\n");
+ fprintf(objFile, "# Mesh exported as triangle faces and not optimized.\n");
+ fprintf(objFile, "# Vertex Count: %i\n", mesh.vertexCount);
+ fprintf(objFile, "# Triangle Count: %i\n\n", mesh.triangleCount);
+ fprintf(objFile, "# LICENSE: zlib/libpng\n");
+ fprintf(objFile, "# Copyright (c) 2018 Ramon Santamaria (@raysan5)\n\n");
+
+ fprintf(objFile, "g mesh\n");
+
+ for (int i = 0, v = 0; i < mesh.vertexCount; i++, v += 3)
+ {
+ fprintf(objFile, "v %.2f %.2f %.2f\n", mesh.vertices[v], mesh.vertices[v + 1], mesh.vertices[v + 2]);
+ }
+
+ for (int i = 0, v = 0; i < mesh.vertexCount; i++, v += 2)
+ {
+ fprintf(objFile, "vt %.2f %.2f\n", mesh.texcoords[v], mesh.texcoords[v + 1]);
+ }
+
+ for (int i = 0, v = 0; i < mesh.vertexCount; i++, v += 3)
+ {
+ fprintf(objFile, "vn %.2f %.2f %.2f\n", mesh.normals[v], mesh.normals[v + 1], mesh.normals[v + 2]);
+ }
+
+ for (int i = 0; i < mesh.triangleCount; i += 3)
+ {
+ fprintf(objFile, "f %i/%i/%i %i/%i/%i %i/%i/%i\n", i, i, i, i + 1, i + 1, i + 1, i + 2, i + 2, i + 2);
+ }
+
+ fprintf(objFile, "\n");
+
+ fclose(objFile);
+
+ success = true;
}
-
- fprintf(objFile, "\n");
-
- fclose(objFile);
+ else if (IsFileExtension(fileName, ".raw")) { } // TODO: Support additional file formats to export mesh vertex data
- TraceLog(LOG_INFO, "Mesh saved: %s", fileName);
+ if (success) TraceLog(LOG_INFO, "Mesh exported successfully: %s", fileName);
+ else TraceLog(LOG_WARNING, "Mesh could not be exported.");
}
#if defined(SUPPORT_MESH_GENERATION)