| 5 | 
  | 
 * Mesh support routines | 
| 6 | 
  | 
 */ | 
| 7 | 
  | 
 | 
| 8 | 
< | 
#include "standard.h" | 
| 8 | 
> | 
#include <string.h> | 
| 9 | 
> | 
 | 
| 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" | 
| 19 | 
  | 
/* An encoded mesh vertex */ | 
| 20 | 
  | 
typedef struct { | 
| 21 | 
  | 
        int             fl; | 
| 22 | 
< | 
        uint4           xyz[3]; | 
| 23 | 
< | 
        int4            norm; | 
| 24 | 
< | 
        uint4           uv[2]; | 
| 22 | 
> | 
        uint32          xyz[3]; | 
| 23 | 
> | 
        int32           norm; | 
| 24 | 
> | 
        uint32          uv[2]; | 
| 25 | 
  | 
} MCVERT; | 
| 26 | 
  | 
 | 
| 27 | 
  | 
#define  MPATCHBLKSIZ   128             /* patch allocation block size */ | 
| 30 | 
  | 
 | 
| 31 | 
  | 
static MESH     *mlist = NULL;          /* list of loaded meshes */ | 
| 32 | 
  | 
 | 
| 33 | 
+ | 
static lut_keycmpf_t cvcmp; | 
| 34 | 
+ | 
static lut_hashf_t cvhash; | 
| 35 | 
  | 
 | 
| 36 | 
+ | 
 | 
| 37 | 
  | 
