Example #1
0
void OBJWriter::write_mesh(Mesh& mesh) {
    using namespace OBJWriterHelper;
    std::ofstream fout(m_filename.c_str());
    if (!is_anonymous()) {
        fout << "# Generated with PyMesh" << std::endl;
    }
    VectorF texture;
    VectorI texture_indices;
    if (mesh.has_attribute("corner_texture")) {
        texture = mesh.get_attribute("corner_texture");
        const size_t num_faces = mesh.get_num_faces();
        const size_t vertex_per_face = mesh.get_vertex_per_face();
        if (texture.size() != num_faces * vertex_per_face * 2) {
            // Texture invalid.
            texture.resize(0);
        } else {
            write_texture(fout, texture);
            texture_indices.resize(num_faces * vertex_per_face);
            for (size_t i=0; i<num_faces; i++) {
                for (size_t j=0; j<vertex_per_face; j++) {
                    texture_indices[i*vertex_per_face+j] = i*vertex_per_face+j;
                }
            }
        }
    }

    write_vertices(fout, mesh.get_vertices(), mesh.get_dim());
    write_faces(fout, mesh.get_faces(), mesh.get_vertex_per_face(),
            texture_indices);
    fout.close();
}