Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@hb3p8
Created February 7, 2020 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hb3p8/b45c6da291c79f7500b5664b209b9419 to your computer and use it in GitHub Desktop.
Save hb3p8/b45c6da291c79f7500b5664b209b9419 to your computer and use it in GitHub Desktop.
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