| 1 |
#ifndef lint |
| 2 |
static const char RCSid[] = "$Id$"; |
| 3 |
#endif |
| 4 |
/* |
| 5 |
* Mesh support routines |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "standard.h" |
| 9 |
#include "octree.h" |
| 10 |
#include "object.h" |
| 11 |
#include "mesh.h" |
| 12 |
#include "lookup.h" |
| 13 |
|
| 14 |
/* An encoded mesh vertex */ |
| 15 |
typedef struct { |
| 16 |
int fl; |
| 17 |
uint4 xyz[3]; |
| 18 |
int4 norm; |
| 19 |
int4 uv[2]; |
| 20 |
} MCVERT; |
| 21 |
|
| 22 |
#define MPATCHBLKSIZ 128 /* patch allocation block size */ |
| 23 |
|
| 24 |
#define IO_LEGAL (IO_BOUNDS|IO_TREE|IO_SCENE) |
| 25 |
|
| 26 |
static MESH *mlist = NULL; /* list of loaded meshes */ |
| 27 |
|
| 28 |
|
| 29 |
static unsigned long |
| 30 |
cvhash(cvp) /* hash an encoded vertex */ |
| 31 |
MCVERT *cvp; |
| 32 |
{ |
| 33 |
unsigned long hval; |
| 34 |
|
| 35 |
if (!(cvp->fl & MT_V)) |
| 36 |
return(0); |
| 37 |
hval = cvp->xyz[0] ^ cvp->xyz[1] << 11 ^ cvp->xyz[2] << 22; |
| 38 |
if (cvp->fl & MT_N) |
| 39 |
hval ^= cvp->norm; |
| 40 |
if (cvp->fl & MT_UV) |
| 41 |
hval ^= cvp->uv[0] ^ cvp->uv[1] << 16; |
| 42 |
return(hval); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
static int |
| 47 |
cvcmp(v1, v2) /* compare encoded vertices */ |
| 48 |
register MCVERT *v1, *v2; |
| 49 |
{ |
| 50 |
if (v1->fl != v2->fl) |
| 51 |
return(1); |
| 52 |
if (v1->xyz[0] != v2->xyz[0]) |
| 53 |
return(1); |
| 54 |
if (v1->xyz[1] != v2->xyz[1]) |
| 55 |
return(1); |
| 56 |
if (v1->xyz[2] != v2->xyz[2]) |
| 57 |
return(1); |
| 58 |
if (v1->fl & MT_N && v1->norm != v2->norm) |
| 59 |
return(1); |
| 60 |
if (v1->fl & MT_UV) { |
| 61 |
if (v1->uv[0] != v2->uv[0]) |
| 62 |
return(1); |
| 63 |
if (v1->uv[1] != v2->uv[1]) |
| 64 |
return(1); |
| 65 |
} |
| 66 |
return(0); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
MESH * |
| 71 |
getmesh(mname, flags) /* get mesh data */ |
| 72 |
char *mname; |
| 73 |
int flags; |
| 74 |
{ |
| 75 |
char *pathname; |
| 76 |
register MESH *ms; |
| 77 |
|
| 78 |
flags &= IO_LEGAL; |
| 79 |
for (ms = mlist; ms != NULL; ms = ms->next) |
| 80 |
if (!strcmp(mname, ms->name)) { |
| 81 |
if ((ms->ldflags & flags) == flags) { |
| 82 |
ms->nref++; |
| 83 |
return(ms); /* loaded */ |
| 84 |
} |
| 85 |
break; /* load the rest */ |
| 86 |
} |
| 87 |
if (ms == NULL) { |
| 88 |
ms = (MESH *)calloc(1, sizeof(MESH)); |
| 89 |
if (ms == NULL) |
| 90 |
error(SYSTEM, "out of memory in getmesh"); |
| 91 |
ms->name = savestr(mname); |
| 92 |
ms->nref = 1; |
| 93 |
ms->mcube.cutree = EMPTY; |
| 94 |
ms->next = mlist; |
| 95 |
mlist = ms; |
| 96 |
} |
| 97 |
if ((pathname = getpath(mname, getlibpath(), R_OK)) == NULL) { |
| 98 |
sprintf(errmsg, "cannot find mesh file \"%s\"", mname); |
| 99 |
error(USER, errmsg); |
| 100 |
} |
| 101 |
flags &= ~ms->ldflags; |
| 102 |
if (flags) |
| 103 |
readmesh(ms, pathname, flags); |
| 104 |
return(ms); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
MESHINST * |
| 109 |
getmeshinst(o, flags) /* create mesh instance */ |
| 110 |
OBJREC *o; |
| 111 |
int flags; |
| 112 |
{ |
| 113 |
register MESHINST *ins; |
| 114 |
|
| 115 |
flags &= IO_LEGAL; |
| 116 |
if ((ins = (MESHINST *)o->os) == NULL) { |
| 117 |
if ((ins = (MESHINST *)malloc(sizeof(MESHINST))) == NULL) |
| 118 |
error(SYSTEM, "out of memory in getmeshinst"); |
| 119 |
if (o->oargs.nsargs < 1) |
| 120 |
objerror(o, USER, "bad # of arguments"); |
| 121 |
if (fullxf(&ins->x, o->oargs.nsargs-1, |
| 122 |
o->oargs.sarg+1) != o->oargs.nsargs-1) |
| 123 |
objerror(o, USER, "bad transform"); |
| 124 |
if (ins->x.f.sca < 0.0) { |
| 125 |
ins->x.f.sca = -ins->x.f.sca; |
| 126 |
ins->x.b.sca = -ins->x.b.sca; |
| 127 |
} |
| 128 |
ins->msh = NULL; |
| 129 |
o->os = (char *)ins; |
| 130 |
} |
| 131 |
if (ins->msh == NULL || (ins->msh->ldflags & flags) != flags) |
| 132 |
ins->msh = getmesh(o->oargs.sarg[0], flags); |
| 133 |
return(ins); |
| 134 |
} |
| 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; |
| 142 |
{ |
| 143 |
int pn = ti >> 10; |
| 144 |
|
| 145 |
if (pn >= mp->npatches) |
| 146 |
return(0); |
| 147 |
ti &= 0x3ff; |
| 148 |
if (!(ti & 0x200)) { /* local triangle */ |
| 149 |
struct PTri *tp; |
| 150 |
if (ti >= mp->patch[pn].ntris) |
| 151 |
return(0); |
| 152 |
tp = &mp->patch[pn].tri[ti]; |
| 153 |
tvid[0] = tvid[1] = tvid[2] = pn << 8; |
| 154 |
tvid[0] |= tp->v1; |
| 155 |
tvid[1] |= tp->v2; |
| 156 |
tvid[2] |= tp->v3; |
| 157 |
return(1); |
| 158 |
} |
| 159 |
ti &= ~0x200; |
| 160 |
if (!(ti & 0x100)) { /* single link vertex */ |
| 161 |
struct PJoin1 *tp1; |
| 162 |
if (ti >= mp->patch[pn].nj1tris) |
| 163 |
return(0); |
| 164 |
tp1 = &mp->patch[pn].j1tri[ti]; |
| 165 |
tvid[0] = tp1->v1j; |
| 166 |
tvid[1] = tvid[2] = pn << 8; |
| 167 |
tvid[1] |= tp1->v2; |
| 168 |
tvid[2] |= tp1->v3; |
| 169 |
return(1); |
| 170 |
} |
| 171 |
ti &= ~0x100; |
| 172 |
{ /* double link vertex */ |
| 173 |
struct PJoin2 *tp2; |
| 174 |
if (ti >= mp->patch[pn].nj2tris) |
| 175 |
return(0); |
| 176 |
tp2 = &mp->patch[pn].j2tri[ti]; |
| 177 |
tvid[0] = tp2->v1j; |
| 178 |
tvid[1] = tp2->v2j; |
| 179 |
tvid[2] = pn << 8 | tp2->v3; |
| 180 |
} |
| 181 |
return(1); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
int |
| 186 |
getmeshvert(vp, mp, vid, what) /* get triangle vertex from ID */ |
| 187 |
MESHVERT *vp; |
| 188 |
register MESH *mp; |
| 189 |
int4 vid; |
| 190 |
int what; |
| 191 |
{ |
| 192 |
int pn = vid >> 8; |
| 193 |
MESHPATCH *pp; |
| 194 |
double vres; |
| 195 |
register int i; |
| 196 |
|
| 197 |
vp->fl = 0; |
| 198 |
if (pn >= mp->npatches) |
| 199 |
return(0); |
| 200 |
pp = &mp->patch[pn]; |
| 201 |
vid &= 0xff; |
| 202 |
if (vid >= pp->nverts) |
| 203 |
return(0); |
| 204 |
/* get location */ |
| 205 |
if (what & MT_V) { |
| 206 |
vres = (1./4294967296.)*mp->mcube.cusize; |
| 207 |
for (i = 0; i < 3; i++) |
| 208 |
vp->v[i] = mp->mcube.cuorg[i] + |
| 209 |
(pp->xyz[vid][i] + .5)*vres; |
| 210 |
vp->fl |= MT_V; |
| 211 |
} |
| 212 |
/* get normal */ |
| 213 |
if (what & MT_N && pp->norm != NULL && pp->norm[vid]) { |
| 214 |
decodedir(vp->n, pp->norm[vid]); |
| 215 |
vp->fl |= MT_N; |
| 216 |
} |
| 217 |
/* get (u,v) */ |
| 218 |
if (what & MT_UV && pp->uv != NULL && pp->uv[vid][0]) { |
| 219 |
for (i = 0; i < 2; i++) |
| 220 |
vp->uv[i] = mp->uvlim[i][0] + |
| 221 |
(mp->uvlim[i][1] - mp->uvlim[i][0])* |
| 222 |
(pp->uv[vid][i] + .5)*(1./4294967296.); |
| 223 |
vp->fl |= MT_UV; |
| 224 |
} |
| 225 |
return(vp->fl); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
int |
| 230 |
getmeshtri(tv, mp, ti, what) /* get triangle vertices */ |
| 231 |
MESHVERT tv[3]; |
| 232 |
MESH *mp; |
| 233 |
OBJECT ti; |
| 234 |
int what; |
| 235 |
{ |
| 236 |
int4 tvid[3]; |
| 237 |
|
| 238 |
if (!getmeshtrivid(tvid, mp, ti)) |
| 239 |
return(0); |
| 240 |
|
| 241 |
getmeshvert(&tv[0], mp, tvid[0], what); |
| 242 |
getmeshvert(&tv[1], mp, tvid[1], what); |
| 243 |
getmeshvert(&tv[2], mp, tvid[2], what); |
| 244 |
|
| 245 |
return(tv[0].fl & tv[1].fl & tv[2].fl); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
int4 |
| 250 |
addmeshvert(mp, vp) /* find/add a mesh vertex */ |
| 251 |
register MESH *mp; |
| 252 |
MESHVERT *vp; |
| 253 |
{ |
| 254 |
LUTAB *ltp; |
| 255 |
LUENT *lvp; |
| 256 |
MCVERT cv; |
| 257 |
register int i; |
| 258 |
|
| 259 |
if (!(vp->fl & MT_V)) |
| 260 |
return(-1); |
| 261 |
/* encode vertex */ |
| 262 |
for (i = 0; i < 3; i++) { |
| 263 |
if (vp->v[i] < mp->mcube.cuorg[i]) |
| 264 |
return(-1); |
| 265 |
if (vp->v[i] >= mp->mcube.cuorg[i] + mp->mcube.cusize) |
| 266 |
return(-1); |
| 267 |
cv.xyz[i] = (uint4)(4294967296. * |
| 268 |
(vp->v[i] - mp->mcube.cuorg[i]) / |
| 269 |
mp->mcube.cusize); |
| 270 |
} |
| 271 |
if (vp->fl & MT_N) |
| 272 |
cv.norm = encodedir(vp->n); |
| 273 |
if (vp->fl & MT_UV) |
| 274 |
for (i = 0; i < 2; i++) { |
| 275 |
if (vp->uv[i] <= mp->uvlim[i][0]) |
| 276 |
return(-1); |
| 277 |
if (vp->uv[i] >= mp->uvlim[i][1]) |
| 278 |
return(-1); |
| 279 |
cv.uv[i] = (uint4)(4294967296. * |
| 280 |
(vp->uv[i] - mp->uvlim[i][0]) / |
| 281 |
(mp->uvlim[i][1] - mp->uvlim[i][0])); |
| 282 |
} |
| 283 |
cv.fl = vp->fl; |
| 284 |
ltp = (LUTAB *)mp->cdata; /* get lookup table */ |
| 285 |
if (ltp == NULL) { |
| 286 |
ltp = (LUTAB *)calloc(1, sizeof(LUTAB)); |
| 287 |
if (ltp == NULL) |
| 288 |
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; |
| 295 |
} |
| 296 |
/* find entry */ |
| 297 |
lvp = lu_find(ltp, (char *)&cv); |
| 298 |
if (lvp == NULL) |
| 299 |
goto nomem; |
| 300 |
if (lvp->key == NULL) { |
| 301 |
lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int4)); |
| 302 |
bcopy((void *)&cv, (void *)lvp->key, sizeof(MCVERT)); |
| 303 |
} |
| 304 |
if (lvp->data == NULL) { /* new vertex */ |
| 305 |
register MESHPATCH *pp; |
| 306 |
if (mp->npatches <= 0) { |
| 307 |
mp->patch = (MESHPATCH *)calloc(MPATCHBLKSIZ, |
| 308 |
sizeof(MESHPATCH)); |
| 309 |
if (mp->patch == NULL) |
| 310 |
goto nomem; |
| 311 |
mp->npatches = 1; |
| 312 |
} else if (mp->patch[mp->npatches-1].nverts >= 256) { |
| 313 |
if (mp->npatches % MPATCHBLKSIZ == 0) { |
| 314 |
mp->patch = (MESHPATCH *)realloc( |
| 315 |
(void *)mp->patch, |
| 316 |
(mp->npatches + MPATCHBLKSIZ)* |
| 317 |
sizeof(MESHPATCH)); |
| 318 |
bzero((void *)(mp->patch + mp->npatches), |
| 319 |
MPATCHBLKSIZ*sizeof(MESHPATCH)); |
| 320 |
} |
| 321 |
if (mp->npatches++ >= 1<<20) |
| 322 |
error(INTERNAL, "too many mesh patches"); |
| 323 |
} |
| 324 |
pp = &mp->patch[mp->npatches-1]; |
| 325 |
if (pp->xyz == NULL) { |
| 326 |
pp->xyz = (uint4 (*)[3])calloc(256, 3*sizeof(int4)); |
| 327 |
if (pp->xyz == NULL) |
| 328 |
goto nomem; |
| 329 |
} |
| 330 |
for (i = 0; i < 3; i++) |
| 331 |
pp->xyz[pp->nverts][i] = cv.xyz[i]; |
| 332 |
if (cv.fl & MT_N) { |
| 333 |
if (pp->norm == NULL) { |
| 334 |
pp->norm = (int4 *)calloc(256, sizeof(int4)); |
| 335 |
if (pp->norm == NULL) |
| 336 |
goto nomem; |
| 337 |
} |
| 338 |
pp->norm[pp->nverts] = cv.norm; |
| 339 |
} |
| 340 |
if (cv.fl & MT_UV) { |
| 341 |
if (pp->uv == NULL) { |
| 342 |
pp->uv = (uint4 (*)[2])calloc(256, |
| 343 |
2*sizeof(uint4)); |
| 344 |
if (pp->uv == NULL) |
| 345 |
goto nomem; |
| 346 |
} |
| 347 |
for (i = 0; i < 2; i++) |
| 348 |
pp->uv[pp->nverts][i] = cv.uv[i]; |
| 349 |
} |
| 350 |
pp->nverts++; |
| 351 |
lvp->data = lvp->key + sizeof(MCVERT); |
| 352 |
*(int4 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1); |
| 353 |
} |
| 354 |
return(*(int4 *)lvp->data); |
| 355 |
nomem: |
| 356 |
error(SYSTEM, "out of memory in addmeshvert"); |
| 357 |
return(-1); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
OBJECT |
| 362 |
addmeshtri(mp, tv) /* add a new mesh triangle */ |
| 363 |
MESH *mp; |
| 364 |
MESHVERT tv[3]; |
| 365 |
{ |
| 366 |
int4 vid[3], t; |
| 367 |
int pn[3], i; |
| 368 |
register MESHPATCH *pp; |
| 369 |
|
| 370 |
if (!(tv[0].fl & tv[1].fl & tv[2].fl & MT_V)) |
| 371 |
return(OVOID); |
| 372 |
/* find/allocate patch vertices */ |
| 373 |
for (i = 0; i < 3; i++) { |
| 374 |
if ((vid[i] = addmeshvert(mp, &tv[i])) < 0) |
| 375 |
return(OVOID); |
| 376 |
pn[i] = vid[i] >> 8; |
| 377 |
} |
| 378 |
/* assign triangle */ |
| 379 |
if (pn[0] == pn[1] && pn[1] == pn[2]) { /* local case */ |
| 380 |
pp = &mp->patch[pn[0]]; |
| 381 |
if (pp->tri == NULL) { |
| 382 |
pp->tri = (struct PTri *)malloc( |
| 383 |
512*sizeof(struct PTri)); |
| 384 |
if (pp->tri == NULL) |
| 385 |
goto nomem; |
| 386 |
} |
| 387 |
if (pp->ntris >= 512) |
| 388 |
goto toomany; |
| 389 |
pp->tri[pp->ntris].v1 = vid[0] & 0xff; |
| 390 |
pp->tri[pp->ntris].v2 = vid[1] & 0xff; |
| 391 |
pp->tri[pp->ntris].v3 = vid[2] & 0xff; |
| 392 |
|
| 393 |
return(pn[0] << 10 | pp->ntris++); |
| 394 |
} |
| 395 |
if (pn[0] == pn[1]) { |
| 396 |
t = vid[2]; vid[2] = vid[0]; vid[0] = t; |
| 397 |
i = pn[2]; pn[2] = pn[0]; pn[0] = i; |
| 398 |
} else if (pn[0] == pn[2]) { |
| 399 |
t = vid[0]; vid[0] = vid[1]; vid[1] = t; |
| 400 |
i = pn[0]; pn[0] = pn[1]; pn[1] = i; |
| 401 |
} |
| 402 |
if (pn[1] == pn[2]) { /* single link */ |
| 403 |
pp = &mp->patch[pn[1]]; |
| 404 |
if (pp->j1tri == NULL) { |
| 405 |
pp->j1tri = (struct PJoin1 *)malloc( |
| 406 |
256*sizeof(struct PJoin1)); |
| 407 |
if (pp->j1tri == NULL) |
| 408 |
goto nomem; |
| 409 |
} |
| 410 |
if (pp->nj1tris >= 256) |
| 411 |
goto toomany; |
| 412 |
pp->j1tri[pp->nj1tris].v1j = pn[0] << 8 | (vid[0] & 0xff); |
| 413 |
pp->j1tri[pp->nj1tris].v2 = vid[1] & 0xff; |
| 414 |
pp->j1tri[pp->nj1tris].v3 = vid[2] & 0xff; |
| 415 |
|
| 416 |
return(pn[1] << 10 | 0x200 | pp->nj1tris++); |
| 417 |
} |
| 418 |
/* double link */ |
| 419 |
pp = &mp->patch[pn[2]]; |
| 420 |
if (pp->j2tri == NULL) { |
| 421 |
pp->j2tri = (struct PJoin2 *)malloc( |
| 422 |
256*sizeof(struct PJoin2)); |
| 423 |
if (pp->j2tri == NULL) |
| 424 |
goto nomem; |
| 425 |
} |
| 426 |
if (pp->nj2tris >= 256) |
| 427 |
goto toomany; |
| 428 |
pp->j2tri[pp->nj2tris].v1j = pn[0] << 8 | (vid[0] & 0xff); |
| 429 |
pp->j2tri[pp->nj2tris].v2j = pn[1] << 8 | (vid[1] & 0xff); |
| 430 |
pp->j2tri[pp->nj2tris].v3 = vid[2] & 0xff; |
| 431 |
|
| 432 |
return(pn[2] << 10 | 0x300 | pp->nj2tris++); |
| 433 |
nomem: |
| 434 |
error(SYSTEM, "out of memory in addmeshtri"); |
| 435 |
return(OVOID); |
| 436 |
toomany: |
| 437 |
error(CONSISTENCY, "too many patch triangles in addmeshtri"); |
| 438 |
return(OVOID); |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
static void |
| 443 |
tallyoctree(ot, ecp, lcp, ocp) /* tally octree size */ |
| 444 |
OCTREE ot; |
| 445 |
int *ecp, *lcp, *ocp; |
| 446 |
{ |
| 447 |
int i; |
| 448 |
|
| 449 |
if (isempty(ot)) { |
| 450 |
(*ecp)++; |
| 451 |
return; |
| 452 |
} |
| 453 |
if (isfull(ot)) { |
| 454 |
OBJECT oset[MAXSET+1]; |
| 455 |
(*lcp)++; |
| 456 |
objset(oset, ot); |
| 457 |
*ocp += oset[0]; |
| 458 |
return; |
| 459 |
} |
| 460 |
for (i = 0; i < 8; i++) |
| 461 |
tallyoctree(octkid(ot, i), ecp, lcp, ocp); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
void |
| 466 |
printmeshstats(ms, fp) /* print out mesh statistics */ |
| 467 |
MESH *ms; |
| 468 |
FILE *fp; |
| 469 |
{ |
| 470 |
int lfcnt=0, lecnt=0, locnt=0; |
| 471 |
int vcnt=0, ncnt=0, uvcnt=0; |
| 472 |
int nscnt=0, uvscnt=0; |
| 473 |
int tcnt=0, t1cnt=0, t2cnt=0; |
| 474 |
int i, j; |
| 475 |
|
| 476 |
tallyoctree(ms->mcube.cutree, &lecnt, &lfcnt, &locnt); |
| 477 |
for (i = 0; i < ms->npatches; i++) { |
| 478 |
register MESHPATCH *pp = &ms->patch[i]; |
| 479 |
vcnt += pp->nverts; |
| 480 |
if (pp->norm != NULL) { |
| 481 |
for (j = pp->nverts; j--; ) |
| 482 |
if (pp->norm[j]) |
| 483 |
ncnt++; |
| 484 |
nscnt += pp->nverts; |
| 485 |
} |
| 486 |
if (pp->uv != NULL) { |
| 487 |
for (j = pp->nverts; j--; ) |
| 488 |
if (pp->uv[j][0]) |
| 489 |
uvcnt++; |
| 490 |
uvscnt += pp->nverts; |
| 491 |
} |
| 492 |
tcnt += pp->ntris; |
| 493 |
t1cnt += pp->nj1tris; |
| 494 |
t2cnt += pp->nj2tris; |
| 495 |
} |
| 496 |
fprintf(fp, "Mesh statistics:\n"); |
| 497 |
fprintf(fp, "\t%d patches (%.2f MBytes)\n", ms->npatches, |
| 498 |
(ms->npatches*sizeof(MESHPATCH) + |
| 499 |
vcnt*3*sizeof(uint4) + |
| 500 |
nscnt*sizeof(int4) + |
| 501 |
uvscnt*2*sizeof(uint4) + |
| 502 |
tcnt*sizeof(struct PTri) + |
| 503 |
t1cnt*sizeof(struct PJoin1) + |
| 504 |
t2cnt*sizeof(struct PJoin2))/(1024.*1024.)); |
| 505 |
fprintf(fp, "\t%d vertices (%.1f%% w/ normals, %.1f%% w/ uv)\n", |
| 506 |
vcnt, 100.*ncnt/vcnt, 100.*uvcnt/vcnt); |
| 507 |
fprintf(fp, "\t%d triangles (%.1f%% local, %.1f%% joiner)\n", |
| 508 |
tcnt+t1cnt+t2cnt, |
| 509 |
100.*tcnt/(tcnt+t1cnt+t2cnt), |
| 510 |
100.*t1cnt/(tcnt+t1cnt+t2cnt)); |
| 511 |
fprintf(fp, |
| 512 |
"\t%d leaves in octree (%.1f%% empty, %.2f avg. set size)\n", |
| 513 |
lfcnt+lecnt, 100.*lecnt/(lfcnt+lecnt), |
| 514 |
(double)locnt/lfcnt); |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
void |
| 519 |
freemesh(ms) /* free mesh data */ |
| 520 |
MESH *ms; |
| 521 |
{ |
| 522 |
MESH mhead; |
| 523 |
MESH *msp; |
| 524 |
|
| 525 |
if (ms == NULL) |
| 526 |
return; |
| 527 |
if (ms->nref <= 0) |
| 528 |
error(CONSISTENCY, "unreferenced mesh in freemesh"); |
| 529 |
ms->nref--; |
| 530 |
if (ms->nref) /* still in use */ |
| 531 |
return; |
| 532 |
/* else remove from list */ |
| 533 |
mhead.next = mlist; |
| 534 |
for (msp = &mhead; msp->next != NULL; msp = msp->next) |
| 535 |
if (msp->next == ms) { |
| 536 |
msp->next = ms->next; |
| 537 |
ms->next = NULL; |
| 538 |
break; |
| 539 |
} |
| 540 |
if (ms->next != NULL) /* can't be in list anymore */ |
| 541 |
error(CONSISTENCY, "unlisted mesh in freemesh"); |
| 542 |
mlist = mhead.next; |
| 543 |
/* free mesh data */ |
| 544 |
freestr(ms->name); |
| 545 |
octfree(ms->mcube.cutree); |
| 546 |
if (ms->cdata != NULL) |
| 547 |
lu_done((LUTAB *)ms->cdata); |
| 548 |
if (ms->npatches > 0) { |
| 549 |
register MESHPATCH *pp = ms->patch + ms->npatches; |
| 550 |
while (pp-- > ms->patch) { |
| 551 |
if (pp->j2tri != NULL) |
| 552 |
free((void *)pp->j2tri); |
| 553 |
if (pp->j1tri != NULL) |
| 554 |
free((void *)pp->j1tri); |
| 555 |
if (pp->tri != NULL) |
| 556 |
free((void *)pp->tri); |
| 557 |
if (pp->uv != NULL) |
| 558 |
free((void *)pp->uv); |
| 559 |
if (pp->norm != NULL) |
| 560 |
free((void *)pp->norm); |
| 561 |
if (pp->xyz != NULL) |
| 562 |
free((void *)pp->xyz); |
| 563 |
} |
| 564 |
free((void *)ms->patch); |
| 565 |
} |
| 566 |
free((void *)ms); |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
void |
| 571 |
freemeshinst(o) /* free mesh instance */ |
| 572 |
OBJREC *o; |
| 573 |
{ |
| 574 |
if (o->os == NULL) |
| 575 |
return; |
| 576 |
freemesh((*(MESHINST *)o->os).msh); |
| 577 |
free((void *)o->os); |
| 578 |
o->os = NULL; |
| 579 |
} |