summaryrefslogtreecommitdiffhomepage
path: root/src/models.c
diff options
context:
space:
mode:
authorRay <[email protected]>2018-04-04 12:00:54 +0200
committerRay <[email protected]>2018-04-04 12:00:54 +0200
commit6edf15b9f9d761536906b9d1144fc49610881ea6 (patch)
tree2fdca14608a015a92085a3719729d338b5605c3f /src/models.c
parent322cebcbaffca15f5a9117127c51ae47ba00a249 (diff)
downloadraylib-6edf15b9f9d761536906b9d1144fc49610881ea6.tar.gz
raylib-6edf15b9f9d761536906b9d1144fc49610881ea6.zip
Added funtion: ExportMesh()
Diffstat (limited to 'src/models.c')
-rw-r--r--src/models.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/models.c b/src/models.c
index f9aa1805..ae1fc968 100644
--- a/src/models.c
+++ b/src/models.c
@@ -650,6 +650,47 @@ void UnloadMesh(Mesh *mesh)
rlUnloadMesh(mesh);
}
+// Export mesh as an OBJ file
+void ExportMesh(const char *fileName, Mesh mesh)
+{
+ 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);
+
+ TraceLog(LOG_INFO, "Mesh saved: %s", fileName);
+}
+
#if defined(SUPPORT_MESH_GENERATION)
// Generate plane mesh (with subdivisions)
Mesh GenMeshPlane(float width, float length, int resX, int resZ)