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

Comparing ray/src/cv/obj2rad.c (file contents):
Revision 2.22 by schorsch, Sat Nov 15 17:54:06 2003 UTC vs.
Revision 2.28 by greg, Tue Aug 20 16:19:03 2013 UTC

# Line 2 | Line 2
2   static const char       RCSid[] = "$Id$";
3   #endif
4   /*
5 < * Convert a Wavefront .obj file to Radiance format.
5 > * Convert a Wavefront .OBJ file to Radiance format.
6   *
7   * Currently, we support only polygonal geometry.  Non-planar
8   * faces are broken rather haphazardly into triangles.
# Line 35 | Line 35 | int    nvns;
35   RREAL   (*vtlist)[2];           /* map vertex list */
36   int     nvts;
37  
38 + int     ndegen = 0;             /* count of degenerate faces */
39 + int     n0norm = 0;             /* count of zero normals */
40 +
41   typedef int     VNDX[3];        /* vertex index (point,map,normal) */
42  
43 < #define CHUNKSIZ        256     /* vertex allocation chunk size */
43 > #define CHUNKSIZ        1024    /* vertex allocation chunk size */
44  
45 < #define MAXARG          64      /* maximum # arguments in a statement */
45 > #define MAXARG          512     /* maximum # arguments in a statement */
46  
47                                  /* qualifiers */
48   #define Q_MTL           0
# Line 70 | Line 73 | int    flatten = 0;            /* discard surface normal informatio
73  
74   char    mapname[128];           /* current picture file */
75   char    matname[64];            /* current material name */
76 < char    group[16][32];          /* current group names */
76 > char    group[8][256];          /* current group name(s) */
77   char    objname[128];           /* current object name */
78   char    *inpfile;               /* input file name */
79   int     lineno;                 /* current line number */
# Line 94 | Line 97 | static void syntax(char *er);
97  
98  
99   int
100 < main(           /* read in .obj file and convert */
100 > main(           /* read in .OBJ file and convert */
101          int     argc,
102          char    *argv[]
103   )
# Line 140 | Line 143 | main(          /* read in .obj file and convert */
143                  printargs(argc, argv, stdout);
144                  convert(stdin);
145          }
146 +        if (ndegen)
147 +                printf("# %d degenerate faces\n", ndegen);
148 +        if (n0norm)
149 +                printf("# %d invalid (zero) normals\n", n0norm);
150          exit(0);
151   userr:
152          fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n",
# Line 156 | Line 163 | getnames(                      /* get valid qualifier names */
163          char    *argv[MAXARG];
164          int     argc;
165          ID      tmpid;
166 <        register int    i;
166 >        int     i;
167  
168          while ( (argc = getstmt(argv, fp)) )
169                  switch (argv[0][0]) {
# Line 200 | Line 207 | getnames(                      /* get valid qualifier names */
207  
208  
209   void
210 < convert(                        /* convert a T-mesh */
210 > convert(                        /* convert an OBJ stream */
211          FILE    *fp
212   )
213   {
214          char    *argv[MAXARG];
215          int     argc;
216          int     nstats, nunknown;
217 <        register int    i;
217 >        int     i;
218  
219          nstats = nunknown = 0;
220                                          /* scan until EOF */
# Line 270 | Line 277 | convert(                       /* convert a T-mesh */
277                                  if (!strcmp(argv[1], "off"))
278                                          mapname[0] = '\0';
279                                  else
280 <                                        sprintf(mapname, "%s.pic", argv[1]);
280 >                                        sprintf(mapname, "%s.hdr", argv[1]);
281                          } else
282                                  goto unknown;
283                          break;
# Line 286 | Line 293 | convert(                       /* convert a T-mesh */
293                                  goto unknown;
294                          for (i = 1; i < argc; i++)
295                                  strcpy(group[i-1], argv[i]);
296 <                        group[i-1][0] = '\0';
296 >                        group[argc-1][0] = '\0';
297                          break;
298                  case '#':               /* comment */
299                          printargs(argc, argv, stdout);
# Line 306 | Line 313 | convert(                       /* convert a T-mesh */
313  
314   int
315   getstmt(                                /* read the next statement from fp */
316 <        register char   *av[MAXARG],
316 >        char    *av[MAXARG],
317          FILE    *fp
318   )
319   {
320 <        extern char     *fgetline();
321 <        static char     sbuf[MAXARG*10];
322 <        register char   *cp;
316 <        register int    i;
320 >        static char     sbuf[MAXARG*16];
321 >        char    *cp;
322 >        int     i;
323  
324          do {
325                  if (fgetline(cp=sbuf, sizeof(sbuf), fp) == NULL)
# Line 325 | Line 331 | getstmt(                               /* read the next statement from fp */
331                                          lineno++;
332                                  *cp++ = '\0';
333                          }
334 <                        if (!*cp || i >= MAXARG-1)
334 >                        if (!*cp)
335                                  break;
336 +                        if (i >= MAXARG-1) {
337 +                                fprintf(stderr,
338 +                        "warning: line %d: too many arguments (limit %d)\n",
339 +                                        lineno+1, MAXARG-1);
340 +                                break;
341 +                        }
342                          av[i++] = cp;
343                          while (*++cp && !isspace(*cp))
344                                  ;
# Line 342 | Line 354 | getstmt(                               /* read the next statement from fp */
354   char *
355   getmtl(void)                            /* figure material for this face */
356   {
357 <        register RULEHD *rp = ourmapping;
357 >        RULEHD  *rp = ourmapping;
358  
359          if (rp == NULL) {               /* no rule set */
360                  if (matname[0])
# Line 369 | Line 381 | char *
381   getonm(void)                            /* invent a good name for object */
382   {
383          static char     name[64];
384 <        register char   *cp1, *cp2;
385 <        register int    i;
384 >        char    *cp1, *cp2;
385 >        int     i;
386                                          /* check for preset */
387          if (objname[0])
388                  return(objname);
# Line 393 | Line 405 | getonm(void)                           /* invent a good name for object */
405  
406   int
407   matchrule(                              /* check for a match on this rule */
408 <        register RULEHD *rp
408 >        RULEHD  *rp
409   )
410   {
411          ID      tmpid;
412          int     gotmatch;
413 <        register int    i;
413 >        int     i;
414  
415          if (rp->qflg & FL(Q_MTL)) {
416                  if (!matname[0])
# Line 446 | Line 458 | matchrule(                             /* check for a match on this rule */
458  
459   int
460   cvtndx(                         /* convert vertex string to index */
461 <        register VNDX   vi,
462 <        register char   *vs
461 >        VNDX    vi,
462 >        char    *vs
463   )
464   {
465                                          /* get point */
# Line 489 | Line 501 | cvtndx(                                /* convert vertex string to index */
501                          return(0);
502          } else
503                  vi[2] = -1;
504 +                                        /* zero normal is not normal */
505 +        if (vi[2] >= 0 && DOT(vnlist[vi[2]],vnlist[vi[2]]) <= FTINY)
506 +                vi[2] = -1;
507          return(1);
508   }
509  
510  
511   int
512   nonplanar(                      /* are vertices non-planar? */
513 <        register int    ac,
514 <        register char   **av
513 >        int     ac,
514 >        char    **av
515   )
516   {
517          VNDX    vi;
518          RREAL   *p0, *p1;
519          FVECT   v1, v2, nsum, newn;
520          double  d;
521 <        register int    i;
521 >        int     i;
522  
523          if (!cvtndx(vi, av[0]))
524                  return(0);
# Line 549 | Line 564 | nonplanar(                     /* are vertices non-planar? */
564   int
565   putface(                                /* put out an N-sided polygon */
566          int     ac,
567 <        register char   **av
567 >        char    **av
568   )
569   {
570          VNDX    vi;
571          char    *cp;
572 <        register int    i;
572 >        int     i;
573  
574          if (nonplanar(ac, av)) {        /* break into triangles */
575                  while (ac > 2) {
# Line 594 | Line 609 | puttri(                        /* put out a triangle */
609          RREAL   bcoor[3][3];
610          int     texOK = 0, patOK;
611          int     flatness;
612 <        register int    i;
612 >        int     i;
613  
614          if ((mod = getmtl()) == NULL)
615                  return(-1);
# Line 610 | Line 625 | puttri(                        /* put out a triangle */
625  
626          switch (flatness) {
627          case DEGEN:                     /* zero area */
628 +                ndegen++;
629                  return(-1);
630          case RVFLAT:                    /* reversed normals, but flat */
631          case ISFLAT:                    /* smoothing unnecessary */
# Line 630 | Line 646 | puttri(                        /* put out a triangle */
646          if (texOK | patOK)
647                  if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]],
648                                  vlist[v3i[0]]) < 0)
649 <                        return(-1);
649 >                        texOK = patOK = 0;
650                                          /* put out texture (if any) */
651          if (texOK) {
652                  printf("\n%s texfunc %s\n", mod, TEXNAME);
# Line 743 | Line 759 | newvn(                 /* create a new vertex normal */
759          vnlist[nvns][0] = x;
760          vnlist[nvns][1] = y;
761          vnlist[nvns][2] = z;
762 <        if (normalize(vnlist[nvns]) == 0.0)
747 <                return(0);
762 >        n0norm += (normalize(vnlist[nvns]) == 0.0);
763          return(++nvns);
764   }
765  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines