5 |
|
* Mesh support routines |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include <string.h> |
9 |
+ |
|
10 |
|
#include "standard.h" |
11 |
|
#include "octree.h" |
12 |
|
#include "object.h" |
16 |
|
/* An encoded mesh vertex */ |
17 |
|
typedef struct { |
18 |
|
int fl; |
19 |
< |
uint4 xyz[3]; |
20 |
< |
int4 norm; |
21 |
< |
uint4 uv[2]; |
19 |
> |
uint32 xyz[3]; |
20 |
> |
int32 norm; |
21 |
> |
uint32 uv[2]; |
22 |
|
} MCVERT; |
23 |
|
|
24 |
|
#define MPATCHBLKSIZ 128 /* patch allocation block size */ |
70 |
|
|
71 |
|
|
72 |
|
MESH * |
73 |
< |
getmesh(mname, flags) /* get mesh data */ |
73 |
> |
getmesh(mname, flags) /* get new mesh data reference */ |
74 |
|
char *mname; |
75 |
|
int flags; |
76 |
|
{ |
79 |
|
|
80 |
|
flags &= IO_LEGAL; |
81 |
|
for (ms = mlist; ms != NULL; ms = ms->next) |
82 |
< |
if (!strcmp(mname, ms->name)) { |
83 |
< |
if ((ms->ldflags & flags) == flags) { |
84 |
< |
ms->nref++; |
83 |
< |
return(ms); /* loaded */ |
84 |
< |
} |
85 |
< |
break; /* load the rest */ |
86 |
< |
} |
87 |
< |
if (ms == NULL) { |
82 |
> |
if (!strcmp(mname, ms->name)) |
83 |
> |
break; |
84 |
> |
if (ms == NULL) { /* load first time */ |
85 |
|
ms = (MESH *)calloc(1, sizeof(MESH)); |
86 |
|
if (ms == NULL) |
87 |
|
error(SYSTEM, "out of memory in getmesh"); |
88 |
|
ms->name = savestr(mname); |
92 |
– |
ms->nref = 1; |
89 |
|
ms->mcube.cutree = EMPTY; |
90 |
|
ms->next = mlist; |
91 |
|
mlist = ms; |
92 |
|
} |
93 |
< |
if ((pathname = getpath(mname, getlibpath(), R_OK)) == NULL) { |
93 |
> |
if ((pathname = getpath(mname, getrlibpath(), R_OK)) == NULL) { |
94 |
|
sprintf(errmsg, "cannot find mesh file \"%s\"", mname); |
95 |
|
error(USER, errmsg); |
96 |
|
} |
97 |
|
flags &= ~ms->ldflags; |
98 |
|
if (flags) |
99 |
|
readmesh(ms, pathname, flags); |
100 |
+ |
ms->nref++; /* increase reference count */ |
101 |
|
return(ms); |
102 |
|
} |
103 |
|
|
125 |
|
ins->msh = NULL; |
126 |
|
o->os = (char *)ins; |
127 |
|
} |
128 |
< |
if (ins->msh == NULL || (ins->msh->ldflags & flags) != flags) |
128 |
> |
if (ins->msh == NULL) |
129 |
|
ins->msh = getmesh(o->oargs.sarg[0], flags); |
130 |
+ |
else if ((flags &= ~ins->msh->ldflags)) |
131 |
+ |
readmesh(ins->msh, |
132 |
+ |
getpath(o->oargs.sarg[0], getrlibpath(), R_OK), |
133 |
+ |
flags); |
134 |
|
return(ins); |
135 |
|
} |
136 |
|
|
137 |
|
|
138 |
|
int |
139 |
|
getmeshtrivid(tvid, mo, mp, ti) /* get triangle vertex ID's */ |
140 |
< |
int4 tvid[3]; |
140 |
> |
int32 tvid[3]; |
141 |
|
OBJECT *mo; |
142 |
|
MESH *mp; |
143 |
|
OBJECT ti; |
198 |
|
|
199 |
|
int |
200 |
|
getmeshvert(vp, mp, vid, what) /* get triangle vertex from ID */ |
201 |
< |
MESHVERT*vp; |
202 |
< |
MESH *mp; |
203 |
< |
int4 vid; |
204 |
< |
int what; |
201 |
> |
MESHVERT *vp; |
202 |
> |
MESH *mp; |
203 |
> |
int32 vid; |
204 |
> |
int what; |
205 |
|
{ |
206 |
|
int pn = vid >> 8; |
207 |
|
MESHPATCH *pp; |
270 |
|
OBJECT ti; |
271 |
|
int wha; |
272 |
|
{ |
273 |
< |
int4 tvid[3]; |
273 |
> |
int32 tvid[3]; |
274 |
|
|
275 |
|
if (!getmeshtrivid(tvid, mo, mp, ti)) |
276 |
|
return(0); |
283 |
|
} |
284 |
|
|
285 |
|
|
286 |
< |
int4 |
286 |
> |
int32 |
287 |
|
addmeshvert(mp, vp) /* find/add a mesh vertex */ |
288 |
|
register MESH *mp; |
289 |
|
MESHVERT *vp; |
300 |
|
return(-1); |
301 |
|
if (vp->v[i] >= mp->mcube.cuorg[i] + mp->mcube.cusize) |
302 |
|
return(-1); |
303 |
< |
cv.xyz[i] = (uint4)(4294967296. * |
303 |
> |
cv.xyz[i] = (uint32)(4294967296. * |
304 |
|
(vp->v[i] - mp->mcube.cuorg[i]) / |
305 |
|
mp->mcube.cusize); |
306 |
|
} |
312 |
|
return(-1); |
313 |
|
if (vp->uv[i] >= mp->uvlim[1][i]) |
314 |
|
return(-1); |
315 |
< |
cv.uv[i] = (uint4)(4294967296. * |
315 |
> |
cv.uv[i] = (uint32)(4294967296. * |
316 |
|
(vp->uv[i] - mp->uvlim[0][i]) / |
317 |
|
(mp->uvlim[1][i] - mp->uvlim[0][i])); |
318 |
|
} |
329 |
|
if (lvp == NULL) |
330 |
|
goto nomem; |
331 |
|
if (lvp->key == NULL) { |
332 |
< |
lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int4)); |
333 |
< |
bcopy((void *)&cv, (void *)lvp->key, sizeof(MCVERT)); |
332 |
> |
lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int32)); |
333 |
> |
memcpy((void *)lvp->key, (void *)&cv, sizeof(MCVERT)); |
334 |
|
} |
335 |
|
if (lvp->data == NULL) { /* new vertex */ |
336 |
|
register MESHPATCH *pp; |
346 |
|
(void *)mp->patch, |
347 |
|
(mp->npatches + MPATCHBLKSIZ)* |
348 |
|
sizeof(MESHPATCH)); |
349 |
< |
bzero((void *)(mp->patch + mp->npatches), |
349 |
> |
memset((void *)(mp->patch + mp->npatches), '\0', |
350 |
|
MPATCHBLKSIZ*sizeof(MESHPATCH)); |
351 |
|
} |
352 |
|
if (mp->npatches++ >= 1L<<22) |
354 |
|
} |
355 |
|
pp = &mp->patch[mp->npatches-1]; |
356 |
|
if (pp->xyz == NULL) { |
357 |
< |
pp->xyz = (uint4 (*)[3])calloc(256, 3*sizeof(int4)); |
357 |
> |
pp->xyz = (uint32 (*)[3])calloc(256, 3*sizeof(int32)); |
358 |
|
if (pp->xyz == NULL) |
359 |
|
goto nomem; |
360 |
|
} |
362 |
|
pp->xyz[pp->nverts][i] = cv.xyz[i]; |
363 |
|
if (cv.fl & MT_N) { |
364 |
|
if (pp->norm == NULL) { |
365 |
< |
pp->norm = (int4 *)calloc(256, sizeof(int4)); |
365 |
> |
pp->norm = (int32 *)calloc(256, sizeof(int32)); |
366 |
|
if (pp->norm == NULL) |
367 |
|
goto nomem; |
368 |
|
} |
370 |
|
} |
371 |
|
if (cv.fl & MT_UV) { |
372 |
|
if (pp->uv == NULL) { |
373 |
< |
pp->uv = (uint4 (*)[2])calloc(256, |
374 |
< |
2*sizeof(uint4)); |
373 |
> |
pp->uv = (uint32 (*)[2])calloc(256, |
374 |
> |
2*sizeof(uint32)); |
375 |
|
if (pp->uv == NULL) |
376 |
|
goto nomem; |
377 |
|
} |
380 |
|
} |
381 |
|
pp->nverts++; |
382 |
|
lvp->data = lvp->key + sizeof(MCVERT); |
383 |
< |
*(int4 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1); |
383 |
> |
*(int32 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1); |
384 |
|
} |
385 |
< |
return(*(int4 *)lvp->data); |
385 |
> |
return(*(int32 *)lvp->data); |
386 |
|
nomem: |
387 |
|
error(SYSTEM, "out of memory in addmeshvert"); |
388 |
|
return(-1); |
395 |
|
MESHVERT tv[3]; |
396 |
|
OBJECT mo; |
397 |
|
{ |
398 |
< |
int4 vid[3], t; |
398 |
> |
int32 vid[3], t; |
399 |
|
int pn[3], i; |
400 |
|
register MESHPATCH *pp; |
401 |
|
|
429 |
|
if (pp->ntris == 0) |
430 |
|
pp->solemat = mo; |
431 |
|
else if (pp->trimat == NULL && mo != pp->solemat) { |
432 |
< |
pp->trimat = (int2 *)malloc( |
433 |
< |
512*sizeof(int2)); |
432 |
> |
pp->trimat = (int16 *)malloc( |
433 |
> |
512*sizeof(int16)); |
434 |
|
if (pp->trimat == NULL) |
435 |
|
goto nomem; |
436 |
|
for (i = pp->ntris; i--; ) |
505 |
|
if (mp->ldflags & IO_BOUNDS) { |
506 |
|
if (mp->mcube.cusize <= FTINY) |
507 |
|
return("illegal octree bounds in mesh"); |
508 |
< |
nouvbounds = (mp->uvlim[0][1] - mp->uvlim[0][0] <= FTINY || |
509 |
< |
mp->uvlim[1][1] - mp->uvlim[1][0] <= FTINY); |
508 |
> |
nouvbounds = (mp->uvlim[1][0] - mp->uvlim[0][0] <= FTINY || |
509 |
> |
mp->uvlim[1][1] - mp->uvlim[0][1] <= FTINY); |
510 |
|
} |
511 |
|
/* check octree */ |
512 |
|
if (mp->ldflags & IO_TREE) { |
612 |
|
fprintf(fp, "\t%d materials\n", ms->nmats); |
613 |
|
fprintf(fp, "\t%d patches (%.2f MBytes)\n", ms->npatches, |
614 |
|
(ms->npatches*sizeof(MESHPATCH) + |
615 |
< |
vcnt*3*sizeof(uint4) + |
616 |
< |
nscnt*sizeof(int4) + |
617 |
< |
uvscnt*2*sizeof(uint4) + |
615 |
> |
vcnt*3*sizeof(uint32) + |
616 |
> |
nscnt*sizeof(int32) + |
617 |
> |
uvscnt*2*sizeof(uint32) + |
618 |
|
tcnt*sizeof(struct PTri) + |
619 |
|
t1cnt*sizeof(struct PJoin1) + |
620 |
|
t2cnt*sizeof(struct PJoin2))/(1024.*1024.)); |