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.13 by greg, Wed Jun 15 15:07:08 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 */
23   #define PATNAME         "M-pat"         /* mesh pattern name (reused) */
24   #define TEXNAME         "M-nor"         /* mesh texture name (reused) */
25 + #define DEFOBJ          "unnamed"       /* default object name */
26 + #define DEFMAT          "white"         /* default material name */
27  
28   #define  ABS(x)         ((x)>=0 ? (x) : -(x))
29  
# Line 32 | Line 33 | FVECT  *vlist;                 /* our vertex list */
33   int     nvs;                    /* number of vertices in our list */
34   FVECT   *vnlist;                /* vertex normal list */
35   int     nvns;
36 < FLOAT   (*vtlist)[2];           /* texture vertex list */
36 > FLOAT   (*vtlist)[2];           /* map vertex list */
37   int     nvts;
38  
39 < typedef FLOAT   BARYCCM[3][4];  /* barycentric coordinate system */
39 > typedef struct {
40 >        int     ax;             /* major axis */
41 >        FLOAT   tm[2][3];       /* transformation */
42 > } BARYCCM;
43  
44 < typedef int     VNDX[3];        /* vertex index (point,texture,normal) */
44 > typedef int     VNDX[3];        /* vertex index (point,map,normal) */
45  
46 < #define CHUNKSIZ        128     /* vertex allocation chunk size */
46 > #define CHUNKSIZ        256     /* vertex allocation chunk size */
47  
48   #define MAXARG          64      /* maximum # arguments in a statement */
49  
50 < char    *defmat = VOIDID;       /* default (starting) material name */
51 < char    *defpat = "";           /* default (starting) picture name */
52 < char    *defobj = "F";          /* default (starting) object name */
50 >                                /* qualifiers */
51 > #define Q_MTL           0
52 > #define Q_MAP           1
53 > #define Q_GRP           2
54 > #define Q_OBJ           3
55 > #define Q_FAC           4
56 > #define NQUALS          5
57  
58 < char    picfile[128];           /* current picture file */
58 > char    *qname[NQUALS] = {
59 >        "Material",
60 >        "Map",
61 >        "Group",
62 >        "Object",
63 >        "Face",
64 > };
65 >
66 > QLIST   qlist = {NQUALS, qname};
67 >                                /* valid qualifier ids */
68 > IDLIST  qual[NQUALS];
69 >                                /* mapping rules */
70 > RULEHD  *ourmapping = NULL;
71 >
72 > char    *defmat = DEFMAT;       /* default (starting) material name */
73 > char    *defobj = DEFOBJ;       /* default (starting) object name */
74 >
75 > int     flatten = 0;            /* discard surface normal information */
76 >
77 > char    *getmtl(), *getonm();
78 >
79 > char    mapname[128];           /* current picture file */
80   char    matname[64];            /* current material name */
81 + char    group[16][32];          /* current group names */
82   char    objname[128];           /* current object name */
83 + char    *inpfile;               /* input file name */
84   int     lineno;                 /* current line number */
85 + int     faceno;                 /* current face number */
86  
55 int     nfaces;                 /* total number of faces output */
87  
88 <
58 < main(argc, argv)                /* read in T-mesh files and convert */
88 > main(argc, argv)                /* read in .obj file and convert */
89   int     argc;
90   char    *argv[];
91   {
92 <        FILE    *fp;
92 >        int     donames = 0;
93          int     i;
94  
95          for (i = 1; i < argc && argv[i][0] == '-'; i++)
# Line 67 | Line 97 | char   *argv[];
97                  case 'o':               /* object name */
98                          defobj = argv[++i];
99                          break;
100 <                case 'm':               /* default material */
101 <                        defmat = argv[++i];
100 >                case 'n':               /* just produce name list */
101 >                        donames++;
102                          break;
103 <                case 'p':               /* default picture */
104 <                        defpat = argv[++i];
103 >                case 'm':               /* use custom mapfile */
104 >                        ourmapping = getmapping(argv[++i], &qlist);
105                          break;
106 +                case 'f':               /* flatten surfaces */
107 +                        flatten++;
108 +                        break;
109                  default:
110 <                        fprintf(stderr,
78 <                        "Usage: %s [-o obj][-m mat][-p pic] [file ..]\n",
79 <                                        argv[0]);
80 <                        exit(1);
110 >                        goto userr;
111                  }
112 <        if (i >= argc)
113 <                convert("<stdin>", stdin);
114 <        else
115 <                for ( ; i < argc; i++) {
116 <                        if ((fp = fopen(argv[i], "r")) == NULL) {
117 <                                perror(argv[i]);
118 <                                exit(1);
112 >        if (i > argc | i < argc-1)
113 >                goto userr;
114 >        if (i == argc)
115 >                inpfile = "<stdin>";
116 >        else if (freopen(inpfile=argv[i], "r", stdin) == NULL) {
117 >                fprintf(stderr, "%s: cannot open\n", inpfile);
118 >                exit(1);
119 >        }
120 >        if (donames) {                          /* scan for ids */
121 >                getnames(stdin);
122 >                printf("filename \"%s\"\n", inpfile);
123 >                printf("filetype \"Wavefront\"\n");
124 >                write_quals(&qlist, qual, stdout);
125 >                printf("qualifier %s begin\n", qlist.qual[Q_FAC]);
126 >                printf("[%d:%d]\n", 1, faceno);
127 >                printf("end\n");
128 >        } else {                                /* translate file */
129 >                printf("# ");
130 >                printargs(argc, argv, stdout);
131 >                convert(stdin);
132 >        }
133 >        exit(0);
134 > userr:
135 >        fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n",
136 >                        argv[0]);
137 >        exit(1);
138 > }
139 >
140 >
141 > getnames(fp)                    /* get valid qualifier names */
142 > FILE    *fp;
143 > {
144 >        char    *argv[MAXARG];
145 >        int     argc;
146 >        ID      tmpid;
147 >        register int    i;
148 >
149 >        while (argc = getstmt(argv, fp))
150 >                switch (argv[0][0]) {
151 >                case 'f':                               /* face */
152 >                        if (!argv[0][1])
153 >                                faceno++;
154 >                        break;
155 >                case 'u':
156 >                        if (!strcmp(argv[0], "usemtl")) {       /* material */
157 >                                if (argc < 2)
158 >                                        break;          /* not fatal */
159 >                                tmpid.number = 0;
160 >                                tmpid.name = argv[1];
161 >                                findid(&qual[Q_MTL], &tmpid, 1);
162 >                        } else if (!strcmp(argv[0], "usemap")) {/* map */
163 >                                if (argc < 2 || !strcmp(argv[1], "off"))
164 >                                        break;          /* not fatal */
165 >                                tmpid.number = 0;
166 >                                tmpid.name = argv[1];
167 >                                findid(&qual[Q_MAP], &tmpid, 1);
168                          }
169 <                        convert(argv[i], fp);
170 <                        fclose(fp);
169 >                        break;
170 >                case 'o':               /* object name */
171 >                        if (argv[0][1] || argc < 2)
172 >                                break;
173 >                        tmpid.number = 0;
174 >                        tmpid.name = argv[1];
175 >                        findid(&qual[Q_OBJ], &tmpid, 1);
176 >                        break;
177 >                case 'g':               /* group name(s) */
178 >                        if (argv[0][1])
179 >                                break;
180 >                        tmpid.number = 0;
181 >                        for (i = 1; i < argc; i++) {
182 >                                tmpid.name = argv[i];
183 >                                findid(&qual[Q_GRP], &tmpid, 1);
184 >                        }
185 >                        break;
186                  }
93        exit(0);
187   }
188  
189  
190 < convert(fname, fp)              /* convert a T-mesh */
98 < char    *fname;
190 > convert(fp)                     /* convert a T-mesh */
191   FILE    *fp;
192   {
193          char    *argv[MAXARG];
194          int     argc;
195          int     nstats, nunknown;
196          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;
197  
198 <        printf("\n# Wavefront file read from: %s\n", fname);
198 >        nstats = nunknown = 0;
199                                          /* scan until EOF */
200          while (argc = getstmt(argv, fp)) {
201                  switch (argv[0][0]) {
# Line 118 | Line 203 | FILE   *fp;
203                          switch (argv[0][1]) {
204                          case '\0':                      /* point */
205                                  if (badarg(argc-1,argv+1,"fff"))
206 <                                        syntax(fname, lineno, "Bad vertex");
206 >                                        syntax("Bad vertex");
207                                  newv(atof(argv[1]), atof(argv[2]),
208                                                  atof(argv[3]));
209                                  break;
# Line 126 | Line 211 | FILE   *fp;
211                                  if (argv[0][2])
212                                          goto unknown;
213                                  if (badarg(argc-1,argv+1,"fff"))
214 <                                        syntax(fname, lineno, "Bad normal");
214 >                                        syntax("Bad normal");
215                                  if (!newvn(atof(argv[1]), atof(argv[2]),
216                                                  atof(argv[3])))
217 <                                        syntax(fname, lineno, "Zero normal");
217 >                                        syntax("Zero normal");
218                                  break;
219 <                        case 't':                       /* texture */
219 >                        case 't':                       /* texture map */
220                                  if (argv[0][2])
221                                          goto unknown;
222                                  if (badarg(argc-1,argv+1,"ff"))
# Line 145 | Line 230 | FILE   *fp;
230                  case 'f':                               /* face */
231                          if (argv[0][1])
232                                  goto unknown;
233 +                        faceno++;
234                          switch (argc-1) {
235                          case 0: case 1: case 2:
236 <                                syntax(fname, lineno, "Too few vertices");
236 >                                syntax("Too few vertices");
237                                  break;
238                          case 3:
239                                  if (!puttri(argv[1], argv[2], argv[3]))
240 <                                        syntax(fname, lineno, "Bad triangle");
240 >                                        syntax("Bad triangle");
241                                  break;
156                        case 4:
157                                if (!putquad(argv[1], argv[2],
158                                                argv[3], argv[4]))
159                                        syntax(fname, lineno, "Bad quad");
160                                break;
242                          default:
243                                  if (!putface(argc-1, argv+1))
244 <                                        syntax(fname, lineno, "Bad face");
244 >                                        syntax("Bad face");
245                                  break;
246                          }
247                          break;
# Line 173 | Line 254 | FILE   *fp;
254                                  if (argc < 2)
255                                          break;          /* not fatal */
256                                  if (!strcmp(argv[1], "off"))
257 <                                        picfile[0] = '\0';
257 >                                        mapname[0] = '\0';
258                                  else
259 <                                        strcpy(picfile, argv[1]);
259 >                                        sprintf(mapname, "%s.pic", argv[1]);
260                          } else
261                                  goto unknown;
262                          break;
# Line 189 | Line 270 | FILE   *fp;
270                  case 'g':               /* group name(s) */
271                          if (argv[0][1])
272                                  goto unknown;
192                        if (argc < 2)
193                                break;          /* not fatal */
194                        objname[0] = '\0';
273                          for (i = 1; i < argc; i++)
274 <                                if (objname[0])
275 <                                        sprintf(objname+strlen(objname),
198 <                                                ".%s", argv[i]);
199 <                                else
200 <                                        strcpy(objname, argv[i]);
274 >                                strcpy(group[i-1], argv[i]);
275 >                        group[i-1][0] = '\0';
276                          break;
277                  case '#':               /* comment */
278 +                        printargs(argc, argv, stdout);
279                          break;
280                  default:;               /* something we don't deal with */
281                  unknown:
# Line 208 | Line 284 | FILE   *fp;
284                  }
285                  nstats++;
286          }
287 <        printf("# Done processing file: %s\n", fname);
287 >        printf("\n# Done processing file: %s\n", inpfile);
288          printf("# %d lines, %d statements, %d unrecognized\n",
289                          lineno, nstats, nunknown);
290   }
# Line 246 | Line 322 | FILE   *fp;
322  
323          return(i);
324   }
249                
325  
326 +
327 + char *
328 + getmtl()                                /* figure material for this face */
329 + {
330 +        register RULEHD *rp = ourmapping;
331 +
332 +        if (rp == NULL) {               /* no rule set */
333 +                if (matname[0])
334 +                        return(matname);
335 +                if (group[0][0])
336 +                        return(group[0]);
337 +                return(defmat);
338 +        }
339 +                                        /* check for match */
340 +        do {
341 +                if (matchrule(rp)) {
342 +                        if (!strcmp(rp->mnam, VOIDID))
343 +                                return(NULL);   /* match is null */
344 +                        return(rp->mnam);
345 +                }
346 +                rp = rp->next;
347 +        } while (rp != NULL);
348 +                                        /* no match found */
349 +        return(NULL);
350 + }
351 +
352 +
353 + char *
354 + getonm()                                /* invent a good name for object */
355 + {
356 +        static char     name[64];
357 +        register char   *cp1, *cp2;
358 +        register int    i;
359 +                                        /* check for preset */
360 +        if (objname[0])
361 +                return(objname);
362 +        if (!group[0][0])
363 +                return(defobj);
364 +        cp1 = name;                     /* else make name out of groups */
365 +        for (i = 0; group[i][0]; i++) {
366 +                cp2 = group[i];
367 +                if (cp1 > name)
368 +                        *cp1++ = '.';
369 +                while (*cp1 = *cp2++)
370 +                        if (++cp1 >= name+sizeof(name)-2) {
371 +                                *cp1 = '\0';
372 +                                return(name);
373 +                        }
374 +        }
375 +        return(name);
376 + }
377 +
378 +
379 + matchrule(rp)                           /* check for a match on this rule */
380 + register RULEHD *rp;
381 + {
382 +        ID      tmpid;
383 +        int     gotmatch;
384 +        register int    i;
385 +
386 +        if (rp->qflg & FL(Q_MTL)) {
387 +                if (!matname[0])
388 +                        return(0);
389 +                tmpid.number = 0;
390 +                tmpid.name = matname;
391 +                if (!matchid(&tmpid, &idm(rp)[Q_MTL]))
392 +                        return(0);
393 +        }
394 +        if (rp->qflg & FL(Q_MAP)) {
395 +                if (!mapname[0])
396 +                        return(0);
397 +                tmpid.number = 0;
398 +                tmpid.name = mapname;
399 +                if (!matchid(&tmpid, &idm(rp)[Q_MAP]))
400 +                        return(0);
401 +        }
402 +        if (rp->qflg & FL(Q_GRP)) {
403 +                tmpid.number = 0;
404 +                gotmatch = 0;
405 +                for (i = 0; group[i][0]; i++) {
406 +                        tmpid.name = group[i];
407 +                        gotmatch |= matchid(&tmpid, &idm(rp)[Q_GRP]);
408 +                }
409 +                if (!gotmatch)
410 +                        return(0);
411 +        }
412 +        if (rp->qflg & FL(Q_OBJ)) {
413 +                if (!objname[0])
414 +                        return(0);
415 +                tmpid.number = 0;
416 +                tmpid.name = objname;
417 +                if (!matchid(&tmpid, &idm(rp)[Q_OBJ]))
418 +                        return(0);
419 +        }
420 +        if (rp->qflg & FL(Q_FAC)) {
421 +                tmpid.name = NULL;
422 +                tmpid.number = faceno;
423 +                if (!matchid(&tmpid, &idm(rp)[Q_FAC]))
424 +                        return(0);
425 +        }
426 +        return(1);
427 + }
428 +
429 +
430   cvtndx(vi, vs)                          /* convert vertex string to index */
431   register VNDX   vi;
432   register char   *vs;
# Line 258 | Line 437 | register char  *vs;
437                  if (vi[0]-- > nvs)
438                          return(0);
439          } else if (vi[0] < 0) {
440 <                vi[0] = nvs + vi[0];
440 >                vi[0] += nvs;
441                  if (vi[0] < 0)
442                          return(0);
443          } else
444                  return(0);
445 <                                        /* get texture */
445 >                                        /* get map */
446          while (*vs)
447                  if (*vs++ == '/')
448                          break;
# Line 272 | Line 451 | register char  *vs;
451                  if (vi[1]-- > nvts)
452                          return(0);
453          } else if (vi[1] < 0) {
454 <                vi[1] = nvts + vi[1];
454 >                vi[1] += nvts;
455                  if (vi[1] < 0)
456                          return(0);
457          } else
# Line 286 | Line 465 | register char  *vs;
465                  if (vi[2]-- > nvns)
466                          return(0);
467          } else if (vi[2] < 0) {
468 <                vi[2] = nvns + vi[2];
468 >                vi[2] += nvns;
469                  if (vi[2] < 0)
470                          return(0);
471          } else
# Line 295 | Line 474 | register char  *vs;
474   }
475  
476  
477 < putface(ac, av)                         /* put out an N-sided polygon */
477 > nonplanar(ac, av)                       /* are vertices non-planar? */
478   register int    ac;
479   register char   **av;
480   {
481          VNDX    vi;
482 +        FLOAT   *p0, *p1;
483 +        FVECT   v1, v2, nsum, newn;
484 +        double  d;
485 +        register int    i;
486  
487 <        printf("\n%s polygon %s.%d\n", matname, objname, ++nfaces);
487 >        if (!cvtndx(vi, av[0]))
488 >                return(0);
489 >        if (!flatten && vi[2] >= 0)
490 >                return(1);              /* has interpolated normals */
491 >        if (ac < 4)
492 >                return(0);              /* it's a triangle! */
493 >                                        /* set up */
494 >        p0 = vlist[vi[0]];
495 >        if (!cvtndx(vi, av[1]))
496 >                return(0);              /* error gets caught later */
497 >        nsum[0] = nsum[1] = nsum[2] = 0.;
498 >        p1 = vlist[vi[0]];
499 >        fvsum(v2, p1, p0, -1.0);
500 >        for (i = 2; i < ac; i++) {
501 >                VCOPY(v1, v2);
502 >                if (!cvtndx(vi, av[i]))
503 >                        return(0);
504 >                p1 = vlist[vi[0]];
505 >                fvsum(v2, p1, p0, -1.0);
506 >                fcross(newn, v1, v2);
507 >                if (normalize(newn) == 0.0) {
508 >                        if (i < 3)
509 >                                return(1);      /* can't deal with this */
510 >                        fvsum(nsum, nsum, nsum, 1./(i-2));
511 >                        continue;
512 >                }
513 >                d = fdot(newn,nsum);
514 >                if (d >= 0) {
515 >                        if (d < (1.0-FTINY)*(i-2))
516 >                                return(1);
517 >                        fvsum(nsum, nsum, newn, 1.0);
518 >                } else {
519 >                        if (d > -(1.0-FTINY)*(i-2))
520 >                                return(1);
521 >                        fvsum(nsum, nsum, newn, -1.0);
522 >                }
523 >        }
524 >        return(0);
525 > }
526 >
527 >
528 > putface(ac, av)                         /* put out an N-sided polygon */
529 > int     ac;
530 > register char   **av;
531 > {
532 >        VNDX    vi;
533 >        char    *cp;
534 >        register int    i;
535 >
536 >        if (nonplanar(ac, av)) {        /* break into triangles */
537 >                while (ac > 2) {
538 >                        if (!puttri(av[0], av[1], av[2]))
539 >                                return(0);
540 >                        ac--;           /* remove vertex & rotate */
541 >                        cp = av[0];
542 >                        for (i = 0; i < ac-1; i++)
543 >                                av[i] = av[i+2];
544 >                        av[i] = cp;
545 >                }
546 >                return(1);
547 >        }
548 >        if ((cp = getmtl()) == NULL)
549 >                return(-1);
550 >        printf("\n%s polygon %s.%d\n", cp, getonm(), faceno);
551          printf("0\n0\n%d\n", 3*ac);
552 <        while (ac--) {
553 <                if (!cvtndx(vi, *av++))
552 >        for (i = 0; i < ac; i++) {
553 >                if (!cvtndx(vi, av[i]))
554                          return(0);
555                  pvect(vlist[vi[0]]);
556          }
# Line 315 | Line 561 | register char  **av;
561   puttri(v1, v2, v3)                      /* put out a triangle */
562   char    *v1, *v2, *v3;
563   {
564 <        char    *mod = matname;
564 >        char    *mod;
565          VNDX    v1i, v2i, v3i;
566          BARYCCM bvecs;
567 +        FVECT   bcoor[3];
568 +        int     texOK, patOK;
569 +        register int    i;
570  
571 +        if ((mod = getmtl()) == NULL)
572 +                return(-1);
573 +
574          if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3))
575                  return(0);
576                                          /* compute barycentric coordinates */
577 <        if ((v1i[2]>=0&&v2i[2]>=0&&v3i[2]>=0) ||
578 <                        (v1i[1]>=0&&v2i[1]>=0&&v3i[1]>=0))
579 <                if (comp_baryc(bvecs, vlist[v1i[0]], vlist[v2i[0]],
577 >        texOK = !flatten && (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0);
578 > #ifdef TEXMAPS
579 >        patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0);
580 > #else
581 >        patOK = 0;
582 > #endif
583 >        if (texOK | patOK)
584 >                if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]],
585                                  vlist[v3i[0]]) < 0)
586 <                        return(0);
586 >                        return(-1);
587                                          /* put out texture (if any) */
588 <        if (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0) {
588 >        if (texOK) {
589                  printf("\n%s texfunc %s\n", mod, TEXNAME);
590                  mod = TEXNAME;
591                  printf("4 dx dy dz %s\n", TCALNAME);
592 <                printf("0\n21\n");
593 <                put_baryc(bvecs);
594 <                printf("\t%14.12g %14.12g %14.12g\n",
595 <                                vnlist[v1i[2]][0], vnlist[v2i[2]][0],
596 <                                vnlist[v3i[2]][0]);
597 <                printf("\t%14.12g %14.12g %14.12g\n",
598 <                                vnlist[v1i[2]][1], vnlist[v2i[2]][1],
342 <                                vnlist[v3i[2]][1]);
343 <                printf("\t%14.12g %14.12g %14.12g\n",
344 <                                vnlist[v1i[2]][2], vnlist[v2i[2]][2],
345 <                                vnlist[v3i[2]][2]);
592 >                printf("0\n");
593 >                for (i = 0; i < 3; i++) {
594 >                        bcoor[i][0] = vnlist[v1i[2]][i];
595 >                        bcoor[i][1] = vnlist[v2i[2]][i];
596 >                        bcoor[i][2] = vnlist[v3i[2]][i];
597 >                }
598 >                put_baryc(&bvecs, bcoor, 3);
599          }
600 + #ifdef TEXMAPS
601                                          /* put out pattern (if any) */
602 <        if (picfile[0] && v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0) {
602 >        if (patOK) {
603                  printf("\n%s colorpict %s\n", mod, PATNAME);
604                  mod = PATNAME;
605 <                printf("7 noneg noneg noneg %s %s u v\n", picfile, TCALNAME);
606 <                printf("0\n18\n");
607 <                put_baryc(bvecs);
608 <                printf("\t%f %f %f\n", vtlist[v1i[1]][0],
609 <                                vtlist[v2i[1]][0], vtlist[v3i[1]][0]);
610 <                printf("\t%f %f %f\n", vtlist[v1i[1]][1],
611 <                                vtlist[v2i[1]][1], vtlist[v3i[1]][1]);
605 >                printf("7 noneg noneg noneg %s %s u v\n", mapname, TCALNAME);
606 >                printf("0\n");
607 >                for (i = 0; i < 2; i++) {
608 >                        bcoor[i][0] = vtlist[v1i[1]][i];
609 >                        bcoor[i][1] = vtlist[v2i[1]][i];
610 >                        bcoor[i][2] = vtlist[v3i[1]][i];
611 >                }
612 >                put_baryc(&bvecs, bcoor, 2);
613          }
614 + #endif
615                                          /* put out triangle */
616 <        printf("\n%s polygon %s.%d\n", matname, objname, ++nfaces);
616 >        printf("\n%s polygon %s.%d\n", mod, getonm(), faceno);
617          printf("0\n0\n9\n");
618          pvect(vlist[v1i[0]]);
619          pvect(vlist[v2i[0]]);
# Line 369 | Line 625 | char   *v1, *v2, *v3;
625  
626   int
627   comp_baryc(bcm, v1, v2, v3)             /* compute barycentric vectors */
628 < register BARYCCM        bcm;
628 > register BARYCCM        *bcm;
629   FLOAT   *v1, *v2, *v3;
630   {
631          FLOAT   *vt;
632          FVECT   va, vab, vcb;
633          double  d;
634 +        int     ax0, ax1;
635          register int    i, j;
636 <
637 <        for (j = 0; j < 3; j++) {
638 <                for (i = 0; i < 3; i++) {
639 <                        vab[i] = v1[i] - v2[i];
640 <                        vcb[i] = v3[i] - v2[i];
641 <                }
642 <                d = DOT(vcb,vcb);
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 = DOT(vcb,vab)/d;
655 <                for (i = 0; i < 3; i++)
656 <                        va[i] = vab[i] - vcb[i]*d;
657 <                d = DOT(va,va);
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 <                for (i = 0; i < 3; i++) {
661 <                        va[i] /= d;
662 <                        bcm[j][i] = va[i];
397 <                }
398 <                bcm[j][3] = -DOT(v2,va);
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;
# Line 406 | Line 670 | FLOAT  *v1, *v2, *v3;
670   }
671  
672  
673 < put_baryc(bcm)                          /* put barycentric coord. vectors */
674 < register BARYCCM        bcm;
673 > put_baryc(bcm, com, n)                  /* put barycentric coord. vectors */
674 > register BARYCCM        *bcm;
675 > register FVECT  com[];
676 > int     n;
677   {
678 <        register int    i;
678 >        double  a, b;
679 >        register int    i, j;
680  
681 <        for (i = 0; i < 3; i++)
682 <                printf("%14.8f %14.8f %14.8f %14.8f\n",
683 <                                bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]);
684 < }
685 <
686 <
687 < putquad(p0, p1, p2, p3)                 /* put out a quadrilateral */
688 < char  *p0, *p1, *p2, *p3;
422 < {
423 <        VNDX  p0i, p1i, p2i, p3i;
424 <        FVECT  norm[4];
425 <        int  axis;
426 <        FVECT  v1, v2, vc1, vc2;
427 <        int  ok1, ok2;
428 <                                        /* get actual indices */
429 <        if (!cvtndx(p0i,p0) || !cvtndx(p1i,p1) ||
430 <                        !cvtndx(p2i,p2) || !cvtndx(p3i,p3))
431 <                return(0);
432 <                                        /* compute exact normals */
433 <        fvsum(v1, vlist[p1i[0]], vlist[p0i[0]], -1.0);
434 <        fvsum(v2, vlist[p2i[0]], vlist[p0i[0]], -1.0);
435 <        fcross(vc1, v1, v2);
436 <        ok1 = normalize(vc1) != 0.0;
437 <        fvsum(v1, vlist[p2i[0]], vlist[p3i[0]], -1.0);
438 <        fvsum(v2, vlist[p1i[0]], vlist[p3i[0]], -1.0);
439 <        fcross(vc2, v1, v2);
440 <        ok2 = normalize(vc2) != 0.0;
441 <        if (!(ok1 | ok2))
442 <                return(0);
443 <                                        /* compute normal interpolation */
444 <        axis = norminterp(norm, p0i, p1i, p2i, p3i);
445 <
446 <                                        /* put out quadrilateral? */
447 <        if (ok1 & ok2 && fdot(vc1,vc2) >= 1.0-FTINY*FTINY) {
448 <                printf("\n%s ", matname);
449 <                if (axis != -1) {
450 <                        printf("texfunc %s\n", TEXNAME);
451 <                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
452 <                        printf("0\n13\t%d\n", axis);
453 <                        pvect(norm[0]);
454 <                        pvect(norm[1]);
455 <                        pvect(norm[2]);
456 <                        fvsum(v1, norm[3], vc1, -0.5);
457 <                        fvsum(v1, v1, vc2, -0.5);
458 <                        pvect(v1);
459 <                        printf("\n%s ", TEXNAME);
460 <                }
461 <                printf("polygon %s.%d\n", objname, ++nfaces);
462 <                printf("0\n0\n12\n");
463 <                pvect(vlist[p0i[0]]);
464 <                pvect(vlist[p1i[0]]);
465 <                pvect(vlist[p3i[0]]);
466 <                pvect(vlist[p2i[0]]);
467 <                return(1);
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          }
469                                        /* put out triangles? */
470        if (ok1) {
471                printf("\n%s ", matname);
472                if (axis != -1) {
473                        printf("texfunc %s\n", TEXNAME);
474                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
475                        printf("0\n13\t%d\n", axis);
476                        pvect(norm[0]);
477                        pvect(norm[1]);
478                        pvect(norm[2]);
479                        fvsum(v1, norm[3], vc1, -1.0);
480                        pvect(v1);
481                        printf("\n%s ", TEXNAME);
482                }
483                printf("polygon %s.%d\n", objname, ++nfaces);
484                printf("0\n0\n9\n");
485                pvect(vlist[p0i[0]]);
486                pvect(vlist[p1i[0]]);
487                pvect(vlist[p2i[0]]);
488        }
489        if (ok2) {
490                printf("\n%s ", matname);
491                if (axis != -1) {
492                        printf("texfunc %s\n", TEXNAME);
493                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
494                        printf("0\n13\t%d\n", axis);
495                        pvect(norm[0]);
496                        pvect(norm[1]);
497                        pvect(norm[2]);
498                        fvsum(v2, norm[3], vc2, -1.0);
499                        pvect(v2);
500                        printf("\n%s ", TEXNAME);
501                }
502                printf("polygon %s.%d\n", objname, ++nfaces);
503                printf("0\n0\n9\n");
504                pvect(vlist[p2i[0]]);
505                pvect(vlist[p1i[0]]);
506                pvect(vlist[p3i[0]]);
507        }
508        return(1);
690   }
691  
692  
512 int
513 norminterp(resmat, p0i, p1i, p2i, p3i)  /* compute normal interpolation */
514 register FVECT  resmat[4];
515 register VNDX  p0i, p1i, p2i, p3i;
516 {
517 #define u  ((ax+1)%3)
518 #define v  ((ax+2)%3)
519
520        register int  ax;
521        MAT4  eqnmat;
522        FVECT  v1;
523        register int  i, j;
524
525        if (!(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0))
526                return(-1);
527                                        /* find dominant axis */
528        VCOPY(v1, vnlist[p0i[2]]);
529        fvsum(v1, v1, vnlist[p1i[2]], 1.0);
530        fvsum(v1, v1, vnlist[p2i[2]], 1.0);
531        fvsum(v1, v1, vnlist[p3i[2]], 1.0);
532        ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1;
533        ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2;
534                                        /* assign equation matrix */
535        eqnmat[0][0] = vlist[p0i[0]][u]*vlist[p0i[0]][v];
536        eqnmat[0][1] = vlist[p0i[0]][u];
537        eqnmat[0][2] = vlist[p0i[0]][v];
538        eqnmat[0][3] = 1.0;
539        eqnmat[1][0] = vlist[p1i[0]][u]*vlist[p1i[0]][v];
540        eqnmat[1][1] = vlist[p1i[0]][u];
541        eqnmat[1][2] = vlist[p1i[0]][v];
542        eqnmat[1][3] = 1.0;
543        eqnmat[2][0] = vlist[p2i[0]][u]*vlist[p2i[0]][v];
544        eqnmat[2][1] = vlist[p2i[0]][u];
545        eqnmat[2][2] = vlist[p2i[0]][v];
546        eqnmat[2][3] = 1.0;
547        eqnmat[3][0] = vlist[p3i[0]][u]*vlist[p3i[0]][v];
548        eqnmat[3][1] = vlist[p3i[0]][u];
549        eqnmat[3][2] = vlist[p3i[0]][v];
550        eqnmat[3][3] = 1.0;
551                                        /* invert matrix (solve system) */
552        if (!invmat4(eqnmat, eqnmat))
553                return(-1);                     /* no solution */
554                                        /* compute result matrix */
555        for (j = 0; j < 4; j++)
556                for (i = 0; i < 3; i++)
557                        resmat[j][i] =  eqnmat[j][0]*vnlist[p0i[2]][i] +
558                                        eqnmat[j][1]*vnlist[p1i[2]][i] +
559                                        eqnmat[j][2]*vnlist[p2i[2]][i] +
560                                        eqnmat[j][3]*vnlist[p3i[2]][i];
561        return(ax);
562
563 #undef u
564 #undef v
565 }
566
567
693   freeverts()                     /* free all vertices */
694   {
695          if (nvs) {
# Line 633 | Line 758 | double x, y, z;
758  
759  
760   int
761 < newvt(x, y)                     /* create a new texture vertex */
761 > newvt(x, y)                     /* create a new texture map vertex */
762   double  x, y;
763   {
764          if (!(nvts%CHUNKSIZ)) {         /* allocate next block */
# Line 649 | Line 774 | double x, y;
774                          exit(1);
775                  }
776          }
777 <                                        /* assign new texture vertex */
777 >                                        /* assign new vertex */
778          vtlist[nvts][0] = x;
779          vtlist[nvts][1] = y;
780          return(++nvts);
781   }
782  
783  
784 < syntax(fn, ln, er)                      /* report syntax error and exit */
660 < char    *fn;
661 < int     ln;
784 > syntax(er)                      /* report syntax error and exit */
785   char    *er;
786   {
787          fprintf(stderr, "%s: Wavefront syntax error near line %d: %s\n",
788 <                        fn, ln, er);
788 >                        inpfile, lineno, er);
789          exit(1);
790   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines