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 { |
16 |
|
int fl; |
17 |
< |
uint4 xyz[3]; |
18 |
< |
int4 norm; |
19 |
< |
uint4 uv[2]; |
17 |
> |
uint32 xyz[3]; |
18 |
> |
int32 norm; |
19 |
> |
uint32 uv[2]; |
20 |
|
} MCVERT; |
21 |
|
|
22 |
|
#define MPATCHBLKSIZ 128 /* patch allocation block size */ |
94 |
|
ms->next = mlist; |
95 |
|
mlist = ms; |
96 |
|
} |
97 |
< |
if ((pathname = getpath(mname, getlibpath(), R_OK)) == NULL) { |
97 |
> |
if ((pathname = getpath(mname, getrlibpath(), R_OK)) == NULL) { |
98 |
|
sprintf(errmsg, "cannot find mesh file \"%s\"", mname); |
99 |
|
error(USER, errmsg); |
100 |
|
} |
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 |
> |
int32 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 |
|
} |
198 |
|
int |
199 |
|
getmeshvert(vp, mp, vid, what) /* get triangle vertex from ID */ |
200 |
|
MESHVERT *vp; |
201 |
< |
register MESH *mp; |
202 |
< |
int4 vid; |
201 |
> |
MESH *mp; |
202 |
> |
int32 vid; |
203 |
|
int what; |
204 |
|
{ |
205 |
|
int pn = vid >> 8; |
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]; |
272 |
> |
int32 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 |
|
} |
283 |
|
|
284 |
|
|
285 |
< |
int4 |
285 |
> |
int32 |
286 |
|
addmeshvert(mp, vp) /* find/add a mesh vertex */ |
287 |
|
register MESH *mp; |
288 |
|
MESHVERT *vp; |
289 |
|
{ |
254 |
– |
LUTAB *ltp; |
290 |
|
LUENT *lvp; |
291 |
|
MCVERT cv; |
292 |
|
register int i; |
299 |
|
return(-1); |
300 |
|
if (vp->v[i] >= mp->mcube.cuorg[i] + mp->mcube.cusize) |
301 |
|
return(-1); |
302 |
< |
cv.xyz[i] = (uint4)(4294967296. * |
302 |
> |
cv.xyz[i] = (uint32)(4294967296. * |
303 |
|
(vp->v[i] - mp->mcube.cuorg[i]) / |
304 |
|
mp->mcube.cusize); |
305 |
|
} |
311 |
|
return(-1); |
312 |
|
if (vp->uv[i] >= mp->uvlim[1][i]) |
313 |
|
return(-1); |
314 |
< |
cv.uv[i] = (uint4)(4294967296. * |
314 |
> |
cv.uv[i] = (uint32)(4294967296. * |
315 |
|
(vp->uv[i] - mp->uvlim[0][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) { |
331 |
< |
lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int4)); |
331 |
> |
lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int32)); |
332 |
|
bcopy((void *)&cv, (void *)lvp->key, sizeof(MCVERT)); |
333 |
|
} |
334 |
|
if (lvp->data == NULL) { /* new vertex */ |
353 |
|
} |
354 |
|
pp = &mp->patch[mp->npatches-1]; |
355 |
|
if (pp->xyz == NULL) { |
356 |
< |
pp->xyz = (uint4 (*)[3])calloc(256, 3*sizeof(int4)); |
356 |
> |
pp->xyz = (uint32 (*)[3])calloc(256, 3*sizeof(int32)); |
357 |
|
if (pp->xyz == NULL) |
358 |
|
goto nomem; |
359 |
|
} |
361 |
|
pp->xyz[pp->nverts][i] = cv.xyz[i]; |
362 |
|
if (cv.fl & MT_N) { |
363 |
|
if (pp->norm == NULL) { |
364 |
< |
pp->norm = (int4 *)calloc(256, sizeof(int4)); |
364 |
> |
pp->norm = (int32 *)calloc(256, sizeof(int32)); |
365 |
|
if (pp->norm == NULL) |
366 |
|
goto nomem; |
367 |
|
} |
369 |
|
} |
370 |
|
if (cv.fl & MT_UV) { |
371 |
|
if (pp->uv == NULL) { |
372 |
< |
pp->uv = (uint4 (*)[2])calloc(256, |
373 |
< |
2*sizeof(uint4)); |
372 |
> |
pp->uv = (uint32 (*)[2])calloc(256, |
373 |
> |
2*sizeof(uint32)); |
374 |
|
if (pp->uv == NULL) |
375 |
|
goto nomem; |
376 |
|
} |
379 |
|
} |
380 |
|
pp->nverts++; |
381 |
|
lvp->data = lvp->key + sizeof(MCVERT); |
382 |
< |
*(int4 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1); |
382 |
> |
*(int32 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1); |
383 |
|
} |
384 |
< |
return(*(int4 *)lvp->data); |
384 |
> |
return(*(int32 *)lvp->data); |
385 |
|
nomem: |
386 |
|
error(SYSTEM, "out of memory in addmeshvert"); |
387 |
|
return(-1); |
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; |
397 |
> |
int32 vid[3], t; |
398 |
|
int pn[3], i; |
399 |
|
register MESHPATCH *pp; |
400 |
|
|
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 = (int16 *)malloc( |
432 |
+ |
512*sizeof(int16)); |
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[1][0] - mp->uvlim[0][0] <= FTINY || |
508 |
+ |
mp->uvlim[1][1] - mp->uvlim[0][1] <= 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) + |
615 |
< |
nscnt*sizeof(int4) + |
616 |
< |
uvscnt*2*sizeof(uint4) + |
614 |
> |
vcnt*3*sizeof(uint32) + |
615 |
> |
nscnt*sizeof(int32) + |
616 |
> |
uvscnt*2*sizeof(uint32) + |
617 |
|
tcnt*sizeof(struct PTri) + |
618 |
|
t1cnt*sizeof(struct PJoin1) + |
619 |
|
t2cnt*sizeof(struct PJoin2))/(1024.*1024.)); |
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 |
|
|