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.5 by greg, Wed Apr 13 17:24:24 1994 UTC vs.
Revision 2.7 by greg, Thu Apr 14 13:36:18 1994 UTC

# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10   * Currently, we support only polygonal geometry.  Non-planar
11   * faces are broken rather haphazardly into triangles.
12   * Also, texture map indices only work for triangles, though
13 < * I'm not sure they work correctly.
13 > * I'm not sure they work correctly.  (Taken out -- see TEXMAPS defines.)
14   */
15  
16   #include "standard.h"
# Line 69 | Line 69 | RULEHD *ourmapping = NULL;
69  
70   char    *defmat = DEFMAT;       /* default (starting) material name */
71   char    *defobj = DEFOBJ;       /* default (starting) object name */
72 int     donames = 0;            /* only get qualifier names */
72  
73   char    *getmtl(), *getonm();
74  
# Line 77 | Line 76 | char   mapname[128];           /* current picture file */
76   char    matname[64];            /* current material name */
77   char    group[16][32];          /* current group names */
78   char    objname[128];           /* current object name */
79 + char    *inpfile;               /* input file name */
80   int     lineno;                 /* current line number */
81 < int     faceno;                 /* number of faces read */
81 > int     faceno;                 /* current face number */
82  
83  
84   main(argc, argv)                /* read in .obj file and convert */
85   int     argc;
86   char    *argv[];
87   {
88 <        char    *fname;
88 >        int     donames = 0;
89          int     i;
90  
91          for (i = 1; i < argc && argv[i][0] == '-'; i++)
# Line 105 | Line 105 | char   *argv[];
105          if (i > argc | i < argc-1)
106                  goto userr;
107          if (i == argc)
108 <                fname = "<stdin>";
109 <        else if (freopen(fname=argv[i], "r", stdin) == NULL) {
110 <                fprintf(stderr, "%s: cannot open\n", fname);
108 >                inpfile = "<stdin>";
109 >        else if (freopen(inpfile=argv[i], "r", stdin) == NULL) {
110 >                fprintf(stderr, "%s: cannot open\n", inpfile);
111                  exit(1);
112          }
113          if (donames) {                          /* scan for ids */
114                  getnames(stdin);
115 <                printf("filename \"%s\"\n", fname);
115 >                printf("filename \"%s\"\n", inpfile);
116                  printf("filetype \"Wavefront\"\n");
117                  write_quals(&qlist, qual, stdout);
118                  printf("qualifier %s begin\n", qlist.qual[Q_FAC]);
# Line 121 | Line 121 | char   *argv[];
121          } else {                                /* translate file */
122                  printf("# ");
123                  printargs(argc, argv, stdout);
124 <                convert(fname, stdin);
124 >                convert(stdin);
125          }
126          exit(0);
127   userr:
# Line 180 | Line 180 | FILE   *fp;
180   }
181  
182  
183 < convert(fname, fp)              /* convert a T-mesh */
184 < char    *fname;
183 > convert(fp)                     /* convert a T-mesh */
184   FILE    *fp;
185   {
186          char    *argv[MAXARG];
187          int     argc;
188          int     nstats, nunknown;
189          register int    i;
190 <                                        /* start fresh */
192 <        freeverts();
193 <        mapname[0] = '\0';
194 <        strcpy(matname, defmat);
195 <        strcpy(objname, defobj);
196 <        lineno = 0;
190 >
191          nstats = nunknown = 0;
192                                          /* scan until EOF */
193          while (argc = getstmt(argv, fp)) {
# Line 202 | Line 196 | FILE   *fp;
196                          switch (argv[0][1]) {
197                          case '\0':                      /* point */
198                                  if (badarg(argc-1,argv+1,"fff"))
199 <                                        syntax(fname, lineno, "Bad vertex");
199 >                                        syntax("Bad vertex");
200                                  newv(atof(argv[1]), atof(argv[2]),
201                                                  atof(argv[3]));
202                                  break;
# Line 210 | Line 204 | FILE   *fp;
204                                  if (argv[0][2])
205                                          goto unknown;
206                                  if (badarg(argc-1,argv+1,"fff"))
207 <                                        syntax(fname, lineno, "Bad normal");
207 >                                        syntax("Bad normal");
208                                  if (!newvn(atof(argv[1]), atof(argv[2]),
209                                                  atof(argv[3])))
210 <                                        syntax(fname, lineno, "Zero normal");
210 >                                        syntax("Zero normal");
211                                  break;
212                          case 't':                       /* texture map */
213                                  if (argv[0][2])
# Line 232 | Line 226 | FILE   *fp;
226                          faceno++;
227                          switch (argc-1) {
228                          case 0: case 1: case 2:
229 <                                syntax(fname, lineno, "Too few vertices");
229 >                                syntax("Too few vertices");
230                                  break;
231                          case 3:
232                                  if (!puttri(argv[1], argv[2], argv[3]))
233 <                                        syntax(fname, lineno, "Bad triangle");
233 >                                        syntax("Bad triangle");
234                                  break;
235                          case 4:
236                                  if (!putquad(argv[1], argv[2],
237                                                  argv[3], argv[4]))
238 <                                        syntax(fname, lineno, "Bad quad");
238 >                                        syntax("Bad quad");
239                                  break;
240                          default:
241                                  if (!putface(argc-1, argv+1))
242 <                                        syntax(fname, lineno, "Bad face");
242 >                                        syntax("Bad face");
243                                  break;
244                          }
245                          break;
# Line 260 | Line 254 | FILE   *fp;
254                                  if (!strcmp(argv[1], "off"))
255                                          mapname[0] = '\0';
256                                  else
257 <                                        strcpy(mapname, argv[1]);
257 >                                        sprintf(mapname, "%s.pic", argv[1]);
258                          } else
259                                  goto unknown;
260                          break;
# Line 279 | Line 273 | FILE   *fp;
273                          group[i-1][0] = '\0';
274                          break;
275                  case '#':               /* comment */
276 +                        printargs(argc, argv, stdout);
277                          break;
278                  default:;               /* something we don't deal with */
279                  unknown:
# Line 287 | Line 282 | FILE   *fp;
282                  }
283                  nstats++;
284          }
285 <        printf("\n# Done processing file: %s\n", fname);
285 >        printf("\n# Done processing file: %s\n", inpfile);
286          printf("# %d lines, %d statements, %d unrecognized\n",
287                          lineno, nstats, nunknown);
288   }
# Line 332 | Line 327 | getmtl()                               /* figure material for this face */
327   {
328          register RULEHD *rp = ourmapping;
329  
330 <        if (rp == NULL)                 /* no rule set */
331 <                return(matname);
330 >        if (rp == NULL) {               /* no rule set */
331 >                if (matname[0])
332 >                        return(matname);
333 >                if (group[0][0])
334 >                        return(group[0]);
335 >                return(defmat);
336 >        }
337                                          /* check for match */
338          do {
339                  if (matchrule(rp)) {
# Line 354 | Line 354 | getonm()                               /* invent a good name for object */
354          static char     name[64];
355          register char   *cp1, *cp2;
356          register int    i;
357 <
358 <        if (!group[0][0] || strcmp(objname, DEFOBJ))
359 <                return(objname);        /* good enough for us */
360 <
357 >                                        /* check for preset */
358 >        if (objname[0])
359 >                return(objname);
360 >        if (!group[0][0])
361 >                return(defobj);
362          cp1 = name;                     /* else make name out of groups */
363          for (i = 0; group[i][0]; i++) {
364                  cp2 = group[i];
# Line 381 | Line 382 | register RULEHD        *rp;
382          register int    i;
383  
384          if (rp->qflg & FL(Q_MTL)) {
385 +                if (!matname[0])
386 +                        return(0);
387                  tmpid.number = 0;
388                  tmpid.name = matname;
389                  if (!matchid(&tmpid, &idm(rp)[Q_MTL]))
390                          return(0);
391          }
392          if (rp->qflg & FL(Q_MAP)) {
393 +                if (!mapname[0])
394 +                        return(0);
395                  tmpid.number = 0;
396                  tmpid.name = mapname;
397                  if (!matchid(&tmpid, &idm(rp)[Q_MAP]))
# Line 403 | Line 408 | register RULEHD        *rp;
408                          return(0);
409          }
410          if (rp->qflg & FL(Q_OBJ)) {
411 +                if (!objname[0])
412 +                        return(0);
413                  tmpid.number = 0;
414                  tmpid.name = objname;
415                  if (!matchid(&tmpid, &idm(rp)[Q_OBJ]))
# Line 521 | Line 528 | int    ac;
528   register char   **av;
529   {
530          VNDX    vi;
531 <        char    *mod;
531 >        char    *cp;
532          register int    i;
533  
534          if (nonplanar(ac, av)) {        /* break into quads and triangles */
535                  while (ac > 3) {
536                          if (!putquad(av[0], av[1], av[2], av[3]))
537                                  return(0);
538 <                        ac -= 2;                /* remove two vertices */
539 <                        for (i = 1; i < ac; i++)
540 <                                av[i] = av[i+2];
538 >                        ac -= 2;        /* remove two vertices & rotate */
539 >                        cp = av[0];
540 >                        for (i = 0; i < ac-1; i++)
541 >                                av[i] = av[i+3];
542 >                        av[i] = cp;
543                  }
544                  if (ac == 3 && !puttri(av[0], av[1], av[2]))
545                          return(0);
546                  return(1);
547          }
548 <        if ((mod = getmtl()) == NULL)
548 >        if ((cp = getmtl()) == NULL)
549                  return(-1);
550 <        printf("\n%s polygon %s.%d\n", mod, getonm(), faceno);
550 >        printf("\n%s polygon %s.%d\n", cp, getonm(), faceno);
551          printf("0\n0\n%d\n", 3*ac);
552          for (i = 0; i < ac; i++) {
553                  if (!cvtndx(vi, av[i]))
# Line 564 | Line 573 | char   *v1, *v2, *v3;
573                  return(0);
574                                          /* compute barycentric coordinates */
575          texOK = (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0);
576 + #ifdef TEXMAPS
577          patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0);
578 + #else
579 +        patOK = 0;
580 + #endif
581          if (texOK | patOK)
582                  if (comp_baryc(bvecs, vlist[v1i[0]], vlist[v2i[0]],
583                                  vlist[v3i[0]]) < 0)
# Line 586 | Line 599 | char   *v1, *v2, *v3;
599                                  vnlist[v1i[2]][2], vnlist[v2i[2]][2],
600                                  vnlist[v3i[2]][2]);
601          }
602 + #ifdef TEXMAPS
603                                          /* put out pattern (if any) */
604          if (patOK) {
605                  printf("\n%s colorpict %s\n", mod, PATNAME);
# Line 598 | Line 612 | char   *v1, *v2, *v3;
612                  printf("\t%f %f %f\n", vtlist[v1i[1]][1],
613                                  vtlist[v2i[1]][1], vtlist[v3i[1]][1]);
614          }
615 + #endif
616                                          /* put out triangle */
617          printf("\n%s polygon %s.%d\n", mod, getonm(), faceno);
618          printf("0\n0\n9\n");
# Line 669 | Line 684 | char  *p0, *p1, *p3, *p2;              /* names correspond to bina
684          FVECT  v1, v2, vc1, vc2;
685          int  ok1, ok2;
686  
687 + #ifdef TEXMAPS
688 +        /* also should output texture index coordinates,
689 +         * which will require new .cal file
690 +         */
691 + #endif
692          if ((mod = getmtl()) == NULL)
693                  return(-1);
694          name = getonm();
# Line 769 | Line 789 | register VNDX  p0i, p1i, p2i, p3i;
789          FVECT  v1;
790          register int  i, j;
791  
792 + #ifdef TEXMAPS
793 +        /* also check for texture indices */
794 + #endif
795          if (!(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0))
796                  return(-1);
797                                          /* find dominant axis */
# Line 805 | Line 828 | register VNDX  p0i, p1i, p2i, p3i;
828                                          eqnmat[j][1]*vnlist[p1i[2]][i] +
829                                          eqnmat[j][2]*vnlist[p2i[2]][i] +
830                                          eqnmat[j][3]*vnlist[p3i[2]][i];
831 + #ifdef TEXMAPS
832 +        /* compute result matrix for texture indices */
833 + #endif
834          return(ax);
835  
836   #undef u
# Line 903 | Line 929 | double x, y;
929   }
930  
931  
932 < syntax(fn, ln, er)                      /* report syntax error and exit */
907 < char    *fn;
908 < int     ln;
932 > syntax(er)                      /* report syntax error and exit */
933   char    *er;
934   {
935          fprintf(stderr, "%s: Wavefront syntax error near line %d: %s\n",
936 <                        fn, ln, er);
936 >                        inpfile, lineno, er);
937          exit(1);
938   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines