Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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