| 5 |
|
* Mesh support routines |
| 6 |
|
*/ |
| 7 |
|
|
| 8 |
– |
#include <string.h> |
| 9 |
– |
|
| 8 |
|
#include "rtio.h" |
| 9 |
|
#include "rtmath.h" |
| 10 |
|
#include "rterror.h" |
| 50 |
|
cvcmp(const char *vv1, const char *vv2) /* compare encoded vertices */ |
| 51 |
|
{ |
| 52 |
|
const MCVERT *v1 = (const MCVERT *)vv1, *v2 = (const MCVERT *)vv2; |
| 53 |
+ |
|
| 54 |
|
if (v1->fl != v2->fl) |
| 55 |
|
return(1); |
| 56 |
|
if (v1->xyz[0] != v2->xyz[0]) |
| 86 |
|
ms->nref++; /* increase reference count */ |
| 87 |
|
break; |
| 88 |
|
} |
| 89 |
< |
if (ms == NULL) { /* load first time */ |
| 89 |
> |
if (ms == NULL) { /* new mesh entry? */ |
| 90 |
|
ms = (MESH *)calloc(1, sizeof(MESH)); |
| 91 |
|
if (ms == NULL) |
| 92 |
|
error(SYSTEM, "out of memory in getmesh"); |
| 96 |
|
ms->next = mlist; |
| 97 |
|
mlist = ms; |
| 98 |
|
} |
| 99 |
+ |
if (!(flags &= ~ms->ldflags)) /* nothing to load? */ |
| 100 |
+ |
return(ms); |
| 101 |
|
if ((pathname = getpath(mname, getrlibpath(), R_OK)) == NULL) { |
| 102 |
|
sprintf(errmsg, "cannot find mesh file \"%s\"", mname); |
| 103 |
|
error(SYSTEM, errmsg); |
| 104 |
|
} |
| 105 |
< |
flags &= ~ms->ldflags; |
| 105 |
< |
if (flags) |
| 106 |
< |
readmesh(ms, pathname, flags); |
| 105 |
> |
readmesh(ms, pathname, flags); |
| 106 |
|
return(ms); |
| 107 |
|
} |
| 108 |
|
|
| 285 |
|
OBJECT mo |
| 286 |
|
) |
| 287 |
|
{ |
| 288 |
< |
if (mo < mp->mat0 || mo >= mp->mat0 + mp->nmats) |
| 288 |
> |
if ((mo < mp->mat0) | (mo >= mp->mat0 + mp->nmats)) |
| 289 |
|
error(INTERNAL, "modifier out of range in getmeshpseudo"); |
| 290 |
|
if (mp->pseudo == NULL) { |
| 291 |
|
int i; |
| 326 |
|
|
| 327 |
|
int32 |
| 328 |
|
addmeshvert( /* find/add a mesh vertex */ |
| 329 |
< |
MESH *mp, |
| 329 |
> |
MESH *mp, |
| 330 |
|
MESHVERT *vp |
| 331 |
|
) |
| 332 |
|
{ |
| 333 |
< |
LUENT *lvp; |
| 334 |
< |
MCVERT cv; |
| 333 |
> |
LUENT *lvp; |
| 334 |
> |
MCVERT cv; |
| 335 |
|
int i; |
| 336 |
|
|
| 337 |
|
if (!(vp->fl & MT_V)) |
| 372 |
|
goto nomem; |
| 373 |
|
if (lvp->key == NULL) { |
| 374 |
|
lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int32)); |
| 375 |
< |
memcpy((void *)lvp->key, (void *)&cv, sizeof(MCVERT)); |
| 375 |
> |
memcpy(lvp->key, &cv, sizeof(MCVERT)); |
| 376 |
|
} |
| 377 |
|
if (lvp->data == NULL) { /* new vertex */ |
| 378 |
|
MESHPATCH *pp; |
| 383 |
|
goto nomem; |
| 384 |
|
mp->npatches = 1; |
| 385 |
|
} else if (mp->patch[mp->npatches-1].nverts >= 256) { |
| 386 |
+ |
if (mp->npatches >= 1L<<22) |
| 387 |
+ |
error(INTERNAL, "too many mesh patches"); |
| 388 |
|
if (mp->npatches % MPATCHBLKSIZ == 0) { |
| 389 |
< |
mp->patch = (MESHPATCH *)realloc( |
| 390 |
< |
(void *)mp->patch, |
| 391 |
< |
(mp->npatches + MPATCHBLKSIZ)* |
| 392 |
< |
sizeof(MESHPATCH)); |
| 392 |
< |
memset((void *)(mp->patch + mp->npatches), '\0', |
| 389 |
> |
mp->patch = (MESHPATCH *)realloc(mp->patch, |
| 390 |
> |
(mp->npatches + MPATCHBLKSIZ)* |
| 391 |
> |
sizeof(MESHPATCH)); |
| 392 |
> |
memset((mp->patch + mp->npatches), '\0', |
| 393 |
|
MPATCHBLKSIZ*sizeof(MESHPATCH)); |
| 394 |
|
} |
| 395 |
< |
if (mp->npatches++ >= 1L<<22) |
| 396 |
< |
error(INTERNAL, "too many mesh patches"); |
| 395 |
> |
mp->npatches++; |
| 396 |
|
} |
| 397 |
|
pp = &mp->patch[mp->npatches-1]; |
| 398 |
|
if (pp->xyz == NULL) { |
| 438 |
|
OBJECT mo |
| 439 |
|
) |
| 440 |
|
{ |
| 441 |
< |
int32 vid[3], t; |
| 442 |
< |
int pn[3], i; |
| 441 |
> |
int32 vid[3], t; |
| 442 |
> |
int pn[3], i; |
| 443 |
|
MESHPATCH *pp; |
| 444 |
|
|
| 445 |
|
if (!(tv[0].fl & tv[1].fl & tv[2].fl & MT_V)) |
| 458 |
|
error(INTERNAL, "modifier range error in addmeshtri"); |
| 459 |
|
} |
| 460 |
|
/* assign triangle */ |
| 461 |
< |
if (pn[0] == pn[1] && pn[1] == pn[2]) { /* local case */ |
| 461 |
> |
if ((pn[0] == pn[1]) & (pn[1] == pn[2])) { /* local case */ |
| 462 |
|
pp = &mp->patch[pn[0]]; |
| 463 |
|
if (pp->tri == NULL) { |
| 464 |
|
pp->tri = (struct PTri *)malloc( |
| 484 |
|
pp->trimat[pp->ntris] = mo; |
| 485 |
|
return(pn[0] << 10 | pp->ntris++); |
| 486 |
|
} |
| 487 |
< |
} |
| 489 |
< |
if (pn[0] == pn[1]) { |
| 487 |
> |
} else if (pn[0] == pn[1]) { |
| 488 |
|
t = vid[2]; vid[2] = vid[1]; vid[1] = vid[0]; vid[0] = t; |
| 489 |
|
i = pn[2]; pn[2] = pn[1]; pn[1] = pn[0]; pn[0] = i; |
| 490 |
|
} else if (pn[0] == pn[2]) { |
| 508 |
|
} |
| 509 |
|
} |
| 510 |
|
/* double link */ |
| 511 |
< |
pp = &mp->patch[pn[2]]; |
| 511 |
> |
pp = &mp->patch[pn[i=0]]; |
| 512 |
> |
if (mp->patch[pn[1]].nj2tris < pp->nj2tris) |
| 513 |
> |
pp = &mp->patch[pn[i=1]]; |
| 514 |
> |
if (mp->patch[pn[2]].nj2tris < pp->nj2tris) |
| 515 |
> |
pp = &mp->patch[pn[i=2]]; |
| 516 |
> |
if (pp->nj2tris >= 256) |
| 517 |
> |
error(INTERNAL, "too many patch triangles in addmeshtri"); |
| 518 |
|
if (pp->j2tri == NULL) { |
| 519 |
|
pp->j2tri = (struct PJoin2 *)malloc( |
| 520 |
|
256*sizeof(struct PJoin2)); |
| 521 |
|
if (pp->j2tri == NULL) |
| 522 |
|
goto nomem; |
| 523 |
|
} |
| 520 |
– |
if (pp->nj2tris >= 256) |
| 521 |
– |
error(INTERNAL, "too many patch triangles in addmeshtri"); |
| 522 |
– |
pp->j2tri[pp->nj2tris].v1j = vid[0]; |
| 523 |
– |
pp->j2tri[pp->nj2tris].v2j = vid[1]; |
| 524 |
– |
pp->j2tri[pp->nj2tris].v3 = vid[2] & 0xff; |
| 524 |
|
pp->j2tri[pp->nj2tris].mat = mo; |
| 525 |
< |
return(pn[2] << 10 | 0x300 | pp->nj2tris++); |
| 525 |
> |
switch (i) { |
| 526 |
> |
case 0: |
| 527 |
> |
pp->j2tri[pp->nj2tris].v3 = vid[0] & 0xff; |
| 528 |
> |
pp->j2tri[pp->nj2tris].v1j = vid[1]; |
| 529 |
> |
pp->j2tri[pp->nj2tris].v2j = vid[2]; |
| 530 |
> |
return(pn[0] << 10 | 0x300 | pp->nj2tris++); |
| 531 |
> |
case 1: |
| 532 |
> |
pp->j2tri[pp->nj2tris].v2j = vid[0]; |
| 533 |
> |
pp->j2tri[pp->nj2tris].v3 = vid[1] & 0xff; |
| 534 |
> |
pp->j2tri[pp->nj2tris].v1j = vid[2]; |
| 535 |
> |
return(pn[1] << 10 | 0x300 | pp->nj2tris++); |
| 536 |
> |
case 2: |
| 537 |
> |
pp->j2tri[pp->nj2tris].v1j = vid[0]; |
| 538 |
> |
pp->j2tri[pp->nj2tris].v2j = vid[1]; |
| 539 |
> |
pp->j2tri[pp->nj2tris].v3 = vid[2] & 0xff; |
| 540 |
> |
return(pn[2] << 10 | 0x300 | pp->nj2tris++); |
| 541 |
> |
} |
| 542 |
|
nomem: |
| 543 |
|
error(SYSTEM, "out of memory in addmeshtri"); |
| 544 |
|
return(OVOID); |
| 550 |
|
{ |
| 551 |
|
static char embuf[128]; |
| 552 |
|
int nouvbounds = 1; |
| 553 |
< |
int i; |
| 553 |
> |
int i, j; |
| 554 |
|
/* basic checks */ |
| 555 |
|
if (mp == NULL) |
| 556 |
|
return("NULL mesh pointer"); |
| 572 |
|
if (isempty(mp->mcube.cutree)) |
| 573 |
|
error(WARNING, "empty mesh octree"); |
| 574 |
|
} |
| 575 |
< |
/* check scene data */ |
| 575 |
> |
/* check patch data */ |
| 576 |
|
if (mp->ldflags & IO_SCENE) { |
| 577 |
+ |
MESHVERT mv; |
| 578 |
|
if (!(mp->ldflags & IO_BOUNDS)) |
| 579 |
|
return("unbounded scene in mesh"); |
| 580 |
|
if (mp->mat0 < 0 || mp->mat0+mp->nmats > nobjects) |
| 581 |
|
return("bad mesh modifier range"); |
| 582 |
+ |
if (mp->nmats > 0) /* allocate during preload_objs()! */ |
| 583 |
+ |
getmeshpseudo(mp, mp->mat0); |
| 584 |
|
for (i = mp->mat0+mp->nmats; i-- > mp->mat0; ) { |
| 585 |
|
int otyp = objptr(i)->otype; |
| 586 |
|
if (!ismodifier(otyp)) { |
| 602 |
|
if (nouvbounds && pp->uv != NULL) |
| 603 |
|
return("unreferenced uv coordinates"); |
| 604 |
|
} |
| 605 |
< |
if (pp->ntris > 0 && pp->tri == NULL) |
| 606 |
< |
return("missing patch triangle list"); |
| 607 |
< |
if (pp->nj1tris > 0 && pp->j1tri == NULL) |
| 608 |
< |
return("missing patch joiner triangle list"); |
| 609 |
< |
if (pp->nj2tris > 0 && pp->j2tri == NULL) |
| 610 |
< |
return("missing patch double-joiner list"); |
| 605 |
> |
if (pp->ntris > 0) { |
| 606 |
> |
struct PTri *tp = pp->tri; |
| 607 |
> |
if (tp == NULL) |
| 608 |
> |
return("missing patch triangle list"); |
| 609 |
> |
if (mp->nmats <= 0) |
| 610 |
> |
j = -1; |
| 611 |
> |
else if (pp->trimat == NULL) |
| 612 |
> |
j = ((pp->solemat < 0) | (pp->solemat >= mp->nmats)) - 1; |
| 613 |
> |
else |
| 614 |
> |
for (j = pp->ntris; j--; ) |
| 615 |
> |
if ((pp->trimat[j] < 0) | |
| 616 |
> |
(pp->trimat[j] >= mp->nmats)) |
| 617 |
> |
break; |
| 618 |
> |
if (j >= 0) |
| 619 |
> |
return("bad local triangle material"); |
| 620 |
> |
for (j = pp->ntris; j--; tp++) |
| 621 |
> |
if ((tp->v1 >= pp->nverts) | (tp->v2 >= pp->nverts) | |
| 622 |
> |
(tp->v3 >= pp->nverts)) |
| 623 |
> |
return("bad local triangle index"); |
| 624 |
> |
} |
| 625 |
> |
if (pp->nj1tris > 0) { |
| 626 |
> |
struct PJoin1 *j1p = pp->j1tri; |
| 627 |
> |
if (j1p == NULL) |
| 628 |
> |
return("missing patch joiner triangle list"); |
| 629 |
> |
for (j = pp->nj1tris; j--; j1p++) { |
| 630 |
> |
if (mp->nmats > 0 && |
| 631 |
> |
(j1p->mat < 0) | (j1p->mat >= mp->nmats)) |
| 632 |
> |
return("bad j1 triangle material"); |
| 633 |
> |
if (!getmeshvert(&mv, mp, j1p->v1j, MT_V)) |
| 634 |
> |
return("bad j1 triangle joiner"); |
| 635 |
> |
if ((j1p->v2 >= pp->nverts) | (j1p->v3 >= pp->nverts)) |
| 636 |
> |
return("bad j1 triangle local index"); |
| 637 |
> |
} |
| 638 |
> |
} |
| 639 |
> |
if (pp->nj2tris > 0) { |
| 640 |
> |
struct PJoin2 *j2p = pp->j2tri; |
| 641 |
> |
if (j2p == NULL) |
| 642 |
> |
return("missing patch double-joiner list"); |
| 643 |
> |
for (j = pp->nj2tris; j--; j2p++) { |
| 644 |
> |
if (mp->nmats > 0 && |
| 645 |
> |
(j2p->mat < 0) | (j2p->mat >= mp->nmats)) |
| 646 |
> |
return("bad j2 triangle material"); |
| 647 |
> |
if (!getmeshvert(&mv, mp, j2p->v1j, MT_V) | |
| 648 |
> |
!getmeshvert(&mv, mp, j2p->v2j, MT_V)) |
| 649 |
> |
return("bad j2 triangle joiner"); |
| 650 |
> |
if (j2p->v3 >= pp->nverts) |
| 651 |
> |
return("bad j2 triangle local index"); |
| 652 |
> |
} |
| 653 |
> |
} |
| 654 |
|
} |
| 655 |
|
} |
| 656 |
< |
return(NULL); /* seems OK */ |
| 656 |
> |
return(NULL); /* seems to be self-consistent */ |
| 657 |
|
} |
| 658 |
|
|
| 659 |
|
|
| 770 |
|
MESHPATCH *pp = ms->patch + ms->npatches; |
| 771 |
|
while (pp-- > ms->patch) { |
| 772 |
|
if (pp->j2tri != NULL) |
| 773 |
< |
free((void *)pp->j2tri); |
| 773 |
> |
free(pp->j2tri); |
| 774 |
|
if (pp->j1tri != NULL) |
| 775 |
< |
free((void *)pp->j1tri); |
| 775 |
> |
free(pp->j1tri); |
| 776 |
|
if (pp->tri != NULL) |
| 777 |
< |
free((void *)pp->tri); |
| 777 |
> |
free(pp->tri); |
| 778 |
|
if (pp->uv != NULL) |
| 779 |
< |
free((void *)pp->uv); |
| 779 |
> |
free(pp->uv); |
| 780 |
|
if (pp->norm != NULL) |
| 781 |
< |
free((void *)pp->norm); |
| 781 |
> |
free(pp->norm); |
| 782 |
|
if (pp->xyz != NULL) |
| 783 |
< |
free((void *)pp->xyz); |
| 783 |
> |
free(pp->xyz); |
| 784 |
> |
if (pp->trimat != NULL) |
| 785 |
> |
free(pp->trimat); |
| 786 |
|
} |
| 787 |
< |
free((void *)ms->patch); |
| 787 |
> |
free(ms->patch); |
| 788 |
|
} |
| 789 |
|
if (ms->pseudo != NULL) |
| 790 |
< |
free((void *)ms->pseudo); |
| 791 |
< |
free((void *)ms); |
| 790 |
> |
free(ms->pseudo); |
| 791 |
> |
free(ms); |
| 792 |
|
} |
| 793 |
|
|
| 794 |
|
|
| 798 |
|
if (o->os == NULL) |
| 799 |
|
return; |
| 800 |
|
freemesh((*(MESHINST *)o->os).msh); |
| 801 |
< |
free((void *)o->os); |
| 801 |
> |
free(o->os); |
| 802 |
|
o->os = NULL; |
| 803 |
|
} |