From b7257d7b793c66969021da056d8783dc6b1111a8 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Mon, 27 Feb 2023 17:07:10 +0200 Subject: [PATCH] updated files from meshfrac_alt --- src/cmesh.c | 34 ++++++++++++++++++++++++++++++---- src/cmesh.h | 7 +++++++ src/meshgen.c | 2 +- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/cmesh.c b/src/cmesh.c index 249c69c..f686b5c 100644 --- a/src/cmesh.c +++ b/src/cmesh.c @@ -33,7 +33,7 @@ struct cmesh { char *name; unsigned int nverts, nfaces; - struct submesh *sublist; + struct submesh *sublist, *subtail; int subcount; /* current value for each attribute for the immediate mode interface */ @@ -319,6 +319,7 @@ static int clone(struct cmesh *cmdest, const struct cmesh *cmsrc, struct submesh } cmdest->sublist = head; + cmdest->subtail = tail; cmdest->subcount = cmsrc->subcount; } @@ -724,6 +725,7 @@ void cmesh_clear_submeshes(struct cmesh *cm) free(sm); } cm->subcount = 0; + cm->subtail = 0; } int cmesh_submesh(struct cmesh *cm, const char *name, int fstart, int fcount) @@ -763,8 +765,13 @@ int cmesh_submesh(struct cmesh *cm, const char *name, int fstart, int fcount) sm->vcount = fcount * 3; } - sm->next = cm->sublist; - cm->sublist = sm; + sm->next = 0; + if(cm->sublist) { + cm->subtail->next = sm; + cm->subtail = sm; + } else { + cm->sublist = cm->subtail = sm; + } cm->subcount++; return 0; } @@ -791,10 +798,18 @@ int cmesh_remove_submesh(struct cmesh *cm, int idx) free(sm->name); free(sm); + if(cm->subtail == sm) { + cm->subtail = prev; + } + cm->subcount--; assert(cm->subcount >= 0); - cm->sublist = dummy.next; + if(!dummy.next) { + cm->sublist = cm->subtail = 0; + } else { + cm->sublist = dummy.next; + } return 0; } @@ -1568,6 +1583,7 @@ int cmesh_dump_obj_file(const struct cmesh *cm, FILE *fp, int voffs) static const char *fmtstr[] = {" %u", " %u//%u", " %u/%u", " %u/%u/%u"}; int i, j, num, nelem; unsigned int aflags = 0; + const struct submesh *sub = cm->sublist; if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) { return -1; @@ -1614,6 +1630,11 @@ int cmesh_dump_obj_file(const struct cmesh *cm, FILE *fp, int voffs) assert(numidx % 3 == 0); for(i=0; iistart <= *idxptr) { + fprintf(fp, "o %s\n", sub->name); + sub = sub->next; + } + fputc('f', fp); for(j=0; j<3; j++) { unsigned int idx = *idxptr++ + 1 + voffs; @@ -1625,6 +1646,11 @@ int cmesh_dump_obj_file(const struct cmesh *cm, FILE *fp, int voffs) int numtri = cm->nverts / 3; unsigned int idx = 1 + voffs; for(i=0; ivstart == idx - 1) { + fprintf(fp, "o %s\n", sub->name); + sub = sub->next; + } + fputc('f', fp); for(j=0; j<3; j++) { fprintf(fp, fmtstr[aflags], idx, idx, idx); diff --git a/src/cmesh.h b/src/cmesh.h index 9c12d01..149e188 100644 --- a/src/cmesh.h +++ b/src/cmesh.h @@ -19,6 +19,10 @@ enum { struct cmesh; +#ifdef __cplusplus +extern "C" { +#endif + /* global state */ void cmesh_set_attrib_sdrloc(int attr, int loc); int cmesh_get_attrib_sdrloc(int attr); @@ -134,6 +138,9 @@ int cmesh_dump_file(const struct cmesh *cm, FILE *fp); int cmesh_dump_obj(const struct cmesh *cm, const char *fname); int cmesh_dump_obj_file(const struct cmesh *cm, FILE *fp, int voffs); +#ifdef __cplusplus +} +#endif #endif /* CMESH_H_ */ diff --git a/src/meshgen.c b/src/meshgen.c index cbce81c..b9e394f 100644 --- a/src/meshgen.c +++ b/src/meshgen.c @@ -285,9 +285,9 @@ void gen_torus(struct cmesh *mesh, float mainrad, float ringrad, int usub, int v *idxarr++ = idx + 1; *idxarr++ = idx + vverts + 1; + *idxarr++ = idx + vverts; *idxarr++ = idx; *idxarr++ = idx + vverts + 1; - *idxarr++ = idx + vverts; } v += dv; -- 1.7.10.4