static unsigned long | 
| 38 | 
< | 
cvhash(cvp)                             /* hash an encoded vertex */ | 
| 39 | 
< | 
MCVERT  *cvp; | 
| 38 | 
> | 
cvhash(p)                               /* hash an encoded vertex */ | 
| 39 | 
> | 
/* MCVERT       *cvp; */ | 
| 40 | 
> | 
const void      *p; | 
| 41 | 
  | 
{ | 
| 42 | 
+ | 
        const MCVERT    *cvp = (const MCVERT *)p; | 
| 43 | 
  | 
        unsigned long   hval; | 
| 44 | 
  | 
         | 
| 45 | 
  | 
        if (!(cvp->fl & MT_V)) | 
| 54 | 
  | 
 | 
| 55 | 
  | 
 | 
| 56 | 
  | 
static int | 
| 57 | 
< | 
cvcmp(v1, v2)                           /* compare encoded vertices */ | 
| 58 | 
< | 
register MCVERT *v1, *v2; | 
| 57 | 
> | 
cvcmp(vv1, vv2)                         /* compare encoded vertices */ | 
| 58 | 
> | 
/* register MCVERT      *v1, *v2; */ | 
| 59 | 
> | 
const void      *vv1, *vv2; | 
| 60 | 
  | 
{ | 
| 61 | 
+ | 
        const MCVERT    *v1 = vv1, *v2 = vv2; | 
| 62 | 
  | 
        if (v1->fl != v2->fl) | 
| 63 | 
  | 
                return(1); | 
| 64 | 
  | 
        if (v1->xyz[0] != v2->xyz[0]) | 
| 80 | 
  | 
 | 
| 81 | 
  | 
 | 
| 82 | 
  | 
MESH * | 
| 83 | 
< | 
getmesh(mname, flags)                   /* get mesh data */ | 
| 83 | 
> | 
getmesh(mname, flags)                   /* get new mesh data reference */ | 
| 84 | 
  | 
char    *mname; | 
| 85 | 
  | 
int     flags; | 
| 86 | 
  | 
{ | 
| 90 | 
  | 
        flags &= IO_LEGAL; | 
| 91 | 
  | 
        for (ms = mlist; ms != NULL; ms = ms->next) | 
| 92 | 
  | 
                if (!strcmp(mname, ms->name)) { | 
| 93 | 
< | 
                        if ((ms->ldflags & flags) == flags) { | 
| 94 | 
< | 
                                ms->nref++; | 
| 83 | 
< | 
                                return(ms);             /* loaded */ | 
| 84 | 
< | 
                        } | 
| 85 | 
< | 
                        break;                  /* load the rest */ | 
| 93 | 
> | 
                        ms->nref++;     /* increase reference count */ | 
| 94 | 
> | 
                        break; | 
| 95 | 
  | 
                } | 
| 96 | 
< | 
        if (ms == NULL) { | 
| 96 | 
> | 
        if (ms == NULL) {               /* load first time */ | 
| 97 | 
  | 
                ms = (MESH *)calloc(1, sizeof(MESH)); | 
| 98 | 
  | 
                if (ms == NULL) | 
| 99 | 
  | 
                        error(SYSTEM, "out of memory in getmesh"); | 
| 103 | 
  | 
                ms->next = mlist; | 
| 104 | 
  | 
                mlist = ms; | 
| 105 | 
  | 
        } | 
| 106 | 
< | 
        if ((pathname = getpath(mname, getlibpath(), R_OK)) == NULL) { | 
| 106 | 
> | 
        if ((pathname = getpath(mname, getrlibpath(), R_OK)) == NULL) { | 
| 107 | 
  | 
                sprintf(errmsg, "cannot find mesh file \"%s\"", mname); | 
| 108 | 
  | 
                error(USER, errmsg); | 
| 109 | 
  | 
        } | 
| 137 | 
  | 
                ins->msh = NULL; | 
| 138 | 
  | 
                o->os = (char *)ins; | 
| 139 | 
  | 
        } | 
| 140 | 
< | 
        if (ins->msh == NULL || (ins->msh->ldflags & flags) != flags) | 
| 140 | 
> | 
        if (ins->msh == NULL) | 
| 141 | 
  | 
                ins->msh = getmesh(o->oargs.sarg[0], flags); | 
| 142 | 
+ | 
        else if ((flags &= ~ins->msh->ldflags)) | 
| 143 | 
+ | 
                readmesh(ins->msh, | 
| 144 | 
+ | 
                        getpath(o->oargs.sarg[0], getrlibpath(), R_OK), | 
| 145 | 
+ | 
                                flags); | 
| 146 | 
  | 
        return(ins); | 
| 147 | 
  | 
} | 
| 148 | 
  | 
 | 
| 149 | 
  | 
 | 
| 150 | 
  | 
int | 
| 151 | 
  | 
getmeshtrivid(tvid, mo, mp, ti)         /* get triangle vertex ID's */ | 
| 152 | 
< | 
int4    tvid[3]; | 
| 152 | 
> | 
int32   tvid[3]; | 
| 153 | 
  | 
OBJECT  *mo; | 
| 154 | 
  | 
MESH    *mp; | 
| 155 | 
  | 
OBJECT  ti; | 
| 210 | 
  | 
 | 
| 211 | 
  | 
int | 
| 212 | 
  | 
getmeshvert(vp, mp, vid, what)  /* get triangle vertex from ID */ | 
| 213 | 
< | 
MESHVERT*vp; | 
| 214 | 
< | 
MESH    *mp; | 
| 215 | 
< | 
int4    vid; | 
| 216 | 
< | 
int     what; | 
| 213 | 
> | 
MESHVERT        *vp; | 
| 214 | 
> | 
MESH            *mp; | 
| 215 | 
> | 
int32           vid; | 
| 216 | 
> | 
int             what; | 
| 217 | 
  | 
{ | 
| 218 | 
  | 
        int             pn = vid >> 8; | 
| 219 | 
  | 
        MESHPATCH       *pp; | 
| 282 | 
  | 
OBJECT          ti; | 
| 283 | 
  | 
int             wha; | 
| 284 | 
  | 
{ | 
| 285 | 
< | 
        int4    tvid[3]; | 
| 285 | 
> | 
        int32   tvid[3]; | 
| 286 | 
  | 
 | 
| 287 | 
  | 
        if (!getmeshtrivid(tvid, mo, mp, ti)) | 
| 288 | 
  | 
                return(0); | 
| 295 | 
  | 
} | 
| 296 | 
  | 
 | 
| 297 | 
  | 
 | 
| 298 | 
< | 
int4 | 
| 298 | 
> | 
int32 | 
| 299 | 
  | 
addmeshvert(mp, vp)             /* find/add a mesh vertex */ | 
| 300 | 
  | 
register MESH   *mp; | 
| 301 | 
  | 
MESHVERT        *vp; | 
| 312 | 
  | 
                        return(-1); | 
| 313 | 
  | 
                if (vp->v[i] >= mp->mcube.cuorg[i] + mp->mcube.cusize) | 
| 314 | 
  | 
                        return(-1); | 
| 315 | 
< | 
                cv.xyz[i] = (uint4)(4294967296. * | 
| 315 | 
> | 
                cv.xyz[i] = (uint32)(4294967296. * | 
| 316 | 
  | 
                                (vp->v[i] - mp->mcube.cuorg[i]) / | 
| 317 | 
  | 
                                mp->mcube.cusize); | 
| 318 | 
  | 
        } | 
| 319 | 
< | 
        if (vp->fl & MT_N) | 
| 319 | 
> | 
        if (vp->fl & MT_N)              /* assumes normalized! */ | 
| 320 | 
  | 
                cv.norm = encodedir(vp->n); | 
| 321 | 
  | 
        if (vp->fl & MT_UV) | 
| 322 | 
  | 
                for (i = 0; i < 2; i++) { | 
| 324 | 
  | 
                                return(-1); | 
| 325 | 
  | 
                        if (vp->uv[i] >= mp->uvlim[1][i]) | 
| 326 | 
  | 
                                return(-1); | 
| 327 | 
< | 
                        cv.uv[i] = (uint4)(4294967296. * | 
| 327 | 
> | 
                        cv.uv[i] = (uint32)(4294967296. * | 
| 328 | 
  | 
                                        (vp->uv[i] - mp->uvlim[0][i]) / | 
| 329 | 
  | 
                                        (mp->uvlim[1][i] - mp->uvlim[0][i])); | 
| 330 | 
  | 
                } | 
| 341 | 
  | 
        if (lvp == NULL) | 
| 342 | 
  | 
                goto nomem; | 
| 343 | 
  | 
        if (lvp->key == NULL) { | 
| 344 | 
< | 
                lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int4)); | 
| 345 | 
< | 
                bcopy((void *)&cv, (void *)lvp->key, sizeof(MCVERT)); | 
| 344 | 
> | 
                lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int32)); | 
| 345 | 
> | 
                memcpy((void *)lvp->key, (void *)&cv, sizeof(MCVERT)); | 
| 346 | 
  | 
        } | 
| 347 | 
  | 
        if (lvp->data == NULL) {        /* new vertex */ | 
| 348 | 
  | 
                register MESHPATCH      *pp; | 
| 358 | 
  | 
                                                (void *)mp->patch, | 
| 359 | 
  | 
                                        (mp->npatches + MPATCHBLKSIZ)* | 
| 360 | 
  | 
                                                sizeof(MESHPATCH)); | 
| 361 | 
< | 
                                bzero((void *)(mp->patch + mp->npatches), | 
| 361 | 
> | 
                                memset((void *)(mp->patch + mp->npatches), '\0', | 
| 362 | 
  | 
                                        MPATCHBLKSIZ*sizeof(MESHPATCH)); | 
| 363 | 
  | 
                        } | 
| 364 | 
  | 
                        if (mp->npatches++ >= 1L<<22) | 
| 366 | 
  | 
                } | 
