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.1 by greg, Tue Apr 12 17:12:42 1994 UTC vs.
Revision 2.10 by greg, Wed Jun 15 12:29:55 1994 UTC

# Line 7 | Line 7 | static char SCCSid[] = "$SunId$ LBL";
7   /*
8   * Convert a Wavefront .obj file to Radiance format.
9   *
10 < * Currently, we support only polygonal geometry, and faces
11 < * must be either quads or triangles for smoothing to work.
12 < * Also, texture indices only work for triangles, though
13 < * I'm not sure they work correctly.
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.  (Taken out -- see TEXMAPS defines.)
14   */
15  
16   #include "standard.h"
17  
18 + #include "trans.h"
19 +
20   #include <ctype.h>
21  
20 #define VOIDID          "void"          /* this is defined in object.h */
21
22   #define TCALNAME        "tmesh.cal"     /* triangle interp. file */
23   #define QCALNAME        "surf.cal"      /* quad 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  
29   #define  ABS(x)         ((x)>=0 ? (x) : -(x))
30  
# Line 32 | Line 34 | FVECT  *vlist;                 /* our vertex list */
34   int     nvs;                    /* number of vertices in our list */
35   FVECT   *vnlist;                /* vertex normal list */
36   int     nvns;
37 < FLOAT   (*vtlist)[2];           /* texture vertex list */
37 > FLOAT   (*vtlist)[2];           /* map vertex list */
38   int     nvts;
39  
40 < typedef FLOAT   BARYCCM[3][4];  /* barycentric coordinate system */
40 > typedef struct {
41 >        int     ax;             /* major axis */
42 >        FLOAT   tm[2][3];       /* transformation */
43 > } BARYCCM;
44  
45 < typedef int     VNDX[3];        /* vertex index (point,texture,normal) */
45 > typedef int     VNDX[3];        /* vertex index (point,map,normal) */
46  
47 < #define CHUNKSIZ        128     /* vertex allocation chunk size */
47 > #define CHUNKSIZ        256     /* vertex allocation chunk size */
48  
49   #define MAXARG          64      /* maximum # arguments in a statement */
50  
51 < char    *defmat = VOIDID;       /* default (starting) material name */
52 < char    *defpat = "";           /* default (starting) picture name */
53 < char    *defobj = "F";          /* default (starting) object name */
51 >                                /* qualifiers */
52 > #define Q_MTL           0
53 > #define Q_MAP           1
54 > #define Q_GRP           2
55 > #define Q_OBJ           3
56 > #define Q_FAC           4
57 > #define NQUALS          5
58  
59 < char    picfile[128];           /* current picture file */
59 > char    *qname[NQUALS] = {
60 >        "Material",
61 >        "Map",
62 >        "Group",
63 >        "Object",
64 >        "Face",
65 > };
66 >
67 > QLIST   qlist = {NQUALS, qname};
68 >                                /* valid qualifier ids */
69 > IDLIST  qual[NQUALS];
70 >                                /* mapping rules */
71 > RULEHD  *ourmapping = NULL;
72 >
73 > char    *defmat = DEFMAT;       /* default (starting) material name */
74 > char    *defobj = DEFOBJ;       /* default (starting) object name */
75 >
76 > int     flatten = 0;            /* discard surface normal information */
77 >
78 > char    *getmtl(), *getonm();
79 >
80 > char    mapname[128];           /* current picture file */
81   char    matname[64];            /* current material name */
82 + char    group[16][32];          /* current group names */
83   char    objname[128];           /* current object name */
84 + char    *inpfile;               /* input file name */
85   int     lineno;                 /* current line number */
86 + int     faceno;                 /* current face number */
87  
55 int     nfaces;                 /* total number of faces output */
88  
89 <
58 < main(argc, argv)                /* read in T-mesh files and convert */
89 > main(argc, argv)                /* read in .obj file and convert */
90   int     argc;
91   char    *argv[];
92   {
93 <        FILE    *fp;
93 >        int     donames = 0;
94          int     i;
95  
96          for (i = 1; i < argc && argv[i][0] == '-'; i++)
# Line 67 | Line 98 | char   *argv[];
98                  case 'o':               /* object name */
99                          defobj = argv[++i];
100                          break;
101 <                case 'm':               /* default material */
102 <                        defmat = argv[++i];
101 >                case 'n':               /* just produce name list */
102 >                        donames++;
103                          break;
104 <                case 'p':               /* default picture */
105 <                        defpat = argv[++i];
104 >                case 'm':               /* use custom mapfile */
105 >                        ourmapping = getmapping(argv[++i], &qlist);
106                          break;
107 +                case 'f':               /* flatten surfaces */
108 +                        flatten++;
109 +                        break;
110                  default:
111 <                        fprintf(stderr,
78 <                        "Usage: %s [-o obj][-m mat][-p pic] [file ..]\n",
79 <                                        argv[0]);
80 <                        exit(1);
111 >                        goto userr;
112                  }
113 <        if (i >= argc)
114 <                convert("<stdin>", stdin);
115 <        else
116 <                for ( ; i < argc; i++) {
117 <                        if ((fp = fopen(argv[i], "r")) == NULL) {
118 <                                perror(argv[i]);
119 <                                exit(1);
113 >        if (i > argc | i < argc-1)
114 >                goto userr;
115 >        if (i == argc)
116 >                inpfile = "<stdin>";
117 >        else if (freopen(inpfile=argv[i], "r", stdin) == NULL) {
118 >                fprintf(stderr, "%s: cannot open\n", inpfile);
119 >                exit(1);
120 >        }
121 >        if (donames) {                          /* scan for ids */
122 >                getnames(stdin);
123 >                printf("filename \"%s\"\n", inpfile);
124 >                printf("filetype \"Wavefront\"\n");
125 >                write_quals(&qlist, qual, stdout);
126 >                printf("qualifier %s begin\n", qlist.qual[Q_FAC]);
127 >                printf("[%d:%d]\n", 1, faceno);
128 >                printf("end\n");
129 >        } else {                                /* translate file */
130 >                printf("# ");
131 >                printargs(argc, argv, stdout);
132 >                convert(stdin);
133 >        }
134 >        exit(0);
135 > userr:
136 >        fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n",
137 >                        argv[0]);
138 >        exit(1);
139 > }
140 >
141 >
142 > getnames(fp)                    /* get valid qualifier names */
143 > FILE    *fp;
144 > {
145 >        char    *argv[MAXARG];
146 >        int     argc;
147 >        ID      tmpid;
148 >        register int    i;
149 >
150 >        while (argc = getstmt(argv, fp))
151 >                switch (argv[0][0]) {
152 >                case 'f':                               /* face */
153 >                        if (!argv[0][1])
154 >                                faceno++;
155 >                        break;
156 >                case 'u':
157 >                        if (!strcmp(argv[0], "usemtl")) {       /* material */
158 >                                if (argc < 2)
159 >                                        break;          /* not fatal */
160 >                                tmpid.number = 0;
161 >                                tmpid.name = argv[1];
162 >                                findid(&qual[Q_MTL], &tmpid, 1);
163 >                        } else if (!strcmp(argv[0], "usemap")) {/* map */
164 >                                if (argc < 2 || !strcmp(argv[1], "off"))
165 >                                        break;          /* not fatal */
166 >                                tmpid.number = 0;
167 >                                tmpid.name = argv[1];
168 >                                findid(&qual[Q_MAP], &tmpid, 1);
169                          }
170 <                        convert(argv[i], fp);
171 <                        fclose(fp);
170 >                        break;
171 >                case 'o':               /* object name */
172 >                        if (argv[0][1] || argc < 2)
173 >                                break;
174 >                        tmpid.number = 0;
175 >                        tmpid.name = argv[1];
176 >                        findid(&qual[Q_OBJ], &tmpid, 1);
177 >                        break;
178 >                case 'g':               /* group name(s) */
179 >                        if (argv[0][1])
180 >                                break;
181 >                        tmpid.number = 0;
182 >                        for (i = 1; i < argc; i++) {
183 >                                tmpid.name = argv[i];
184 >                                findid(&qual[Q_GRP], &tmpid, 1);
185 >                        }
186 >                        break;
187                  }
93        exit(0);
188   }
189  
190  
191 < convert(fname, fp)              /* convert a T-mesh */
98 < char    *fname;
191 > convert(fp)                     /* convert a T-mesh */
192   FILE    *fp;
193   {
194          char    *argv[MAXARG];
195          int     argc;
196          int     nstats, nunknown;
197          register int    i;
105                                        /* start fresh */
106        freeverts();
107        strcpy(picfile, defpat);
108        strcpy(matname, defmat);
109        strcpy(objname, defobj);
110        lineno = 0;
111        nstats = nunknown = 0;
198  
199 <        printf("\n# Wavefront file read from: %s\n", fname);
199 >        nstats = nunknown = 0;
200                                          /* scan until EOF */
201          while (argc = getstmt(argv, fp)) {
202                  switch (argv[0][0]) {
# Line 118 | Line 204 | FILE   *fp;
204                          switch (argv[0][1]) {
205                          case '\0':                      /* point */
206                                  if (badarg(argc-1,argv+1,"fff"))
207 <                                        syntax(fname, lineno, "Bad vertex");
207 >                                        syntax("Bad vertex");
208                                  newv(atof(argv[1]), atof(argv[2]),
209                                                  atof(argv[3]));
210                                  break;
# Line 126 | Line 212 | FILE   *fp;
212                                  if (argv[0][2])
213                                          goto unknown;
214                                  if (badarg(argc-1,argv+1,"fff"))
215 <                                        syntax(fname, lineno, "Bad normal");
215 >                                        syntax("Bad normal");
216                                  if (!newvn(atof(argv[1]), atof(argv[2]),
217                                                  atof(argv[3])))
218 <                                        syntax(fname, lineno, "Zero normal");
218 >                                        syntax("Zero normal");
219                                  break;
220 <                        case 't':                       /* texture */
220 >                        case 't':                       /* texture map */
221                                  if (argv[0][2])
222                                          goto unknown;
223                                  if (badarg(argc-1,argv+1,"ff"))
# Line 145 | Line 231 | FILE   *fp;
231                  case 'f':                               /* face */
232                          if (argv[0][1])
233                                  goto unknown;
234 +                        faceno++;
235                          switch (argc-1) {
236                          case 0: case 1: case 2:
237 <                                syntax(fname, lineno, "Too few vertices");
237 >                                syntax("Too few vertices");
238                                  break;
239                          case 3:
240                                  if (!puttri(argv[1], argv[2], argv[3]))
241 <                                        syntax(fname, lineno, "Bad triangle");
241 >                                        syntax("Bad triangle");
242                                  break;
243                          case 4:
244                                  if (!putquad(argv[1], argv[2],
245                                                  argv[3], argv[4]))
246 <                                        syntax(fname, lineno, "Bad quad");
246 >                                        syntax("Bad quad");
247                                  break;
248                          default:
249                                  if (!putface(argc-1, argv+1))
250 <                                        syntax(fname, lineno, "Bad face");
250 >                                        syntax("Bad face");
251                                  break;
252                          }
253                          break;
# Line 173 | Line 260 | FILE   *fp;
260                                  if (argc < 2)
261                                          break;          /* not fatal */
262                                  if (!strcmp(argv[1], "off"))
263 <                                        picfile[0] = '\0';
263 >                                        mapname[0] = '\0';
264                                  else
265 <                                        strcpy(picfile, argv[1]);
265 >                                        sprintf(mapname, "%s.pic", argv[1]);
266                          } else
267                                  goto unknown;
268                          break;
# Line 189 | Line 276 | FILE   *fp;
276                  case 'g':               /* group name(s) */
277                          if (argv[0][1])
278                                  goto unknown;
192                        if (argc < 2)
193                                break;          /* not fatal */
194                        objname[0] = '\0';
279                          for (i = 1; i < argc; i++)
280 <                                if (objname[0])
281 <                                        sprintf(objname+strlen(objname),
198 <                                                ".%s", argv[i]);
199 <                                else
200 <                                        strcpy(objname, argv[i]);
280 >                                strcpy(group[i-1], argv[i]);
281 >                        group[i-1][0] = '\0';
282                          break;
283                  case '#':               /* comment */
284 +                        printargs(argc, argv, stdout);
285                          break;
286                  default:;               /* something we don't deal with */
287                  unknown:
# Line 208 | Line 290 | FILE   *fp;
290                  }
291                  nstats++;
292          }
293 <        printf("# Done processing file: %s\n", fname);
293 >        printf("\n# Done processing file: %s\n", inpfile);
294          printf("# %d lines, %d statements, %d unrecognized\n",
295                          lineno, nstats, nunknown);
296   }
# Line 246 | Line 328 | FILE   *fp;
328  
329          return(i);
330   }
249                
331  
332 +
333 + char *
334 + getmtl()                                /* figure material for this face */
335 + {
336 +        register RULEHD *rp = ourmapping;
337 +
338 +        if (rp == NULL) {               /* no rule set */
339 +                if (matname[0])
340 +                        return(matname);
341 +                if (group[0][0])
342 +                        return(group[0]);
343 +                return(defmat);
344 +        }
345 +                                        /* check for match */
346 +        do {
347 +                if (matchrule(rp)) {
348 +                        if (!strcmp(rp->mnam, VOIDID))
349 +                                return(NULL);   /* match is null */
350 +                        return(rp->mnam);
351 +                }
352 +                rp = rp->next;
353 +        } while (rp != NULL);
354 +                                        /* no match found */
355 +        return(NULL);
356 + }
357 +
358 +
359 + char *
360 + getonm()                                /* invent a good name for object */
361 + {
362 +        static char     name[64];
363 +        register char   *cp1, *cp2;
364 +        register int    i;
365 +                                        /* check for preset */
366 +        if (objname[0])
367 +                return(objname);
368 +        if (!group[0][0])
369 +                return(defobj);
370 +        cp1 = name;                     /* else make name out of groups */
371 +        for (i = 0; group[i][0]; i++) {
372 +                cp2 = group[i];
373 +                if (cp1 > name)
374 +                        *cp1++ = '.';
375 +                while (*cp1 = *cp2++)
376 +                        if (++cp1 >= name+sizeof(name)-2) {
377 +                                *cp1 = '\0';
378 +                                return(name);
379 +                        }
380 +        }
381 +        return(name);
382 + }
383 +
384 +
385 + matchrule(rp)                           /* check for a match on this rule */
386 + register RULEHD *rp;
387 + {
388 +        ID      tmpid;
389 +        int     gotmatch;
390 +        register int    i;
391 +
392 +        if (rp->qflg & FL(Q_MTL)) {
393 +                if (!matname[0])
394 +                        return(0);
395 +                tmpid.number = 0;
396 +                tmpid.name = matname;
397 +                if (!matchid(&tmpid, &idm(rp)[Q_MTL]))
398 +                        return(0);
399 +        }
400 +        if (rp->qflg & FL(Q_MAP)) {
401 +                if (!mapname[0])
402 +                        return(0);
403 +                tmpid.number = 0;
404 +                tmpid.name = mapname;
405 +                if (!matchid(&tmpid, &idm(rp)[Q_MAP]))
406 +                        return(0);
407 +        }
408 +        if (rp->qflg & FL(Q_GRP)) {
409 +                tmpid.number = 0;
410 +                gotmatch = 0;
411 +                for (i = 0; group[i][0]; i++) {
412 +                        tmpid.name = group[i];
413 +                        gotmatch |= matchid(&tmpid, &idm(rp)[Q_GRP]);
414 +                }
415 +                if (!gotmatch)
416 +                        return(0);
417 +        }
418 +        if (rp->qflg & FL(Q_OBJ)) {
419 +                if (!objname[0])
420 +                        return(0);
421 +                tmpid.number = 0;
422 +                tmpid.name = objname;
423 +                if (!matchid(&tmpid, &idm(rp)[Q_OBJ]))
424 +                        return(0);
425 +        }
426 +        if (rp->qflg & FL(Q_FAC)) {
427 +                tmpid.name = NULL;
428 +                tmpid.number = faceno;
429 +                if (!matchid(&tmpid, &idm(rp)[Q_FAC]))
430 +                        return(0);
431 +        }
432 +        return(1);
433 + }
434 +
435 +
436   cvtndx(vi, vs)                          /* convert vertex string to index */
437   register VNDX   vi;
438   register char   *vs;
# Line 258 | Line 443 | register char  *vs;
443                  if (vi[0]-- > nvs)
444                          return(0);
445          } else if (vi[0] < 0) {
446 <                vi[0] = nvs + vi[0];
446 >                vi[0] += nvs;
447                  if (vi[0] < 0)
448                          return(0);
449          } else
450                  return(0);
451 <                                        /* get texture */
451 >                                        /* get map */
452          while (*vs)
453                  if (*vs++ == '/')
454                          break;
# Line 272 | Line 457 | register char  *vs;
457                  if (vi[1]-- > nvts)
458                          return(0);
459          } else if (vi[1] < 0) {
460 <                vi[1] = nvts + vi[1];
460 >                vi[1] += nvts;
461                  if (vi[1] < 0)
462                          return(0);
463          } else
# Line 286 | Line 471 | register char  *vs;
471                  if (vi[2]-- > nvns)
472                          return(0);
473          } else if (vi[2] < 0) {
474 <                vi[2] = nvns + vi[2];
474 >                vi[2] += nvns;
475                  if (vi[2] < 0)
476                          return(0);
477          } else
# Line 295 | Line 480 | register char  *vs;
480   }
481  
482  
483 < putface(ac, av)                         /* put out an N-sided polygon */
483 > nonplanar(ac, av)                       /* are vertices non-planar? */
484   register int    ac;
485   register char   **av;
486   {
487          VNDX    vi;
488 +        FLOAT   *p0, *p1;
489 +        FVECT   v1, v2, nsum, newn;
490 +        double  d;
491 +        register int    i;
492  
493 <        printf("\n%s polygon %s.%d\n", matname, objname, ++nfaces);
493 >        if (!cvtndx(vi, av[0]))
494 >                return(0);
495 >        if (vi[2] >= 0)
496 >                return(1);              /* has interpolated normals */
497 >        if (ac < 4)
498 >                return(0);              /* it's a triangle! */
499 >                                        /* set up */
500 >        p0 = vlist[vi[0]];
501 >        if (!cvtndx(vi, av[1]))
502 >                return(0);              /* error gets caught later */
503 >        nsum[0] = nsum[1] = nsum[2] = 0.;
504 >        p1 = vlist[vi[0]];
505 >        fvsum(v2, p1, p0, -1.0);
506 >        for (i = 2; i < ac; i++) {
507 >                VCOPY(v1, v2);
508 >                if (!cvtndx(vi, av[i]))
509 >                        return(0);
510 >                p1 = vlist[vi[0]];
511 >                fvsum(v2, p1, p0, -1.0);
512 >                fcross(newn, v1, v2);
513 >                if (normalize(newn) == 0.0) {
514 >                        if (i < 3)
515 >                                return(1);      /* can't deal with this */
516 >                        fvsum(nsum, nsum, nsum, 1./(i-2));
517 >                        continue;
518 >                }
519 >                d = fdot(newn,nsum);
520 >                if (d >= 0) {
521 >                        if (d < (1.0-FTINY)*(i-2))
522 >                                return(1);
523 >                        fvsum(nsum, nsum, newn, 1.0);
524 >                } else {
525 >                        if (d > -(1.0-FTINY)*(i-2))
526 >                                return(1);
527 >                        fvsum(nsum, nsum, newn, -1.0);
528 >                }
529 >        }
530 >        return(0);
531 > }
532 >
533 >
534 > putface(ac, av)                         /* put out an N-sided polygon */
535 > int     ac;
536 > register char   **av;
537 > {
538 >        VNDX    vi;
539 >        char    *cp;
540 >        register int    i;
541 >
542 >        if (nonplanar(ac, av)) {        /* break into quads and triangles */
543 >                while (ac > 3) {
544 >                        if (!putquad(av[0], av[1], av[2], av[3]))
545 >                                return(0);
546 >                        ac -= 2;        /* remove two vertices & rotate */
547 >                        cp = av[0];
548 >                        for (i = 0; i < ac-1; i++)
549 >                                av[i] = av[i+3];
550 >                        av[i] = cp;
551 >                }
552 >                if (ac == 3 && !puttri(av[0], av[1], av[2]))
553 >                        return(0);
554 >                return(1);
555 >        }
556 >        if ((cp = getmtl()) == NULL)
557 >                return(-1);
558 >        printf("\n%s polygon %s.%d\n", cp, getonm(), faceno);
559          printf("0\n0\n%d\n", 3*ac);
560 <        while (ac--) {
561 <                if (!cvtndx(vi, *av++))
560 >        for (i = 0; i < ac; i++) {
561 >                if (!cvtndx(vi, av[i]))
562                          return(0);
563                  pvect(vlist[vi[0]]);
564          }
# Line 315 | Line 569 | register char  **av;
569   puttri(v1, v2, v3)                      /* put out a triangle */
570   char    *v1, *v2, *v3;
571   {
572 <        char    *mod = matname;
572 >        char    *mod;
573          VNDX    v1i, v2i, v3i;
574          BARYCCM bvecs;
575 +        int     texOK, patOK;
576  
577 +        if ((mod = getmtl()) == NULL)
578 +                return(-1);
579 +
580          if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3))
581                  return(0);
582                                          /* compute barycentric coordinates */
583 <        if ((v1i[2]>=0&&v2i[2]>=0&&v3i[2]>=0) ||
584 <                        (v1i[1]>=0&&v2i[1]>=0&&v3i[1]>=0))
585 <                if (comp_baryc(bvecs, vlist[v1i[0]], vlist[v2i[0]],
583 >        texOK = !flatten && (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0);
584 > #ifdef TEXMAPS
585 >        patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0);
586 > #else
587 >        patOK = 0;
588 > #endif
589 >        if (texOK | patOK)
590 >                if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]],
591                                  vlist[v3i[0]]) < 0)
592 <                        return(0);
592 >                        return(-1);
593                                          /* put out texture (if any) */
594 <        if (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0) {
594 >        if (texOK) {
595                  printf("\n%s texfunc %s\n", mod, TEXNAME);
596                  mod = TEXNAME;
597                  printf("4 dx dy dz %s\n", TCALNAME);
598 <                printf("0\n21\n");
599 <                put_baryc(bvecs);
598 >                printf("0\n16 ");
599 >                put_baryc(&bvecs);
600                  printf("\t%14.12g %14.12g %14.12g\n",
601                                  vnlist[v1i[2]][0], vnlist[v2i[2]][0],
602                                  vnlist[v3i[2]][0]);
# Line 344 | Line 607 | char   *v1, *v2, *v3;
607                                  vnlist[v1i[2]][2], vnlist[v2i[2]][2],
608                                  vnlist[v3i[2]][2]);
609          }
610 + #ifdef TEXMAPS
611                                          /* put out pattern (if any) */
612 <        if (picfile[0] && v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0) {
612 >        if (patOK) {
613                  printf("\n%s colorpict %s\n", mod, PATNAME);
614                  mod = PATNAME;
615 <                printf("7 noneg noneg noneg %s %s u v\n", picfile, TCALNAME);
616 <                printf("0\n18\n");
617 <                put_baryc(bvecs);
615 >                printf("7 noneg noneg noneg %s %s u v\n", mapname, TCALNAME);
616 >                printf("0\n13 ");
617 >                put_baryc(&bvecs);
618                  printf("\t%f %f %f\n", vtlist[v1i[1]][0],
619                                  vtlist[v2i[1]][0], vtlist[v3i[1]][0]);
620                  printf("\t%f %f %f\n", vtlist[v1i[1]][1],
621                                  vtlist[v2i[1]][1], vtlist[v3i[1]][1]);
622          }
623 + #endif
624                                          /* put out triangle */
625 <        printf("\n%s polygon %s.%d\n", matname, objname, ++nfaces);
625 >        printf("\n%s polygon %s.%d\n", mod, getonm(), faceno);
626          printf("0\n0\n9\n");
627          pvect(vlist[v1i[0]]);
628          pvect(vlist[v2i[0]]);
# Line 369 | Line 634 | char   *v1, *v2, *v3;
634  
635   int
636   comp_baryc(bcm, v1, v2, v3)             /* compute barycentric vectors */
637 < register BARYCCM        bcm;
637 > register BARYCCM        *bcm;
638   FLOAT   *v1, *v2, *v3;
639   {
640          FLOAT   *vt;
641          FVECT   va, vab, vcb;
642          double  d;
643 +        int     ax0, ax1;
644          register int    i, j;
645 <
646 <        for (j = 0; j < 3; j++) {
647 <                for (i = 0; i < 3; i++) {
648 <                        vab[i] = v1[i] - v2[i];
649 <                        vcb[i] = v3[i] - v2[i];
650 <                }
651 <                d = DOT(vcb,vcb);
645 >                                        /* compute major axis */
646 >        for (i = 0; i < 3; i++) {
647 >                vab[i] = v1[i] - v2[i];
648 >                vcb[i] = v3[i] - v2[i];
649 >        }
650 >        fcross(va, vab, vcb);
651 >        bcm->ax = ABS(va[0]) > ABS(va[1]) ? 0 : 1;
652 >        bcm->ax = ABS(va[bcm->ax]) > ABS(va[2]) ? bcm->ax : 2;
653 >        ax0 = (bcm->ax + 1) % 3;
654 >        ax1 = (bcm->ax + 2) % 3;
655 >        for (j = 0; j < 2; j++) {
656 >                vab[0] = v1[ax0] - v2[ax0];
657 >                vcb[0] = v3[ax0] - v2[ax0];
658 >                vab[1] = v1[ax1] - v2[ax1];
659 >                vcb[1] = v3[ax1] - v2[ax1];
660 >                d = vcb[0]*vcb[0] + vcb[1]*vcb[1];
661                  if (d <= FTINY)
662                          return(-1);
663 <                d = DOT(vcb,vab)/d;
664 <                for (i = 0; i < 3; i++)
665 <                        va[i] = vab[i] - vcb[i]*d;
666 <                d = DOT(va,va);
663 >                d = (vcb[0]*vab[0]+vcb[1]*vab[1])/d;
664 >                va[0] = vab[0] - vcb[0]*d;
665 >                va[1] = vab[1] - vcb[1]*d;
666 >                d = va[0]*va[0] + va[1]*va[1];
667                  if (d <= FTINY)
668                          return(-1);
669 <                for (i = 0; i < 3; i++) {
670 <                        va[i] /= d;
671 <                        bcm[j][i] = va[i];
397 <                }
398 <                bcm[j][3] = -DOT(v2,va);
669 >                bcm->tm[j][0] = va[0] /= d;
670 >                bcm->tm[j][1] = va[1] /= d;
671 >                bcm->tm[j][2] = -(v2[ax0]*va[0]+v2[ax1]*va[1]);
672                                          /* rotate vertices */
673                  vt = v1;
674                  v1 = v2;
# Line 407 | Line 680 | FLOAT  *v1, *v2, *v3;
680  
681  
682   put_baryc(bcm)                          /* put barycentric coord. vectors */
683 < register BARYCCM        bcm;
683 > register BARYCCM        *bcm;
684   {
685 <        register int    i;
686 <
687 <        for (i = 0; i < 3; i++)
688 <                printf("%14.8f %14.8f %14.8f %14.8f\n",
689 <                                bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]);
685 >        printf("\t%d\n", bcm->ax);
686 >        printf("%14.8f %14.8f %14.8f\n",
687 >                                bcm->tm[0][0], bcm->tm[0][1], bcm->tm[0][2]);
688 >        printf("%14.8f %14.8f %14.8f\n",
689 >                                bcm->tm[1][0], bcm->tm[1][1], bcm->tm[1][2]);
690   }
691  
692  
693 < putquad(p0, p1, p2, p3)                 /* put out a quadrilateral */
694 < char  *p0, *p1, *p2, *p3;
693 > putquad(p0, p1, p3, p2)                 /* put out a quadrilateral */
694 > char  *p0, *p1, *p3, *p2;               /* names correspond to binary pos. */
695   {
696          VNDX  p0i, p1i, p2i, p3i;
697          FVECT  norm[4];
698 +        char  *mod, *name;
699          int  axis;
700          FVECT  v1, v2, vc1, vc2;
701          int  ok1, ok2;
702 +
703 + #ifdef TEXMAPS
704 +        /* also should output texture index coordinates,
705 +         * which will require new .cal file
706 +         */
707 + #endif
708 +        if ((mod = getmtl()) == NULL)
709 +                return(-1);
710 +        name = getonm();
711                                          /* get actual indices */
712          if (!cvtndx(p0i,p0) || !cvtndx(p1i,p1) ||
713                          !cvtndx(p2i,p2) || !cvtndx(p3i,p3))
# Line 439 | Line 722 | char  *p0, *p1, *p2, *p3;
722          fcross(vc2, v1, v2);
723          ok2 = normalize(vc2) != 0.0;
724          if (!(ok1 | ok2))
725 <                return(0);
725 >                return(-1);
726                                          /* compute normal interpolation */
727          axis = norminterp(norm, p0i, p1i, p2i, p3i);
728  
729                                          /* put out quadrilateral? */
730 <        if (ok1 & ok2 && fdot(vc1,vc2) >= 1.0-FTINY*FTINY) {
731 <                printf("\n%s ", matname);
730 >        if (ok1 & ok2 && fabs(fdot(vc1,vc2)) >= 1.0-FTINY) {
731 >                printf("\n%s ", mod);
732                  if (axis != -1) {
733                          printf("texfunc %s\n", TEXNAME);
734                          printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
# Line 458 | Line 741 | char  *p0, *p1, *p2, *p3;
741                          pvect(v1);
742                          printf("\n%s ", TEXNAME);
743                  }
744 <                printf("polygon %s.%d\n", objname, ++nfaces);
744 >                printf("polygon %s.%d\n", name, faceno);
745                  printf("0\n0\n12\n");
746                  pvect(vlist[p0i[0]]);
747                  pvect(vlist[p1i[0]]);
# Line 468 | Line 751 | char  *p0, *p1, *p2, *p3;
751          }
752                                          /* put out triangles? */
753          if (ok1) {
754 <                printf("\n%s ", matname);
754 >                printf("\n%s ", mod);
755                  if (axis != -1) {
756                          printf("texfunc %s\n", TEXNAME);
757                          printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
# Line 480 | Line 763 | char  *p0, *p1, *p2, *p3;
763                          pvect(v1);
764                          printf("\n%s ", TEXNAME);
765                  }
766 <                printf("polygon %s.%d\n", objname, ++nfaces);
766 >                printf("polygon %s.%da\n", name, faceno);
767                  printf("0\n0\n9\n");
768                  pvect(vlist[p0i[0]]);
769                  pvect(vlist[p1i[0]]);
770                  pvect(vlist[p2i[0]]);
771          }
772          if (ok2) {
773 <                printf("\n%s ", matname);
773 >                printf("\n%s ", mod);
774                  if (axis != -1) {
775                          printf("texfunc %s\n", TEXNAME);
776                          printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
# Line 499 | Line 782 | char  *p0, *p1, *p2, *p3;
782                          pvect(v2);
783                          printf("\n%s ", TEXNAME);
784                  }
785 <                printf("polygon %s.%d\n", objname, ++nfaces);
785 >                printf("polygon %s.%db\n", name, faceno);
786                  printf("0\n0\n9\n");
787                  pvect(vlist[p2i[0]]);
788                  pvect(vlist[p1i[0]]);
# Line 522 | Line 805 | register VNDX  p0i, p1i, p2i, p3i;
805          FVECT  v1;
806          register int  i, j;
807  
808 <        if (!(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0))
808 > #ifdef TEXMAPS
809 >        /* also check for texture indices */
810 > #endif
811 >        if (flatten || !(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0))
812                  return(-1);
813                                          /* find dominant axis */
814          VCOPY(v1, vnlist[p0i[2]]);
# Line 558 | Line 844 | register VNDX  p0i, p1i, p2i, p3i;
844                                          eqnmat[j][1]*vnlist[p1i[2]][i] +
845                                          eqnmat[j][2]*vnlist[p2i[2]][i] +
846                                          eqnmat[j][3]*vnlist[p3i[2]][i];
847 + #ifdef TEXMAPS
848 +        /* compute result matrix for texture indices */
849 + #endif
850          return(ax);
851  
852   #undef u
# Line 633 | Line 922 | double x, y, z;
922  
923  
924   int
925 < newvt(x, y)                     /* create a new texture vertex */
925 > newvt(x, y)                     /* create a new texture map vertex */
926   double  x, y;
927   {
928          if (!(nvts%CHUNKSIZ)) {         /* allocate next block */
# Line 649 | Line 938 | double x, y;
938                          exit(1);
939                  }
940          }
941 <                                        /* assign new texture vertex */
941 >                                        /* assign new vertex */
942          vtlist[nvts][0] = x;
943          vtlist[nvts][1] = y;
944          return(++nvts);
945   }
946  
947  
948 < syntax(fn, ln, er)                      /* report syntax error and exit */
660 < char    *fn;
661 < int     ln;
948 > syntax(er)                      /* report syntax error and exit */
949   char    *er;
950   {
951          fprintf(stderr, "%s: Wavefront syntax error near line %d: %s\n",
952 <                        fn, ln, er);
952 >                        inpfile, lineno, er);
953          exit(1);
954   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines