| 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 "octree.h" | 
| 14 | 
  | 
#include "object.h" | 
| 15 | 
  | 
#include "otypes.h" | 
| 18 | 
  | 
/* An encoded mesh vertex */ | 
| 19 | 
  | 
typedef struct { | 
| 20 | 
  | 
        int             fl; | 
| 21 | 
< | 
        uint4           xyz[3]; | 
| 22 | 
< | 
        int4            norm; | 
| 23 | 
< | 
        uint4           uv[2]; | 
| 21 | 
> | 
        uint32          xyz[3]; | 
| 22 | 
> | 
        int32           norm; | 
| 23 | 
> | 
        uint32          uv[2]; | 
| 24 | 
  | 
} MCVERT; | 
| 25 | 
  | 
 | 
| 26 | 
  | 
#define  MPATCHBLKSIZ   128             /* patch allocation block size */ | 
| 72 | 
  | 
 | 
| 73 | 
  | 
 | 
| 74 | 
  | 
MESH * | 
| 75 | 
< | 
getmesh(mname, flags)                   /* get mesh data */ | 
| 75 | 
> | 
getmesh(mname, flags)                   /* get new mesh data reference */ | 
| 76 | 
  | 
char    *mname; | 
| 77 | 
  | 
int     flags; | 
| 78 | 
  | 
{ | 
| 82 | 
  | 
        flags &= IO_LEGAL; | 
| 83 | 
  | 
        for (ms = mlist; ms != NULL; ms = ms->next) | 
| 84 | 
  | 
                if (!strcmp(mname, ms->name)) { | 
| 85 | 
< | 
                        if ((ms->ldflags & flags) == flags) { | 
| 86 | 
< | 
                                ms->nref++; | 
| 83 | 
< | 
                                return(ms);             /* loaded */ | 
| 84 | 
< | 
                        } | 
| 85 | 
< | 
                        break;                  /* load the rest */ | 
| 85 | 
> | 
                        ms->nref++;     /* increase reference count */ | 
| 86 | 
> | 
                        break; | 
| 87 | 
  | 
                } | 
| 88 | 
< | 
        if (ms == NULL) { | 
| 88 | 
> | 
        if (ms == NULL) {               /* load first time */ | 
| 89 | 
  | 
                ms = (MESH *)calloc(1, sizeof(MESH)); | 
| 90 | 
  | 
                if (ms == NULL) | 
| 91 | 
  | 
                        error(SYSTEM, "out of memory in getmesh"); | 
| 95 | 
  | 
                ms->next = mlist; | 
| 96 | 
  | 
                mlist = ms; | 
| 97 | 
  | 
        } | 
| 98 | 
< | 
        if ((pathname = getpath(mname, getlibpath(), R_OK)) == NULL) { | 
| 98 | 
> | 
        if ((pathname = getpath(mname, getrlibpath(), R_OK)) == NULL) { | 
| 99 | 
  | 
                sprintf(errmsg, "cannot find mesh file \"%s\"", mname); | 
| 100 | 
  | 
                error(USER, errmsg); | 
| 101 | 
  | 
        } | 
| 129 | 
  | 
                ins->msh = NULL; | 
| 130 | 
  | 
                o->os = (char *)ins; | 
| 131 | 
  | 
        } | 
| 132 | 
< | 
        if (ins->msh == NULL || (ins->msh->ldflags & flags) != flags) | 
| 132 | 
> | 
        if (ins->msh == NULL) | 
| 133 | 
  | 
                ins->msh = getmesh(o->oargs.sarg[0], flags); | 
| 134 | 
+ | 
        else if ((flags &= ~ins->msh->ldflags)) | 
| 135 | 
+ | 
                readmesh(ins->msh, | 
| 136 | 
+ | 
                        getpath(o->oargs.sarg[0], getrlibpath(), R_OK), | 
| 137 | 
+ | 
                                flags); | 
| 138 | 
  | 
        return(ins); | 
| 139 | 
  | 
} | 
| 140 | 
  | 
 | 
| 141 | 
  | 
 | 
| 142 | 
  | 
int | 
| 143 | 
  | 
getmeshtrivid(tvid, mo, mp, ti)         /* get triangle vertex ID's */ | 
| 144 | 
< | 
int4    tvid[3]; | 
| 144 | 
> | 
int32   tvid[3]; | 
| 145 | 
  | 
OBJECT  *mo; | 
| 146 | 
  | 
MESH    *mp; | 
| 147 | 
  | 
OBJECT  ti; | 
| 202 | 
  | 
 | 
| 203 | 
  | 
int | 
| 204 | 
  | 
getmeshvert(vp, mp, vid, what)  /* get triangle vertex from ID */ | 
| 205 | 
< | 
MESHVERT*vp; | 
| 206 | 
< | 
MESH    *mp; | 
| 207 | 
< | 
int4    vid; | 
| 208 | 
< | 
int     what; | 
| 205 | 
> | 
MESHVERT        *vp; | 
| 206 | 
> | 
MESH            *mp; | 
| 207 | 
> | 
int32           vid; | 
| 208 | 
> | 
int             what; | 
| 209 | 
  | 
{ | 
| 210 | 
  | 
        int             pn = vid >> 8; | 
| 211 | 
  | 
        MESHPATCH       *pp; | 
| 274 | 
  | 
OBJECT          ti; | 
| 275 | 
  | 
int             wha; | 
| 276 | 
  | 
{ | 
| 277 | 
< | 
        int4    tvid[3]; | 
| 277 | 
> | 
        int32   tvid[3]; | 
| 278 | 
  | 
 | 
| 279 | 
  | 
        if (!getmeshtrivid(tvid, mo, mp, ti)) | 
| 280 | 
  | 
                return(0); | 
| 287 | 
  | 
} | 
| 288 | 
  | 
 | 
| 289 | 
  | 
 | 
| 290 | 
< | 
int4 | 
| 290 | 
> | 
int32 | 
| 291 | 
  | 
addmeshvert(mp, vp)             /* find/add a mesh vertex */ | 
| 292 | 
  | 
register MESH   *mp; | 
| 293 | 
  | 
MESHVERT        *vp; | 
| 304 | 
  | 
                        return(-1); | 
| 305 | 
  | 
                if (vp->v[i] >= mp->mcube.cuorg[i] + mp->mcube.cusize) | 
| 306 | 
  | 
                        return(-1); | 
| 307 | 
< | 
                cv.xyz[i] = (uint4)(4294967296. * | 
| 307 | 
> | 
                cv.xyz[i] = (uint32)(4294967296. * | 
| 308 | 
  | 
                                (vp->v[i] - mp->mcube.cuorg[i]) / | 
| 309 | 
  | 
                                mp->mcube.cusize); | 
| 310 | 
  | 
        } | 
| 311 | 
< | 
        if (vp->fl & MT_N) | 
| 311 | 
> | 
        if (vp->fl & MT_N)              /* assumes normalized! */ | 
| 312 | 
  | 
                cv.norm = encodedir(vp->n); | 
| 313 | 
  | 
        if (vp->fl & MT_UV) | 
| 314 | 
  | 
                for (i = 0; i < 2; i++) { | 
| 316 | 
  | 
                                return(-1); | 
| 317 | 
  | 
                        if (vp->uv[i] >= mp->uvlim[1][i]) | 
| 318 | 
  | 
                                return(-1); | 
| 319 | 
< | 
                        cv.uv[i] = (uint4)(4294967296. * | 
| 319 | 
> | 
                        cv.uv[i] = (uint32)(4294967296. * | 
| 320 | 
  | 
                                        (vp->uv[i] - mp->uvlim[0][i]) / | 
| 321 | 
  | 
                                        (mp->uvlim[1][i] - mp->uvlim[0][i])); | 
| 322 | 
  | 
                } | 
| 333 | 
  | 
        if (lvp == NULL) | 
| 334 | 
  | 
                goto nomem; | 
| 335 | 
  | 
        if (lvp->key == NULL) { | 
| 336 | 
< | 
                lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int4)); | 
| 337 | 
< | 
                bcopy((void *)&cv, (void *)lvp->key, sizeof(MCVERT)); | 
| 336 | 
> | 
                lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int32)); | 
