| 8 |
|
#include "standard.h" |
| 9 |
|
#include "octree.h" |
| 10 |
|
#include "object.h" |
| 11 |
+ |
#include "otypes.h" |
| 12 |
|
#include "mesh.h" |
| 12 |
– |
#include "lookup.h" |
| 13 |
|
|
| 14 |
|
/* An encoded mesh vertex */ |
| 15 |
|
typedef struct { |
| 135 |
|
|
| 136 |
|
|
| 137 |
|
int |
| 138 |
< |
getmeshtrivid(tvid, mp, ti) /* get triangle vertex ID's */ |
| 139 |
< |
int4 tvid[3]; |
| 140 |
< |
register MESH *mp; |
| 141 |
< |
OBJECT ti; |
| 138 |
> |
getmeshtrivid(tvid, mo, mp, ti) /* get triangle vertex ID's */ |
| 139 |
> |
int4 tvid[3]; |
| 140 |
> |
OBJECT *mo; |
| 141 |
> |
MESH *mp; |
| 142 |
> |
OBJECT ti; |
| 143 |
|
{ |
| 144 |
|
int pn = ti >> 10; |
| 145 |
+ |
MESHPATCH *pp; |
| 146 |
|
|
| 147 |
|
if (pn >= mp->npatches) |
| 148 |
|
return(0); |
| 149 |
+ |
pp = &mp->patch[pn]; |
| 150 |
|
ti &= 0x3ff; |
| 151 |
|
if (!(ti & 0x200)) { /* local triangle */ |
| 152 |
|
struct PTri *tp; |
| 153 |
< |
if (ti >= mp->patch[pn].ntris) |
| 153 |
> |
if (ti >= pp->ntris) |
| 154 |
|
return(0); |
| 155 |
< |
tp = &mp->patch[pn].tri[ti]; |
| 155 |
> |
tp = &pp->tri[ti]; |
| 156 |
|
tvid[0] = tvid[1] = tvid[2] = pn << 8; |
| 157 |
|
tvid[0] |= tp->v1; |
| 158 |
|
tvid[1] |= tp->v2; |
| 159 |
|
tvid[2] |= tp->v3; |
| 160 |
+ |
if (pp->trimat != NULL) |
| 161 |
+ |
*mo = pp->trimat[ti]; |
| 162 |
+ |
else |
| 163 |
+ |
*mo = pp->solemat; |
| 164 |
+ |
if (*mo != OVOID) |
| 165 |
+ |
*mo += mp->mat0; |
| 166 |
|
return(1); |
| 167 |
|
} |
| 168 |
|
ti &= ~0x200; |
| 169 |
|
if (!(ti & 0x100)) { /* single link vertex */ |
| 170 |
|
struct PJoin1 *tp1; |
| 171 |
< |
if (ti >= mp->patch[pn].nj1tris) |
| 171 |
> |
if (ti >= pp->nj1tris) |
| 172 |
|
return(0); |
| 173 |
< |
tp1 = &mp->patch[pn].j1tri[ti]; |
| 173 |
> |
tp1 = &pp->j1tri[ti]; |
| 174 |
|
tvid[0] = tp1->v1j; |
| 175 |
|
tvid[1] = tvid[2] = pn << 8; |
| 176 |
|
tvid[1] |= tp1->v2; |
| 177 |
|
tvid[2] |= tp1->v3; |
| 178 |
+ |
if ((*mo = tp1->mat) != OVOID) |
| 179 |
+ |
*mo += mp->mat0; |
| 180 |
|
return(1); |
| 181 |
|
} |
| 182 |
|
ti &= ~0x100; |
| 183 |
|
{ /* double link vertex */ |
| 184 |
|
struct PJoin2 *tp2; |
| 185 |
< |
if (ti >= mp->patch[pn].nj2tris) |
| 185 |
> |
if (ti >= pp->nj2tris) |
| 186 |
|
return(0); |
| 187 |
< |
tp2 = &mp->patch[pn].j2tri[ti]; |
| 187 |
> |
tp2 = &pp->j2tri[ti]; |
| 188 |
|
tvid[0] = tp2->v1j; |
| 189 |
|
tvid[1] = tp2->v2j; |
| 190 |
|
tvid[2] = pn << 8 | tp2->v3; |
| 191 |
+ |
if ((*mo = tp2->mat) != OVOID) |
| 192 |
+ |
*mo += mp->mat0; |
| 193 |
|
} |
| 194 |
|
return(1); |
| 195 |
|
} |
| 197 |
|
|
| 198 |
|
int |
| 199 |
|
getmeshvert(vp, mp, vid, what) /* get triangle vertex from ID */ |
| 200 |
< |
MESHVERT *vp; |
| 201 |
< |
register MESH *mp; |
| 202 |
< |
int4 vid; |
| 203 |
< |
int what; |
| 200 |
> |
MESHVERT*vp; |
| 201 |
> |
MESH *mp; |
| 202 |
> |
int4 vid; |
| 203 |
> |
int what; |
| 204 |
|
{ |
| 205 |
|
int pn = vid >> 8; |
| 206 |
|
MESHPATCH *pp; |
| 239 |
|
} |
| 240 |
|
|
| 241 |
|
|
| 242 |
+ |
OBJREC * |
| 243 |
+ |
getmeshpseudo(mp, mo) /* get mesh pseudo object for material */ |
| 244 |
+ |
MESH *mp; |
| 245 |
+ |
OBJECT mo; |
| 246 |
+ |
{ |
| 247 |
+ |
if (mo < mp->mat0 || mo >= mp->mat0 + mp->nmats) |
| 248 |
+ |
error(INTERNAL, "modifier out of range in getmeshpseudo"); |
| 249 |
+ |
if (mp->pseudo == NULL) { |
| 250 |
+ |
register int i; |
| 251 |
+ |
mp->pseudo = (OBJREC *)calloc(mp->nmats, sizeof(OBJREC)); |
| 252 |
+ |
if (mp->pseudo == NULL) |
| 253 |
+ |
error(SYSTEM, "out of memory in getmeshpseudo"); |
| 254 |
+ |
for (i = mp->nmats; i--; ) { |
| 255 |
+ |
mp->pseudo[i].omod = mp->mat0 + i; |
| 256 |
+ |
mp->pseudo[i].otype = OBJ_FACE; |
| 257 |
+ |
mp->pseudo[i].oname = "M-Tri"; |
| 258 |
+ |
} |
| 259 |
+ |
} |
| 260 |
+ |
return(&mp->pseudo[mo - mp->mat0]); |
| 261 |
+ |
} |
| 262 |
+ |
|
| 263 |
+ |
|
| 264 |
|
int |
| 265 |
< |
getmeshtri(tv, mp, ti, what) /* get triangle vertices */ |
| 265 |
> |
getmeshtri(tv, mo, mp, ti, wha) /* get triangle vertices */ |
| 266 |
|
MESHVERT tv[3]; |
| 267 |
+ |
OBJECT *mo; |
| 268 |
|
MESH *mp; |
| 269 |
|
OBJECT ti; |
| 270 |
< |
int what; |
| 270 |
> |
int wha; |
| 271 |
|
{ |
| 272 |
|
int4 tvid[3]; |
| 273 |
|
|
| 274 |
< |
if (!getmeshtrivid(tvid, mp, ti)) |
| 274 |
> |
if (!getmeshtrivid(tvid, mo, mp, ti)) |
| 275 |
|
return(0); |
| 276 |
|
|
| 277 |
< |
getmeshvert(&tv[0], mp, tvid[0], what); |
| 278 |
< |
getmeshvert(&tv[1], mp, tvid[1], what); |
| 279 |
< |
getmeshvert(&tv[2], mp, tvid[2], what); |
| 277 |
> |
getmeshvert(&tv[0], mp, tvid[0], wha); |
| 278 |
> |
getmeshvert(&tv[1], mp, tvid[1], wha); |
| 279 |
> |
getmeshvert(&tv[2], mp, tvid[2], wha); |
| 280 |
|
|
| 281 |
|
return(tv[0].fl & tv[1].fl & tv[2].fl); |
| 282 |
|
} |
| 287 |
|
register MESH *mp; |
| 288 |
|
MESHVERT *vp; |
| 289 |
|
{ |
| 254 |
– |
LUTAB *ltp; |
| 290 |
|
LUENT *lvp; |
| 291 |
|
MCVERT cv; |
| 292 |
|
register int i; |
| 316 |
|
(mp->uvlim[1][i] - mp->uvlim[0][i])); |
| 317 |
|
} |
| 318 |
|
cv.fl = vp->fl; |
| 319 |
< |
ltp = (LUTAB *)mp->cdata; /* get lookup table */ |
| 320 |
< |
if (ltp == NULL) { |
| 321 |
< |
ltp = (LUTAB *)calloc(1, sizeof(LUTAB)); |
| 322 |
< |
if (ltp == NULL) |
| 319 |
> |
if (mp->lut.tsiz == 0) { |
| 320 |
> |
mp->lut.hashf = cvhash; |
| 321 |
> |
mp->lut.keycmp = cvcmp; |
| 322 |
> |
mp->lut.freek = free; |
| 323 |
> |
if (!lu_init(&mp->lut, 50000)) |
| 324 |
|
goto nomem; |
| 289 |
– |
ltp->hashf = cvhash; |
| 290 |
– |
ltp->keycmp = cvcmp; |
| 291 |
– |
ltp->freek = free; |
| 292 |
– |
if (!lu_init(ltp, 50000)) |
| 293 |
– |
goto nomem; |
| 294 |
– |
mp->cdata = (char *)ltp; |
| 325 |
|
} |
| 326 |
|
/* find entry */ |
| 327 |
< |
lvp = lu_find(ltp, (char *)&cv); |
| 327 |
> |
lvp = lu_find(&mp->lut, (char *)&cv); |
| 328 |
|
if (lvp == NULL) |
| 329 |
|
goto nomem; |
| 330 |
|
if (lvp->key == NULL) { |
| 389 |
|
|
| 390 |
|
|
| 391 |
|
OBJECT |
| 392 |
< |
addmeshtri(mp, tv) /* add a new mesh triangle */ |
| 392 |
> |
addmeshtri(mp, tv, mo) /* add a new mesh triangle */ |
| 393 |
|
MESH *mp; |
| 394 |
|
MESHVERT tv[3]; |
| 395 |
+ |
OBJECT mo; |
| 396 |
|
{ |
| 397 |
|
int4 vid[3], t; |
| 398 |
|
int pn[3], i; |
| 406 |
|
return(OVOID); |
| 407 |
|
pn[i] = vid[i] >> 8; |
| 408 |
|
} |
| 409 |
+ |
/* normalize material index */ |
| 410 |
+ |
if (mo != OVOID) |
| 411 |
+ |
if ((mo -= mp->mat0) >= mp->nmats) |
| 412 |
+ |
mp->nmats = mo+1; |
| 413 |
+ |
else if (mo < 0) |
| 414 |
+ |
error(INTERNAL, "modifier range error in addmeshtri"); |
| 415 |
|
/* assign triangle */ |
| 416 |
|
if (pn[0] == pn[1] && pn[1] == pn[2]) { /* local case */ |
| 417 |
|
pp = &mp->patch[pn[0]]; |
| 425 |
|
pp->tri[pp->ntris].v1 = vid[0] & 0xff; |
| 426 |
|
pp->tri[pp->ntris].v2 = vid[1] & 0xff; |
| 427 |
|
pp->tri[pp->ntris].v3 = vid[2] & 0xff; |
| 428 |
+ |
if (pp->ntris == 0) |
| 429 |
+ |
pp->solemat = mo; |
| 430 |
+ |
else if (pp->trimat == NULL && mo != pp->solemat) { |
| 431 |
+ |
pp->trimat = (int2 *)malloc( |
| 432 |
+ |
512*sizeof(int2)); |
| 433 |
+ |
if (pp->trimat == NULL) |
| 434 |
+ |
goto nomem; |
| 435 |
+ |
for (i = pp->ntris; i--; ) |
| 436 |
+ |
pp->trimat[i] = pp->solemat; |
| 437 |
+ |
} |
| 438 |
+ |
if (pp->trimat != NULL) |
| 439 |
+ |
pp->trimat[pp->ntris] = mo; |
| 440 |
|
return(pn[0] << 10 | pp->ntris++); |
| 441 |
|
} |
| 442 |
|
} |
| 459 |
|
pp->j1tri[pp->nj1tris].v1j = vid[0]; |
| 460 |
|
pp->j1tri[pp->nj1tris].v2 = vid[1] & 0xff; |
| 461 |
|
pp->j1tri[pp->nj1tris].v3 = vid[2] & 0xff; |
| 462 |
+ |
pp->j1tri[pp->nj1tris].mat = mo; |
| 463 |
|
return(pn[1] << 10 | 0x200 | pp->nj1tris++); |
| 464 |
|
} |
| 465 |
|
} |
| 476 |
|
pp->j2tri[pp->nj2tris].v1j = vid[0]; |
| 477 |
|
pp->j2tri[pp->nj2tris].v2j = vid[1]; |
| 478 |
|
pp->j2tri[pp->nj2tris].v3 = vid[2] & 0xff; |
| 479 |
+ |
pp->j2tri[pp->nj2tris].mat = mo; |
| 480 |
|
return(pn[2] << 10 | 0x300 | pp->nj2tris++); |
| 481 |
|
nomem: |
| 482 |
|
error(SYSTEM, "out of memory in addmeshtri"); |
| 484 |
|
} |
| 485 |
|
|
| 486 |
|
|
| 487 |
+ |
char * |
| 488 |
+ |
checkmesh(mp) /* validate mesh data */ |
| 489 |
+ |
register MESH *mp; |
| 490 |
+ |
{ |
| 491 |
+ |
static char embuf[128]; |
| 492 |
+ |
int nouvbounds = 1; |
| 493 |
+ |
register int i; |
| 494 |
+ |
/* basic checks */ |
| 495 |
+ |
if (mp == NULL) |
| 496 |
+ |
return("NULL mesh pointer"); |
| 497 |
+ |
if (!mp->ldflags) |
| 498 |
+ |
return("unassigned mesh"); |
| 499 |
+ |
if (mp->name == NULL) |
| 500 |
+ |
return("missing mesh name"); |
| 501 |
+ |
if (mp->nref <= 0) |
| 502 |
+ |
return("unreferenced mesh"); |
| 503 |
+ |
/* check boundaries */ |
| 504 |
+ |
if (mp->ldflags & IO_BOUNDS) { |
| 505 |
+ |
if (mp->mcube.cusize <= FTINY) |
| 506 |
+ |
return("illegal octree bounds in mesh"); |
| 507 |
+ |
nouvbounds = (mp->uvlim[0][1] - mp->uvlim[0][0] <= FTINY || |
| 508 |
+ |
mp->uvlim[1][1] - mp->uvlim[1][0] <= FTINY); |
| 509 |
+ |
} |
| 510 |
+ |
/* check octree */ |
| 511 |
+ |
if (mp->ldflags & IO_TREE) { |
| 512 |
+ |
if (isempty(mp->mcube.cutree)) |
| 513 |
+ |
error(WARNING, "empty mesh octree"); |
| 514 |
+ |
} |
| 515 |
+ |
/* check scene data */ |
| 516 |
+ |
if (mp->ldflags & IO_SCENE) { |
| 517 |
+ |
if (!(mp->ldflags & IO_BOUNDS)) |
| 518 |
+ |
return("unbounded scene in mesh"); |
| 519 |
+ |
if (mp->mat0 < 0 || mp->mat0+mp->nmats > nobjects) |
| 520 |
+ |
return("bad mesh modifier range"); |
| 521 |
+ |
for (i = mp->mat0+mp->nmats; i-- > mp->mat0; ) { |
| 522 |
+ |
int otyp = objptr(i)->otype; |
| 523 |
+ |
if (!ismodifier(otyp)) { |
| 524 |
+ |
sprintf(embuf, |
| 525 |
+ |
"non-modifier in mesh (%s \"%s\")", |
| 526 |
+ |
ofun[otyp].funame, objptr(i)->oname); |
| 527 |
+ |
return(embuf); |
| 528 |
+ |
} |
| 529 |
+ |
} |
| 530 |
+ |
if (mp->npatches <= 0) |
| 531 |
+ |
error(WARNING, "no patches in mesh"); |
| 532 |
+ |
for (i = 0; i < mp->npatches; i++) { |
| 533 |
+ |
register MESHPATCH *pp = &mp->patch[i]; |
| 534 |
+ |
if (pp->nverts <= 0) |
| 535 |
+ |
error(WARNING, "no vertices in patch"); |
| 536 |
+ |
else { |
| 537 |
+ |
if (pp->xyz == NULL) |
| 538 |
+ |
return("missing patch vertex list"); |
| 539 |
+ |
if (nouvbounds && pp->uv != NULL) |
| 540 |
+ |
return("unreferenced uv coordinates"); |
| 541 |
+ |
} |
| 542 |
+ |
if (pp->ntris + pp->nj1tris + pp->nj2tris <= 0) |
| 543 |
+ |
error(WARNING, "no triangles in patch"); |
| 544 |
+ |
if (pp->ntris > 0 && pp->tri == NULL) |
| 545 |
+ |
return("missing patch triangle list"); |
| 546 |
+ |
if (pp->nj1tris > 0 && pp->j1tri == NULL) |
| 547 |
+ |
return("missing patch joiner triangle list"); |
| 548 |
+ |
if (pp->nj2tris > 0 && pp->j2tri == NULL) |
| 549 |
+ |
return("missing patch double-joiner list"); |
| 550 |
+ |
} |
| 551 |
+ |
} |
| 552 |
+ |
return(NULL); /* seems OK */ |
| 553 |
+ |
} |
| 554 |
+ |
|
| 555 |
+ |
|
| 556 |
|
static void |
| 557 |
|
tallyoctree(ot, ecp, lcp, ocp) /* tally octree size */ |
| 558 |
|
OCTREE ot; |
| 608 |
|
t2cnt += pp->nj2tris; |
| 609 |
|
} |
| 610 |
|
fprintf(fp, "Mesh statistics:\n"); |
| 611 |
+ |
fprintf(fp, "\t%d materials\n", ms->nmats); |
| 612 |
|
fprintf(fp, "\t%d patches (%.2f MBytes)\n", ms->npatches, |
| 613 |
|
(ms->npatches*sizeof(MESHPATCH) + |
| 614 |
|
vcnt*3*sizeof(uint4) + |
| 632 |
|
|
| 633 |
|
void |
| 634 |
|
freemesh(ms) /* free mesh data */ |
| 635 |
< |
MESH *ms; |
| 635 |
> |
register MESH *ms; |
| 636 |
|
{ |
| 637 |
|
MESH mhead; |
| 638 |
|
MESH *msp; |
| 658 |
|
/* free mesh data */ |
| 659 |
|
freestr(ms->name); |
| 660 |
|
octfree(ms->mcube.cutree); |
| 661 |
< |
if (ms->cdata != NULL) |
| 541 |
< |
lu_done((LUTAB *)ms->cdata); |
| 661 |
> |
lu_done(&ms->lut); |
| 662 |
|
if (ms->npatches > 0) { |
| 663 |
|
register MESHPATCH *pp = ms->patch + ms->npatches; |
| 664 |
|
while (pp-- > ms->patch) { |
| 677 |
|
} |
| 678 |
|
free((void *)ms->patch); |
| 679 |
|
} |
| 680 |
+ |
if (ms->pseudo != NULL) |
| 681 |
+ |
free((void *)ms->pseudo); |
| 682 |
|
free((void *)ms); |
| 683 |
|
} |
| 684 |
|
|