ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/mesh.c
(Generate patch)

Comparing ray/src/common/mesh.c (file contents):
Revision 2.3 by greg, Wed Mar 12 17:26:58 2003 UTC vs.
Revision 2.34 by greg, Tue Jul 14 23:13:50 2020 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   * Mesh support routines
6   */
7  
8 < #include "standard.h"
8 > #include "rtio.h"
9 > #include "rtmath.h"
10 > #include "rterror.h"
11 > #include "paths.h"
12   #include "octree.h"
13   #include "object.h"
14 + #include "otypes.h"
15   #include "mesh.h"
12 #include "lookup.h"
16  
17   /* An encoded mesh vertex */
18   typedef struct {
19          int             fl;
20 <        uint4           xyz[3];
21 <        int4            norm;
22 <        uint4           uv[2];
20 >        uint32          xyz[3];
21 >        int32           norm;
22 >        uint32          uv[2];
23   } MCVERT;
24  
25   #define  MPATCHBLKSIZ   128             /* patch allocation block size */
# Line 27 | Line 30 | static MESH    *mlist = NULL;          /* list of loaded meshes *
30  
31  
32   static unsigned long
33 < cvhash(cvp)                             /* hash an encoded vertex */
31 < MCVERT  *cvp;
33 > cvhash(const char *p)                   /* hash an encoded vertex */
34   {
35 +        const MCVERT    *cvp = (const MCVERT *)p;
36          unsigned long   hval;
37          
38          if (!(cvp->fl & MT_V))
# Line 44 | Line 47 | MCVERT *cvp;
47  
48  
49   static int
50 < cvcmp(v1, v2)                           /* compare encoded vertices */
48 < register MCVERT *v1, *v2;
50 > cvcmp(const char *vv1, const char *vv2)         /* compare encoded vertices */
51   {
52 +        const MCVERT    *v1 = (const MCVERT *)vv1, *v2 = (const MCVERT *)vv2;
53 +
54          if (v1->fl != v2->fl)
55                  return(1);
56          if (v1->xyz[0] != v2->xyz[0])
# Line 68 | Line 72 | register MCVERT        *v1, *v2;
72  
73  
74   MESH *
75 < getmesh(mname, flags)                   /* get mesh data */
76 < char    *mname;
77 < int     flags;
75 > getmesh(                                /* get new mesh data reference */
76 >        char    *mname,
77 >        int     flags
78 > )
79   {
80          char  *pathname;
81 <        register MESH  *ms;
81 >        MESH  *ms;
82  
83          flags &= IO_LEGAL;
84          for (ms = mlist; ms != NULL; ms = ms->next)
85                  if (!strcmp(mname, ms->name)) {
86 <                        if ((ms->ldflags & flags) == flags) {
87 <                                ms->nref++;
83 <                                return(ms);             /* loaded */
84 <                        }
85 <                        break;                  /* load the rest */
86 >                        ms->nref++;     /* increase reference count */
87 >                        break;
88                  }
89 <        if (ms == NULL) {
89 >        if (ms == NULL) {               /* load first time */
90                  ms = (MESH *)calloc(1, sizeof(MESH));
91                  if (ms == NULL)
92                          error(SYSTEM, "out of memory in getmesh");
# Line 94 | Line 96 | int    flags;
96                  ms->next = mlist;
97                  mlist = ms;
98          }
99 <        if ((pathname = getpath(mname, getlibpath(), R_OK)) == NULL) {
99 >        if ((pathname = getpath(mname, getrlibpath(), R_OK)) == NULL) {
100                  sprintf(errmsg, "cannot find mesh file \"%s\"", mname);
101 <                error(USER, errmsg);
101 >                error(SYSTEM, errmsg);
102          }
103          flags &= ~ms->ldflags;
104          if (flags)
# Line 106 | Line 108 | int    flags;
108  
109  
110   MESHINST *
111 < getmeshinst(o, flags)                   /* create mesh instance */
112 < OBJREC  *o;
113 < int     flags;
111 > getmeshinst(                            /* create mesh instance */
112 >        OBJREC  *o,
113 >        int     flags
114 > )
115   {
116 <        register MESHINST  *ins;
116 >        MESHINST  *ins;
117  
118          flags &= IO_LEGAL;
119          if ((ins = (MESHINST *)o->os) == NULL) {
# Line 128 | Line 131 | int    flags;
131                  ins->msh = NULL;
132                  o->os = (char *)ins;
133          }
134 <        if (ins->msh == NULL || (ins->msh->ldflags & flags) != flags)
134 >        if (ins->msh == NULL)
135                  ins->msh = getmesh(o->oargs.sarg[0], flags);
136 +        else if ((flags &= ~ins->msh->ldflags))
137 +                readmesh(ins->msh,
138 +                        getpath(o->oargs.sarg[0], getrlibpath(), R_OK),
139 +                                flags);
140          return(ins);
141   }
142  
143  
144   int
145 < getmeshtrivid(tvid, mp, ti)             /* get triangle vertex ID's */
146 < int4            tvid[3];
147 < register MESH   *mp;
148 < OBJECT          ti;
145 > nextmeshtri(                            /* get next triangle ID */
146 >        OBJECT *tip,
147 >        MESH *mp
148 > )
149   {
150 +        int             pn;
151 +        MESHPATCH       *pp;
152 +
153 +        pn = ++(*tip) >> 10;                    /* next triangle (OVOID init) */
154 +        while (pn < mp->npatches) {
155 +                pp = &mp->patch[pn];
156 +                if (!(*tip & 0x200)) {          /* local triangle? */
157 +                        if ((*tip & 0x1ff) < pp->ntris)
158 +                                return(1);
159 +                        *tip &= ~0x1ff;         /* move on to single-joiners */
160 +                        *tip |= 0x200;
161 +                }
162 +                if (!(*tip & 0x100)) {          /* single joiner? */
163 +                        if ((*tip & 0xff) < pp->nj1tris)
164 +                                return(1);
165 +                        *tip &= ~0xff;          /* move on to double-joiners */
166 +                        *tip |= 0x100;
167 +                }
168 +                if ((*tip & 0xff) < pp->nj2tris)
169 +                        return(1);
170 +                *tip = ++pn << 10;              /* first in next patch */
171 +        }
172 +        return(0);                              /* out of patches */
173 + }
174 +
175 + int
176 + getmeshtrivid(                          /* get triangle vertex ID's */
177 +        int32   tvid[3],
178 +        OBJECT  *mo,
179 +        MESH    *mp,
180 +        OBJECT  ti
181 + )
182 + {
183          int             pn = ti >> 10;
184 +        MESHPATCH       *pp;
185  
186          if (pn >= mp->npatches)
187                  return(0);
188 +        pp = &mp->patch[pn];
189          ti &= 0x3ff;
190          if (!(ti & 0x200)) {            /* local triangle */
191                  struct PTri     *tp;
192 <                if (ti >= mp->patch[pn].ntris)
192 >                if (ti >= pp->ntris)
193                          return(0);
194 <                tp = &mp->patch[pn].tri[ti];
194 >                tp = &pp->tri[ti];
195                  tvid[0] = tvid[1] = tvid[2] = pn << 8;
196                  tvid[0] |= tp->v1;
197                  tvid[1] |= tp->v2;
198                  tvid[2] |= tp->v3;
199 +                if (pp->trimat != NULL)
200 +                        *mo = pp->trimat[ti];
201 +                else
202 +                        *mo = pp->solemat;
203 +                if (*mo != OVOID)
204 +                        *mo += mp->mat0;
205                  return(1);
206          }
207          ti &= ~0x200;
208          if (!(ti & 0x100)) {            /* single link vertex */
209                  struct PJoin1   *tp1;
210 <                if (ti >= mp->patch[pn].nj1tris)
210 >                if (ti >= pp->nj1tris)
211                          return(0);
212 <                tp1 = &mp->patch[pn].j1tri[ti];
212 >                tp1 = &pp->j1tri[ti];
213                  tvid[0] = tp1->v1j;
214                  tvid[1] = tvid[2] = pn << 8;
215                  tvid[1] |= tp1->v2;
216                  tvid[2] |= tp1->v3;
217 +                if ((*mo = tp1->mat) != OVOID)
218 +                        *mo += mp->mat0;
219                  return(1);
220          }
221          ti &= ~0x100;
222          {                               /* double link vertex */
223                  struct PJoin2   *tp2;
224 <                if (ti >= mp->patch[pn].nj2tris)
224 >                if (ti >= pp->nj2tris)
225                          return(0);
226 <                tp2 = &mp->patch[pn].j2tri[ti];
226 >                tp2 = &pp->j2tri[ti];
227                  tvid[0] = tp2->v1j;
228                  tvid[1] = tp2->v2j;
229                  tvid[2] = pn << 8 | tp2->v3;
230 +                if ((*mo = tp2->mat) != OVOID)
231 +                        *mo += mp->mat0;
232          }
233          return(1);
234   }
235  
236  
237   int
238 < getmeshvert(vp, mp, vid, what)  /* get triangle vertex from ID */
239 < MESHVERT        *vp;
240 < register MESH   *mp;
241 < int4            vid;
242 < int             what;
238 > getmeshvert(                            /* get triangle vertex from ID */
239 >        MESHVERT        *vp,
240 >        MESH            *mp,
241 >        int32           vid,
242 >        int             what
243 > )
244   {
245          int             pn = vid >> 8;
246          MESHPATCH       *pp;
247          double          vres;
248 <        register int    i;
248 >        int     i;
249          
250          vp->fl = 0;
251          if (pn >= mp->npatches)
# Line 226 | Line 279 | int            what;
279   }
280  
281  
282 + OBJREC *
283 + getmeshpseudo(                  /* get mesh pseudo object for material */
284 +        MESH    *mp,
285 +        OBJECT  mo
286 + )
287 + {
288 +        if ((mo < mp->mat0) | (mo >= mp->mat0 + mp->nmats))
289 +                error(INTERNAL, "modifier out of range in getmeshpseudo");
290 +        if (mp->pseudo == NULL) {
291 +                int     i;
292 +                mp->pseudo = (OBJREC *)calloc(mp->nmats, sizeof(OBJREC));
293 +                if (mp->pseudo == NULL)
294 +                        error(SYSTEM, "out of memory in getmeshpseudo");
295 +                for (i = mp->nmats; i--; ) {
296 +                        mp->pseudo[i].omod = mp->mat0 + i;
297 +                        mp->pseudo[i].otype = OBJ_FACE;
298 +                        mp->pseudo[i].oname = "M-Tri";
299 +                }
300 +        }
301 +        return(&mp->pseudo[mo - mp->mat0]);
302 + }
303 +
304 +
305   int
306 < getmeshtri(tv, mp, ti, what)    /* get triangle vertices */
307 < MESHVERT        tv[3];
308 < MESH            *mp;
309 < OBJECT          ti;
310 < int             what;
306 > getmeshtri(                     /* get triangle vertices */
307 >        MESHVERT        tv[3],
308 >        OBJECT          *mo,
309 >        MESH            *mp,
310 >        OBJECT          ti,
311 >        int             wha
312 > )
313   {
314 <        int4    tvid[3];
314 >        int32   tvid[3];
315  
316 <        if (!getmeshtrivid(tvid, mp, ti))
316 >        if (!getmeshtrivid(tvid, mo, mp, ti))
317                  return(0);
318  
319 <        getmeshvert(&tv[0], mp, tvid[0], what);
320 <        getmeshvert(&tv[1], mp, tvid[1], what);
321 <        getmeshvert(&tv[2], mp, tvid[2], what);
319 >        getmeshvert(&tv[0], mp, tvid[0], wha);
320 >        getmeshvert(&tv[1], mp, tvid[1], wha);
321 >        getmeshvert(&tv[2], mp, tvid[2], wha);
322  
323          return(tv[0].fl & tv[1].fl & tv[2].fl);
324   }
325  
326  
327 < int4
328 < addmeshvert(mp, vp)             /* find/add a mesh vertex */
329 < register MESH   *mp;
330 < MESHVERT        *vp;
327 > int32
328 > addmeshvert(                    /* find/add a mesh vertex */
329 >        MESH            *mp,
330 >        MESHVERT        *vp
331 > )
332   {
333 <        LUTAB           *ltp;
334 <        LUENT           *lvp;
335 <        MCVERT          cv;
257 <        register int    i;
333 >        LUENT   *lvp;
334 >        MCVERT  cv;
335 >        int     i;
336  
337          if (!(vp->fl & MT_V))
338                  return(-1);
# Line 264 | Line 342 | MESHVERT       *vp;
342                          return(-1);
343                  if (vp->v[i] >= mp->mcube.cuorg[i] + mp->mcube.cusize)
344                          return(-1);
345 <                cv.xyz[i] = (uint4)(4294967296. *
345 >                cv.xyz[i] = (uint32)(4294967296. *
346                                  (vp->v[i] - mp->mcube.cuorg[i]) /
347                                  mp->mcube.cusize);
348          }
349 <        if (vp->fl & MT_N)
349 >        if (vp->fl & MT_N)              /* assumes normalized! */
350                  cv.norm = encodedir(vp->n);
351          if (vp->fl & MT_UV)
352                  for (i = 0; i < 2; i++) {
# Line 276 | Line 354 | MESHVERT       *vp;
354                                  return(-1);
355                          if (vp->uv[i] >= mp->uvlim[1][i])
356                                  return(-1);
357 <                        cv.uv[i] = (uint4)(4294967296. *
357 >                        cv.uv[i] = (uint32)(4294967296. *
358                                          (vp->uv[i] - mp->uvlim[0][i]) /
359                                          (mp->uvlim[1][i] - mp->uvlim[0][i]));
360                  }
361          cv.fl = vp->fl;
362 <        ltp = (LUTAB *)mp->cdata;       /* get lookup table */
363 <        if (ltp == NULL) {
364 <                ltp = (LUTAB *)calloc(1, sizeof(LUTAB));
365 <                if (ltp == NULL)
362 >        if (mp->lut.tsiz == 0) {
363 >                mp->lut.hashf = cvhash;
364 >                mp->lut.keycmp = cvcmp;
365 >                mp->lut.freek = free;
366 >                if (!lu_init(&mp->lut, 50000))
367                          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;
368          }
369                                          /* find entry */
370 <        lvp = lu_find(ltp, (char *)&cv);
370 >        lvp = lu_find(&mp->lut, (char *)&cv);
371          if (lvp == NULL)
372                  goto nomem;
373          if (lvp->key == NULL) {
374 <                lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int4));
375 <                bcopy((void *)&cv, (void *)lvp->key, sizeof(MCVERT));
374 >                lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int32));
375 >                memcpy(lvp->key, &cv, sizeof(MCVERT));
376          }
377          if (lvp->data == NULL) {        /* new vertex */
378 <                register MESHPATCH      *pp;
378 >                MESHPATCH       *pp;
379                  if (mp->npatches <= 0) {
380                          mp->patch = (MESHPATCH *)calloc(MPATCHBLKSIZ,
381                                          sizeof(MESHPATCH));
# Line 310 | Line 383 | MESHVERT       *vp;
383                                  goto nomem;
384                          mp->npatches = 1;
385                  } else if (mp->patch[mp->npatches-1].nverts >= 256) {
386 +                        if (mp->npatches >= 1L<<22)
387 +                                error(INTERNAL, "too many mesh patches");
388                          if (mp->npatches % MPATCHBLKSIZ == 0) {
389 <                                mp->patch = (MESHPATCH *)realloc(
390 <                                                (void *)mp->patch,
391 <                                        (mp->npatches + MPATCHBLKSIZ)*
392 <                                                sizeof(MESHPATCH));
318 <                                bzero((void *)(mp->patch + mp->npatches),
389 >                                mp->patch = (MESHPATCH *)realloc(mp->patch,
390 >                                                (mp->npatches + MPATCHBLKSIZ)*
391 >                                                        sizeof(MESHPATCH));
392 >                                memset((mp->patch + mp->npatches), '\0',
393                                          MPATCHBLKSIZ*sizeof(MESHPATCH));
394                          }
395 <                        if (mp->npatches++ >= 1L<<22)
322 <                                error(INTERNAL, "too many mesh patches");
395 >                        mp->npatches++;
396                  }
397                  pp = &mp->patch[mp->npatches-1];
398                  if (pp->xyz == NULL) {
399 <                        pp->xyz = (uint4 (*)[3])calloc(256, 3*sizeof(int4));
399 >                        pp->xyz = (uint32 (*)[3])calloc(256, 3*sizeof(int32));
400                          if (pp->xyz == NULL)
401                                  goto nomem;
402                  }
# Line 331 | Line 404 | MESHVERT       *vp;
404                          pp->xyz[pp->nverts][i] = cv.xyz[i];
405                  if (cv.fl & MT_N) {
406                          if (pp->norm == NULL) {
407 <                                pp->norm = (int4 *)calloc(256, sizeof(int4));
407 >                                pp->norm = (int32 *)calloc(256, sizeof(int32));
408                                  if (pp->norm == NULL)
409                                          goto nomem;
410                          }
# Line 339 | Line 412 | MESHVERT       *vp;
412                  }
413                  if (cv.fl & MT_UV) {
414                          if (pp->uv == NULL) {
415 <                                pp->uv = (uint4 (*)[2])calloc(256,
416 <                                                2*sizeof(uint4));
415 >                                pp->uv = (uint32 (*)[2])calloc(256,
416 >                                                2*sizeof(uint32));
417                                  if (pp->uv == NULL)
418                                          goto nomem;
419                          }
# Line 349 | Line 422 | MESHVERT       *vp;
422                  }
423                  pp->nverts++;
424                  lvp->data = lvp->key + sizeof(MCVERT);
425 <                *(int4 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1);
425 >                *(int32 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1);
426          }
427 <        return(*(int4 *)lvp->data);
427 >        return(*(int32 *)lvp->data);
428   nomem:
429          error(SYSTEM, "out of memory in addmeshvert");
430          return(-1);
# Line 359 | Line 432 | nomem:
432  
433  
434   OBJECT
435 < addmeshtri(mp, tv)              /* add a new mesh triangle */
436 < MESH            *mp;
437 < MESHVERT        tv[3];
435 > addmeshtri(                     /* add a new mesh triangle */
436 >        MESH            *mp,
437 >        MESHVERT        tv[3],
438 >        OBJECT          mo
439 > )
440   {
441 <        int4                    vid[3], t;
442 <        int                     pn[3], i;
443 <        register MESHPATCH      *pp;
441 >        int32           vid[3], t;
442 >        int             pn[3], i;
443 >        MESHPATCH       *pp;
444  
445          if (!(tv[0].fl & tv[1].fl & tv[2].fl & MT_V))
446                  return(OVOID);
# Line 375 | Line 450 | MESHVERT       tv[3];
450                          return(OVOID);
451                  pn[i] = vid[i] >> 8;
452          }
453 +                                /* normalize material index */
454 +        if (mo != OVOID) {
455 +                if ((mo -= mp->mat0) >= mp->nmats)
456 +                        mp->nmats = mo+1;
457 +                else if (mo < 0)
458 +                        error(INTERNAL, "modifier range error in addmeshtri");
459 +        }
460                                  /* assign triangle */
461 <        if (pn[0] == pn[1] && pn[1] == pn[2]) { /* local case */
461 >        if ((pn[0] == pn[1]) & (pn[1] == pn[2])) {      /* local case */
462                  pp = &mp->patch[pn[0]];
463                  if (pp->tri == NULL) {
464                          pp->tri = (struct PTri *)malloc(
# Line 388 | Line 470 | MESHVERT       tv[3];
470                          pp->tri[pp->ntris].v1 = vid[0] & 0xff;
471                          pp->tri[pp->ntris].v2 = vid[1] & 0xff;
472                          pp->tri[pp->ntris].v3 = vid[2] & 0xff;
473 +                        if (pp->ntris == 0)
474 +                                pp->solemat = mo;
475 +                        else if (pp->trimat == NULL && mo != pp->solemat) {
476 +                                pp->trimat = (int16 *)malloc(
477 +                                                512*sizeof(int16));
478 +                                if (pp->trimat == NULL)
479 +                                        goto nomem;
480 +                                for (i = pp->ntris; i--; )
481 +                                        pp->trimat[i] = pp->solemat;
482 +                        }
483 +                        if (pp->trimat != NULL)
484 +                                pp->trimat[pp->ntris] = mo;
485                          return(pn[0] << 10 | pp->ntris++);
486                  }
487 <        }
394 <        if (pn[0] == pn[1]) {
487 >        } else if (pn[0] == pn[1]) {
488                  t = vid[2]; vid[2] = vid[1]; vid[1] = vid[0]; vid[0] = t;
489                  i = pn[2]; pn[2] = pn[1]; pn[1] = pn[0]; pn[0] = i;
490          } else if (pn[0] == pn[2]) {
# Line 410 | Line 503 | MESHVERT       tv[3];
503                          pp->j1tri[pp->nj1tris].v1j = vid[0];
504                          pp->j1tri[pp->nj1tris].v2 = vid[1] & 0xff;
505                          pp->j1tri[pp->nj1tris].v3 = vid[2] & 0xff;
506 +                        pp->j1tri[pp->nj1tris].mat = mo;
507                          return(pn[1] << 10 | 0x200 | pp->nj1tris++);
508                  }
509          }
510                                                  /* double link */
511 <        pp = &mp->patch[pn[2]];
511 >        pp = &mp->patch[pn[i=0]];
512 >        if (mp->patch[pn[1]].nj2tris < pp->nj2tris)
513 >                pp = &mp->patch[pn[i=1]];
514 >        if (mp->patch[pn[2]].nj2tris < pp->nj2tris)
515 >                pp = &mp->patch[pn[i=2]];
516 >        if (pp->nj2tris >= 256)
517 >                error(INTERNAL, "too many patch triangles in addmeshtri");
518          if (pp->j2tri == NULL) {
519                  pp->j2tri = (struct PJoin2 *)malloc(
520                                          256*sizeof(struct PJoin2));
521                  if (pp->j2tri == NULL)
522                          goto nomem;
523          }
524 <        if (pp->nj2tris >= 256)
525 <                error(INTERNAL, "too many patch triangles in addmeshtri");
526 <        pp->j2tri[pp->nj2tris].v1j = vid[0];
527 <        pp->j2tri[pp->nj2tris].v2j = vid[1];
528 <        pp->j2tri[pp->nj2tris].v3 = vid[2] & 0xff;
529 <        return(pn[2] << 10 | 0x300 | pp->nj2tris++);
524 >        pp->j2tri[pp->nj2tris].mat = mo;
525 >        switch (i) {
526 >        case 0:
527 >                pp->j2tri[pp->nj2tris].v3 = vid[0] & 0xff;
528 >                pp->j2tri[pp->nj2tris].v1j = vid[1];
529 >                pp->j2tri[pp->nj2tris].v2j = vid[2];
530 >                return(pn[0] << 10 | 0x300 | pp->nj2tris++);
531 >        case 1:
532 >                pp->j2tri[pp->nj2tris].v2j = vid[0];
533 >                pp->j2tri[pp->nj2tris].v3 = vid[1] & 0xff;
534 >                pp->j2tri[pp->nj2tris].v1j = vid[2];
535 >                return(pn[1] << 10 | 0x300 | pp->nj2tris++);
536 >        case 2:
537 >                pp->j2tri[pp->nj2tris].v1j = vid[0];
538 >                pp->j2tri[pp->nj2tris].v2j = vid[1];
539 >                pp->j2tri[pp->nj2tris].v3 = vid[2] & 0xff;
540 >                return(pn[2] << 10 | 0x300 | pp->nj2tris++);
541 >        }
542   nomem:
543          error(SYSTEM, "out of memory in addmeshtri");
544          return(OVOID);
545   }
546  
547  
548 + char *
549 + checkmesh(MESH *mp)                     /* validate mesh data */
550 + {
551 +        static char     embuf[128];
552 +        int             nouvbounds = 1;
553 +        int             i;
554 +                                        /* basic checks */
555 +        if (mp == NULL)
556 +                return("NULL mesh pointer");
557 +        if (!mp->ldflags)
558 +                return("unassigned mesh");
559 +        if (mp->name == NULL)
560 +                return("missing mesh name");
561 +        if (mp->nref <= 0)
562 +                return("unreferenced mesh");
563 +                                        /* check boundaries */
564 +        if (mp->ldflags & IO_BOUNDS) {
565 +                if (mp->mcube.cusize <= FTINY)
566 +                        return("illegal octree bounds in mesh");
567 +                nouvbounds = (mp->uvlim[1][0] - mp->uvlim[0][0] <= FTINY ||
568 +                                mp->uvlim[1][1] - mp->uvlim[0][1] <= FTINY);
569 +        }
570 +                                        /* check octree */
571 +        if (mp->ldflags & IO_TREE) {
572 +                if (isempty(mp->mcube.cutree))
573 +                        error(WARNING, "empty mesh octree");
574 +        }
575 +                                        /* check scene data */
576 +        if (mp->ldflags & IO_SCENE) {
577 +                if (!(mp->ldflags & IO_BOUNDS))
578 +                        return("unbounded scene in mesh");
579 +                if (mp->mat0 < 0 || mp->mat0+mp->nmats > nobjects)
580 +                        return("bad mesh modifier range");
581 +                if (mp->nmats > 0)      /* allocate during preload_objs()! */
582 +                        getmeshpseudo(mp, mp->mat0);
583 +                for (i = mp->mat0+mp->nmats; i-- > mp->mat0; ) {
584 +                        int     otyp = objptr(i)->otype;
585 +                        if (!ismodifier(otyp)) {
586 +                                sprintf(embuf,
587 +                                        "non-modifier in mesh (%s \"%s\")",
588 +                                        ofun[otyp].funame, objptr(i)->oname);
589 +                                return(embuf);
590 +                        }
591 +                }
592 +                if (mp->npatches <= 0)
593 +                        error(WARNING, "no patches in mesh");
594 +                for (i = 0; i < mp->npatches; i++) {
595 +                        MESHPATCH       *pp = &mp->patch[i];
596 +                        if (pp->nverts <= 0)
597 +                                error(WARNING, "no vertices in patch");
598 +                        else {
599 +                                if (pp->xyz == NULL)
600 +                                        return("missing patch vertex list");
601 +                                if (nouvbounds && pp->uv != NULL)
602 +                                        return("unreferenced uv coordinates");
603 +                        }
604 +                        if (pp->ntris > 0 && pp->tri == NULL)
605 +                                return("missing patch triangle list");
606 +                        if (pp->nj1tris > 0 && pp->j1tri == NULL)
607 +                                return("missing patch joiner triangle list");
608 +                        if (pp->nj2tris > 0 && pp->j2tri == NULL)
609 +                                return("missing patch double-joiner list");
610 +                }
611 +        }
612 +        return(NULL);                   /* seems OK */
613 + }
614 +
615 +
616   static void
617 < tallyoctree(ot, ecp, lcp, ocp)  /* tally octree size */
618 < OCTREE  ot;
619 < int     *ecp, *lcp, *ocp;
617 > tallyoctree(                    /* tally octree size */
618 >        OCTREE  ot,
619 >        int     *ecp,
620 >        int     *lcp,
621 >        int     *ocp
622 > )
623   {
624          int     i;
625  
# Line 457 | Line 640 | int    *ecp, *lcp, *ocp;
640  
641  
642   void
643 < printmeshstats(ms, fp)          /* print out mesh statistics */
644 < MESH    *ms;
645 < FILE    *fp;
643 > printmeshstats(                 /* print out mesh statistics */
644 >        MESH    *ms,
645 >        FILE    *fp
646 > )
647   {
648          int     lfcnt=0, lecnt=0, locnt=0;
649          int     vcnt=0, ncnt=0, uvcnt=0;
# Line 469 | Line 653 | FILE   *fp;
653          
654          tallyoctree(ms->mcube.cutree, &lecnt, &lfcnt, &locnt);
655          for (i = 0; i < ms->npatches; i++) {
656 <                register MESHPATCH      *pp = &ms->patch[i];
656 >                MESHPATCH       *pp = &ms->patch[i];
657                  vcnt += pp->nverts;
658                  if (pp->norm != NULL) {
659                          for (j = pp->nverts; j--; )
# Line 488 | Line 672 | FILE   *fp;
672                  t2cnt += pp->nj2tris;
673          }
674          fprintf(fp, "Mesh statistics:\n");
675 +        fprintf(fp, "\t%ld materials\n", (long)ms->nmats);
676          fprintf(fp, "\t%d patches (%.2f MBytes)\n", ms->npatches,
677                          (ms->npatches*sizeof(MESHPATCH) +
678 <                        vcnt*3*sizeof(uint4) +
679 <                        nscnt*sizeof(int4) +
680 <                        uvscnt*2*sizeof(uint4) +
678 >                        vcnt*3*sizeof(uint32) +
679 >                        nscnt*sizeof(int32) +
680 >                        uvscnt*2*sizeof(uint32) +
681                          tcnt*sizeof(struct PTri) +
682                          t1cnt*sizeof(struct PJoin1) +
683                          t2cnt*sizeof(struct PJoin2))/(1024.*1024.));
# Line 510 | Line 695 | FILE   *fp;
695  
696  
697   void
698 < freemesh(ms)                    /* free mesh data */
514 < MESH    *ms;
698 > freemesh(MESH *ms)              /* free mesh data */
699   {
700          MESH    mhead;
701          MESH    *msp;
# Line 537 | Line 721 | MESH   *ms;
721                                  /* free mesh data */
722          freestr(ms->name);
723          octfree(ms->mcube.cutree);
724 <        if (ms->cdata != NULL)
541 <                lu_done((LUTAB *)ms->cdata);
724 >        lu_done(&ms->lut);
725          if (ms->npatches > 0) {
726 <                register MESHPATCH      *pp = ms->patch + ms->npatches;
726 >                MESHPATCH       *pp = ms->patch + ms->npatches;
727                  while (pp-- > ms->patch) {
728                          if (pp->j2tri != NULL)
729 <                                free((void *)pp->j2tri);
729 >                                free(pp->j2tri);
730                          if (pp->j1tri != NULL)
731 <                                free((void *)pp->j1tri);
731 >                                free(pp->j1tri);
732                          if (pp->tri != NULL)
733 <                                free((void *)pp->tri);
733 >                                free(pp->tri);
734                          if (pp->uv != NULL)
735 <                                free((void *)pp->uv);
735 >                                free(pp->uv);
736                          if (pp->norm != NULL)
737 <                                free((void *)pp->norm);
737 >                                free(pp->norm);
738                          if (pp->xyz != NULL)
739 <                                free((void *)pp->xyz);
739 >                                free(pp->xyz);
740 >                        if (pp->trimat != NULL)
741 >                                free(pp->trimat);
742                  }
743 <                free((void *)ms->patch);
743 >                free(ms->patch);
744          }
745 <        free((void *)ms);
745 >        if (ms->pseudo != NULL)
746 >                free(ms->pseudo);
747 >        free(ms);
748   }
749  
750  
751   void
752 < freemeshinst(o)                 /* free mesh instance */
566 < OBJREC  *o;
752 > freemeshinst(OBJREC *o)         /* free mesh instance */
753   {
754          if (o->os == NULL)
755                  return;
756          freemesh((*(MESHINST *)o->os).msh);
757 <        free((void *)o->os);
757 >        free(o->os);
758          o->os = NULL;
759   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines