| 604 |
|
fp->v[j].fnext = f; /* append new face */ |
| 605 |
|
} |
| 606 |
|
|
| 607 |
– |
/* Allocate an empty scene */ |
| 608 |
– |
Scene * |
| 609 |
– |
newScene(void) |
| 610 |
– |
{ |
| 611 |
– |
Scene *sc = (Scene *)ecalloc(1, sizeof(Scene)); |
| 612 |
– |
/* default group & material */ |
| 613 |
– |
sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps); |
| 614 |
– |
sc->grpname[sc->ngrps++] = savqstr("DEFAULT_GROUP"); |
| 615 |
– |
sc->matname = chunk_alloc(char *, sc->matname, sc->nmats); |
| 616 |
– |
sc->matname[sc->nmats++] = savqstr("DEFAULT_MATERIAL"); |
| 617 |
– |
|
| 618 |
– |
return(sc); |
| 619 |
– |
} |
| 620 |
– |
|
| 607 |
|
/* Add a vertex to a scene */ |
| 608 |
|
int |
| 609 |
|
addVertex(Scene *sc, double x, double y, double z) |
| 700 |
|
return(f); |
| 701 |
|
} |
| 702 |
|
|
| 703 |
+ |
/* Callback for growBoundingBox() */ |
| 704 |
+ |
static int |
| 705 |
+ |
addBBox(Scene *sc, Face *f, void *p) |
| 706 |
+ |
{ |
| 707 |
+ |
double (*bbox)[3] = (double (*)[3])p; |
| 708 |
+ |
int i, j; |
| 709 |
+ |
|
| 710 |
+ |
for (i = f->nv; i-- > 0; ) { |
| 711 |
+ |
double *p3 = sc->vert[f->v[i].vid].p; |
| 712 |
+ |
for (j = 3; j--; ) { |
| 713 |
+ |
if (p3[j] < bbox[0][j]) |
| 714 |
+ |
bbox[0][j] = p3[j]; |
| 715 |
+ |
if (p3[j] > bbox[1][j]) |
| 716 |
+ |
bbox[1][j] = p3[j]; |
| 717 |
+ |
} |
| 718 |
+ |
} |
| 719 |
+ |
return(1); |
| 720 |
+ |
} |
| 721 |
+ |
|
| 722 |
+ |
/* Expand bounding box min & max (initialize bbox to all zeroes) */ |
| 723 |
+ |
int |
| 724 |
+ |
growBoundingBox(Scene *sc, double bbox[2][3], int flreq, int flexc) |
| 725 |
+ |
{ |
| 726 |
+ |
if (sc == NULL || sc->nfaces <= 0 || bbox == NULL) |
| 727 |
+ |
return(0); |
| 728 |
+ |
|
| 729 |
+ |
if (VABSEQ(bbox[0], bbox[1])) { /* first run */ |
| 730 |
+ |
bbox[0][0] = bbox[0][1] = bbox[0][2] = FHUGE; |
| 731 |
+ |
bbox[1][0] = bbox[1][1] = bbox[1][2] = -FHUGE; |
| 732 |
+ |
} |
| 733 |
+ |
return(foreachFace(sc, addBBox, flreq, flexc, bbox)); |
| 734 |
+ |
} |
| 735 |
+ |
|
| 736 |
+ |
/* Allocate an empty scene */ |
| 737 |
+ |
Scene * |
| 738 |
+ |
newScene(void) |
| 739 |
+ |
{ |
| 740 |
+ |
Scene *sc = (Scene *)ecalloc(1, sizeof(Scene)); |
| 741 |
+ |
/* default group & material */ |
| 742 |
+ |
sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps); |
| 743 |
+ |
sc->grpname[sc->ngrps++] = savqstr("DEFAULT_GROUP"); |
| 744 |
+ |
sc->matname = chunk_alloc(char *, sc->matname, sc->nmats); |
| 745 |
+ |
sc->matname[sc->nmats++] = savqstr("DEFAULT_MATERIAL"); |
| 746 |
+ |
|
| 747 |
+ |
return(sc); |
| 748 |
+ |
} |
| 749 |
+ |
|
| 750 |
|
/* Duplicate a scene, optionally selecting faces */ |
| 751 |
|
Scene * |
| 752 |
|
dupScene(const Scene *osc, int flreq, int flexc) |
| 762 |
|
sc = newScene(); |
| 763 |
|
for (i = 0; i < osc->ndescr; i++) |
| 764 |
|
addComment(sc, osc->descr[i]); |
| 765 |
< |
if (osc->ngrps) { |
| 766 |
< |
sc->grpname = (char **)emalloc(sizeof(char *) * |
| 767 |
< |
(osc->ngrps+(CHUNKSIZ-1))); |
| 768 |
< |
for (i = 0; i < osc->ngrps; i++) |
| 765 |
> |
if (osc->ngrps > 1) { |
| 766 |
> |
sc->grpname = (char **)erealloc((char *)sc->grpname, |
| 767 |
> |
sizeof(char *) * (osc->ngrps+(CHUNKSIZ-1))); |
| 768 |
> |
for (i = 1; i < osc->ngrps; i++) |
| 769 |
|
sc->grpname[i] = savqstr(osc->grpname[i]); |
| 770 |
|
sc->ngrps = osc->ngrps; |
| 771 |
|
} |
| 772 |
< |
if (osc->nmats) { |
| 773 |
< |
sc->matname = (char **)emalloc(sizeof(char *) * |
| 774 |
< |
(osc->nmats+(CHUNKSIZ-1))); |
| 775 |
< |
for (i = 0; i < osc->nmats; i++) |
| 772 |
> |
if (osc->nmats > 1) { |
| 773 |
> |
sc->matname = (char **)erealloc((char *)sc->matname, |
| 774 |
> |
sizeof(char *) * (osc->nmats+(CHUNKSIZ-1))); |
| 775 |
> |
for (i = 1; i < osc->nmats; i++) |
| 776 |
|
sc->matname[i] = savqstr(osc->matname[i]); |
| 777 |
|
sc->nmats = osc->nmats; |
| 778 |
|
} |
| 779 |
|
if (osc->nverts) { |
| 780 |
|
sc->vert = (Vertex *)emalloc(sizeof(Vertex) * |
| 781 |
|
(osc->nverts+(CHUNKSIZ-1))); |
| 782 |
< |
memcpy((void *)sc->vert, (const void *)osc->vert, |
| 750 |
< |
sizeof(Vertex)*osc->nverts); |
| 782 |
> |
memcpy(sc->vert, osc->vert, sizeof(Vertex)*osc->nverts); |
| 783 |
|
sc->nverts = osc->nverts; |
| 784 |
|
for (i = 0; i < sc->nverts; i++) |
| 785 |
|
sc->vert[i].vflist = NULL; |
| 787 |
|
if (osc->ntex) { |
| 788 |
|
sc->tex = (TexCoord *)emalloc(sizeof(TexCoord) * |
| 789 |
|
(osc->ntex+(CHUNKSIZ-1))); |
| 790 |
< |
memcpy((void *)sc->tex, (const void *)osc->tex, |
| 759 |
< |
sizeof(TexCoord)*osc->ntex); |
| 790 |
> |
memcpy(sc->tex, osc->tex, sizeof(TexCoord)*osc->ntex); |
| 791 |
|
sc->ntex = osc->ntex; |
| 792 |
|
} |
| 793 |
|
if (osc->nnorms) { |
| 794 |
|
sc->norm = (Normal *)emalloc(sizeof(Normal) * |
| 795 |
|
(osc->nnorms+CHUNKSIZ)); |
| 796 |
< |
memcpy((void *)sc->norm, (const void *)osc->norm, |
| 766 |
< |
sizeof(Normal)*osc->nnorms); |
| 796 |
> |
memcpy(sc->norm, osc->norm, sizeof(Normal)*osc->nnorms); |
| 797 |
|
sc->nnorms = osc->nnorms; |
| 798 |
|
} |
| 799 |
|
for (fo = osc->flist; fo != NULL; fo = fo->next) { |
| 800 |
|
if ((fo->flags & fltest) != flreq) |
| 801 |
|
continue; |
| 802 |
< |
f = (Face *)emalloc(sizeof(Face) + |
| 803 |
< |
sizeof(VertEnt)*(fo->nv-3)); |
| 774 |
< |
memcpy((void *)f, (const void *)fo, sizeof(Face) + |
| 775 |
< |
sizeof(VertEnt)*(fo->nv-3)); |
| 802 |
> |
f = (Face *)emalloc(sizeof(Face) + sizeof(VertEnt)*(fo->nv-3)); |
| 803 |
> |
memcpy(f, fo, sizeof(Face) + sizeof(VertEnt)*(fo->nv-3)); |
| 804 |
|
for (i = 0; i < f->nv; i++) |
| 805 |
|
add2facelist(sc, f, i); |
| 806 |
|
f->next = sc->flist; |
| 817 |
|
{ |
| 818 |
|
VNDX my_vlist[4]; |
| 819 |
|
int *vert_map = NULL; |
| 820 |
< |
int *norm_map = NULL; |
| 821 |
< |
int *tex_map = NULL; |
| 820 |
> |
int tex_off = 0; |
| 821 |
> |
int norm_off = 0; |
| 822 |
|
VNDX *vlist = my_vlist; |
| 823 |
|
int vllen = sizeof(my_vlist)/sizeof(VNDX); |
| 824 |
|
int cur_mat = 0; |
| 832 |
|
if (scsrc->nfaces <= 0) |
| 833 |
|
return(0); |
| 834 |
|
/* map vertices */ |
| 835 |
< |
vert_map = (int *)emalloc(sizeof(int *)*scsrc->nverts); |
| 835 |
> |
vert_map = (int *)emalloc(sizeof(int)*scsrc->nverts); |
| 836 |
|
for (i = 0; i < scsrc->nverts; i++) { |
| 837 |
|
const Vertex *v = scsrc->vert + i; |
| 838 |
|
if (v->vflist == NULL) { |
| 841 |
|
} |
| 842 |
|
vert_map[i] = addVertex(scdst, v->p[0], v->p[1], v->p[2]); |
| 843 |
|
} |
| 844 |
< |
if (scsrc->ntex > 0) /* map texture coords */ |
| 845 |
< |
tex_map = (int *)emalloc(sizeof(int *)*scsrc->ntex); |
| 846 |
< |
for (i = 0; i < scsrc->ntex; i++) { |
| 847 |
< |
const TexCoord *t = scsrc->tex + i; |
| 848 |
< |
tex_map[i] = addTexture(scdst, t->u, t->v); |
| 844 |
> |
tex_off = scdst->ntex; /* append texture coords */ |
| 845 |
> |
if (scsrc->ntex > 0) { |
| 846 |
> |
scdst->tex = (TexCoord *)erealloc((char *)scdst->tex, |
| 847 |
> |
sizeof(TexCoord)*(tex_off+scsrc->ntex+(CHUNKSIZ-1))); |
| 848 |
> |
memcpy(scdst->tex+tex_off, scsrc->tex, |
| 849 |
> |
sizeof(TexCoord)*scsrc->ntex); |
| 850 |
|
} |
| 851 |
< |
if (scsrc->nnorms > 0) /* map normals */ |
| 852 |
< |
norm_map = (int *)emalloc(sizeof(int *)*scsrc->nnorms); |
| 853 |
< |
for (i = 0; i < scsrc->nnorms; i++) { |
| 854 |
< |
const float *n = scsrc->norm[i]; |
| 855 |
< |
norm_map[i] = addNormal(scdst, n[0], n[1], n[2]); |
| 851 |
> |
norm_off = scdst->nnorms; /* append normals */ |
| 852 |
> |
if (scsrc->nnorms > 0) { |
| 853 |
> |
scdst->norm = (Normal *)erealloc((char *)scdst->norm, |
| 854 |
> |
sizeof(Normal)*(norm_off+scsrc->nnorms+(CHUNKSIZ-1))); |
| 855 |
> |
memcpy(scdst->norm+norm_off, scsrc->norm, |
| 856 |
> |
sizeof(Normal)*scsrc->nnorms); |
| 857 |
|
} |
| 858 |
|
/* add faces */ |
| 859 |
|
scdst->lastgrp = scdst->lastmat = 0; |
| 863 |
|
if (f->mat != cur_mat) |
| 864 |
|
setMaterial(scdst, scsrc->matname[cur_mat = f->mat]); |
| 865 |
|
if (f->nv > vllen) { |
| 866 |
< |
if (vlist == my_vlist) |
| 867 |
< |
vlist = (VNDX *)emalloc( |
| 868 |
< |
sizeof(VNDX)*(vllen = f->nv)); |
| 869 |
< |
else |
| 840 |
< |
vlist = (VNDX *)erealloc((char *)vlist, |
| 841 |
< |
sizeof(VNDX)*(vllen = f->nv)); |
| 866 |
> |
vlist = (VNDX *)( vlist == my_vlist ? |
| 867 |
> |
emalloc(sizeof(VNDX)*f->nv) : |
| 868 |
> |
erealloc((char *)vlist, sizeof(VNDX)*f->nv) ); |
| 869 |
> |
vllen = f->nv; |
| 870 |
|
} |
| 871 |
|
memset(vlist, 0xff, sizeof(VNDX)*f->nv); |
| 872 |
|
for (i = f->nv; i-- > 0; ) { |
| 873 |
|
if (f->v[i].vid >= 0) |
| 874 |
|
vlist[i][0] = vert_map[f->v[i].vid]; |
| 875 |
|
if (f->v[i].tid >= 0) |
| 876 |
< |
vlist[i][1] = tex_map[f->v[i].tid]; |
| 876 |
> |
vlist[i][1] = f->v[i].tid + tex_off; |
| 877 |
|
if (f->v[i].nid >= 0) |
| 878 |
< |
vlist[i][2] = norm_map[f->v[i].nid]; |
| 878 |
> |
vlist[i][2] = f->v[i].nid + norm_off; |
| 879 |
|
} |
| 880 |
|
fcnt += (addFace(scdst, vlist, f->nv) != NULL); |
| 881 |
|
} |
| 882 |
|
/* clean up */ |
| 883 |
|
if (vlist != my_vlist) efree((char *)vlist); |
| 856 |
– |
if (norm_map != NULL) efree((char *)norm_map); |
| 857 |
– |
if (tex_map != NULL) efree((char *)tex_map); |
| 884 |
|
efree((char *)vert_map); |
| 885 |
|
return(fcnt); |
| 886 |
|
} |
| 1038 |
|
if (!vmap[i]) |
| 1039 |
|
continue; |
| 1040 |
|
if (nused != i) |
| 1041 |
< |
memcpy((void *)sc->norm[nused], |
| 1016 |
< |
(void *)sc->norm[i], |
| 1017 |
< |
sizeof(Normal)); |
| 1041 |
> |
memcpy(sc->norm[nused], sc->norm[i], sizeof(Normal)); |
| 1042 |
|
vmap[i] = nused++; |
| 1043 |
|
} |
| 1044 |
|
if (nused == sc->nnorms) |