+ dynarr_free(polys);
+ return 0;
+}
+
+static int mesh_poly(struct poly *poly, const struct cmesh *mesh, int faceidx)
+{
+ int i, vsz, nsz, uvsz;
+ struct vertex vert, *tmpvert;
+ const float *varr, *narr, *uvarr;
+ unsigned int vidx;
+
+ if(init_poly(poly) == -1) {
+ return -1;
+ }
+
+ varr = cmesh_attrib_ro(mesh, CMESH_ATTR_VERTEX);
+ narr = cmesh_attrib_ro(mesh, CMESH_ATTR_NORMAL);
+ uvarr = cmesh_attrib_ro(mesh, CMESH_ATTR_TEXCOORD);
+ vsz = cmesh_attrib_nelem(mesh, CMESH_ATTR_VERTEX);
+ nsz = cmesh_attrib_nelem(mesh, CMESH_ATTR_NORMAL);
+ uvsz = cmesh_attrib_nelem(mesh, CMESH_ATTR_TEXCOORD);
+
+ for(i=0; i<3; i++) {
+ if(cmesh_indexed(mesh)) {
+ vidx = cmesh_index_ro(mesh)[faceidx * 3 + i];
+ } else {
+ vidx = faceidx * 3 + i;
+ }
+ vert.pos.x = varr[vidx * vsz];
+ vert.pos.y = varr[vidx * vsz + 1];
+ vert.pos.z = varr[vidx * vsz + 2];
+ vert.norm.x = narr[vidx * nsz];
+ vert.norm.y = narr[vidx * nsz + 1];
+ vert.norm.z = narr[vidx * nsz + 2];
+ vert.uv.x = uvarr[vidx * uvsz];
+ vert.uv.y = uvarr[vidx * uvsz + 1];
+
+ if(!(tmpvert = dynarr_push(poly->verts, &vert))) {
+ destroy_poly(poly);
+ return -1;
+ }
+ poly->verts = tmpvert;
+ }
+ return 0;
+}
+
+#define ADD_VERTEX(mesh, vert) \
+ do { \
+ cmesh_normal(mesh, (vert)->norm.x, (vert)->norm.y, (vert)->norm.z); \
+ cmesh_texcoord(mesh, (vert)->uv.x, (vert)->uv.y, 0); \
+ if(cmesh_vertex(mesh, (vert)->pos.x, (vert)->pos.y, (vert)->pos.z) == -1) { \
+ fprintf(stderr, "SKATA\n"); \
+ abort(); \
+ } \
+ } while(0)
+
+static int build_cell_shell(struct cmesh *mesh, const struct cmesh *orig,
+ const struct frac_cell *cell)
+{
+ int i, j, k, nfaces, clipres;
+ struct plane plane;
+ struct poly poly, clipped;
+ struct vertex *vert;
+
+ nfaces = cmesh_poly_count(orig);
+ for(i=0; i<nfaces; i++) {
+ if(mesh_poly(&poly, orig, i) == -1) {
+ cmesh_destroy(mesh);
+ return -1;
+ }
+
+ for(j=0; j<cell->num_polys; j++) {
+ poly_plane(cell->polys + j, &plane);
+
+ init_poly(&clipped);
+ if((clipres = clip_poly(&clipped, &poly, &plane)) < 0) {
+ destroy_poly(&clipped);
+ break;
+ }
+ destroy_poly(&poly);
+ poly = clipped;
+ }
+
+ if(j < cell->num_polys) {
+ continue;
+ }
+
+ vert = poly.verts + 1;
+ for(k=0; k<dynarr_size(poly.verts)-2; k++) {
+ ADD_VERTEX(mesh, poly.verts);
+ ADD_VERTEX(mesh, vert); vert++;
+ ADD_VERTEX(mesh, vert);
+ }
+ destroy_poly(&poly);
+ }