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.23 by greg, Fri Apr 23 16:20:56 2004 UTC vs.
Revision 2.32 by greg, Thu Apr 15 23:51:04 2021 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 11 | Line 11 | static const char      RCSid[] = "$Id$";
11   */
12  
13   #include <stdlib.h>
14 #include <stdio.h>
14   #include <ctype.h>
15  
16   #include "rtmath.h"
# Line 26 | Line 25 | static const char      RCSid[] = "$Id$";
25   #define DEFOBJ          "unnamed"       /* default object name */
26   #define DEFMAT          "white"         /* default material name */
27  
28 < #define pvect(v)        printf("%18.12g %18.12g %18.12g\n",(v)[0],(v)[1],(v)[2])
28 > #define pvect(v)        printf(" %18.12g %18.12g %18.12g\n",(v)[0],(v)[1],(v)[2])
29  
30   FVECT   *vlist;                 /* our vertex list */
31   int     nvs;                    /* number of vertices in our list */
# Line 35 | Line 34 | int    nvns;
34   RREAL   (*vtlist)[2];           /* map vertex list */
35   int     nvts;
36  
37 + int     ndegen = 0;             /* count of degenerate faces */
38 + int     n0norm = 0;             /* count of zero normals */
39 +
40   typedef int     VNDX[3];        /* vertex index (point,map,normal) */
41  
42   #define CHUNKSIZ        1024    /* vertex allocation chunk size */
# Line 68 | Line 70 | char   *defobj = DEFOBJ;       /* default (starting) object na
70  
71   int     flatten = 0;            /* discard surface normal information */
72  
73 < char    mapname[128];           /* current picture file */
74 < char    matname[64];            /* current material name */
75 < char    group[16][32];          /* current group names */
76 < char    objname[128];           /* current object name */
73 > char    mapname[256];           /* current picture file */
74 > char    matname[256];           /* current material name */
75 > char    group[8][256];          /* current group name(s) */
76 > char    objname[256];           /* current object name */
77   char    *inpfile;               /* input file name */
78   int     lineno;                 /* current line number */
79   int     faceno;                 /* current face number */
# Line 94 | Line 96 | static void syntax(char *er);
96  
97  
98   int
99 < main(           /* read in .obj file and convert */
99 > main(           /* read in .OBJ file and convert */
100          int     argc,
101          char    *argv[]
102   )
# Line 140 | Line 142 | main(          /* read in .obj file and convert */
142                  printargs(argc, argv, stdout);
143                  convert(stdin);
144          }
145 +        if (ndegen)
146 +                printf("# %d degenerate faces\n", ndegen);
147 +        if (n0norm)
148 +                printf("# %d invalid (zero) normals\n", n0norm);
149          exit(0);
150   userr:
151          fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n",
# Line 156 | Line 162 | getnames(                      /* get valid qualifier names */
162          char    *argv[MAXARG];
163          int     argc;
164          ID      tmpid;
165 <        register int    i;
165 >        int     i;
166  
167          while ( (argc = getstmt(argv, fp)) )
168                  switch (argv[0][0]) {
# Line 200 | Line 206 | getnames(                      /* get valid qualifier names */
206  
207  
208   void
209 < convert(                        /* convert a T-mesh */
209 > convert(                        /* convert an OBJ stream */
210          FILE    *fp
211   )
212   {
213          char    *argv[MAXARG];
214          int     argc;
215          int     nstats, nunknown;
216 <        register int    i;
216 >        int     i;
217  
218          nstats = nunknown = 0;
219                                          /* scan until EOF */
# Line 270 | Line 276 | convert(                       /* convert a T-mesh */
276                                  if (!strcmp(argv[1], "off"))
277                                          mapname[0] = '\0';
278                                  else
279 <                                        sprintf(mapname, "%s.pic", argv[1]);
279 >                                        sprintf(mapname, "%s.hdr", argv[1]);
280                          } else
281                                  goto unknown;
282                          break;
# Line 286 | Line 292 | convert(                       /* convert a T-mesh */
292                                  goto unknown;
293                          for (i = 1; i < argc; i++)
294                                  strcpy(group[i-1], argv[i]);
295 <                        group[i-1][0] = '\0';
295 >                        group[argc-1][0] = '\0';
296                          break;
297                  case '#':               /* comment */
298                          printargs(argc, argv, stdout);
# Line 306 | Line 312 | convert(                       /* convert a T-mesh */
312  
313   int
314   getstmt(                                /* read the next statement from fp */
315 <        register char   *av[MAXARG],
315 >        char    *av[MAXARG],
316          FILE    *fp
317   )
318   {
319          static char     sbuf[MAXARG*16];
320 <        register char   *cp;
321 <        register int    i;
320 >        char    *cp;
321 >        int     i;
322  
323          do {
324                  if (fgetline(cp=sbuf, sizeof(sbuf), fp) == NULL)
# Line 347 | Line 353 | getstmt(                               /* read the next statement from fp */
353   char *
354   getmtl(void)                            /* figure material for this face */
355   {
356 <        register RULEHD *rp = ourmapping;
356 >        RULEHD  *rp = ourmapping;
357  
358          if (rp == NULL) {               /* no rule set */
359                  if (matname[0])
# Line 374 | Line 380 | char *
380   getonm(void)                            /* invent a good name for object */
381   {
382          static char     name[64];
383 <        register char   *cp1, *cp2;
384 <        register int    i;
383 >        char    *cp1, *cp2;
384 >        int     i;
385                                          /* check for preset */
386          if (objname[0])
387                  return(objname);
# Line 398 | Line 404 | getonm(void)                           /* invent a good name for object */
404  
405   int
406   matchrule(                              /* check for a match on this rule */
407 <        register RULEHD *rp
407 >        RULEHD  *rp
408   )
409   {
410          ID      tmpid;
411          int     gotmatch;
412 <        register int    i;
412 >        int     i;
413  
414          if (rp->qflg & FL(Q_MTL)) {
415                  if (!matname[0])
# Line 451 | Line 457 | matchrule(                             /* check for a match on this rule */
457  
458   int
459   cvtndx(                         /* convert vertex string to index */
460 <        register VNDX   vi,
461 <        register char   *vs
460 >        VNDX    vi,
461 >        char    *vs
462   )
463   {
464                                          /* get point */
# Line 494 | Line 500 | cvtndx(                                /* convert vertex string to index */
500                          return(0);
501          } else
502                  vi[2] = -1;
503 +                                        /* zero normal is not normal */
504 +        if (vi[2] >= 0 && DOT(vnlist[vi[2]],vnlist[vi[2]]) <= FTINY)
505 +                vi[2] = -1;
506          return(1);
507   }
508  
509  
510   int
511   nonplanar(                      /* are vertices non-planar? */
512 <        register int    ac,
513 <        register char   **av
512 >        int     ac,
513 >        char    **av
514   )
515   {
516          VNDX    vi;
517          RREAL   *p0, *p1;
518          FVECT   v1, v2, nsum, newn;
519          double  d;
520 <        register int    i;
520 >        int     i;
521  
522          if (!cvtndx(vi, av[0]))
523                  return(0);
# Line 554 | Line 563 | nonplanar(                     /* are vertices non-planar? */
563   int
564   putface(                                /* put out an N-sided polygon */
565          int     ac,
566 <        register char   **av
566 >        char    **av
567   )
568   {
569          VNDX    vi;
570          char    *cp;
571 <        register int    i;
571 >        int     i;
572  
573          if (nonplanar(ac, av)) {        /* break into triangles */
574                  while (ac > 2) {
# Line 599 | Line 608 | puttri(                        /* put out a triangle */
608          RREAL   bcoor[3][3];
609          int     texOK = 0, patOK;
610          int     flatness;
611 <        register int    i;
611 >        int     i;
612  
613          if ((mod = getmtl()) == NULL)
614                  return(-1);
# Line 615 | Line 624 | puttri(                        /* put out a triangle */
624  
625          switch (flatness) {
626          case DEGEN:                     /* zero area */
627 +                ndegen++;
628                  return(-1);
629          case RVFLAT:                    /* reversed normals, but flat */
630          case ISFLAT:                    /* smoothing unnecessary */
# Line 635 | Line 645 | puttri(                        /* put out a triangle */
645          if (texOK | patOK)
646                  if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]],
647                                  vlist[v3i[0]]) < 0)
648 <                        return(-1);
648 >                        texOK = patOK = 0;
649                                          /* put out texture (if any) */
650          if (texOK) {
651                  printf("\n%s texfunc %s\n", mod, TEXNAME);
# Line 647 | Line 657 | puttri(                        /* put out a triangle */
657                          bcoor[i][1] = vnlist[v2i[2]][i];
658                          bcoor[i][2] = vnlist[v3i[2]][i];
659                  }
660 <                put_baryc(&bvecs, bcoor, 3);
660 >                fput_baryc(&bvecs, bcoor, 3, stdout);
661          }
662   #ifdef TEXMAPS
663                                          /* put out pattern (if any) */
# Line 661 | Line 671 | puttri(                        /* put out a triangle */
671                          bcoor[i][1] = vtlist[v2i[1]][i];
672                          bcoor[i][2] = vtlist[v3i[1]][i];
673                  }
674 <                put_baryc(&bvecs, bcoor, 2);
674 >                fput_baryc(&bvecs, bcoor, 2, stdout);
675          }
676   #endif
677                                          /* put out (reversed) triangle */
# Line 748 | Line 758 | newvn(                 /* create a new vertex normal */
758          vnlist[nvns][0] = x;
759          vnlist[nvns][1] = y;
760          vnlist[nvns][2] = z;
761 <        if (normalize(vnlist[nvns]) == 0.0)
752 <                return(0);
761 >        n0norm += (normalize(vnlist[nvns]) == 0.0);
762          return(++nvns);
763   }
764  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines