| 7 | 
  | 
 | 
| 8 | 
  | 
#include <string.h> | 
| 9 | 
  | 
 | 
| 10 | 
< | 
#include "standard.h" | 
| 10 | 
> | 
#include "rtio.h" | 
| 11 | 
> | 
#include "rtmath.h" | 
| 12 | 
> | 
#include "rterror.h" | 
| 13 | 
> | 
#include "paths.h" | 
| 14 | 
  | 
#include "octree.h" | 
| 15 | 
  | 
#include "object.h" | 
| 16 | 
  | 
#include "otypes.h" | 
| 32 | 
  | 
 | 
| 33 | 
  | 
 | 
| 34 | 
  | 
static unsigned long | 
| 35 | 
< | 
cvhash(cvp)                             /* hash an encoded vertex */ | 
| 33 | 
< | 
MCVERT  *cvp; | 
| 35 | 
> | 
cvhash(const char *p)                   /* hash an encoded vertex */ | 
| 36 | 
  | 
{ | 
| 37 | 
+ | 
        const MCVERT    *cvp = (const MCVERT *)p; | 
| 38 | 
  | 
        unsigned long   hval; | 
| 39 | 
  | 
         | 
| 40 | 
  | 
        if (!(cvp->fl & MT_V)) | 
| 49 | 
  | 
 | 
| 50 | 
  | 
 | 
| 51 | 
  | 
static int | 
| 52 | 
< | 
cvcmp(v1, v2)                           /* compare encoded vertices */ | 
| 50 | 
< | 
register MCVERT *v1, *v2; | 
| 52 | 
> | 
cvcmp(const char *vv1, const char *vv2)         /* compare encoded vertices */ | 
| 53 | 
  | 
{ | 
| 54 | 
+ | 
        const MCVERT    *v1 = (const MCVERT *)vv1, *v2 = (const MCVERT *)vv2; | 
| 55 | 
  | 
        if (v1->fl != v2->fl) | 
| 56 | 
  | 
                return(1); | 
| 57 | 
  | 
        if (v1->xyz[0] != v2->xyz[0]) | 
| 73 | 
  | 
 | 
| 74 | 
  | 
 | 
| 75 | 
  | 
MESH * | 
| 76 | 
< | 
getmesh(mname, flags)                   /* get new mesh data reference */ | 
| 77 | 
< | 
char    *mname; | 
| 78 | 
< | 
int     flags; | 
| 76 | 
> | 
getmesh(                                /* get new mesh data reference */ | 
| 77 | 
> | 
        char    *mname, | 
| 78 | 
> | 
        int     flags | 
| 79 | 
> | 
) | 
| 80 | 
  | 
{ | 
| 81 | 
  | 
        char  *pathname; | 
| 82 | 
< | 
        register MESH  *ms; | 
| 82 | 
> | 
        MESH  *ms; | 
| 83 | 
  | 
 | 
| 84 | 
  | 
        flags &= IO_LEGAL; | 
| 85 | 
  | 
        for (ms = mlist; ms != NULL; ms = ms->next) | 
| 86 | 
< | 
                if (!strcmp(mname, ms->name)) | 
| 86 | 
> | 
                if (!strcmp(mname, ms->name)) { | 
| 87 | 
> | 
                        ms->nref++;     /* increase reference count */ | 
| 88 | 
  | 
                        break; | 
| 89 | 
+ | 
                } | 
| 90 | 
  | 
        if (ms == NULL) {               /* load first time */ | 
| 91 | 
  | 
                ms = (MESH *)calloc(1, sizeof(MESH)); | 
| 92 | 
  | 
                if (ms == NULL) | 
| 93 | 
  | 
                        error(SYSTEM, "out of memory in getmesh"); | 
| 94 | 
  | 
                ms->name = savestr(mname); | 
| 95 | 
+ | 
                ms->nref = 1; | 
| 96 | 
  | 
                ms->mcube.cutree = EMPTY; | 
| 97 | 
  | 
                ms->next = mlist; | 
| 98 | 
  | 
                mlist = ms; | 
| 104 | 
  | 
        flags &= ~ms->ldflags; | 
| 105 | 
  | 
        if (flags) | 
| 106 | 
  | 
                readmesh(ms, pathname, flags); | 
| 100 | 
– | 
        ms->nref++;                     /* increase reference count */ | 
| 107 | 
  | 
        return(ms); | 
| 108 | 
  | 
} | 
| 109 | 
  | 
 | 
| 110 | 
  | 
 | 
| 111 | 
  | 
MESHINST * | 
| 112 | 
< | 
getmeshinst(o, flags)                   /* create mesh instance */ | 
| 113 | 
< | 
OBJREC  *o; | 
| 114 | 
< | 
int     flags; | 
| 112 | 
> | 
getmeshinst(                            /* create mesh instance */ | 
| 113 | 
> | 
        OBJREC  *o, | 
| 114 | 
> | 
        int     flags | 
| 115 | 
> | 
) | 
| 116 | 
  | 
{ | 
| 117 | 
< | 
        register MESHINST  *ins; | 
| 117 | 
> | 
        MESHINST  *ins; | 
| 118 | 
  | 
 | 
| 119 | 
  | 
        flags &= IO_LEGAL; | 
| 120 | 
  | 
        if ((ins = (MESHINST *)o->os) == NULL) { | 
| 143 | 
  | 
 | 
| 144 | 
  | 
 | 
| 145 | 
  | 
int | 
| 146 | 
< | 
getmeshtrivid(tvid, mo, mp, ti)         /* get triangle vertex ID's */ | 
| 147 | 
< | 
int32   tvid[3]; | 
| 148 | 
< | 
OBJECT  *mo; | 
| 149 | 
< | 
MESH    *mp; | 
| 143 | 
< | 
OBJECT  ti; | 
| 146 | 
> | 
nextmeshtri(                            /* get next triangle ID */ | 
| 147 | 
> | 
        OBJECT *tip, | 
| 148 | 
> | 
        MESH *mp | 
| 149 | 
> | 
) | 
| 150 | 
  | 
{ | 
| 151 | 
+ | 
        int             advance = 1; | 
| 152 | 
+ | 
        int             pn; | 
| 153 | 
+ | 
        MESHPATCH       *pp; | 
| 154 | 
+ | 
 | 
| 155 | 
+ | 
        if (*tip == OVOID) {                    /* check for first index */ | 
| 156 | 
+ | 
                *tip = 0; | 
| 157 | 
+ | 
                advance = 0; | 
| 158 | 
+ | 
        } | 
| 159 | 
+ | 
        pn = *tip >> 10; | 
| 160 | 
+ | 
        while (pn < mp->npatches) { | 
| 161 | 
+ | 
                pp = &mp->patch[pn]; | 
| 162 | 
+ | 
                if (!(*tip & 0x200)) {          /* local triangle? */ | 
| 163 | 
+ | 
                        if ((*tip & 0x1ff) < pp->ntris - advance) { | 
| 164 | 
+ | 
                                *tip += advance; | 
| 165 | 
+ | 
                                return(1); | 
| 166 | 
+ | 
                        } | 
| 167 | 
+ | 
                        *tip &= ~0x1ff;         /* move on to single-joiners */ | 
| 168 | 
+ | 
                        *tip |= 0x200; | 
| 169 | 
+ | 
                        advance = 0; | 
| 170 | 
+ | 
                } | 
| 171 | 
+ | 
                if (!(*tip & 0x100)) {          /* single joiner? */ | 
| 172 | 
+ | 
                        if ((*tip & 0xff) < pp->nj1tris - advance) { | 
| 173 | 
+ | 
                                *tip += advance; | 
| 174 | 
+ | 
                                return(1); | 
| 175 | 
+ | 
                        } | 
| 176 | 
+ | 
                        *tip &= ~0xff;          /* move on to double-joiners */ | 
| 177 | 
+ | 
                        *tip |= 0x100; | 
| 178 | 
+ | 
                        advance = 0; | 
| 179 | 
+ | 
                } | 
| 180 | 
+ | 
                if ((*tip & 0xff) < pp->nj2tris - advance) { | 
| 181 | 
+ | 
                        *tip += advance; | 
| 182 | 
+ | 
                        return(1); | 
| 183 | 
+ | 
                } | 
| 184 | 
+ | 
                *tip = ++pn << 10;              /* first in next patch */ | 
| 185 | 
+ | 
                advance = 0; | 
| 186 | 
+ | 
        } | 
| 187 | 
+ | 
        return(0);                              /* out of patches */ | 
| 188 | 
+ | 
} | 
| 189 | 
+ | 
 | 
| 190 | 
+ | 
int | 
| 191 | 
+ | 
getmeshtrivid(                          /* get triangle vertex ID's */ | 
| 192 | 
+ | 
        int32   tvid[3], | 
| 193 | 
+ | 
        OBJECT  *mo, | 
| 194 | 
+ | 
        MESH    *mp, | 
| 195 | 
+ | 
        OBJECT  ti | 
| 196 | 
+ | 
) | 
| 197 | 
+ | 
{ | 
| 198 | 
  | 
        int             pn = ti >> 10; | 
| 199 | 
  | 
        MESHPATCH       *pp; | 
| 200 | 
  | 
 | 
| 250 | 
  | 
 | 
| 251 | 
  | 
 | 
| 252 | 
  | 
int | 
| 253 | 
< | 
getmeshvert(vp, mp, vid, what)  /* get triangle vertex from ID */ | 
| 254 | 
< | 
MESHVERT        *vp; | 
| 255 | 
< | 
MESH            *mp; | 
| 256 | 
< | 
int32           vid; | 
| 257 | 
< | 
int             what; | 
| 253 | 
> | 
getmeshvert(                            /* get triangle vertex from ID */ | 
| 254 | 
> | 
        MESHVERT        *vp, | 
| 255 | 
> | 
        MESH            *mp, | 
| 256 | 
> | 
        int32           vid, | 
| 257 | 
> | 
        int             what | 
| 258 | 
> | 
) | 
| 259 | 
  | 
{ | 
| 260 | 
  | 
        int             pn = vid >> 8; | 
| 261 | 
  | 
        MESHPATCH       *pp; | 
| 262 | 
  | 
        double          vres; | 
| 263 | 
< | 
        register int    i; | 
| 263 | 
> | 
        int     i; | 
| 264 | 
  | 
         | 
| 265 | 
  | 
        vp->fl = 0; | 
| 266 | 
  | 
        if (pn >= mp->npatches) | 
| 295 | 
  | 
 | 
| 296 | 
  | 
 | 
| 297 | 
  | 
OBJREC * | 
| 298 | 
< | 
getmeshpseudo(mp, mo)           /* get mesh pseudo object for material */ | 
| 299 | 
< | 
MESH    *mp; | 
| 300 | 
< | 
OBJECT  mo; | 
| 298 | 
> | 
getmeshpseudo(                  /* get mesh pseudo object for material */ | 
| 299 | 
> | 
        MESH    *mp, | 
| 300 | 
> | 
        OBJECT  mo | 
| 301 | 
> | 
) | 
| 302 | 
  | 
{ | 
| 303 | 
  | 
        if (mo < mp->mat0 || mo >= mp->mat0 + mp->nmats) | 
| 304 | 
  | 
                error(INTERNAL, "modifier out of range in getmeshpseudo"); | 
| 305 | 
  | 
        if (mp->pseudo == NULL) { | 
| 306 | 
< | 
                register int    i; | 
| 306 | 
> | 
                int     i; | 
| 307 | 
  | 
                mp->pseudo = (OBJREC *)calloc(mp->nmats, sizeof(OBJREC)); | 
| 308 | 
  | 
                if (mp->pseudo == NULL) | 
| 309 | 
  | 
                        error(SYSTEM, "out of memory in getmeshpseudo"); | 
| 318 | 
  | 
 | 
| 319 | 
  | 
 | 
| 320 | 
  | 
int | 
| 321 | 
< | 
getmeshtri(tv, mo, mp, ti, wha) /* get triangle vertices */ | 
| 322 | 
< | 
MESHVERT        tv[3]; | 
| 323 | 
< | 
OBJECT          *mo; | 
| 324 | 
< | 
MESH            *mp; | 
| 325 | 
< | 
OBJECT          ti; | 
| 326 | 
< | 
int             wha; | 
| 321 | 
> | 
getmeshtri(                     /* get triangle vertices */ | 
| 322 | 
> | 
        MESHVERT        tv[3], | 
| 323 | 
> | 
        OBJECT          *mo, | 
| 324 | 
> | 
        MESH            *mp, | 
| 325 | 
> | 
        OBJECT          ti, | 
| 326 | 
> | 
        int             wha | 
| 327 | 
> | 
) | 
| 328 | 
  | 
{ | 
| 329 | 
  | 
        int32   tvid[3]; | 
| 330 | 
  | 
 | 
| 340 | 
  | 
 | 
| 341 | 
  | 
 | 
| 342 | 
  | 
int32 | 
| 343 | 
< | 
addmeshvert(mp, vp)             /* find/add a mesh vertex */ | 
| 344 | 
< | 
register MESH   *mp; | 
| 345 | 
< | 
MESHVERT        *vp; | 
| 343 | 
> | 
addmeshvert(                    /* find/add a mesh vertex */ | 
| 344 | 
> | 
        MESH    *mp, | 
| 345 | 
> | 
        MESHVERT        *vp | 
| 346 | 
> | 
) | 
| 347 | 
  | 
{ | 
| 348 | 
  | 
        LUENT           *lvp; | 
| 349 | 
  | 
        MCVERT          cv; | 
| 350 | 
< | 
        register int    i; | 
| 350 | 
> | 
        int     i; | 
| 351 | 
  | 
 | 
| 352 | 
  | 
        if (!(vp->fl & MT_V)) | 
| 353 | 
  | 
                return(-1); | 
| 361 | 
  | 
                                (vp->v[i] - mp->mcube.cuorg[i]) / | 
| 362 | 
  | 
                                mp->mcube.cusize); | 
| 363 | 
  | 
        } | 
| 364 | 
< | 
        if (vp->fl & MT_N) | 
| 364 | 
> | 
        if (vp->fl & MT_N)              /* assumes normalized! */ | 
| 365 | 
  | 
                cv.norm = encodedir(vp->n); | 
| 366 | 
  | 
        if (vp->fl & MT_UV) | 
| 367 | 
  | 
                for (i = 0; i < 2; i++) { | 
| 390 | 
  | 
                memcpy((void *)lvp->key, (void *)&cv, sizeof(MCVERT)); | 
| 391 | 
  | 
        } | 
| 392 | 
  | 
        if (lvp->data == NULL) {        /* new vertex */ | 
| 393 | 
< | 
                register MESHPATCH      *pp; | 
| 393 | 
> | 
                MESHPATCH       *pp; | 
| 394 | 
  | 
                if (mp->npatches <= 0) { | 
| 395 | 
  | 
                        mp->patch = (MESHPATCH *)calloc(MPATCHBLKSIZ, | 
| 396 | 
  | 
                                        sizeof(MESHPATCH)); | 
| 447 | 
  | 
 | 
| 448 | 
  | 
 | 
| 449 | 
  | 
OBJECT | 
| 450 | 
< | 
addmeshtri(mp, tv, mo)          /* add a new mesh triangle */ | 
| 451 | 
< | 
MESH            *mp; | 
| 452 | 
< | 
MESHVERT        tv[3]; | 
| 453 | 
< | 
OBJECT          mo; | 
| 450 | 
> | 
addmeshtri(                     /* add a new mesh triangle */ | 
| 451 | 
> | 
        MESH            *mp, | 
| 452 | 
> | 
        MESHVERT        tv[3], | 
| 453 | 
> | 
        OBJECT          mo | 
| 454 | 
> | 
) | 
| 455 | 
  | 
{ | 
| 456 | 
  | 
        int32                   vid[3], t; | 
| 457 | 
  | 
        int                     pn[3], i; | 
| 458 | 
< | 
        register MESHPATCH      *pp; | 
| 458 | 
> | 
        MESHPATCH       *pp; | 
| 459 | 
  | 
 | 
| 460 | 
  | 
        if (!(tv[0].fl & tv[1].fl & tv[2].fl & MT_V)) | 
| 461 | 
  | 
                return(OVOID); | 
| 466 | 
  | 
                pn[i] = vid[i] >> 8; | 
| 467 | 
  | 
        } | 
| 468 | 
  | 
                                /* normalize material index */ | 
| 469 | 
< | 
        if (mo != OVOID) | 
| 469 | 
> | 
        if (mo != OVOID) { | 
| 470 | 
  | 
                if ((mo -= mp->mat0) >= mp->nmats) | 
| 471 | 
  | 
                        mp->nmats = mo+1; | 
| 472 | 
  | 
                else if (mo < 0) | 
| 473 | 
  | 
                        error(INTERNAL, "modifier range error in addmeshtri"); | 
| 474 | 
+ | 
        } | 
| 475 | 
  | 
                                /* assign triangle */ | 
| 476 | 
  | 
        if (pn[0] == pn[1] && pn[1] == pn[2]) { /* local case */ | 
| 477 | 
  | 
                pp = &mp->patch[pn[0]]; | 
| 545 | 
  | 
 | 
| 546 | 
  | 
 | 
| 547 | 
  | 
char * | 
| 548 | 
< | 
checkmesh(mp)                           /* validate mesh data */ | 
| 490 | 
< | 
register MESH   *mp; | 
| 548 | 
> | 
checkmesh(MESH *mp)                     /* validate mesh data */ | 
| 549 | 
  | 
{ | 
| 550 | 
  | 
        static char     embuf[128]; | 
| 551 | 
  | 
        int             nouvbounds = 1; | 
| 552 | 
< | 
        register int    i; | 
| 552 | 
> | 
        int     i; | 
| 553 | 
  | 
                                        /* basic checks */ | 
| 554 | 
  | 
        if (mp == NULL) | 
| 555 | 
  | 
                return("NULL mesh pointer"); | 
| 589 | 
  | 
                if (mp->npatches <= 0) | 
| 590 | 
  | 
                        error(WARNING, "no patches in mesh"); | 
| 591 | 
  | 
                for (i = 0; i < mp->npatches; i++) { | 
| 592 | 
< | 
                        register MESHPATCH      *pp = &mp->patch[i]; | 
| 592 | 
> | 
                        MESHPATCH       *pp = &mp->patch[i]; | 
| 593 | 
  | 
                        if (pp->nverts <= 0) | 
| 594 | 
  | 
                                error(WARNING, "no vertices in patch"); | 
| 595 | 
  | 
                        else { | 
| 598 | 
  | 
                                if (nouvbounds && pp->uv != NULL) | 
| 599 | 
  | 
                                        return("unreferenced uv coordinates"); | 
| 600 | 
  | 
                        } | 
| 543 | 
– | 
                        if (pp->ntris + pp->nj1tris + pp->nj2tris <= 0) | 
| 544 | 
– | 
                                error(WARNING, "no triangles in patch"); | 
| 601 | 
  | 
                        if (pp->ntris > 0 && pp->tri == NULL) | 
| 602 | 
  | 
                                return("missing patch triangle list"); | 
| 603 | 
  | 
                        if (pp->nj1tris > 0 && pp->j1tri == NULL) | 
| 611 | 
  | 
 | 
| 612 | 
  | 
 | 
| 613 | 
  | 
static void | 
| 614 | 
< | 
tallyoctree(ot, ecp, lcp, ocp)  /* tally octree size */ | 
| 615 | 
< | 
OCTREE  ot; | 
| 616 | 
< | 
int     *ecp, *lcp, *ocp; | 
| 614 | 
> | 
tallyoctree(                    /* tally octree size */ | 
| 615 | 
> | 
        OCTREE  ot, | 
| 616 | 
> | 
        int     *ecp, | 
| 617 | 
> | 
        int     *lcp, | 
| 618 | 
> | 
        int     *ocp | 
| 619 | 
> | 
) | 
| 620 | 
  | 
{ | 
| 621 | 
  | 
        int     i; | 
| 622 | 
  | 
 | 
| 637 | 
  | 
 | 
| 638 | 
  | 
 | 
| 639 | 
  | 
void | 
| 640 | 
< | 
printmeshstats(ms, fp)          /* print out mesh statistics */ | 
| 641 | 
< | 
MESH    *ms; | 
| 642 | 
< | 
FILE    *fp; | 
| 640 | 
> | 
printmeshstats(                 /* print out mesh statistics */ | 
| 641 | 
> | 
        MESH    *ms, | 
| 642 | 
> | 
        FILE    *fp | 
| 643 | 
> | 
) | 
| 644 | 
  | 
{ | 
| 645 | 
  | 
        int     lfcnt=0, lecnt=0, locnt=0; | 
| 646 | 
  | 
        int     vcnt=0, ncnt=0, uvcnt=0; | 
| 650 | 
  | 
         | 
| 651 | 
  | 
        tallyoctree(ms->mcube.cutree, &lecnt, &lfcnt, &locnt); | 
| 652 | 
  | 
        for (i = 0; i < ms->npatches; i++) { | 
| 653 | 
< | 
                register MESHPATCH      *pp = &ms->patch[i]; | 
| 653 | 
> | 
                MESHPATCH       *pp = &ms->patch[i]; | 
| 654 | 
  | 
                vcnt += pp->nverts; | 
| 655 | 
  | 
                if (pp->norm != NULL) { | 
| 656 | 
  | 
                        for (j = pp->nverts; j--; ) | 
| 669 | 
  | 
                t2cnt += pp->nj2tris; | 
| 670 | 
  | 
        } | 
| 671 | 
  | 
        fprintf(fp, "Mesh statistics:\n"); | 
| 672 | 
< | 
        fprintf(fp, "\t%d materials\n", ms->nmats); | 
| 672 | 
> | 
        fprintf(fp, "\t%ld materials\n", (long)ms->nmats); | 
| 673 | 
  | 
        fprintf(fp, "\t%d patches (%.2f MBytes)\n", ms->npatches, | 
| 674 | 
  | 
                        (ms->npatches*sizeof(MESHPATCH) + | 
| 675 | 
  | 
                        vcnt*3*sizeof(uint32) + | 
| 692 | 
  | 
 | 
| 693 | 
  | 
 | 
| 694 | 
  | 
void | 
| 695 | 
< | 
freemesh(ms)                    /* free mesh data */ | 
| 636 | 
< | 
register MESH   *ms; | 
| 695 | 
> | 
freemesh(MESH *ms)              /* free mesh data */ | 
| 696 | 
  | 
{ | 
| 697 | 
  | 
        MESH    mhead; | 
| 698 | 
  | 
        MESH    *msp; | 
| 720 | 
  | 
        octfree(ms->mcube.cutree); | 
| 721 | 
  | 
        lu_done(&ms->lut); | 
| 722 | 
  | 
        if (ms->npatches > 0) { | 
| 723 | 
< | 
                register MESHPATCH      *pp = ms->patch + ms->npatches; | 
| 723 | 
> | 
                MESHPATCH       *pp = ms->patch + ms->npatches; | 
| 724 | 
  | 
                while (pp-- > ms->patch) { | 
| 725 | 
  | 
                        if (pp->j2tri != NULL) | 
| 726 | 
  | 
                                free((void *)pp->j2tri); | 
| 744 | 
  | 
 | 
| 745 | 
  | 
 | 
| 746 | 
  | 
void | 
| 747 | 
< | 
freemeshinst(o)                 /* free mesh instance */ | 
| 689 | 
< | 
OBJREC  *o; | 
| 747 | 
> | 
freemeshinst(OBJREC *o)         /* free mesh instance */ | 
| 748 | 
  | 
{ | 
| 749 | 
  | 
        if (o->os == NULL) | 
| 750 | 
  | 
                return; |