Skip to content

Commit 5f660b6

Browse files
committed
Added an OBJ file saving feature to Mesh
1 parent 8199141 commit 5f660b6

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Core/Contents/Include/PolyMesh.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ namespace Polycode {
381381

382382
static Vector3 calculateFaceTangent(const Vector3 &v1, const Vector3 &v2, const Vector3 &v3, const Vector2 &texCoord1, const Vector2 &texCoord2, const Vector2 &texCoord3);
383383

384+
void saveAsOBJ(const String fileName);
385+
384386
void normalizeBoneWeights();
385387

386388
VertexDataArray vertexPositionArray;

Core/Contents/Source/PolyMesh.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,40 @@ void Mesh::calculateNormals() {
13201320
}
13211321
}
13221322

1323+
void Mesh::saveAsOBJ(const String fileName) {
1324+
FILE *f = fopen(fileName.c_str(), "w");
1325+
1326+
if (!f) {
1327+
return;
1328+
}
1329+
1330+
char buffer[256];
1331+
1332+
for(int i=0; i < vertexPositionArray.data.size()-2; i += 3) {
1333+
sprintf(buffer, "v %f %f %f\n", vertexPositionArray.data[i], vertexPositionArray.data[i+1], vertexPositionArray.data[i+2]);
1334+
fputs(buffer, f);
1335+
}
1336+
1337+
for(int i=0; i < vertexTexCoordArray.data.size()-1; i += 2) {
1338+
sprintf(buffer, "vt %f %f\n", vertexTexCoordArray.data[i], vertexTexCoordArray.data[i+1]);
1339+
fputs(buffer, f);
1340+
}
1341+
1342+
1343+
for(int i=0; i < vertexNormalArray.data.size()-2; i += 3) {
1344+
sprintf(buffer, "vn %f %f %f\n", vertexNormalArray.data[i], vertexNormalArray.data[i+1], vertexNormalArray.data[i+2]);
1345+
fputs(buffer, f);
1346+
}
1347+
1348+
1349+
for(int i=0; i < indexArray.data.size()-2; i += 3) {
1350+
sprintf(buffer, "f %d %d %d\n", indexArray.data[i]+1, indexArray.data[i+1]+1, indexArray.data[i+2]+1);
1351+
fputs(buffer, f);
1352+
}
1353+
1354+
fclose(f);
1355+
}
1356+
13231357
int Mesh::getMeshType() {
13241358
return meshType;
13251359
}

0 commit comments

Comments
 (0)