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.19 by greg, Wed Apr 23 00:52:33 2003 UTC vs.
Revision 2.26 by greg, Thu Sep 16 03:28:21 2010 UTC

# Line 10 | Line 10 | static const char      RCSid[] = "$Id$";
10   * I'm not sure they work correctly.  (Taken out -- see TEXMAPS defines.)
11   */
12  
13 < #include "standard.h"
13 > #include <stdlib.h>
14 > #include <stdio.h>
15 > #include <ctype.h>
16  
17 + #include "rtmath.h"
18 + #include "rtio.h"
19 + #include "resolu.h"
20   #include "trans.h"
16
21   #include "tmesh.h"
22  
19 #include <ctype.h>
23  
24   #define PATNAME         "M-pat"         /* mesh pattern name (reused) */
25   #define TEXNAME         "M-nor"         /* mesh texture name (reused) */
# Line 29 | Line 32 | FVECT  *vlist;                 /* our vertex list */
32   int     nvs;                    /* number of vertices in our list */
33   FVECT   *vnlist;                /* vertex normal list */
34   int     nvns;
35 < FLOAT   (*vtlist)[2];           /* map vertex list */
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 65 | Line 71 | char   *defobj = DEFOBJ;       /* default (starting) object na
71  
72   int     flatten = 0;            /* discard surface normal information */
73  
68 char    *getmtl(), *getonm();
69
74   char    mapname[128];           /* current picture file */
75   char    matname[64];            /* current material name */
76   char    group[16][32];          /* current group names */
# Line 75 | Line 79 | char   *inpfile;               /* input file name */
79   int     lineno;                 /* current line number */
80   int     faceno;                 /* current face number */
81  
82 + static void getnames(FILE *fp);
83 + static void convert(FILE *fp);
84 + static int getstmt(char *av[MAXARG], FILE *fp);
85 + static char * getmtl(void);
86 + static char * getonm(void);
87 + static int matchrule(RULEHD *rp);
88 + static int cvtndx(VNDX vi, char *vs);
89 + static int nonplanar(int ac, char **av);
90 + static int putface(int ac, char **av);
91 + static int puttri(char *v1, char *v2, char *v3);
92 + static void freeverts(void);
93 + static int newv(double x, double y, double z);
94 + static int newvn(double x, double y, double z);
95 + static int newvt(double x, double y);
96 + static void syntax(char *er);
97  
98 < main(argc, argv)                /* read in .obj file and convert */
99 < int     argc;
100 < char    *argv[];
98 >
99 > int
100 > main(           /* read in .obj file and convert */
101 >        int     argc,
102 >        char    *argv[]
103 > )
104   {
105          int     donames = 0;
106          int     i;
# Line 100 | Line 122 | char   *argv[];
122                  default:
123                          goto userr;
124                  }
125 <        if (i > argc | i < argc-1)
125 >        if ((i > argc) | (i < argc-1))
126                  goto userr;
127          if (i == argc)
128                  inpfile = "<stdin>";
# Line 121 | Line 143 | char   *argv[];
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 129 | Line 155 | userr:
155   }
156  
157  
158 < getnames(fp)                    /* get valid qualifier names */
159 < FILE    *fp;
158 > void
159 > getnames(                       /* get valid qualifier names */
160 >        FILE    *fp
161 > )
162   {
163          char    *argv[MAXARG];
164          int     argc;
165          ID      tmpid;
166          register int    i;
167  
168 <        while (argc = getstmt(argv, fp))
168 >        while ( (argc = getstmt(argv, fp)) )
169                  switch (argv[0][0]) {
170                  case 'f':                               /* face */
171                          if (!argv[0][1])
# Line 178 | Line 206 | FILE   *fp;
206   }
207  
208  
209 < convert(fp)                     /* convert a T-mesh */
210 < FILE    *fp;
209 > void
210 > convert(                        /* convert a T-mesh */
211 >        FILE    *fp
212 > )
213   {
214          char    *argv[MAXARG];
215          int     argc;
# Line 188 | Line 218 | FILE   *fp;
218  
219          nstats = nunknown = 0;
220                                          /* scan until EOF */
221 <        while (argc = getstmt(argv, fp)) {
221 >        while ( (argc = getstmt(argv, fp)) ) {
222                  switch (argv[0][0]) {
223                  case 'v':               /* vertex */
224                          switch (argv[0][1]) {
# Line 247 | Line 277 | FILE   *fp;
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 282 | Line 312 | FILE   *fp;
312  
313  
314   int
315 < getstmt(av, fp)                         /* read the next statement from fp */
316 < register char   *av[MAXARG];
317 < FILE    *fp;
315 > getstmt(                                /* read the next statement from fp */
316 >        register char   *av[MAXARG],
317 >        FILE    *fp
318 > )
319   {
320 <        extern char     *fgetline();
290 <        static char     sbuf[MAXARG*10];
320 >        static char     sbuf[MAXARG*16];
321          register char   *cp;
322          register int    i;
323  
# Line 301 | Line 331 | FILE   *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 316 | Line 352 | FILE   *fp;
352  
353  
354   char *
355 < getmtl()                                /* figure material for this face */
355 > getmtl(void)                            /* figure material for this face */
356   {
357          register RULEHD *rp = ourmapping;
358  
# Line 342 | Line 378 | getmtl()                               /* figure material for this face */
378  
379  
380   char *
381 < getonm()                                /* invent a good name for object */
381 > getonm(void)                            /* invent a good name for object */
382   {
383          static char     name[64];
384          register char   *cp1, *cp2;
# Line 357 | Line 393 | getonm()                               /* invent a good name for object */
393                  cp2 = group[i];
394                  if (cp1 > name)
395                          *cp1++ = '.';
396 <                while (*cp1 = *cp2++)
396 >                while ( (*cp1 = *cp2++) )
397                          if (++cp1 >= name+sizeof(name)-2) {
398                                  *cp1 = '\0';
399                                  return(name);
# Line 367 | Line 403 | getonm()                               /* invent a good name for object */
403   }
404  
405  
406 < matchrule(rp)                           /* check for a match on this rule */
407 < register RULEHD *rp;
406 > int
407 > matchrule(                              /* check for a match on this rule */
408 >        register RULEHD *rp
409 > )
410   {
411          ID      tmpid;
412          int     gotmatch;
# Line 418 | Line 456 | register RULEHD        *rp;
456   }
457  
458  
459 < cvtndx(vi, vs)                          /* convert vertex string to index */
460 < register VNDX   vi;
461 < register char   *vs;
459 > int
460 > cvtndx(                         /* convert vertex string to index */
461 >        register VNDX   vi,
462 >        register char   *vs
463 > )
464   {
465                                          /* get point */
466          vi[0] = atoi(vs);
# Line 461 | Line 501 | register char  *vs;
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 < nonplanar(ac, av)                       /* are vertices non-planar? */
512 < register int    ac;
513 < register char   **av;
511 > int
512 > nonplanar(                      /* are vertices non-planar? */
513 >        register int    ac,
514 >        register char   **av
515 > )
516   {
517          VNDX    vi;
518 <        FLOAT   *p0, *p1;
518 >        RREAL   *p0, *p1;
519          FVECT   v1, v2, nsum, newn;
520          double  d;
521          register int    i;
# Line 516 | Line 561 | register char  **av;
561   }
562  
563  
564 < putface(ac, av)                         /* put out an N-sided polygon */
565 < int     ac;
566 < register char   **av;
564 > int
565 > putface(                                /* put out an N-sided polygon */
566 >        int     ac,
567 >        register char   **av
568 > )
569   {
570          VNDX    vi;
571          char    *cp;
# Line 537 | Line 584 | register char  **av;
584                  return(1);
585          }
586          if ((cp = getmtl()) == NULL)
587 <                return(-1);
587 >                return(0);
588          printf("\n%s polygon %s.%d\n", cp, getonm(), faceno);
589          printf("0\n0\n%d\n", 3*ac);
590          for (i = 0; i < ac; i++) {
# Line 549 | Line 596 | register char  **av;
596   }
597  
598  
599 < puttri(v1, v2, v3)                      /* put out a triangle */
600 < char    *v1, *v2, *v3;
599 > int
600 > puttri(                 /* put out a triangle */
601 >        char *v1,
602 >        char *v2,
603 >        char *v3
604 > )
605   {
606          char    *mod;
607          VNDX    v1i, v2i, v3i;
608          BARYCCM bvecs;
609 <        FLOAT   bcoor[3][3];
610 <        int     texOK, patOK;
609 >        RREAL   bcoor[3][3];
610 >        int     texOK = 0, patOK;
611          int     flatness;
612          register int    i;
613  
614          if ((mod = getmtl()) == NULL)
615 <                return(-1);
615 >                return(0);
616  
617          if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3))
618                  return(0);
# Line 574 | Line 625 | char   *v1, *v2, *v3;
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 594 | Line 646 | char   *v1, *v2, *v3;
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 639 | Line 691 | char   *v1, *v2, *v3;
691   }
692  
693  
694 < freeverts()                     /* free all vertices */
694 > void
695 > freeverts(void)                 /* free all vertices */
696   {
697          if (nvs) {
698                  free((void *)vlist);
# Line 657 | Line 710 | freeverts()                    /* free all vertices */
710  
711  
712   int
713 < newv(x, y, z)                   /* create a new vertex */
714 < double  x, y, z;
713 > newv(                   /* create a new vertex */
714 >        double  x,
715 >        double  y,
716 >        double  z
717 > )
718   {
719          if (!(nvs%CHUNKSIZ)) {          /* allocate next block */
720                  if (nvs == 0)
# Line 681 | Line 737 | double x, y, z;
737  
738  
739   int
740 < newvn(x, y, z)                  /* create a new vertex normal */
741 < double  x, y, z;
740 > newvn(                  /* create a new vertex normal */
741 >        double  x,
742 >        double  y,
743 >        double  z
744 > )
745   {
746          if (!(nvns%CHUNKSIZ)) {         /* allocate next block */
747                  if (nvns == 0)
# Line 700 | Line 759 | double x, y, z;
759          vnlist[nvns][0] = x;
760          vnlist[nvns][1] = y;
761          vnlist[nvns][2] = z;
762 <        if (normalize(vnlist[nvns]) == 0.0)
704 <                return(0);
762 >        n0norm += (normalize(vnlist[nvns]) == 0.0);
763          return(++nvns);
764   }
765  
766  
767   int
768 < newvt(x, y)                     /* create a new texture map vertex */
769 < double  x, y;
768 > newvt(                  /* create a new texture map vertex */
769 >        double  x,
770 >        double  y
771 > )
772   {
773          if (!(nvts%CHUNKSIZ)) {         /* allocate next block */
774                  if (nvts == 0)
775 <                        vtlist = (FLOAT (*)[2])malloc(CHUNKSIZ*2*sizeof(FLOAT));
775 >                        vtlist = (RREAL (*)[2])malloc(CHUNKSIZ*2*sizeof(RREAL));
776                  else
777 <                        vtlist = (FLOAT (*)[2])realloc((void *)vtlist,
778 <                                        (nvts+CHUNKSIZ)*2*sizeof(FLOAT));
777 >                        vtlist = (RREAL (*)[2])realloc((void *)vtlist,
778 >                                        (nvts+CHUNKSIZ)*2*sizeof(RREAL));
779                  if (vtlist == NULL) {
780                          fprintf(stderr,
781                          "Out of memory while allocating texture vertex %d\n",
# Line 730 | Line 790 | double x, y;
790   }
791  
792  
793 < syntax(er)                      /* report syntax error and exit */
794 < char    *er;
793 > void
794 > syntax(                 /* report syntax error and exit */
795 >        char    *er
796 > )
797   {
798          fprintf(stderr, "%s: Wavefront syntax error near line %d: %s\n",
799                          inpfile, lineno, er);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines