5 |
|
* Mesh support routines |
6 |
|
*/ |
7 |
|
|
8 |
– |
#include <string.h> |
9 |
– |
|
8 |
|
#include "rtio.h" |
9 |
|
#include "rtmath.h" |
10 |
|
#include "rterror.h" |
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; |
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)); |
393 |
< |
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) |
397 |
< |
error(INTERNAL, "too many mesh patches"); |
395 |
> |
mp->npatches++; |
396 |
|
} |
397 |
|
pp = &mp->patch[mp->npatches-1]; |
398 |
|
if (pp->xyz == NULL) { |
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 |
|
} |