| 337 | 
> | 
                memcpy((void *)lvp->key, (void *)&cv, sizeof(MCVERT)); | 
| 338 | 
  | 
        } | 
| 339 | 
  | 
        if (lvp->data == NULL) {        /* new vertex */ | 
| 340 | 
  | 
                register MESHPATCH      *pp; | 
| 350 | 
  | 
                                                (void *)mp->patch, | 
| 351 | 
  | 
                                        (mp->npatches + MPATCHBLKSIZ)* | 
| 352 | 
  | 
                                                sizeof(MESHPATCH)); | 
| 353 | 
< | 
                                bzero((void *)(mp->patch + mp->npatches), | 
| 353 | 
> | 
                                memset((void *)(mp->patch + mp->npatches), '\0', | 
| 354 | 
  | 
                                        MPATCHBLKSIZ*sizeof(MESHPATCH)); | 
| 355 | 
  | 
                        } | 
| 356 | 
  | 
                        if (mp->npatches++ >= 1L<<22) | 
| 358 | 
  | 
                } | 
| 359 | 
  | 
                pp = &mp->patch[mp->npatches-1]; | 
| 360 | 
  | 
                if (pp->xyz == NULL) { | 
| 361 | 
< | 
                        pp->xyz = (uint4 (*)[3])calloc(256, 3*sizeof(int4)); | 
| 361 | 
> | 
                        pp->xyz = (uint32 (*)[3])calloc(256, 3*sizeof(int32)); | 
| 362 | 
  | 
                        if (pp->xyz == NULL) | 
| 363 | 
  | 
                                goto nomem; | 
| 364 | 
  | 
                } | 
| 366 | 
  | 
                        pp->xyz[pp->nverts][i] = cv.xyz[i]; | 
| 367 | 
  | 
                if (cv.fl & MT_N) { | 
| 368 | 
  | 
                        if (pp->norm == NULL) { | 
| 369 | 
< | 
                                pp->norm = (int4 *)calloc(256, sizeof(int4)); | 
| 369 | 
> | 
                                pp->norm = (int32 *)calloc(256, sizeof(int32)); | 
| 370 | 
  | 
                                if (pp->norm == NULL) | 
| 371 | 
  | 
                                        goto nomem; | 
| 372 | 
  | 
                        } | 
| 374 | 
  | 
                } | 
| 375 | 
  | 
                if (cv.fl & MT_UV) { | 
| 376 | 
  | 
                        if (pp->uv == NULL) { | 
| 377 | 
< | 
                                pp->uv = (uint4 (*)[2])calloc(256, | 
| 378 | 
< | 
                                                2*sizeof(uint4)); | 
| 377 | 
> | 
                                pp->uv = (uint32 (*)[2])calloc(256, | 
| 378 | 
> | 
                                                2*sizeof(uint32)); | 
| 379 | 
  | 
                                if (pp->uv == NULL) | 
| 380 | 
  | 
                                        goto nomem; | 
| 381 | 
  | 
                        } | 
| 384 | 
  | 
                } | 
| 385 | 
  | 
                pp->nverts++; | 
| 386 | 
  | 
                lvp->data = lvp->key + sizeof(MCVERT); | 
| 387 | 
< | 
                *(int4 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1); | 
| 387 | 
> | 
                *(int32 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1); | 
| 388 | 
  | 
        } | 
| 389 | 
< | 
        return(*(int4 *)lvp->data); | 
| 389 | 
> | 
        return(*(int32 *)lvp->data); | 
| 390 | 
  | 
nomem: | 
| 391 | 
  | 
        error(SYSTEM, "out of memory in addmeshvert"); | 
| 392 | 
  | 
        return(-1); | 
| 399 | 
  | 
MESHVERT        tv[3]; | 
| 400 | 
  | 
OBJECT          mo; | 
| 401 | 
  | 
{ | 
| 402 | 
< | 
        int4                    vid[3], t; | 
| 402 | 
> | 
        int32                   vid[3], t; | 
| 403 | 
  | 
        int                     pn[3], i; | 
| 404 | 
  | 
        register MESHPATCH      *pp; | 
| 405 | 
  | 
 | 
| 412 | 
  | 
                pn[i] = vid[i] >> 8; | 
| 413 | 
  | 
        } | 
| 414 | 
  | 
                                /* normalize material index */ | 
| 415 | 
< | 
        if (mo != OVOID) | 
| 415 | 
> | 
        if (mo != OVOID) { | 
| 416 | 
  | 
                if ((mo -= mp->mat0) >= mp->nmats) | 
| 417 | 
  | 
                        mp->nmats = mo+1; | 
| 418 | 
  | 
                else if (mo < 0) | 
| 419 | 
  | 
                        error(INTERNAL, "modifier range error in addmeshtri"); | 
| 420 | 
+ | 
        } | 
| 421 | 
  | 
                                /* assign triangle */ | 
| 422 | 
  | 
        if (pn[0] == pn[1] && pn[1] == pn[2]) { /* local case */ | 
| 423 | 
  | 
                pp = &mp->patch[pn[0]]; | 
| 434 | 
  | 
                        if (pp->ntris == 0) | 
| 435 | 
  | 
                                pp->solemat = mo; | 
| 436 | 
  | 
                        else if (pp->trimat == NULL && mo != pp->solemat) { | 
| 437 | 
< | 
                                pp->trimat = (int2 *)malloc( | 
| 438 | 
< | 
                                                512*sizeof(int2)); | 
| 437 | 
> | 
                                pp->trimat = (int16 *)malloc( | 
| 438 | 
> | 
                                                512*sizeof(int16)); | 
| 439 | 
  | 
                                if (pp->trimat == NULL) | 
| 440 | 
  | 
                                        goto nomem; | 
| 441 | 
  | 
                                for (i = pp->ntris; i--; ) | 
| 614 | 
  | 
                t2cnt += pp->nj2tris; | 
| 615 | 
  | 
        } | 
| 616 | 
  | 
        fprintf(fp, "Mesh statistics:\n"); | 
| 617 | 
< | 
        fprintf(fp, "\t%d materials\n", ms->nmats); | 
| 617 | 
> | 
        fprintf(fp, "\t%ld materials\n", ms->nmats); | 
| 618 | 
  | 
        fprintf(fp, "\t%d patches (%.2f MBytes)\n", ms->npatches, | 
| 619 | 
  | 
                        (ms->npatches*sizeof(MESHPATCH) + | 
| 620 | 
< | 
                        vcnt*3*sizeof(uint4) + | 
| 621 | 
< | 
                        nscnt*sizeof(int4) + | 
| 622 | 
< | 
                        uvscnt*2*sizeof(uint4) + | 
| 620 | 
> | 
                        vcnt*3*sizeof(uint32) + | 
| 621 | 
> | 
                        nscnt*sizeof(int32) + | 
| 622 | 
> | 
                        uvscnt*2*sizeof(uint32) + | 
| 623 | 
  | 
                        tcnt*sizeof(struct PTri) + | 
| 624 | 
  | 
                        t1cnt*sizeof(struct PJoin1) + | 
| 625 | 
  | 
                        t2cnt*sizeof(struct PJoin2))/(1024.*1024.)); |