+ // Sort the set of vertices (to deduplicate)
+ parallel_sort(verts.begin(), verts.end(), threads);
+
+ // This vector will store triangles as sets of 3 indices
+ std::vector<GLuint> indices(tri_count*3);
+
+ // Go through the sorted vertex list, deduplicating and creating
+ // an indexed geometry representation for the triangles.
+ // Unique vertices are moved so that they occupy the first vertex_count
+ // positions in the verts array.
+ size_t vertex_count = 0;
+ for (auto v : verts)
+ {
+ if (!vertex_count || v != verts[vertex_count-1])
+ {
+ verts[vertex_count++] = v;
+ }
+ indices[v.i] = vertex_count - 1;
+ }
+ verts.resize(vertex_count);
+
+ std::vector<GLfloat> flat_verts;
+ flat_verts.reserve(vertex_count*3);
+ for (auto v : verts)
+ {
+ flat_verts.push_back(v.x);
+ flat_verts.push_back(v.y);
+ flat_verts.push_back(v.z);
+ }
+
+ return new Mesh(std::move(flat_verts), std::move(indices));
+}