| 367 | 
  | 
                pp = &mp->patch[mp->npatches-1]; | 
| 368 | 
  | 
                if (pp->xyz == NULL) { | 
| 369 | 
< | 
                        pp->xyz = (uint4 (*)[3])calloc(256, 3*sizeof(int4)); | 
| 369 | 
> | 
                        pp->xyz = (uint32 (*)[3])calloc(256, 3*sizeof(int32)); | 
| 370 | 
  | 
                        if (pp->xyz == NULL) | 
| 371 | 
  | 
                                goto nomem; | 
| 372 | 
  | 
                } | 
| 374 | 
  | 
                        pp->xyz[pp->nverts][i] = cv.xyz[i]; | 
| 375 | 
  | 
                if (cv.fl & MT_N) { | 
| 376 | 
  | 
                        if (pp->norm == NULL) { | 
| 377 | 
< | 
                                pp->norm = (int4 *)calloc(256, sizeof(int4)); | 
| 377 | 
> | 
                                pp->norm = (int32 *)calloc(256, sizeof(int32)); | 
| 378 | 
  | 
                                if (pp->norm == NULL) | 
| 379 | 
  | 
                                        goto nomem; | 
| 380 | 
  | 
                        } | 
| 382 | 
  | 
                } | 
| 383 | 
  | 
                if (cv.fl & MT_UV) { | 
| 384 | 
  | 
                        if (pp->uv == NULL) { | 
| 385 | 
< | 
                                pp->uv = (uint4 (*)[2])calloc(256, | 
| 386 | 
< | 
                                                2*sizeof(uint4)); | 
| 385 | 
> | 
                                pp->uv = (uint32 (*)[2])calloc(256, | 
| 386 | 
> | 
                                                2*sizeof(uint32)); | 
| 387 | 
  | 
                                if (pp->uv == NULL) | 
| 388 | 
  | 
                                        goto nomem; | 
| 389 | 
  | 
                        } | 
| 392 | 
  | 
                } | 
| 393 | 
  | 
                pp->nverts++; | 
| 394 | 
  | 
                lvp->data = lvp->key + sizeof(MCVERT); | 
| 395 | 
< | 
                *(int4 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1); | 
| 395 | 
> | 
                *(int32 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1); | 
| 396 | 
  | 
        } | 
