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.4 by greg, Wed Apr 13 15:16:49 1994 UTC vs.
Revision 2.23 by greg, Fri Apr 23 16:20:56 2004 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   *
7 < * Currently, we support only polygonal geometry, and faces
8 < * must be either quads or triangles for smoothing to work.
7 > * Currently, we support only polygonal geometry.  Non-planar
8 > * faces are broken rather haphazardly into triangles.
9   * Also, texture map indices only work for triangles, though
10 < * I'm not sure they work correctly.
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 */
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
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  
40 typedef FLOAT   BARYCCM[3][4];  /* barycentric coordinate system */
41
38   typedef int     VNDX[3];        /* vertex index (point,map,normal) */
39  
40 < #define CHUNKSIZ        256     /* vertex allocation chunk size */
40 > #define CHUNKSIZ        1024    /* vertex allocation chunk size */
41  
42 < #define MAXARG          64      /* maximum # arguments in a statement */
42 > #define MAXARG          512     /* maximum # arguments in a statement */
43  
44                                  /* qualifiers */
45   #define Q_MTL           0
# Line 69 | Line 65 | RULEHD *ourmapping = NULL;
65  
66   char    *defmat = DEFMAT;       /* default (starting) material name */
67   char    *defobj = DEFOBJ;       /* default (starting) object name */
72 int     donames = 0;            /* only get qualifier names */
68  
69 < char    *getmtl(), *getonm();
69 > int     flatten = 0;            /* discard surface normal information */
70  
71   char    mapname[128];           /* current picture file */
72   char    matname[64];            /* current material name */
73   char    group[16][32];          /* current group names */
74   char    objname[128];           /* current object name */
75 + char    *inpfile;               /* input file name */
76   int     lineno;                 /* current line number */
77 < int     faceno;                 /* number of faces read */
77 > int     faceno;                 /* current face number */
78  
79 + static void getnames(FILE *fp);
80 + static void convert(FILE *fp);
81 + static int getstmt(char *av[MAXARG], FILE *fp);
82 + static char * getmtl(void);
83 + static char * getonm(void);
84 + static int matchrule(RULEHD *rp);
85 + static int cvtndx(VNDX vi, char *vs);
86 + static int nonplanar(int ac, char **av);
87 + static int putface(int ac, char **av);
88 + static int puttri(char *v1, char *v2, char *v3);
89 + static void freeverts(void);
90 + static int newv(double x, double y, double z);
91 + static int newvn(double x, double y, double z);
92 + static int newvt(double x, double y);
93 + static void syntax(char *er);
94  
95 < main(argc, argv)                /* read in .obj file and convert */
96 < int     argc;
97 < char    *argv[];
95 >
96 > int
97 > main(           /* read in .obj file and convert */
98 >        int     argc,
99 >        char    *argv[]
100 > )
101   {
102 <        char    *fname;
102 >        int     donames = 0;
103          int     i;
104  
105          for (i = 1; i < argc && argv[i][0] == '-'; i++)
# Line 99 | Line 113 | char   *argv[];
113                  case 'm':               /* use custom mapfile */
114                          ourmapping = getmapping(argv[++i], &qlist);
115                          break;
116 +                case 'f':               /* flatten surfaces */
117 +                        flatten++;
118 +                        break;
119                  default:
120                          goto userr;
121                  }
122 <        if (i > argc | i < argc-1)
122 >        if ((i > argc) | (i < argc-1))
123                  goto userr;
124          if (i == argc)
125 <                fname = "<stdin>";
126 <        else if (freopen(fname=argv[i], "r", stdin) == NULL) {
127 <                fprintf(stderr, "%s: cannot open\n", fname);
125 >                inpfile = "<stdin>";
126 >        else if (freopen(inpfile=argv[i], "r", stdin) == NULL) {
127 >                fprintf(stderr, "%s: cannot open\n", inpfile);
128                  exit(1);
129          }
130          if (donames) {                          /* scan for ids */
131                  getnames(stdin);
132 <                printf("filename \"%s\"\n", fname);
132 >                printf("filename \"%s\"\n", inpfile);
133                  printf("filetype \"Wavefront\"\n");
134                  write_quals(&qlist, qual, stdout);
135                  printf("qualifier %s begin\n", qlist.qual[Q_FAC]);
# Line 121 | Line 138 | char   *argv[];
138          } else {                                /* translate file */
139                  printf("# ");
140                  printargs(argc, argv, stdout);
141 <                convert(fname, stdin);
141 >                convert(stdin);
142          }
143          exit(0);
144   userr:
145 <        fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n] [file.obj]\n",
145 >        fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n",
146                          argv[0]);
147          exit(1);
148   }
149  
150  
151 < getnames(fp)                    /* get valid qualifier names */
152 < FILE    *fp;
151 > void
152 > getnames(                       /* get valid qualifier names */
153 >        FILE    *fp
154 > )
155   {
156          char    *argv[MAXARG];
157          int     argc;
158          ID      tmpid;
159          register int    i;
160  
161 <        while (argc = getstmt(argv, fp))
161 >        while ( (argc = getstmt(argv, fp)) )
162                  switch (argv[0][0]) {
163                  case 'f':                               /* face */
164                          if (!argv[0][1])
# Line 180 | Line 199 | FILE   *fp;
199   }
200  
201  
202 < convert(fname, fp)              /* convert a T-mesh */
203 < char    *fname;
204 < FILE    *fp;
202 > void
203 > convert(                        /* convert a T-mesh */
204 >        FILE    *fp
205 > )
206   {
207          char    *argv[MAXARG];
208          int     argc;
209          int     nstats, nunknown;
210          register int    i;
211 <                                        /* start fresh */
192 <        freeverts();
193 <        mapname[0] = '\0';
194 <        strcpy(matname, defmat);
195 <        strcpy(objname, defobj);
196 <        lineno = 0;
211 >
212          nstats = nunknown = 0;
213                                          /* scan until EOF */
214 <        while (argc = getstmt(argv, fp)) {
214 >        while ( (argc = getstmt(argv, fp)) ) {
215                  switch (argv[0][0]) {
216                  case 'v':               /* vertex */
217                          switch (argv[0][1]) {
218                          case '\0':                      /* point */
219                                  if (badarg(argc-1,argv+1,"fff"))
220 <                                        syntax(fname, lineno, "Bad vertex");
220 >                                        syntax("Bad vertex");
221                                  newv(atof(argv[1]), atof(argv[2]),
222                                                  atof(argv[3]));
223                                  break;
# Line 210 | Line 225 | FILE   *fp;
225                                  if (argv[0][2])
226                                          goto unknown;
227                                  if (badarg(argc-1,argv+1,"fff"))
228 <                                        syntax(fname, lineno, "Bad normal");
228 >                                        syntax("Bad normal");
229                                  if (!newvn(atof(argv[1]), atof(argv[2]),
230                                                  atof(argv[3])))
231 <                                        syntax(fname, lineno, "Zero normal");
231 >                                        syntax("Zero normal");
232                                  break;
233                          case 't':                       /* texture map */
234                                  if (argv[0][2])
# Line 232 | Line 247 | FILE   *fp;
247                          faceno++;
248                          switch (argc-1) {
249                          case 0: case 1: case 2:
250 <                                syntax(fname, lineno, "Too few vertices");
250 >                                syntax("Too few vertices");
251                                  break;
252                          case 3:
253                                  if (!puttri(argv[1], argv[2], argv[3]))
254 <                                        syntax(fname, lineno, "Bad triangle");
254 >                                        syntax("Bad triangle");
255                                  break;
241                        case 4:
242                                if (!putquad(argv[1], argv[2],
243                                                argv[3], argv[4]))
244                                        syntax(fname, lineno, "Bad quad");
245                                break;
256                          default:
257                                  if (!putface(argc-1, argv+1))
258 <                                        syntax(fname, lineno, "Bad face");
258 >                                        syntax("Bad face");
259                                  break;
260                          }
261                          break;
# Line 260 | Line 270 | FILE   *fp;
270                                  if (!strcmp(argv[1], "off"))
271                                          mapname[0] = '\0';
272                                  else
273 <                                        strcpy(mapname, argv[1]);
273 >                                        sprintf(mapname, "%s.pic", argv[1]);
274                          } else
275                                  goto unknown;
276                          break;
# Line 279 | Line 289 | FILE   *fp;
289                          group[i-1][0] = '\0';
290                          break;
291                  case '#':               /* comment */
292 +                        printargs(argc, argv, stdout);
293                          break;
294                  default:;               /* something we don't deal with */
295                  unknown:
# Line 287 | Line 298 | FILE   *fp;
298                  }
299                  nstats++;
300          }
301 <        printf("\n# Done processing file: %s\n", fname);
301 >        printf("\n# Done processing file: %s\n", inpfile);
302          printf("# %d lines, %d statements, %d unrecognized\n",
303                          lineno, nstats, nunknown);
304   }
305  
306  
307   int
308 < getstmt(av, fp)                         /* read the next statement from fp */
309 < register char   *av[MAXARG];
310 < FILE    *fp;
308 > getstmt(                                /* read the next statement from fp */
309 >        register char   *av[MAXARG],
310 >        FILE    *fp
311 > )
312   {
313 <        extern char     *fgetline();
302 <        static char     sbuf[MAXARG*10];
313 >        static char     sbuf[MAXARG*16];
314          register char   *cp;
315          register int    i;
316  
# Line 313 | Line 324 | FILE   *fp;
324                                          lineno++;
325                                  *cp++ = '\0';
326                          }
327 <                        if (!*cp || i >= MAXARG-1)
327 >                        if (!*cp)
328                                  break;
329 +                        if (i >= MAXARG-1) {
330 +                                fprintf(stderr,
331 +                        "warning: line %d: too many arguments (limit %d)\n",
332 +                                        lineno+1, MAXARG-1);
333 +                                break;
334 +                        }
335                          av[i++] = cp;
336                          while (*++cp && !isspace(*cp))
337                                  ;
# Line 328 | Line 345 | FILE   *fp;
345  
346  
347   char *
348 < getmtl()                                /* figure material for this face */
348 > getmtl(void)                            /* figure material for this face */
349   {
350          register RULEHD *rp = ourmapping;
351  
352 <        if (rp == NULL)                 /* no rule set */
353 <                return(matname);
352 >        if (rp == NULL) {               /* no rule set */
353 >                if (matname[0])
354 >                        return(matname);
355 >                if (group[0][0])
356 >                        return(group[0]);
357 >                return(defmat);
358 >        }
359                                          /* check for match */
360          do {
361                  if (matchrule(rp)) {
# Line 349 | Line 371 | getmtl()                               /* figure material for this face */
371  
372  
373   char *
374 < getonm()                                /* invent a good name for object */
374 > getonm(void)                            /* invent a good name for object */
375   {
376          static char     name[64];
377          register char   *cp1, *cp2;
378          register int    i;
379 <
380 <        if (!group[0][0] || strcmp(objname, DEFOBJ))
381 <                return(objname);        /* good enough for us */
382 <
379 >                                        /* check for preset */
380 >        if (objname[0])
381 >                return(objname);
382 >        if (!group[0][0])
383 >                return(defobj);
384          cp1 = name;                     /* else make name out of groups */
385          for (i = 0; group[i][0]; i++) {
386                  cp2 = group[i];
387                  if (cp1 > name)
388                          *cp1++ = '.';
389 <                while (*cp1 = *cp2++)
389 >                while ( (*cp1 = *cp2++) )
390                          if (++cp1 >= name+sizeof(name)-2) {
391                                  *cp1 = '\0';
392                                  return(name);
# Line 373 | Line 396 | getonm()                               /* invent a good name for object */
396   }
397  
398  
399 < matchrule(rp)                           /* check for a match on this rule */
400 < register RULEHD *rp;
399 > int
400 > matchrule(                              /* check for a match on this rule */
401 >        register RULEHD *rp
402 > )
403   {
404          ID      tmpid;
405          int     gotmatch;
406          register int    i;
407  
408          if (rp->qflg & FL(Q_MTL)) {
409 +                if (!matname[0])
410 +                        return(0);
411                  tmpid.number = 0;
412                  tmpid.name = matname;
413                  if (!matchid(&tmpid, &idm(rp)[Q_MTL]))
414                          return(0);
415          }
416          if (rp->qflg & FL(Q_MAP)) {
417 +                if (!mapname[0])
418 +                        return(0);
419                  tmpid.number = 0;
420                  tmpid.name = mapname;
421                  if (!matchid(&tmpid, &idm(rp)[Q_MAP]))
# Line 403 | Line 432 | register RULEHD        *rp;
432                          return(0);
433          }
434          if (rp->qflg & FL(Q_OBJ)) {
435 +                if (!objname[0])
436 +                        return(0);
437                  tmpid.number = 0;
438                  tmpid.name = objname;
439                  if (!matchid(&tmpid, &idm(rp)[Q_OBJ]))
# Line 418 | Line 449 | register RULEHD        *rp;
449   }
450  
451  
452 < cvtndx(vi, vs)                          /* convert vertex string to index */
453 < register VNDX   vi;
454 < register char   *vs;
452 > int
453 > cvtndx(                         /* convert vertex string to index */
454 >        register VNDX   vi,
455 >        register char   *vs
456 > )
457   {
458                                          /* get point */
459          vi[0] = atoi(vs);
# Line 428 | Line 461 | register char  *vs;
461                  if (vi[0]-- > nvs)
462                          return(0);
463          } else if (vi[0] < 0) {
464 <                vi[0] = nvs + vi[0];
464 >                vi[0] += nvs;
465                  if (vi[0] < 0)
466                          return(0);
467          } else
# Line 442 | Line 475 | register char  *vs;
475                  if (vi[1]-- > nvts)
476                          return(0);
477          } else if (vi[1] < 0) {
478 <                vi[1] = nvts + vi[1];
478 >                vi[1] += nvts;
479                  if (vi[1] < 0)
480                          return(0);
481          } else
# Line 456 | Line 489 | register char  *vs;
489                  if (vi[2]-- > nvns)
490                          return(0);
491          } else if (vi[2] < 0) {
492 <                vi[2] = nvns + vi[2];
492 >                vi[2] += nvns;
493                  if (vi[2] < 0)
494                          return(0);
495          } else
# Line 465 | Line 498 | register char  *vs;
498   }
499  
500  
501 < putface(ac, av)                         /* put out an N-sided polygon */
502 < register int    ac;
503 < register char   **av;
501 > int
502 > nonplanar(                      /* are vertices non-planar? */
503 >        register int    ac,
504 >        register char   **av
505 > )
506   {
507          VNDX    vi;
508 <        char    *mod;
508 >        RREAL   *p0, *p1;
509 >        FVECT   v1, v2, nsum, newn;
510 >        double  d;
511 >        register int    i;
512  
513 <        if ((mod = getmtl()) == NULL)
513 >        if (!cvtndx(vi, av[0]))
514 >                return(0);
515 >        if (!flatten && vi[2] >= 0)
516 >                return(1);              /* has interpolated normals */
517 >        if (ac < 4)
518 >                return(0);              /* it's a triangle! */
519 >                                        /* set up */
520 >        p0 = vlist[vi[0]];
521 >        if (!cvtndx(vi, av[1]))
522 >                return(0);              /* error gets caught later */
523 >        nsum[0] = nsum[1] = nsum[2] = 0.;
524 >        p1 = vlist[vi[0]];
525 >        fvsum(v2, p1, p0, -1.0);
526 >        for (i = 2; i < ac; i++) {
527 >                VCOPY(v1, v2);
528 >                if (!cvtndx(vi, av[i]))
529 >                        return(0);
530 >                p1 = vlist[vi[0]];
531 >                fvsum(v2, p1, p0, -1.0);
532 >                fcross(newn, v1, v2);
533 >                if (normalize(newn) == 0.0) {
534 >                        if (i < 3)
535 >                                return(1);      /* can't deal with this */
536 >                        fvsum(nsum, nsum, nsum, 1./(i-2));
537 >                        continue;
538 >                }
539 >                d = fdot(newn,nsum);
540 >                if (d >= 0) {
541 >                        if (d < (1.0-FTINY)*(i-2))
542 >                                return(1);
543 >                        fvsum(nsum, nsum, newn, 1.0);
544 >                } else {
545 >                        if (d > -(1.0-FTINY)*(i-2))
546 >                                return(1);
547 >                        fvsum(nsum, nsum, newn, -1.0);
548 >                }
549 >        }
550 >        return(0);
551 > }
552 >
553 >
554 > int
555 > putface(                                /* put out an N-sided polygon */
556 >        int     ac,
557 >        register char   **av
558 > )
559 > {
560 >        VNDX    vi;
561 >        char    *cp;
562 >        register int    i;
563 >
564 >        if (nonplanar(ac, av)) {        /* break into triangles */
565 >                while (ac > 2) {
566 >                        if (!puttri(av[0], av[1], av[2]))
567 >                                return(0);
568 >                        ac--;           /* remove vertex & rotate */
569 >                        cp = av[0];
570 >                        for (i = 0; i < ac-1; i++)
571 >                                av[i] = av[i+2];
572 >                        av[i] = cp;
573 >                }
574 >                return(1);
575 >        }
576 >        if ((cp = getmtl()) == NULL)
577                  return(-1);
578 <        printf("\n%s polygon %s.%d\n", mod, getonm(), faceno);
578 >        printf("\n%s polygon %s.%d\n", cp, getonm(), faceno);
579          printf("0\n0\n%d\n", 3*ac);
580 <        while (ac--) {
581 <                if (!cvtndx(vi, *av++))
580 >        for (i = 0; i < ac; i++) {
581 >                if (!cvtndx(vi, av[i]))
582                          return(0);
583                  pvect(vlist[vi[0]]);
584          }
# Line 485 | Line 586 | register char  **av;
586   }
587  
588  
589 < puttri(v1, v2, v3)                      /* put out a triangle */
590 < char    *v1, *v2, *v3;
589 > int
590 > puttri(                 /* put out a triangle */
591 >        char *v1,
592 >        char *v2,
593 >        char *v3
594 > )
595   {
596          char    *mod;
597          VNDX    v1i, v2i, v3i;
598          BARYCCM bvecs;
599 <        int     texOK, patOK;
599 >        RREAL   bcoor[3][3];
600 >        int     texOK = 0, patOK;
601 >        int     flatness;
602 >        register int    i;
603  
604          if ((mod = getmtl()) == NULL)
605                  return(-1);
# Line 499 | Line 607 | char   *v1, *v2, *v3;
607          if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3))
608                  return(0);
609                                          /* compute barycentric coordinates */
610 <        texOK = (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0);
610 >        if (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0)
611 >                flatness = flat_tri(vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]],
612 >                                vnlist[v1i[2]], vnlist[v2i[2]], vnlist[v3i[2]]);
613 >        else
614 >                flatness = ISFLAT;
615 >
616 >        switch (flatness) {
617 >        case DEGEN:                     /* zero area */
618 >                return(-1);
619 >        case RVFLAT:                    /* reversed normals, but flat */
620 >        case ISFLAT:                    /* smoothing unnecessary */
621 >                texOK = 0;
622 >                break;
623 >        case RVBENT:                    /* reversed normals with smoothing */
624 >        case ISBENT:                    /* proper smoothing */
625 >                texOK = 1;
626 >                break;
627 >        }
628 >        if (flatten)
629 >                texOK = 0;
630 > #ifdef TEXMAPS
631          patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0);
632 + #else
633 +        patOK = 0;
634 + #endif
635          if (texOK | patOK)
636 <                if (comp_baryc(bvecs, vlist[v1i[0]], vlist[v2i[0]],
636 >                if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]],
637                                  vlist[v3i[0]]) < 0)
638                          return(-1);
639                                          /* put out texture (if any) */
# Line 510 | Line 641 | char   *v1, *v2, *v3;
641                  printf("\n%s texfunc %s\n", mod, TEXNAME);
642                  mod = TEXNAME;
643                  printf("4 dx dy dz %s\n", TCALNAME);
644 <                printf("0\n21\n");
645 <                put_baryc(bvecs);
646 <                printf("\t%14.12g %14.12g %14.12g\n",
647 <                                vnlist[v1i[2]][0], vnlist[v2i[2]][0],
648 <                                vnlist[v3i[2]][0]);
649 <                printf("\t%14.12g %14.12g %14.12g\n",
650 <                                vnlist[v1i[2]][1], vnlist[v2i[2]][1],
520 <                                vnlist[v3i[2]][1]);
521 <                printf("\t%14.12g %14.12g %14.12g\n",
522 <                                vnlist[v1i[2]][2], vnlist[v2i[2]][2],
523 <                                vnlist[v3i[2]][2]);
644 >                printf("0\n");
645 >                for (i = 0; i < 3; i++) {
646 >                        bcoor[i][0] = vnlist[v1i[2]][i];
647 >                        bcoor[i][1] = vnlist[v2i[2]][i];
648 >                        bcoor[i][2] = vnlist[v3i[2]][i];
649 >                }
650 >                put_baryc(&bvecs, bcoor, 3);
651          }
652 + #ifdef TEXMAPS
653                                          /* put out pattern (if any) */
654          if (patOK) {
655                  printf("\n%s colorpict %s\n", mod, PATNAME);
656                  mod = PATNAME;
657                  printf("7 noneg noneg noneg %s %s u v\n", mapname, TCALNAME);
658 <                printf("0\n18\n");
659 <                put_baryc(bvecs);
660 <                printf("\t%f %f %f\n", vtlist[v1i[1]][0],
661 <                                vtlist[v2i[1]][0], vtlist[v3i[1]][0]);
662 <                printf("\t%f %f %f\n", vtlist[v1i[1]][1],
663 <                                vtlist[v2i[1]][1], vtlist[v3i[1]][1]);
658 >                printf("0\n");
659 >                for (i = 0; i < 2; i++) {
660 >                        bcoor[i][0] = vtlist[v1i[1]][i];
661 >                        bcoor[i][1] = vtlist[v2i[1]][i];
662 >                        bcoor[i][2] = vtlist[v3i[1]][i];
663 >                }
664 >                put_baryc(&bvecs, bcoor, 2);
665          }
666 <                                        /* put out triangle */
666 > #endif
667 >                                        /* put out (reversed) triangle */
668          printf("\n%s polygon %s.%d\n", mod, getonm(), faceno);
669          printf("0\n0\n9\n");
670 <        pvect(vlist[v1i[0]]);
671 <        pvect(vlist[v2i[0]]);
672 <        pvect(vlist[v3i[0]]);
673 <
674 <        return(1);
675 < }
676 <
677 <
548 < int
549 < comp_baryc(bcm, v1, v2, v3)             /* compute barycentric vectors */
550 < register BARYCCM        bcm;
551 < FLOAT   *v1, *v2, *v3;
552 < {
553 <        FLOAT   *vt;
554 <        FVECT   va, vab, vcb;
555 <        double  d;
556 <        register int    i, j;
557 <
558 <        for (j = 0; j < 3; j++) {
559 <                for (i = 0; i < 3; i++) {
560 <                        vab[i] = v1[i] - v2[i];
561 <                        vcb[i] = v3[i] - v2[i];
562 <                }
563 <                d = DOT(vcb,vcb);
564 <                if (d <= FTINY)
565 <                        return(-1);
566 <                d = DOT(vcb,vab)/d;
567 <                for (i = 0; i < 3; i++)
568 <                        va[i] = vab[i] - vcb[i]*d;
569 <                d = DOT(va,va);
570 <                if (d <= FTINY)
571 <                        return(-1);
572 <                for (i = 0; i < 3; i++) {
573 <                        va[i] /= d;
574 <                        bcm[j][i] = va[i];
575 <                }
576 <                bcm[j][3] = -DOT(v2,va);
577 <                                        /* rotate vertices */
578 <                vt = v1;
579 <                v1 = v2;
580 <                v2 = v3;
581 <                v3 = vt;
670 >        if (flatness == RVFLAT || flatness == RVBENT) {
671 >                pvect(vlist[v3i[0]]);
672 >                pvect(vlist[v2i[0]]);
673 >                pvect(vlist[v1i[0]]);
674 >        } else {
675 >                pvect(vlist[v1i[0]]);
676 >                pvect(vlist[v2i[0]]);
677 >                pvect(vlist[v3i[0]]);
678          }
583        return(0);
584 }
585
586
587 put_baryc(bcm)                          /* put barycentric coord. vectors */
588 register BARYCCM        bcm;
589 {
590        register int    i;
591
592        for (i = 0; i < 3; i++)
593                printf("%14.8f %14.8f %14.8f %14.8f\n",
594                                bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]);
595 }
596
597
598 putquad(p0, p1, p3, p2)                 /* put out a quadrilateral */
599 char  *p0, *p1, *p3, *p2;               /* names correspond to binary pos. */
600 {
601        VNDX  p0i, p1i, p2i, p3i;
602        FVECT  norm[4];
603        char  *mod, *name;
604        int  axis;
605        FVECT  v1, v2, vc1, vc2;
606        int  ok1, ok2;
607
608        if ((mod = getmtl()) == NULL)
609                return(-1);
610        name = getonm();
611                                        /* get actual indices */
612        if (!cvtndx(p0i,p0) || !cvtndx(p1i,p1) ||
613                        !cvtndx(p2i,p2) || !cvtndx(p3i,p3))
614                return(0);
615                                        /* compute exact normals */
616        fvsum(v1, vlist[p1i[0]], vlist[p0i[0]], -1.0);
617        fvsum(v2, vlist[p2i[0]], vlist[p0i[0]], -1.0);
618        fcross(vc1, v1, v2);
619        ok1 = normalize(vc1) != 0.0;
620        fvsum(v1, vlist[p2i[0]], vlist[p3i[0]], -1.0);
621        fvsum(v2, vlist[p1i[0]], vlist[p3i[0]], -1.0);
622        fcross(vc2, v1, v2);
623        ok2 = normalize(vc2) != 0.0;
624        if (!(ok1 | ok2))
625                return(-1);
626                                        /* compute normal interpolation */
627        axis = norminterp(norm, p0i, p1i, p2i, p3i);
628
629                                        /* put out quadrilateral? */
630        if (ok1 & ok2 && fdot(vc1,vc2) >= 1.0-FTINY*FTINY) {
631                printf("\n%s ", mod);
632                if (axis != -1) {
633                        printf("texfunc %s\n", TEXNAME);
634                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
635                        printf("0\n13\t%d\n", axis);
636                        pvect(norm[0]);
637                        pvect(norm[1]);
638                        pvect(norm[2]);
639                        fvsum(v1, norm[3], vc1, -0.5);
640                        fvsum(v1, v1, vc2, -0.5);
641                        pvect(v1);
642                        printf("\n%s ", TEXNAME);
643                }
644                printf("polygon %s.%d\n", name, faceno);
645                printf("0\n0\n12\n");
646                pvect(vlist[p0i[0]]);
647                pvect(vlist[p1i[0]]);
648                pvect(vlist[p3i[0]]);
649                pvect(vlist[p2i[0]]);
650                return(1);
651        }
652                                        /* put out triangles? */
653        if (ok1) {
654                printf("\n%s ", mod);
655                if (axis != -1) {
656                        printf("texfunc %s\n", TEXNAME);
657                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
658                        printf("0\n13\t%d\n", axis);
659                        pvect(norm[0]);
660                        pvect(norm[1]);
661                        pvect(norm[2]);
662                        fvsum(v1, norm[3], vc1, -1.0);
663                        pvect(v1);
664                        printf("\n%s ", TEXNAME);
665                }
666                printf("polygon %s.%da\n", name, faceno);
667                printf("0\n0\n9\n");
668                pvect(vlist[p0i[0]]);
669                pvect(vlist[p1i[0]]);
670                pvect(vlist[p2i[0]]);
671        }
672        if (ok2) {
673                printf("\n%s ", mod);
674                if (axis != -1) {
675                        printf("texfunc %s\n", TEXNAME);
676                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
677                        printf("0\n13\t%d\n", axis);
678                        pvect(norm[0]);
679                        pvect(norm[1]);
680                        pvect(norm[2]);
681                        fvsum(v2, norm[3], vc2, -1.0);
682                        pvect(v2);
683                        printf("\n%s ", TEXNAME);
684                }
685                printf("polygon %s.%db\n", name, faceno);
686                printf("0\n0\n9\n");
687                pvect(vlist[p2i[0]]);
688                pvect(vlist[p1i[0]]);
689                pvect(vlist[p3i[0]]);
690        }
679          return(1);
680   }
681  
682  
683 < int
684 < norminterp(resmat, p0i, p1i, p2i, p3i)  /* compute normal interpolation */
697 < register FVECT  resmat[4];
698 < register VNDX  p0i, p1i, p2i, p3i;
683 > void
684 > freeverts(void)                 /* free all vertices */
685   {
700 #define u  ((ax+1)%3)
701 #define v  ((ax+2)%3)
702
703        register int  ax;
704        MAT4  eqnmat;
705        FVECT  v1;
706        register int  i, j;
707
708        if (!(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0))
709                return(-1);
710                                        /* find dominant axis */
711        VCOPY(v1, vnlist[p0i[2]]);
712        fvsum(v1, v1, vnlist[p1i[2]], 1.0);
713        fvsum(v1, v1, vnlist[p2i[2]], 1.0);
714        fvsum(v1, v1, vnlist[p3i[2]], 1.0);
715        ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1;
716        ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2;
717                                        /* assign equation matrix */
718        eqnmat[0][0] = vlist[p0i[0]][u]*vlist[p0i[0]][v];
719        eqnmat[0][1] = vlist[p0i[0]][u];
720        eqnmat[0][2] = vlist[p0i[0]][v];
721        eqnmat[0][3] = 1.0;
722        eqnmat[1][0] = vlist[p1i[0]][u]*vlist[p1i[0]][v];
723        eqnmat[1][1] = vlist[p1i[0]][u];
724        eqnmat[1][2] = vlist[p1i[0]][v];
725        eqnmat[1][3] = 1.0;
726        eqnmat[2][0] = vlist[p2i[0]][u]*vlist[p2i[0]][v];
727        eqnmat[2][1] = vlist[p2i[0]][u];
728        eqnmat[2][2] = vlist[p2i[0]][v];
729        eqnmat[2][3] = 1.0;
730        eqnmat[3][0] = vlist[p3i[0]][u]*vlist[p3i[0]][v];
731        eqnmat[3][1] = vlist[p3i[0]][u];
732        eqnmat[3][2] = vlist[p3i[0]][v];
733        eqnmat[3][3] = 1.0;
734                                        /* invert matrix (solve system) */
735        if (!invmat4(eqnmat, eqnmat))
736                return(-1);                     /* no solution */
737                                        /* compute result matrix */
738        for (j = 0; j < 4; j++)
739                for (i = 0; i < 3; i++)
740                        resmat[j][i] =  eqnmat[j][0]*vnlist[p0i[2]][i] +
741                                        eqnmat[j][1]*vnlist[p1i[2]][i] +
742                                        eqnmat[j][2]*vnlist[p2i[2]][i] +
743                                        eqnmat[j][3]*vnlist[p3i[2]][i];
744        return(ax);
745
746 #undef u
747 #undef v
748 }
749
750
751 freeverts()                     /* free all vertices */
752 {
686          if (nvs) {
687 <                free((char *)vlist);
687 >                free((void *)vlist);
688                  nvs = 0;
689          }
690          if (nvts) {
691 <                free((char *)vtlist);
691 >                free((void *)vtlist);
692                  nvts = 0;
693          }
694          if (nvns) {
695 <                free((char *)vnlist);
695 >                free((void *)vnlist);
696                  nvns = 0;
697          }
698   }
699  
700  
701   int
702 < newv(x, y, z)                   /* create a new vertex */
703 < double  x, y, z;
702 > newv(                   /* create a new vertex */
703 >        double  x,
704 >        double  y,
705 >        double  z
706 > )
707   {
708          if (!(nvs%CHUNKSIZ)) {          /* allocate next block */
709                  if (nvs == 0)
710                          vlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT));
711                  else
712 <                        vlist = (FVECT *)realloc((char *)vlist,
712 >                        vlist = (FVECT *)realloc((void *)vlist,
713                                          (nvs+CHUNKSIZ)*sizeof(FVECT));
714                  if (vlist == NULL) {
715                          fprintf(stderr,
# Line 790 | Line 726 | double x, y, z;
726  
727  
728   int
729 < newvn(x, y, z)                  /* create a new vertex normal */
730 < double  x, y, z;
729 > newvn(                  /* create a new vertex normal */
730 >        double  x,
731 >        double  y,
732 >        double  z
733 > )
734   {
735          if (!(nvns%CHUNKSIZ)) {         /* allocate next block */
736                  if (nvns == 0)
737                          vnlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT));
738                  else
739 <                        vnlist = (FVECT *)realloc((char *)vnlist,
739 >                        vnlist = (FVECT *)realloc((void *)vnlist,
740                                          (nvns+CHUNKSIZ)*sizeof(FVECT));
741                  if (vnlist == NULL) {
742                          fprintf(stderr,
# Line 816 | Line 755 | double x, y, z;
755  
756  
757   int
758 < newvt(x, y)                     /* create a new texture map vertex */
759 < double  x, y;
758 > newvt(                  /* create a new texture map vertex */
759 >        double  x,
760 >        double  y
761 > )
762   {
763          if (!(nvts%CHUNKSIZ)) {         /* allocate next block */
764                  if (nvts == 0)
765 <                        vtlist = (FLOAT (*)[2])malloc(CHUNKSIZ*2*sizeof(FLOAT));
765 >                        vtlist = (RREAL (*)[2])malloc(CHUNKSIZ*2*sizeof(RREAL));
766                  else
767 <                        vtlist = (FLOAT (*)[2])realloc((char *)vtlist,
768 <                                        (nvts+CHUNKSIZ)*2*sizeof(FLOAT));
767 >                        vtlist = (RREAL (*)[2])realloc((void *)vtlist,
768 >                                        (nvts+CHUNKSIZ)*2*sizeof(RREAL));
769                  if (vtlist == NULL) {
770                          fprintf(stderr,
771                          "Out of memory while allocating texture vertex %d\n",
# Line 839 | Line 780 | double x, y;
780   }
781  
782  
783 < syntax(fn, ln, er)                      /* report syntax error and exit */
784 < char    *fn;
785 < int     ln;
786 < char    *er;
783 > void
784 > syntax(                 /* report syntax error and exit */
785 >        char    *er
786 > )
787   {
788          fprintf(stderr, "%s: Wavefront syntax error near line %d: %s\n",
789 <                        fn, ln, er);
789 >                        inpfile, lineno, er);
790          exit(1);
791   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines