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.14 by greg, Wed Jun 15 19:06:04 1994 UTC vs.
Revision 2.24 by greg, Tue Jun 24 02:01:19 2008 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1994 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Convert a Wavefront .obj file to Radiance format.
6   *
# Line 13 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
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"
21 + #include "tmesh.h"
22  
20 #include <ctype.h>
23  
22 #define TCALNAME        "tmesh.cal"     /* triangle interp. file */
24   #define PATNAME         "M-pat"         /* mesh pattern name (reused) */
25   #define TEXNAME         "M-nor"         /* mesh texture name (reused) */
26   #define DEFOBJ          "unnamed"       /* default object name */
27   #define DEFMAT          "white"         /* default material name */
28  
28 #define  ABS(x)         ((x)>=0 ? (x) : -(x))
29
29   #define pvect(v)        printf("%18.12g %18.12g %18.12g\n",(v)[0],(v)[1],(v)[2])
30  
31   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 < typedef struct {
40 <        int     ax;             /* major axis */
41 <        FLOAT   tm[2][3];       /* transformation */
42 < } BARYCCM;
38 > int     ndegen = 0;             /* count of degenerate faces */
39  
40   typedef int     VNDX[3];        /* vertex index (point,map,normal) */
41  
42 < #define CHUNKSIZ        256     /* vertex allocation chunk size */
42 > #define CHUNKSIZ        1024    /* vertex allocation chunk size */
43  
44 < #define MAXARG          64      /* maximum # arguments in a statement */
44 > #define MAXARG          512     /* maximum # arguments in a statement */
45  
46                                  /* qualifiers */
47   #define Q_MTL           0
# Line 74 | Line 70 | char   *defobj = DEFOBJ;       /* default (starting) object na
70  
71   int     flatten = 0;            /* discard surface normal information */
72  
77 char    *getmtl(), *getonm();
78
73   char    mapname[128];           /* current picture file */
74   char    matname[64];            /* current material name */
75   char    group[16][32];          /* current group names */
# Line 84 | Line 78 | char   *inpfile;               /* input file name */
78   int     lineno;                 /* current line number */
79   int     faceno;                 /* current face number */
80  
81 + static void getnames(FILE *fp);
82 + static void convert(FILE *fp);
83 + static int getstmt(char *av[MAXARG], FILE *fp);
84 + static char * getmtl(void);
85 + static char * getonm(void);
86 + static int matchrule(RULEHD *rp);
87 + static int cvtndx(VNDX vi, char *vs);
88 + static int nonplanar(int ac, char **av);
89 + static int putface(int ac, char **av);
90 + static int puttri(char *v1, char *v2, char *v3);
91 + static void freeverts(void);
92 + static int newv(double x, double y, double z);
93 + static int newvn(double x, double y, double z);
94 + static int newvt(double x, double y);
95 + static void syntax(char *er);
96  
97 < main(argc, argv)                /* read in .obj file and convert */
98 < int     argc;
99 < char    *argv[];
97 >
98 > int
99 > main(           /* read in .obj file and convert */
100 >        int     argc,
101 >        char    *argv[]
102 > )
103   {
104          int     donames = 0;
105          int     i;
# Line 109 | Line 121 | char   *argv[];
121                  default:
122                          goto userr;
123                  }
124 <        if (i > argc | i < argc-1)
124 >        if ((i > argc) | (i < argc-1))
125                  goto userr;
126          if (i == argc)
127                  inpfile = "<stdin>";
# Line 130 | Line 142 | char   *argv[];
142                  printargs(argc, argv, stdout);
143                  convert(stdin);
144          }
145 +        if (ndegen)
146 +                printf("# %d degenerate faces\n", ndegen);
147          exit(0);
148   userr:
149          fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n",
# Line 138 | Line 152 | userr:
152   }
153  
154  
155 < getnames(fp)                    /* get valid qualifier names */
156 < FILE    *fp;
155 > void
156 > getnames(                       /* get valid qualifier names */
157 >        FILE    *fp
158 > )
159   {
160          char    *argv[MAXARG];
161          int     argc;
162          ID      tmpid;
163          register int    i;
164  
165 <        while (argc = getstmt(argv, fp))
165 >        while ( (argc = getstmt(argv, fp)) )
166                  switch (argv[0][0]) {
167                  case 'f':                               /* face */
168                          if (!argv[0][1])
# Line 187 | Line 203 | FILE   *fp;
203   }
204  
205  
206 < convert(fp)                     /* convert a T-mesh */
207 < FILE    *fp;
206 > void
207 > convert(                        /* convert a T-mesh */
208 >        FILE    *fp
209 > )
210   {
211          char    *argv[MAXARG];
212          int     argc;
# Line 197 | Line 215 | FILE   *fp;
215  
216          nstats = nunknown = 0;
217                                          /* scan until EOF */
218 <        while (argc = getstmt(argv, fp)) {
218 >        while ( (argc = getstmt(argv, fp)) ) {
219                  switch (argv[0][0]) {
220                  case 'v':               /* vertex */
221                          switch (argv[0][1]) {
# Line 291 | Line 309 | FILE   *fp;
309  
310  
311   int
312 < getstmt(av, fp)                         /* read the next statement from fp */
313 < register char   *av[MAXARG];
314 < FILE    *fp;
312 > getstmt(                                /* read the next statement from fp */
313 >        register char   *av[MAXARG],
314 >        FILE    *fp
315 > )
316   {
317 <        extern char     *fgetline();
299 <        static char     sbuf[MAXARG*10];
317 >        static char     sbuf[MAXARG*16];
318          register char   *cp;
319          register int    i;
320  
# Line 310 | Line 328 | FILE   *fp;
328                                          lineno++;
329                                  *cp++ = '\0';
330                          }
331 <                        if (!*cp || i >= MAXARG-1)
331 >                        if (!*cp)
332                                  break;
333 +                        if (i >= MAXARG-1) {
334 +                                fprintf(stderr,
335 +                        "warning: line %d: too many arguments (limit %d)\n",
336 +                                        lineno+1, MAXARG-1);
337 +                                break;
338 +                        }
339                          av[i++] = cp;
340                          while (*++cp && !isspace(*cp))
341                                  ;
# Line 325 | Line 349 | FILE   *fp;
349  
350  
351   char *
352 < getmtl()                                /* figure material for this face */
352 > getmtl(void)                            /* figure material for this face */
353   {
354          register RULEHD *rp = ourmapping;
355  
# Line 351 | Line 375 | getmtl()                               /* figure material for this face */
375  
376  
377   char *
378 < getonm()                                /* invent a good name for object */
378 > getonm(void)                            /* invent a good name for object */
379   {
380          static char     name[64];
381          register char   *cp1, *cp2;
# Line 366 | Line 390 | getonm()                               /* invent a good name for object */
390                  cp2 = group[i];
391                  if (cp1 > name)
392                          *cp1++ = '.';
393 <                while (*cp1 = *cp2++)
393 >                while ( (*cp1 = *cp2++) )
394                          if (++cp1 >= name+sizeof(name)-2) {
395                                  *cp1 = '\0';
396                                  return(name);
# Line 376 | Line 400 | getonm()                               /* invent a good name for object */
400   }
401  
402  
403 < matchrule(rp)                           /* check for a match on this rule */
404 < register RULEHD *rp;
403 > int
404 > matchrule(                              /* check for a match on this rule */
405 >        register RULEHD *rp
406 > )
407   {
408          ID      tmpid;
409          int     gotmatch;
# Line 427 | Line 453 | register RULEHD        *rp;
453   }
454  
455  
456 < cvtndx(vi, vs)                          /* convert vertex string to index */
457 < register VNDX   vi;
458 < register char   *vs;
456 > int
457 > cvtndx(                         /* convert vertex string to index */
458 >        register VNDX   vi,
459 >        register char   *vs
460 > )
461   {
462                                          /* get point */
463          vi[0] = atoi(vs);
# Line 474 | Line 502 | register char  *vs;
502   }
503  
504  
505 < nonplanar(ac, av)                       /* are vertices non-planar? */
506 < register int    ac;
507 < register char   **av;
505 > int
506 > nonplanar(                      /* are vertices non-planar? */
507 >        register int    ac,
508 >        register char   **av
509 > )
510   {
511          VNDX    vi;
512 <        FLOAT   *p0, *p1;
512 >        RREAL   *p0, *p1;
513          FVECT   v1, v2, nsum, newn;
514          double  d;
515          register int    i;
# Line 525 | Line 555 | register char  **av;
555   }
556  
557  
558 < putface(ac, av)                         /* put out an N-sided polygon */
559 < int     ac;
560 < register char   **av;
558 > int
559 > putface(                                /* put out an N-sided polygon */
560 >        int     ac,
561 >        register char   **av
562 > )
563   {
564          VNDX    vi;
565          char    *cp;
# Line 546 | Line 578 | register char  **av;
578                  return(1);
579          }
580          if ((cp = getmtl()) == NULL)
581 <                return(-1);
581 >                return(0);
582          printf("\n%s polygon %s.%d\n", cp, getonm(), faceno);
583          printf("0\n0\n%d\n", 3*ac);
584          for (i = 0; i < ac; i++) {
# Line 558 | Line 590 | register char  **av;
590   }
591  
592  
593 < puttri(v1, v2, v3)                      /* put out a triangle */
594 < char    *v1, *v2, *v3;
593 > int
594 > puttri(                 /* put out a triangle */
595 >        char *v1,
596 >        char *v2,
597 >        char *v3
598 > )
599   {
600          char    *mod;
601          VNDX    v1i, v2i, v3i;
602          BARYCCM bvecs;
603 <        FLOAT   bcoor[3][3];
604 <        int     texOK, patOK;
603 >        RREAL   bcoor[3][3];
604 >        int     texOK = 0, patOK;
605 >        int     flatness;
606          register int    i;
607  
608          if ((mod = getmtl()) == NULL)
609 <                return(-1);
609 >                return(0);
610  
611          if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3))
612                  return(0);
613                                          /* compute barycentric coordinates */
614 <        texOK = !flatten && (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0);
614 >        if (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0)
615 >                flatness = flat_tri(vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]],
616 >                                vnlist[v1i[2]], vnlist[v2i[2]], vnlist[v3i[2]]);
617 >        else
618 >                flatness = ISFLAT;
619 >
620 >        switch (flatness) {
621 >        case DEGEN:                     /* zero area */
622 >                ndegen++;
623 >                return(-1);
624 >        case RVFLAT:                    /* reversed normals, but flat */
625 >        case ISFLAT:                    /* smoothing unnecessary */
626 >                texOK = 0;
627 >                break;
628 >        case RVBENT:                    /* reversed normals with smoothing */
629 >        case ISBENT:                    /* proper smoothing */
630 >                texOK = 1;
631 >                break;
632 >        }
633 >        if (flatten)
634 >                texOK = 0;
635   #ifdef TEXMAPS
636          patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0);
637   #else
# Line 583 | Line 640 | char   *v1, *v2, *v3;
640          if (texOK | patOK)
641                  if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]],
642                                  vlist[v3i[0]]) < 0)
643 <                        return(-1);
643 >                        texOK = patOK = 0;
644                                          /* put out texture (if any) */
645          if (texOK) {
646                  printf("\n%s texfunc %s\n", mod, TEXNAME);
# Line 612 | Line 669 | char   *v1, *v2, *v3;
669                  put_baryc(&bvecs, bcoor, 2);
670          }
671   #endif
672 <                                        /* put out triangle */
672 >                                        /* put out (reversed) triangle */
673          printf("\n%s polygon %s.%d\n", mod, getonm(), faceno);
674          printf("0\n0\n9\n");
675 <        pvect(vlist[v1i[0]]);
676 <        pvect(vlist[v2i[0]]);
677 <        pvect(vlist[v3i[0]]);
678 <
675 >        if (flatness == RVFLAT || flatness == RVBENT) {
676 >                pvect(vlist[v3i[0]]);
677 >                pvect(vlist[v2i[0]]);
678 >                pvect(vlist[v1i[0]]);
679 >        } else {
680 >                pvect(vlist[v1i[0]]);
681 >                pvect(vlist[v2i[0]]);
682 >                pvect(vlist[v3i[0]]);
683 >        }
684          return(1);
685   }
686  
687  
688 < int
689 < comp_baryc(bcm, v1, v2, v3)             /* compute barycentric vectors */
628 < register BARYCCM        *bcm;
629 < FLOAT   *v1, *v2, *v3;
688 > void
689 > freeverts(void)                 /* free all vertices */
690   {
631        FLOAT   *vt;
632        FVECT   va, vab, vcb;
633        double  d;
634        int     ax0, ax1;
635        register int    i, j;
636                                        /* compute major axis */
637        for (i = 0; i < 3; i++) {
638                vab[i] = v1[i] - v2[i];
639                vcb[i] = v3[i] - v2[i];
640        }
641        fcross(va, vab, vcb);
642        bcm->ax = ABS(va[0]) > ABS(va[1]) ? 0 : 1;
643        bcm->ax = ABS(va[bcm->ax]) > ABS(va[2]) ? bcm->ax : 2;
644        ax0 = (bcm->ax + 1) % 3;
645        ax1 = (bcm->ax + 2) % 3;
646        for (j = 0; j < 2; j++) {
647                vab[0] = v1[ax0] - v2[ax0];
648                vcb[0] = v3[ax0] - v2[ax0];
649                vab[1] = v1[ax1] - v2[ax1];
650                vcb[1] = v3[ax1] - v2[ax1];
651                d = vcb[0]*vcb[0] + vcb[1]*vcb[1];
652                if (d <= FTINY)
653                        return(-1);
654                d = (vcb[0]*vab[0]+vcb[1]*vab[1])/d;
655                va[0] = vab[0] - vcb[0]*d;
656                va[1] = vab[1] - vcb[1]*d;
657                d = va[0]*va[0] + va[1]*va[1];
658                if (d <= FTINY)
659                        return(-1);
660                bcm->tm[j][0] = va[0] /= d;
661                bcm->tm[j][1] = va[1] /= d;
662                bcm->tm[j][2] = -(v2[ax0]*va[0]+v2[ax1]*va[1]);
663                                        /* rotate vertices */
664                vt = v1;
665                v1 = v2;
666                v2 = v3;
667                v3 = vt;
668        }
669        return(0);
670 }
671
672
673 put_baryc(bcm, com, n)                  /* put barycentric coord. vectors */
674 register BARYCCM        *bcm;
675 register FLOAT  com[][3];
676 int     n;
677 {
678        double  a, b;
679        register int    i, j;
680
681        printf("%d\t%d\n", 1+3*n, bcm->ax);
682        for (i = 0; i < n; i++) {
683                a = com[i][0] - com[i][2];
684                b = com[i][1] - com[i][2];
685                printf("%14.8f %14.8f %14.8f\n",
686                        bcm->tm[0][0]*a + bcm->tm[1][0]*b,
687                        bcm->tm[0][1]*a + bcm->tm[1][1]*b,
688                        bcm->tm[0][2]*a + bcm->tm[1][2]*b + com[i][2]);
689        }
690 }
691
692
693 freeverts()                     /* free all vertices */
694 {
691          if (nvs) {
692 <                free((char *)vlist);
692 >                free((void *)vlist);
693                  nvs = 0;
694          }
695          if (nvts) {
696 <                free((char *)vtlist);
696 >                free((void *)vtlist);
697                  nvts = 0;
698          }
699          if (nvns) {
700 <                free((char *)vnlist);
700 >                free((void *)vnlist);
701                  nvns = 0;
702          }
703   }
704  
705  
706   int
707 < newv(x, y, z)                   /* create a new vertex */
708 < double  x, y, z;
707 > newv(                   /* create a new vertex */
708 >        double  x,
709 >        double  y,
710 >        double  z
711 > )
712   {
713          if (!(nvs%CHUNKSIZ)) {          /* allocate next block */
714                  if (nvs == 0)
715                          vlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT));
716                  else
717 <                        vlist = (FVECT *)realloc((char *)vlist,
717 >                        vlist = (FVECT *)realloc((void *)vlist,
718                                          (nvs+CHUNKSIZ)*sizeof(FVECT));
719                  if (vlist == NULL) {
720                          fprintf(stderr,
# Line 732 | Line 731 | double x, y, z;
731  
732  
733   int
734 < newvn(x, y, z)                  /* create a new vertex normal */
735 < double  x, y, z;
734 > newvn(                  /* create a new vertex normal */
735 >        double  x,
736 >        double  y,
737 >        double  z
738 > )
739   {
740          if (!(nvns%CHUNKSIZ)) {         /* allocate next block */
741                  if (nvns == 0)
742                          vnlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT));
743                  else
744 <                        vnlist = (FVECT *)realloc((char *)vnlist,
744 >                        vnlist = (FVECT *)realloc((void *)vnlist,
745                                          (nvns+CHUNKSIZ)*sizeof(FVECT));
746                  if (vnlist == NULL) {
747                          fprintf(stderr,
# Line 758 | Line 760 | double x, y, z;
760  
761  
762   int
763 < newvt(x, y)                     /* create a new texture map vertex */
764 < double  x, y;
763 > newvt(                  /* create a new texture map vertex */
764 >        double  x,
765 >        double  y
766 > )
767   {
768          if (!(nvts%CHUNKSIZ)) {         /* allocate next block */
769                  if (nvts == 0)
770 <                        vtlist = (FLOAT (*)[2])malloc(CHUNKSIZ*2*sizeof(FLOAT));
770 >                        vtlist = (RREAL (*)[2])malloc(CHUNKSIZ*2*sizeof(RREAL));
771                  else
772 <                        vtlist = (FLOAT (*)[2])realloc((char *)vtlist,
773 <                                        (nvts+CHUNKSIZ)*2*sizeof(FLOAT));
772 >                        vtlist = (RREAL (*)[2])realloc((void *)vtlist,
773 >                                        (nvts+CHUNKSIZ)*2*sizeof(RREAL));
774                  if (vtlist == NULL) {
775                          fprintf(stderr,
776                          "Out of memory while allocating texture vertex %d\n",
# Line 781 | Line 785 | double x, y;
785   }
786  
787  
788 < syntax(er)                      /* report syntax error and exit */
789 < char    *er;
788 > void
789 > syntax(                 /* report syntax error and exit */
790 >        char    *er
791 > )
792   {
793          fprintf(stderr, "%s: Wavefront syntax error near line %d: %s\n",
794                          inpfile, lineno, er);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines