--- ray/src/common/mesh.c 2003/06/20 00:25:49 2.8 +++ ray/src/common/mesh.c 2020/07/14 23:13:50 2.34 @@ -1,11 +1,14 @@ #ifndef lint -static const char RCSid[] = "$Id: mesh.c,v 2.8 2003/06/20 00:25:49 greg Exp $"; +static const char RCSid[] = "$Id: mesh.c,v 2.34 2020/07/14 23:13:50 greg Exp $"; #endif /* * Mesh support routines */ -#include "standard.h" +#include "rtio.h" +#include "rtmath.h" +#include "rterror.h" +#include "paths.h" #include "octree.h" #include "object.h" #include "otypes.h" @@ -27,9 +30,9 @@ static MESH *mlist = NULL; /* list of loaded meshes * static unsigned long -cvhash(cvp) /* hash an encoded vertex */ -MCVERT *cvp; +cvhash(const char *p) /* hash an encoded vertex */ { + const MCVERT *cvp = (const MCVERT *)p; unsigned long hval; if (!(cvp->fl & MT_V)) @@ -44,9 +47,10 @@ MCVERT *cvp; static int -cvcmp(v1, v2) /* compare encoded vertices */ -register MCVERT *v1, *v2; +cvcmp(const char *vv1, const char *vv2) /* compare encoded vertices */ { + const MCVERT *v1 = (const MCVERT *)vv1, *v2 = (const MCVERT *)vv2; + if (v1->fl != v2->fl) return(1); if (v1->xyz[0] != v2->xyz[0]) @@ -68,23 +72,21 @@ register MCVERT *v1, *v2; MESH * -getmesh(mname, flags) /* get mesh data */ -char *mname; -int flags; +getmesh( /* get new mesh data reference */ + char *mname, + int flags +) { char *pathname; - register MESH *ms; + MESH *ms; flags &= IO_LEGAL; for (ms = mlist; ms != NULL; ms = ms->next) if (!strcmp(mname, ms->name)) { - if ((ms->ldflags & flags) == flags) { - ms->nref++; - return(ms); /* loaded */ - } - break; /* load the rest */ + ms->nref++; /* increase reference count */ + break; } - if (ms == NULL) { + if (ms == NULL) { /* load first time */ ms = (MESH *)calloc(1, sizeof(MESH)); if (ms == NULL) error(SYSTEM, "out of memory in getmesh"); @@ -96,7 +98,7 @@ int flags; } if ((pathname = getpath(mname, getrlibpath(), R_OK)) == NULL) { sprintf(errmsg, "cannot find mesh file \"%s\"", mname); - error(USER, errmsg); + error(SYSTEM, errmsg); } flags &= ~ms->ldflags; if (flags) @@ -106,11 +108,12 @@ int flags; MESHINST * -getmeshinst(o, flags) /* create mesh instance */ -OBJREC *o; -int flags; +getmeshinst( /* create mesh instance */ + OBJREC *o, + int flags +) { - register MESHINST *ins; + MESHINST *ins; flags &= IO_LEGAL; if ((ins = (MESHINST *)o->os) == NULL) { @@ -128,19 +131,55 @@ int flags; ins->msh = NULL; o->os = (char *)ins; } - if (ins->msh == NULL || (ins->msh->ldflags & flags) != flags) + if (ins->msh == NULL) ins->msh = getmesh(o->oargs.sarg[0], flags); + else if ((flags &= ~ins->msh->ldflags)) + readmesh(ins->msh, + getpath(o->oargs.sarg[0], getrlibpath(), R_OK), + flags); return(ins); } int -getmeshtrivid(tvid, mo, mp, ti) /* get triangle vertex ID's */ -int32 tvid[3]; -OBJECT *mo; -MESH *mp; -OBJECT ti; +nextmeshtri( /* get next triangle ID */ + OBJECT *tip, + MESH *mp +) { + int pn; + MESHPATCH *pp; + + pn = ++(*tip) >> 10; /* next triangle (OVOID init) */ + while (pn < mp->npatches) { + pp = &mp->patch[pn]; + if (!(*tip & 0x200)) { /* local triangle? */ + if ((*tip & 0x1ff) < pp->ntris) + return(1); + *tip &= ~0x1ff; /* move on to single-joiners */ + *tip |= 0x200; + } + if (!(*tip & 0x100)) { /* single joiner? */ + if ((*tip & 0xff) < pp->nj1tris) + return(1); + *tip &= ~0xff; /* move on to double-joiners */ + *tip |= 0x100; + } + if ((*tip & 0xff) < pp->nj2tris) + return(1); + *tip = ++pn << 10; /* first in next patch */ + } + return(0); /* out of patches */ +} + +int +getmeshtrivid( /* get triangle vertex ID's */ + int32 tvid[3], + OBJECT *mo, + MESH *mp, + OBJECT ti +) +{ int pn = ti >> 10; MESHPATCH *pp; @@ -196,16 +235,17 @@ OBJECT ti; int -getmeshvert(vp, mp, vid, what) /* get triangle vertex from ID */ -MESHVERT *vp; -MESH *mp; -int32 vid; -int what; +getmeshvert( /* get triangle vertex from ID */ + MESHVERT *vp, + MESH *mp, + int32 vid, + int what +) { int pn = vid >> 8; MESHPATCH *pp; double vres; - register int i; + int i; vp->fl = 0; if (pn >= mp->npatches) @@ -240,14 +280,15 @@ int what; OBJREC * -getmeshpseudo(mp, mo) /* get mesh pseudo object for material */ -MESH *mp; -OBJECT mo; +getmeshpseudo( /* get mesh pseudo object for material */ + MESH *mp, + OBJECT mo +) { - if (mo < mp->mat0 || mo >= mp->mat0 + mp->nmats) + if ((mo < mp->mat0) | (mo >= mp->mat0 + mp->nmats)) error(INTERNAL, "modifier out of range in getmeshpseudo"); if (mp->pseudo == NULL) { - register int i; + int i; mp->pseudo = (OBJREC *)calloc(mp->nmats, sizeof(OBJREC)); if (mp->pseudo == NULL) error(SYSTEM, "out of memory in getmeshpseudo"); @@ -262,12 +303,13 @@ OBJECT mo; int -getmeshtri(tv, mo, mp, ti, wha) /* get triangle vertices */ -MESHVERT tv[3]; -OBJECT *mo; -MESH *mp; -OBJECT ti; -int wha; +getmeshtri( /* get triangle vertices */ + MESHVERT tv[3], + OBJECT *mo, + MESH *mp, + OBJECT ti, + int wha +) { int32 tvid[3]; @@ -283,13 +325,14 @@ int wha; int32 -addmeshvert(mp, vp) /* find/add a mesh vertex */ -register MESH *mp; -MESHVERT *vp; +addmeshvert( /* find/add a mesh vertex */ + MESH *mp, + MESHVERT *vp +) { - LUENT *lvp; - MCVERT cv; - register int i; + LUENT *lvp; + MCVERT cv; + int i; if (!(vp->fl & MT_V)) return(-1); @@ -303,7 +346,7 @@ MESHVERT *vp; (vp->v[i] - mp->mcube.cuorg[i]) / mp->mcube.cusize); } - if (vp->fl & MT_N) + if (vp->fl & MT_N) /* assumes normalized! */ cv.norm = encodedir(vp->n); if (vp->fl & MT_UV) for (i = 0; i < 2; i++) { @@ -329,10 +372,10 @@ MESHVERT *vp; goto nomem; if (lvp->key == NULL) { lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int32)); - bcopy((void *)&cv, (void *)lvp->key, sizeof(MCVERT)); + memcpy(lvp->key, &cv, sizeof(MCVERT)); } if (lvp->data == NULL) { /* new vertex */ - register MESHPATCH *pp; + MESHPATCH *pp; if (mp->npatches <= 0) { mp->patch = (MESHPATCH *)calloc(MPATCHBLKSIZ, sizeof(MESHPATCH)); @@ -340,16 +383,16 @@ MESHVERT *vp; goto nomem; mp->npatches = 1; } else if (mp->patch[mp->npatches-1].nverts >= 256) { + if (mp->npatches >= 1L<<22) + error(INTERNAL, "too many mesh patches"); if (mp->npatches % MPATCHBLKSIZ == 0) { - mp->patch = (MESHPATCH *)realloc( - (void *)mp->patch, - (mp->npatches + MPATCHBLKSIZ)* - sizeof(MESHPATCH)); - bzero((void *)(mp->patch + mp->npatches), + mp->patch = (MESHPATCH *)realloc(mp->patch, + (mp->npatches + MPATCHBLKSIZ)* + sizeof(MESHPATCH)); + memset((mp->patch + mp->npatches), '\0', MPATCHBLKSIZ*sizeof(MESHPATCH)); } - if (mp->npatches++ >= 1L<<22) - error(INTERNAL, "too many mesh patches"); + mp->npatches++; } pp = &mp->patch[mp->npatches-1]; if (pp->xyz == NULL) { @@ -389,14 +432,15 @@ nomem: OBJECT -addmeshtri(mp, tv, mo) /* add a new mesh triangle */ -MESH *mp; -MESHVERT tv[3]; -OBJECT mo; +addmeshtri( /* add a new mesh triangle */ + MESH *mp, + MESHVERT tv[3], + OBJECT mo +) { - int32 vid[3], t; - int pn[3], i; - register MESHPATCH *pp; + int32 vid[3], t; + int pn[3], i; + MESHPATCH *pp; if (!(tv[0].fl & tv[1].fl & tv[2].fl & MT_V)) return(OVOID); @@ -407,13 +451,14 @@ OBJECT mo; pn[i] = vid[i] >> 8; } /* normalize material index */ - if (mo != OVOID) + if (mo != OVOID) { if ((mo -= mp->mat0) >= mp->nmats) mp->nmats = mo+1; else if (mo < 0) error(INTERNAL, "modifier range error in addmeshtri"); + } /* assign triangle */ - if (pn[0] == pn[1] && pn[1] == pn[2]) { /* local case */ + if ((pn[0] == pn[1]) & (pn[1] == pn[2])) { /* local case */ pp = &mp->patch[pn[0]]; if (pp->tri == NULL) { pp->tri = (struct PTri *)malloc( @@ -439,8 +484,7 @@ OBJECT mo; pp->trimat[pp->ntris] = mo; return(pn[0] << 10 | pp->ntris++); } - } - if (pn[0] == pn[1]) { + } else if (pn[0] == pn[1]) { t = vid[2]; vid[2] = vid[1]; vid[1] = vid[0]; vid[0] = t; i = pn[2]; pn[2] = pn[1]; pn[1] = pn[0]; pn[0] = i; } else if (pn[0] == pn[2]) { @@ -464,20 +508,37 @@ OBJECT mo; } } /* double link */ - pp = &mp->patch[pn[2]]; + pp = &mp->patch[pn[i=0]]; + if (mp->patch[pn[1]].nj2tris < pp->nj2tris) + pp = &mp->patch[pn[i=1]]; + if (mp->patch[pn[2]].nj2tris < pp->nj2tris) + pp = &mp->patch[pn[i=2]]; + if (pp->nj2tris >= 256) + error(INTERNAL, "too many patch triangles in addmeshtri"); if (pp->j2tri == NULL) { pp->j2tri = (struct PJoin2 *)malloc( 256*sizeof(struct PJoin2)); if (pp->j2tri == NULL) goto nomem; } - if (pp->nj2tris >= 256) - error(INTERNAL, "too many patch triangles in addmeshtri"); - pp->j2tri[pp->nj2tris].v1j = vid[0]; - pp->j2tri[pp->nj2tris].v2j = vid[1]; - pp->j2tri[pp->nj2tris].v3 = vid[2] & 0xff; pp->j2tri[pp->nj2tris].mat = mo; - return(pn[2] << 10 | 0x300 | pp->nj2tris++); + switch (i) { + case 0: + pp->j2tri[pp->nj2tris].v3 = vid[0] & 0xff; + pp->j2tri[pp->nj2tris].v1j = vid[1]; + pp->j2tri[pp->nj2tris].v2j = vid[2]; + return(pn[0] << 10 | 0x300 | pp->nj2tris++); + case 1: + pp->j2tri[pp->nj2tris].v2j = vid[0]; + pp->j2tri[pp->nj2tris].v3 = vid[1] & 0xff; + pp->j2tri[pp->nj2tris].v1j = vid[2]; + return(pn[1] << 10 | 0x300 | pp->nj2tris++); + case 2: + pp->j2tri[pp->nj2tris].v1j = vid[0]; + pp->j2tri[pp->nj2tris].v2j = vid[1]; + pp->j2tri[pp->nj2tris].v3 = vid[2] & 0xff; + return(pn[2] << 10 | 0x300 | pp->nj2tris++); + } nomem: error(SYSTEM, "out of memory in addmeshtri"); return(OVOID); @@ -485,12 +546,11 @@ nomem: char * -checkmesh(mp) /* validate mesh data */ -register MESH *mp; +checkmesh(MESH *mp) /* validate mesh data */ { static char embuf[128]; int nouvbounds = 1; - register int i; + int i; /* basic checks */ if (mp == NULL) return("NULL mesh pointer"); @@ -518,6 +578,8 @@ register MESH *mp; return("unbounded scene in mesh"); if (mp->mat0 < 0 || mp->mat0+mp->nmats > nobjects) return("bad mesh modifier range"); + if (mp->nmats > 0) /* allocate during preload_objs()! */ + getmeshpseudo(mp, mp->mat0); for (i = mp->mat0+mp->nmats; i-- > mp->mat0; ) { int otyp = objptr(i)->otype; if (!ismodifier(otyp)) { @@ -530,7 +592,7 @@ register MESH *mp; if (mp->npatches <= 0) error(WARNING, "no patches in mesh"); for (i = 0; i < mp->npatches; i++) { - register MESHPATCH *pp = &mp->patch[i]; + MESHPATCH *pp = &mp->patch[i]; if (pp->nverts <= 0) error(WARNING, "no vertices in patch"); else { @@ -539,8 +601,6 @@ register MESH *mp; if (nouvbounds && pp->uv != NULL) return("unreferenced uv coordinates"); } - if (pp->ntris + pp->nj1tris + pp->nj2tris <= 0) - error(WARNING, "no triangles in patch"); if (pp->ntris > 0 && pp->tri == NULL) return("missing patch triangle list"); if (pp->nj1tris > 0 && pp->j1tri == NULL) @@ -554,9 +614,12 @@ register MESH *mp; static void -tallyoctree(ot, ecp, lcp, ocp) /* tally octree size */ -OCTREE ot; -int *ecp, *lcp, *ocp; +tallyoctree( /* tally octree size */ + OCTREE ot, + int *ecp, + int *lcp, + int *ocp +) { int i; @@ -577,9 +640,10 @@ int *ecp, *lcp, *ocp; void -printmeshstats(ms, fp) /* print out mesh statistics */ -MESH *ms; -FILE *fp; +printmeshstats( /* print out mesh statistics */ + MESH *ms, + FILE *fp +) { int lfcnt=0, lecnt=0, locnt=0; int vcnt=0, ncnt=0, uvcnt=0; @@ -589,7 +653,7 @@ FILE *fp; tallyoctree(ms->mcube.cutree, &lecnt, &lfcnt, &locnt); for (i = 0; i < ms->npatches; i++) { - register MESHPATCH *pp = &ms->patch[i]; + MESHPATCH *pp = &ms->patch[i]; vcnt += pp->nverts; if (pp->norm != NULL) { for (j = pp->nverts; j--; ) @@ -608,7 +672,7 @@ FILE *fp; t2cnt += pp->nj2tris; } fprintf(fp, "Mesh statistics:\n"); - fprintf(fp, "\t%d materials\n", ms->nmats); + fprintf(fp, "\t%ld materials\n", (long)ms->nmats); fprintf(fp, "\t%d patches (%.2f MBytes)\n", ms->npatches, (ms->npatches*sizeof(MESHPATCH) + vcnt*3*sizeof(uint32) + @@ -631,8 +695,7 @@ FILE *fp; void -freemesh(ms) /* free mesh data */ -register MESH *ms; +freemesh(MESH *ms) /* free mesh data */ { MESH mhead; MESH *msp; @@ -660,36 +723,37 @@ register MESH *ms; octfree(ms->mcube.cutree); lu_done(&ms->lut); if (ms->npatches > 0) { - register MESHPATCH *pp = ms->patch + ms->npatches; + MESHPATCH *pp = ms->patch + ms->npatches; while (pp-- > ms->patch) { if (pp->j2tri != NULL) - free((void *)pp->j2tri); + free(pp->j2tri); if (pp->j1tri != NULL) - free((void *)pp->j1tri); + free(pp->j1tri); if (pp->tri != NULL) - free((void *)pp->tri); + free(pp->tri); if (pp->uv != NULL) - free((void *)pp->uv); + free(pp->uv); if (pp->norm != NULL) - free((void *)pp->norm); + free(pp->norm); if (pp->xyz != NULL) - free((void *)pp->xyz); + free(pp->xyz); + if (pp->trimat != NULL) + free(pp->trimat); } - free((void *)ms->patch); + free(ms->patch); } if (ms->pseudo != NULL) - free((void *)ms->pseudo); - free((void *)ms); + free(ms->pseudo); + free(ms); } void -freemeshinst(o) /* free mesh instance */ -OBJREC *o; +freemeshinst(OBJREC *o) /* free mesh instance */ { if (o->os == NULL) return; freemesh((*(MESHINST *)o->os).msh); - free((void *)o->os); + free(o->os); o->os = NULL; }