| 397 | 
< | 
        return(*(int4 *)lvp->data); | 
| 397 | 
> | 
        return(*(int32 *)lvp->data); | 
| 398 | 
  | 
nomem: | 
| 399 | 
  | 
        error(SYSTEM, "out of memory in addmeshvert"); | 
| 400 | 
  | 
        return(-1); | 
| 407 | 
  | 
MESHVERT        tv[3]; | 
| 408 | 
  | 
OBJECT          mo; | 
| 409 | 
  | 
{ | 
| 410 | 
< | 
        int4                    vid[3], t; | 
| 410 | 
> | 
        int32                   vid[3], t; | 
| 411 | 
  | 
        int                     pn[3], i; | 
| 412 | 
  | 
        register MESHPATCH      *pp; | 
| 413 | 
  | 
 | 
| 420 | 
  | 
                pn[i] = vid[i] >> 8; | 
| 421 | 
  | 
        } | 
| 422 | 
  | 
                                /* normalize material index */ | 
| 423 | 
< | 
        if (mo != OVOID) | 
| 423 | 
> | 
        if (mo != OVOID) { | 
| 424 | 
  | 
                if ((mo -= mp->mat0) >= mp->nmats) | 
| 425 | 
  | 
                        mp->nmats = mo+1; | 
| 426 | 
  | 
                else if (mo < 0) | 
| 427 | 
  | 
                        error(INTERNAL, "modifier range error in addmeshtri"); | 
| 428 | 
+ | 
        } | 
| 429 | 
  | 
                                /* assign triangle */ | 
| 430 | 
  | 
        if (pn[0] == pn[1] && pn[1] == pn[2]) { /* local case */ | 
| 431 | 
  | 
                pp = &mp->patch[pn[0]]; | 
| 442 | 
  | 
                        if (pp->ntris == 0) | 
| 443 | 
  | 
                                pp->solemat = mo; | 
| 444 | 
  | 
                        else if (pp->trimat == NULL && mo != pp->solemat) { | 
| 445 | 
< | 
                                pp->trimat = (int2 *)malloc( | 
| 446 | 
< | 
                                                512*sizeof(int2)); | 
| 445 | 
> | 
                                pp->trimat = (int16 *)malloc( | 
| 446 | 
> | 
                                                512*sizeof(int16)); | 
| 447 | 
  | 
                                if (pp->trimat == NULL) | 
| 448 | 
  | 
                                        goto nomem; | 
| 449 | 
  | 
                                for (i = pp->ntris; i--; ) | 
| 553 | 
  | 
                                if (nouvbounds && pp->uv != NULL) | 
| 554 | 
  | 
                                        return("unreferenced uv coordinates"); | 
| 555 | 
  | 
                        } | 
| 542 | 
– | 
                        if (pp->ntris + pp->nj1tris + pp->nj2tris <= 0) | 
| 543 | 
– | 
                                error(WARNING, "no triangles in patch"); | 
| 556 | 
  | 
                        if (pp->ntris > 0 && pp->tri == NULL) | 
| 557 | 
  | 
                                return("missing patch triangle list"); | 
| 558 | 
  | 
                        if (pp->nj1tris > 0 && pp->j1tri == NULL) | 
| 620 | 
  | 
                t2cnt += pp->nj2tris; | 
| 621 | 
  | 
        } | 
| 622 | 
  | 
        fprintf(fp, "Mesh statistics:\n"); | 
| 623 | 
< | 
        fprintf(fp, "\t%d materials\n", ms->nmats); | 
| 623 | 
> | 
        fprintf(fp, "\t%ld materials\n", (long)ms->nmats); | 
| 624 | 
  | 
        fprintf(fp, "\t%d patches (%.2f MBytes)\n", ms->npatches, | 
| 625 | 
  | 
                        (ms->npatches*sizeof(MESHPATCH) + | 
| 626 | 
< | 
                        vcnt*3*sizeof(uint4) + | 
| 627 | 
< | 
                        nscnt*sizeof(int4) + | 
| 628 | 
< | 
                        uvscnt*2*sizeof(uint4) + | 
| 626 | 
> | 
                        vcnt*3*sizeof(uint32) + | 
| 627 | 
> | 
                        nscnt*sizeof(int32) + | 
| 628 | 
> | 
                        uvscnt*2*sizeof(uint32) + | 
| 629 | 
  | 
                        tcnt*sizeof(struct PTri) + | 
| 630 | 
  | 
                        t1cnt*sizeof(struct PJoin1) + | 
| 631 | 
  | 
                        t2cnt*sizeof(struct PJoin2))/(1024.*1024.)); |