Created
February 7, 2020 17:30
-
-
Save hb3p8/b45c6da291c79f7500b5664b209b9419 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::ofstream file ("Mesh.obj"); | |
// Every vertex is a line in output file: | |
// v pos_x pos_y pos_z | |
for (int i = 0; i < mesh->numVertices(); ++i) { | |
file << "v " << mesh->vertices()[i].x() << " " | |
<< mesh->vertices()[i].y() << " " | |
<< mesh->vertices()[i].z() << std::endl; | |
} | |
// Every polygon is a line in output file: | |
// f vert_idx1 vert_idx2 ... vert_idxn | |
// WARNING: In OBJ vertex indices start from 1 ("+ 1" in the code below) | |
// Here 3 indices for each triangle | |
for (int i = 0; i < mesh->numElements(); ++i) { | |
file << "f " << (mesh->elements()[i][0] + 1) << " " | |
<< (mesh->elements()[i][1] + 1) << " " | |
<< (mesh->elements()[i][2] + 1) << std::endl; | |
} | |
// The output file for a single triangle could look like: | |
// v 0.0 0.0 0.0 | |
// v 0.0 1.0 0.0 | |
// v 0.0 0.0 1.0 | |
// f 1 2 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment