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.6 by greg, Thu Apr 14 04:29:10 1994 UTC vs.
Revision 2.32 by greg, Thu Apr 15 23:51:04 2021 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.
5 > * Convert a Wavefront .OBJ file to Radiance format.
6   *
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 <ctype.h>
15  
16 + #include "rtmath.h"
17 + #include "rtio.h"
18 + #include "resolu.h"
19   #include "trans.h"
20 + #include "tmesh.h"
21  
20 #include <ctype.h>
22  
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))
28 > #define pvect(v)        printf(" %18.12g %18.12g %18.12g\n",(v)[0],(v)[1],(v)[2])
29  
31 #define pvect(v)        printf("%18.12g %18.12g %18.12g\n",(v)[0],(v)[1],(v)[2])
32
30   FVECT   *vlist;                 /* our vertex list */
31   int     nvs;                    /* number of vertices in our list */
32   FVECT   *vnlist;                /* vertex normal list */
33   int     nvns;
34 < FLOAT   (*vtlist)[2];           /* map vertex list */
34 > RREAL   (*vtlist)[2];           /* map vertex list */
35   int     nvts;
36  
37 < typedef FLOAT   BARYCCM[3][4];  /* barycentric coordinate system */
37 > int     ndegen = 0;             /* count of degenerate faces */
38 > int     n0norm = 0;             /* count of zero normals */
39  
40   typedef int     VNDX[3];        /* vertex index (point,map,normal) */
41  
42 < #define CHUNKSIZ        256     /* vertex allocation chunk size */
42 > #define CHUNKSIZ        1024    /* vertex allocation chunk size */
43  
44 < #define MAXARG          64      /* maximum # arguments in a statement */
44 > #define MAXARG          512     /* maximum # arguments in a statement */
45  
46                                  /* qualifiers */
47   #define Q_MTL           0
# Line 70 | Line 68 | RULEHD *ourmapping = NULL;
68   char    *defmat = DEFMAT;       /* default (starting) material name */
69   char    *defobj = DEFOBJ;       /* default (starting) object name */
70  
71 < char    *getmtl(), *getonm();
71 > int     flatten = 0;            /* discard surface normal information */
72  
73 < char    mapname[128];           /* current picture file */
74 < char    matname[64];            /* current material name */
75 < char    group[16][32];          /* current group names */
76 < char    objname[128];           /* current object name */
73 > char    mapname[256];           /* current picture file */
74 > char    matname[256];           /* current material name */
75 > char    group[8][256];          /* current group name(s) */
76 > char    objname[256];           /* current object name */
77   char    *inpfile;               /* input file name */
78   int     lineno;                 /* current line number */
79   int     faceno;                 /* current face number */
80  
81 + static void getnames(FILE *fp);
82 + static void convert(FILE *fp);
83 + static int getstmt(char *av[MAXARG], FILE *fp);
84 + static char * getmtl(void);
85 + static char * getonm(void);
86 + static int matchrule(RULEHD *rp);
87 + static int cvtndx(VNDX vi, char *vs);
88 + static int nonplanar(int ac, char **av);
89 + static int putface(int ac, char **av);
90 + static int puttri(char *v1, char *v2, char *v3);
91 + static void freeverts(void);
92 + static int newv(double x, double y, double z);
93 + static int newvn(double x, double y, double z);
94 + static int newvt(double x, double y);
95 + static void syntax(char *er);
96  
97 < main(argc, argv)                /* read in .obj file and convert */
98 < int     argc;
99 < char    *argv[];
97 >
98 > int
99 > main(           /* read in .OBJ file and convert */
100 >        int     argc,
101 >        char    *argv[]
102 > )
103   {
104          int     donames = 0;
105          int     i;
# Line 99 | Line 115 | char   *argv[];
115                  case 'm':               /* use custom mapfile */
116                          ourmapping = getmapping(argv[++i], &qlist);
117                          break;
118 +                case 'f':               /* flatten surfaces */
119 +                        flatten++;
120 +                        break;
121                  default:
122                          goto userr;
123                  }
124 <        if (i > argc | i < argc-1)
124 >        if ((i > argc) | (i < argc-1))
125                  goto userr;
126          if (i == argc)
127                  inpfile = "<stdin>";
# Line 123 | Line 142 | char   *argv[];
142                  printargs(argc, argv, stdout);
143                  convert(stdin);
144          }
145 +        if (ndegen)
146 +                printf("# %d degenerate faces\n", ndegen);
147 +        if (n0norm)
148 +                printf("# %d invalid (zero) normals\n", n0norm);
149          exit(0);
150   userr:
151 <        fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n] [file.obj]\n",
151 >        fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n",
152                          argv[0]);
153          exit(1);
154   }
155  
156  
157 < getnames(fp)                    /* get valid qualifier names */
158 < FILE    *fp;
157 > void
158 > getnames(                       /* get valid qualifier names */
159 >        FILE    *fp
160 > )
161   {
162          char    *argv[MAXARG];
163          int     argc;
164          ID      tmpid;
165 <        register int    i;
165 >        int     i;
166  
167 <        while (argc = getstmt(argv, fp))
167 >        while ( (argc = getstmt(argv, fp)) )
168                  switch (argv[0][0]) {
169                  case 'f':                               /* face */
170                          if (!argv[0][1])
# Line 180 | Line 205 | FILE   *fp;
205   }
206  
207  
208 < convert(fp)                     /* convert a T-mesh */
209 < FILE    *fp;
208 > void
209 > convert(                        /* convert an OBJ stream */
210 >        FILE    *fp
211 > )
212   {
213          char    *argv[MAXARG];
214          int     argc;
215          int     nstats, nunknown;
216 <        register int    i;
216 >        int     i;
217  
218          nstats = nunknown = 0;
219                                          /* scan until EOF */
220 <        while (argc = getstmt(argv, fp)) {
220 >        while ( (argc = getstmt(argv, fp)) ) {
221                  switch (argv[0][0]) {
222                  case 'v':               /* vertex */
223                          switch (argv[0][1]) {
# Line 232 | Line 259 | FILE   *fp;
259                                  if (!puttri(argv[1], argv[2], argv[3]))
260                                          syntax("Bad triangle");
261                                  break;
235                        case 4:
236                                if (!putquad(argv[1], argv[2],
237                                                argv[3], argv[4]))
238                                        syntax("Bad quad");
239                                break;
262                          default:
263                                  if (!putface(argc-1, argv+1))
264                                          syntax("Bad face");
# Line 254 | Line 276 | FILE   *fp;
276                                  if (!strcmp(argv[1], "off"))
277                                          mapname[0] = '\0';
278                                  else
279 <                                        strcpy(mapname, argv[1]);
279 >                                        sprintf(mapname, "%s.hdr", argv[1]);
280                          } else
281                                  goto unknown;
282                          break;
# Line 270 | Line 292 | FILE   *fp;
292                                  goto unknown;
293                          for (i = 1; i < argc; i++)
294                                  strcpy(group[i-1], argv[i]);
295 <                        group[i-1][0] = '\0';
295 >                        group[argc-1][0] = '\0';
296                          break;
297                  case '#':               /* comment */
298 +                        printargs(argc, argv, stdout);
299                          break;
300                  default:;               /* something we don't deal with */
301                  unknown:
# Line 288 | Line 311 | FILE   *fp;
311  
312  
313   int
314 < getstmt(av, fp)                         /* read the next statement from fp */
315 < register char   *av[MAXARG];
316 < FILE    *fp;
314 > getstmt(                                /* read the next statement from fp */
315 >        char    *av[MAXARG],
316 >        FILE    *fp
317 > )
318   {
319 <        extern char     *fgetline();
320 <        static char     sbuf[MAXARG*10];
321 <        register char   *cp;
298 <        register int    i;
319 >        static char     sbuf[MAXARG*16];
320 >        char    *cp;
321 >        int     i;
322  
323          do {
324                  if (fgetline(cp=sbuf, sizeof(sbuf), fp) == NULL)
# Line 307 | Line 330 | FILE   *fp;
330                                          lineno++;
331                                  *cp++ = '\0';
332                          }
333 <                        if (!*cp || i >= MAXARG-1)
333 >                        if (!*cp)
334                                  break;
335 +                        if (i >= MAXARG-1) {
336 +                                fprintf(stderr,
337 +                        "warning: line %d: too many arguments (limit %d)\n",
338 +                                        lineno+1, MAXARG-1);
339 +                                break;
340 +                        }
341                          av[i++] = cp;
342                          while (*++cp && !isspace(*cp))
343                                  ;
# Line 322 | Line 351 | FILE   *fp;
351  
352  
353   char *
354 < getmtl()                                /* figure material for this face */
354 > getmtl(void)                            /* figure material for this face */
355   {
356 <        register RULEHD *rp = ourmapping;
356 >        RULEHD  *rp = ourmapping;
357  
358          if (rp == NULL) {               /* no rule set */
359                  if (matname[0])
# Line 348 | Line 377 | getmtl()                               /* figure material for this face */
377  
378  
379   char *
380 < getonm()                                /* invent a good name for object */
380 > getonm(void)                            /* invent a good name for object */
381   {
382          static char     name[64];
383 <        register char   *cp1, *cp2;
384 <        register int    i;
383 >        char    *cp1, *cp2;
384 >        int     i;
385                                          /* check for preset */
386          if (objname[0])
387                  return(objname);
# Line 363 | Line 392 | getonm()                               /* invent a good name for object */
392                  cp2 = group[i];
393                  if (cp1 > name)
394                          *cp1++ = '.';
395 <                while (*cp1 = *cp2++)
395 >                while ( (*cp1 = *cp2++) )
396                          if (++cp1 >= name+sizeof(name)-2) {
397                                  *cp1 = '\0';
398                                  return(name);
# Line 373 | Line 402 | getonm()                               /* invent a good name for object */
402   }
403  
404  
405 < matchrule(rp)                           /* check for a match on this rule */
406 < register RULEHD *rp;
405 > int
406 > matchrule(                              /* check for a match on this rule */
407 >        RULEHD  *rp
408 > )
409   {
410          ID      tmpid;
411          int     gotmatch;
412 <        register int    i;
412 >        int     i;
413  
414          if (rp->qflg & FL(Q_MTL)) {
415                  if (!matname[0])
# Line 424 | Line 455 | register RULEHD        *rp;
455   }
456  
457  
458 < cvtndx(vi, vs)                          /* convert vertex string to index */
459 < register VNDX   vi;
460 < register char   *vs;
458 > int
459 > cvtndx(                         /* convert vertex string to index */
460 >        VNDX    vi,
461 >        char    *vs
462 > )
463   {
464                                          /* get point */
465          vi[0] = atoi(vs);
# Line 434 | Line 467 | register char  *vs;
467                  if (vi[0]-- > nvs)
468                          return(0);
469          } else if (vi[0] < 0) {
470 <                vi[0] = nvs + vi[0];
470 >                vi[0] += nvs;
471                  if (vi[0] < 0)
472                          return(0);
473          } else
# Line 448 | Line 481 | register char  *vs;
481                  if (vi[1]-- > nvts)
482                          return(0);
483          } else if (vi[1] < 0) {
484 <                vi[1] = nvts + vi[1];
484 >                vi[1] += nvts;
485                  if (vi[1] < 0)
486                          return(0);
487          } else
# Line 462 | Line 495 | register char  *vs;
495                  if (vi[2]-- > nvns)
496                          return(0);
497          } else if (vi[2] < 0) {
498 <                vi[2] = nvns + vi[2];
498 >                vi[2] += nvns;
499                  if (vi[2] < 0)
500                          return(0);
501          } else
502                  vi[2] = -1;
503 +                                        /* zero normal is not normal */
504 +        if (vi[2] >= 0 && DOT(vnlist[vi[2]],vnlist[vi[2]]) <= FTINY)
505 +                vi[2] = -1;
506          return(1);
507   }
508  
509  
510 < nonplanar(ac, av)                       /* are vertices are non-planar? */
511 < register int    ac;
512 < register char   **av;
510 > int
511 > nonplanar(                      /* are vertices non-planar? */
512 >        int     ac,
513 >        char    **av
514 > )
515   {
516          VNDX    vi;
517 <        FLOAT   *p0, *p1;
517 >        RREAL   *p0, *p1;
518          FVECT   v1, v2, nsum, newn;
519          double  d;
520 <        register int    i;
520 >        int     i;
521  
522          if (!cvtndx(vi, av[0]))
523                  return(0);
524 <        if (vi[2] >= 0)
524 >        if (!flatten && vi[2] >= 0)
525                  return(1);              /* has interpolated normals */
526          if (ac < 4)
527                  return(0);              /* it's a triangle! */
# Line 522 | Line 560 | register char  **av;
560   }
561  
562  
563 < putface(ac, av)                         /* put out an N-sided polygon */
564 < int     ac;
565 < register char   **av;
563 > int
564 > putface(                                /* put out an N-sided polygon */
565 >        int     ac,
566 >        char    **av
567 > )
568   {
569          VNDX    vi;
570 <        char    *mod;
571 <        register int    i;
570 >        char    *cp;
571 >        int     i;
572  
573 <        if (nonplanar(ac, av)) {        /* break into quads and triangles */
574 <                while (ac > 3) {
575 <                        if (!putquad(av[0], av[1], av[2], av[3]))
573 >        if (nonplanar(ac, av)) {        /* break into triangles */
574 >                while (ac > 2) {
575 >                        if (!puttri(av[0], av[1], av[2]))
576                                  return(0);
577 <                                        /* remove two vertices & rotate */
578 <                        av[ac -= 2] = av[0];
579 <                        for (i = 0; i <= ac; i++)
577 >                        ac--;           /* remove vertex & rotate */
578 >                        cp = av[0];
579 >                        for (i = 0; i < ac-1; i++)
580                                  av[i] = av[i+2];
581 +                        av[i] = cp;
582                  }
542                if (ac == 3 && !puttri(av[0], av[1], av[2]))
543                        return(0);
583                  return(1);
584          }
585 <        if ((mod = getmtl()) == NULL)
585 >        if ((cp = getmtl()) == NULL)
586                  return(-1);
587 <        printf("\n%s polygon %s.%d\n", mod, getonm(), faceno);
587 >        printf("\n%s polygon %s.%d\n", cp, getonm(), faceno);
588          printf("0\n0\n%d\n", 3*ac);
589          for (i = 0; i < ac; i++) {
590                  if (!cvtndx(vi, av[i]))
# Line 556 | Line 595 | register char  **av;
595   }
596  
597  
598 < puttri(v1, v2, v3)                      /* put out a triangle */
599 < char    *v1, *v2, *v3;
598 > int
599 > puttri(                 /* put out a triangle */
600 >        char *v1,
601 >        char *v2,
602 >        char *v3
603 > )
604   {
605          char    *mod;
606          VNDX    v1i, v2i, v3i;
607          BARYCCM bvecs;
608 <        int     texOK, patOK;
608 >        RREAL   bcoor[3][3];
609 >        int     texOK = 0, patOK;
610 >        int     flatness;
611 >        int     i;
612  
613          if ((mod = getmtl()) == NULL)
614                  return(-1);
# Line 570 | Line 616 | char   *v1, *v2, *v3;
616          if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3))
617                  return(0);
618                                          /* compute barycentric coordinates */
619 <        texOK = (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0);
619 >        if (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0)
620 >                flatness = flat_tri(vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]],
621 >                                vnlist[v1i[2]], vnlist[v2i[2]], vnlist[v3i[2]]);
622 >        else
623 >                flatness = ISFLAT;
624 >
625 >        switch (flatness) {
626 >        case DEGEN:                     /* zero area */
627 >                ndegen++;
628 >                return(-1);
629 >        case RVFLAT:                    /* reversed normals, but flat */
630 >        case ISFLAT:                    /* smoothing unnecessary */
631 >                texOK = 0;
632 >                break;
633 >        case RVBENT:                    /* reversed normals with smoothing */
634 >        case ISBENT:                    /* proper smoothing */
635 >                texOK = 1;
636 >                break;
637 >        }
638 >        if (flatten)
639 >                texOK = 0;
640 > #ifdef TEXMAPS
641          patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0);
642 + #else
643 +        patOK = 0;
644 + #endif
645          if (texOK | patOK)
646 <                if (comp_baryc(bvecs, vlist[v1i[0]], vlist[v2i[0]],
646 >                if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]],
647                                  vlist[v3i[0]]) < 0)
648 <                        return(-1);
648 >                        texOK = patOK = 0;
649                                          /* put out texture (if any) */
650          if (texOK) {
651                  printf("\n%s texfunc %s\n", mod, TEXNAME);
652                  mod = TEXNAME;
653                  printf("4 dx dy dz %s\n", TCALNAME);
654 <                printf("0\n21\n");
655 <                put_baryc(bvecs);
656 <                printf("\t%14.12g %14.12g %14.12g\n",
657 <                                vnlist[v1i[2]][0], vnlist[v2i[2]][0],
658 <                                vnlist[v3i[2]][0]);
659 <                printf("\t%14.12g %14.12g %14.12g\n",
660 <                                vnlist[v1i[2]][1], vnlist[v2i[2]][1],
591 <                                vnlist[v3i[2]][1]);
592 <                printf("\t%14.12g %14.12g %14.12g\n",
593 <                                vnlist[v1i[2]][2], vnlist[v2i[2]][2],
594 <                                vnlist[v3i[2]][2]);
654 >                printf("0\n");
655 >                for (i = 0; i < 3; i++) {
656 >                        bcoor[i][0] = vnlist[v1i[2]][i];
657 >                        bcoor[i][1] = vnlist[v2i[2]][i];
658 >                        bcoor[i][2] = vnlist[v3i[2]][i];
659 >                }
660 >                fput_baryc(&bvecs, bcoor, 3, stdout);
661          }
662 + #ifdef TEXMAPS
663                                          /* put out pattern (if any) */
664          if (patOK) {
665                  printf("\n%s colorpict %s\n", mod, PATNAME);
666                  mod = PATNAME;
667                  printf("7 noneg noneg noneg %s %s u v\n", mapname, TCALNAME);
668 <                printf("0\n18\n");
669 <                put_baryc(bvecs);
670 <                printf("\t%f %f %f\n", vtlist[v1i[1]][0],
671 <                                vtlist[v2i[1]][0], vtlist[v3i[1]][0]);
672 <                printf("\t%f %f %f\n", vtlist[v1i[1]][1],
673 <                                vtlist[v2i[1]][1], vtlist[v3i[1]][1]);
668 >                printf("0\n");
669 >                for (i = 0; i < 2; i++) {
670 >                        bcoor[i][0] = vtlist[v1i[1]][i];
671 >                        bcoor[i][1] = vtlist[v2i[1]][i];
672 >                        bcoor[i][2] = vtlist[v3i[1]][i];
673 >                }
674 >                fput_baryc(&bvecs, bcoor, 2, stdout);
675          }
676 <                                        /* put out triangle */
676 > #endif
677 >                                        /* put out (reversed) triangle */
678          printf("\n%s polygon %s.%d\n", mod, getonm(), faceno);
679          printf("0\n0\n9\n");
680 <        pvect(vlist[v1i[0]]);
681 <        pvect(vlist[v2i[0]]);
682 <        pvect(vlist[v3i[0]]);
683 <
684 <        return(1);
685 < }
686 <
687 <
619 < int
620 < comp_baryc(bcm, v1, v2, v3)             /* compute barycentric vectors */
621 < register BARYCCM        bcm;
622 < FLOAT   *v1, *v2, *v3;
623 < {
624 <        FLOAT   *vt;
625 <        FVECT   va, vab, vcb;
626 <        double  d;
627 <        register int    i, j;
628 <
629 <        for (j = 0; j < 3; j++) {
630 <                for (i = 0; i < 3; i++) {
631 <                        vab[i] = v1[i] - v2[i];
632 <                        vcb[i] = v3[i] - v2[i];
633 <                }
634 <                d = DOT(vcb,vcb);
635 <                if (d <= FTINY)
636 <                        return(-1);
637 <                d = DOT(vcb,vab)/d;
638 <                for (i = 0; i < 3; i++)
639 <                        va[i] = vab[i] - vcb[i]*d;
640 <                d = DOT(va,va);
641 <                if (d <= FTINY)
642 <                        return(-1);
643 <                for (i = 0; i < 3; i++) {
644 <                        va[i] /= d;
645 <                        bcm[j][i] = va[i];
646 <                }
647 <                bcm[j][3] = -DOT(v2,va);
648 <                                        /* rotate vertices */
649 <                vt = v1;
650 <                v1 = v2;
651 <                v2 = v3;
652 <                v3 = vt;
680 >        if (flatness == RVFLAT || flatness == RVBENT) {
681 >                pvect(vlist[v3i[0]]);
682 >                pvect(vlist[v2i[0]]);
683 >                pvect(vlist[v1i[0]]);
684 >        } else {
685 >                pvect(vlist[v1i[0]]);
686 >                pvect(vlist[v2i[0]]);
687 >                pvect(vlist[v3i[0]]);
688          }
654        return(0);
655 }
656
657
658 put_baryc(bcm)                          /* put barycentric coord. vectors */
659 register BARYCCM        bcm;
660 {
661        register int    i;
662
663        for (i = 0; i < 3; i++)
664                printf("%14.8f %14.8f %14.8f %14.8f\n",
665                                bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]);
666 }
667
668
669 putquad(p0, p1, p3, p2)                 /* put out a quadrilateral */
670 char  *p0, *p1, *p3, *p2;               /* names correspond to binary pos. */
671 {
672        VNDX  p0i, p1i, p2i, p3i;
673        FVECT  norm[4];
674        char  *mod, *name;
675        int  axis;
676        FVECT  v1, v2, vc1, vc2;
677        int  ok1, ok2;
678
679        if ((mod = getmtl()) == NULL)
680                return(-1);
681        name = getonm();
682                                        /* get actual indices */
683        if (!cvtndx(p0i,p0) || !cvtndx(p1i,p1) ||
684                        !cvtndx(p2i,p2) || !cvtndx(p3i,p3))
685                return(0);
686                                        /* compute exact normals */
687        fvsum(v1, vlist[p1i[0]], vlist[p0i[0]], -1.0);
688        fvsum(v2, vlist[p2i[0]], vlist[p0i[0]], -1.0);
689        fcross(vc1, v1, v2);
690        ok1 = normalize(vc1) != 0.0;
691        fvsum(v1, vlist[p2i[0]], vlist[p3i[0]], -1.0);
692        fvsum(v2, vlist[p1i[0]], vlist[p3i[0]], -1.0);
693        fcross(vc2, v1, v2);
694        ok2 = normalize(vc2) != 0.0;
695        if (!(ok1 | ok2))
696                return(-1);
697                                        /* compute normal interpolation */
698        axis = norminterp(norm, p0i, p1i, p2i, p3i);
699
700                                        /* put out quadrilateral? */
701        if (ok1 & ok2 && fabs(fdot(vc1,vc2)) >= 1.0-FTINY) {
702                printf("\n%s ", mod);
703                if (axis != -1) {
704                        printf("texfunc %s\n", TEXNAME);
705                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
706                        printf("0\n13\t%d\n", axis);
707                        pvect(norm[0]);
708                        pvect(norm[1]);
709                        pvect(norm[2]);
710                        fvsum(v1, norm[3], vc1, -0.5);
711                        fvsum(v1, v1, vc2, -0.5);
712                        pvect(v1);
713                        printf("\n%s ", TEXNAME);
714                }
715                printf("polygon %s.%d\n", name, faceno);
716                printf("0\n0\n12\n");
717                pvect(vlist[p0i[0]]);
718                pvect(vlist[p1i[0]]);
719                pvect(vlist[p3i[0]]);
720                pvect(vlist[p2i[0]]);
721                return(1);
722        }
723                                        /* put out triangles? */
724        if (ok1) {
725                printf("\n%s ", mod);
726                if (axis != -1) {
727                        printf("texfunc %s\n", TEXNAME);
728                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
729                        printf("0\n13\t%d\n", axis);
730                        pvect(norm[0]);
731                        pvect(norm[1]);
732                        pvect(norm[2]);
733                        fvsum(v1, norm[3], vc1, -1.0);
734                        pvect(v1);
735                        printf("\n%s ", TEXNAME);
736                }
737                printf("polygon %s.%da\n", name, faceno);
738                printf("0\n0\n9\n");
739                pvect(vlist[p0i[0]]);
740                pvect(vlist[p1i[0]]);
741                pvect(vlist[p2i[0]]);
742        }
743        if (ok2) {
744                printf("\n%s ", mod);
745                if (axis != -1) {
746                        printf("texfunc %s\n", TEXNAME);
747                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
748                        printf("0\n13\t%d\n", axis);
749                        pvect(norm[0]);
750                        pvect(norm[1]);
751                        pvect(norm[2]);
752                        fvsum(v2, norm[3], vc2, -1.0);
753                        pvect(v2);
754                        printf("\n%s ", TEXNAME);
755                }
756                printf("polygon %s.%db\n", name, faceno);
757                printf("0\n0\n9\n");
758                pvect(vlist[p2i[0]]);
759                pvect(vlist[p1i[0]]);
760                pvect(vlist[p3i[0]]);
761        }
689          return(1);
690   }
691  
692  
693 < int
694 < norminterp(resmat, p0i, p1i, p2i, p3i)  /* compute normal interpolation */
768 < register FVECT  resmat[4];
769 < register VNDX  p0i, p1i, p2i, p3i;
693 > void
694 > freeverts(void)                 /* free all vertices */
695   {
771 #define u  ((ax+1)%3)
772 #define v  ((ax+2)%3)
773
774        register int  ax;
775        MAT4  eqnmat;
776        FVECT  v1;
777        register int  i, j;
778
779        if (!(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0))
780                return(-1);
781                                        /* find dominant axis */
782        VCOPY(v1, vnlist[p0i[2]]);
783        fvsum(v1, v1, vnlist[p1i[2]], 1.0);
784        fvsum(v1, v1, vnlist[p2i[2]], 1.0);
785        fvsum(v1, v1, vnlist[p3i[2]], 1.0);
786        ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1;
787        ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2;
788                                        /* assign equation matrix */
789        eqnmat[0][0] = vlist[p0i[0]][u]*vlist[p0i[0]][v];
790        eqnmat[0][1] = vlist[p0i[0]][u];
791        eqnmat[0][2] = vlist[p0i[0]][v];
792        eqnmat[0][3] = 1.0;
793        eqnmat[1][0] = vlist[p1i[0]][u]*vlist[p1i[0]][v];
794        eqnmat[1][1] = vlist[p1i[0]][u];
795        eqnmat[1][2] = vlist[p1i[0]][v];
796        eqnmat[1][3] = 1.0;
797        eqnmat[2][0] = vlist[p2i[0]][u]*vlist[p2i[0]][v];
798        eqnmat[2][1] = vlist[p2i[0]][u];
799        eqnmat[2][2] = vlist[p2i[0]][v];
800        eqnmat[2][3] = 1.0;
801        eqnmat[3][0] = vlist[p3i[0]][u]*vlist[p3i[0]][v];
802        eqnmat[3][1] = vlist[p3i[0]][u];
803        eqnmat[3][2] = vlist[p3i[0]][v];
804        eqnmat[3][3] = 1.0;
805                                        /* invert matrix (solve system) */
806        if (!invmat4(eqnmat, eqnmat))
807                return(-1);                     /* no solution */
808                                        /* compute result matrix */
809        for (j = 0; j < 4; j++)
810                for (i = 0; i < 3; i++)
811                        resmat[j][i] =  eqnmat[j][0]*vnlist[p0i[2]][i] +
812                                        eqnmat[j][1]*vnlist[p1i[2]][i] +
813                                        eqnmat[j][2]*vnlist[p2i[2]][i] +
814                                        eqnmat[j][3]*vnlist[p3i[2]][i];
815        return(ax);
816
817 #undef u
818 #undef v
819 }
820
821
822 freeverts()                     /* free all vertices */
823 {
696          if (nvs) {
697 <                free((char *)vlist);
697 >                free((void *)vlist);
698                  nvs = 0;
699          }
700          if (nvts) {
701 <                free((char *)vtlist);
701 >                free((void *)vtlist);
702                  nvts = 0;
703          }
704          if (nvns) {
705 <                free((char *)vnlist);
705 >                free((void *)vnlist);
706                  nvns = 0;
707          }
708   }
709  
710  
711   int
712 < newv(x, y, z)                   /* create a new vertex */
713 < double  x, y, z;
712 > newv(                   /* create a new vertex */
713 >        double  x,
714 >        double  y,
715 >        double  z
716 > )
717   {
718          if (!(nvs%CHUNKSIZ)) {          /* allocate next block */
719                  if (nvs == 0)
720                          vlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT));
721                  else
722 <                        vlist = (FVECT *)realloc((char *)vlist,
722 >                        vlist = (FVECT *)realloc((void *)vlist,
723                                          (nvs+CHUNKSIZ)*sizeof(FVECT));
724                  if (vlist == NULL) {
725                          fprintf(stderr,
# Line 861 | Line 736 | double x, y, z;
736  
737  
738   int
739 < newvn(x, y, z)                  /* create a new vertex normal */
740 < double  x, y, z;
739 > newvn(                  /* create a new vertex normal */
740 >        double  x,
741 >        double  y,
742 >        double  z
743 > )
744   {
745          if (!(nvns%CHUNKSIZ)) {         /* allocate next block */
746                  if (nvns == 0)
747                          vnlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT));
748                  else
749 <                        vnlist = (FVECT *)realloc((char *)vnlist,
749 >                        vnlist = (FVECT *)realloc((void *)vnlist,
750                                          (nvns+CHUNKSIZ)*sizeof(FVECT));
751                  if (vnlist == NULL) {
752                          fprintf(stderr,
# Line 880 | Line 758 | double x, y, z;
758          vnlist[nvns][0] = x;
759          vnlist[nvns][1] = y;
760          vnlist[nvns][2] = z;
761 <        if (normalize(vnlist[nvns]) == 0.0)
884 <                return(0);
761 >        n0norm += (normalize(vnlist[nvns]) == 0.0);
762          return(++nvns);
763   }
764  
765  
766   int
767 < newvt(x, y)                     /* create a new texture map vertex */
768 < double  x, y;
767 > newvt(                  /* create a new texture map vertex */
768 >        double  x,
769 >        double  y
770 > )
771   {
772          if (!(nvts%CHUNKSIZ)) {         /* allocate next block */
773                  if (nvts == 0)
774 <                        vtlist = (FLOAT (*)[2])malloc(CHUNKSIZ*2*sizeof(FLOAT));
774 >                        vtlist = (RREAL (*)[2])malloc(CHUNKSIZ*2*sizeof(RREAL));
775                  else
776 <                        vtlist = (FLOAT (*)[2])realloc((char *)vtlist,
777 <                                        (nvts+CHUNKSIZ)*2*sizeof(FLOAT));
776 >                        vtlist = (RREAL (*)[2])realloc((void *)vtlist,
777 >                                        (nvts+CHUNKSIZ)*2*sizeof(RREAL));
778                  if (vtlist == NULL) {
779                          fprintf(stderr,
780                          "Out of memory while allocating texture vertex %d\n",
# Line 910 | Line 789 | double x, y;
789   }
790  
791  
792 < syntax(er)                      /* report syntax error and exit */
793 < char    *er;
792 > void
793 > syntax(                 /* report syntax error and exit */
794 >        char    *er
795 > )
796   {
797          fprintf(stderr, "%s: Wavefront syntax error near line %d: %s\n",
798                          inpfile, lineno, er);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines