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.10 by greg, Wed Jun 15 12:29:55 1994 UTC vs.
Revision 2.29 by greg, Fri Nov 8 16:49:04 2013 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.
# Line 13 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
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  
38 < typedef struct {
39 <        int     ax;             /* major axis */
42 <        FLOAT   tm[2][3];       /* transformation */
43 < } BARYCCM;
38 > int     ndegen = 0;             /* count of degenerate faces */
39 > int     n0norm = 0;             /* count of zero normals */
40  
41   typedef int     VNDX[3];        /* vertex index (point,map,normal) */
42  
43 < #define CHUNKSIZ        256     /* vertex allocation chunk size */
43 > #define CHUNKSIZ        1024    /* vertex allocation chunk size */
44  
45 < #define MAXARG          64      /* maximum # arguments in a statement */
45 > #define MAXARG          512     /* maximum # arguments in a statement */
46  
47                                  /* qualifiers */
48   #define Q_MTL           0
# Line 75 | Line 71 | char   *defobj = DEFOBJ;       /* default (starting) object na
71  
72   int     flatten = 0;            /* discard surface normal information */
73  
74 < char    *getmtl(), *getonm();
75 <
76 < char    mapname[128];           /* current picture file */
77 < char    matname[64];            /* current material name */
82 < char    group[16][32];          /* current group names */
83 < char    objname[128];           /* current object name */
74 > char    mapname[256];           /* current picture file */
75 > char    matname[256];           /* current material name */
76 > char    group[8][256];          /* current group name(s) */
77 > char    objname[256];           /* current object name */
78   char    *inpfile;               /* input file name */
79   int     lineno;                 /* current line number */
80   int     faceno;                 /* current face number */
81  
82 + static void getnames(FILE *fp);
83 + static void convert(FILE *fp);
84 + static int getstmt(char *av[MAXARG], FILE *fp);
85 + static char * getmtl(void);
86 + static char * getonm(void);
87 + static int matchrule(RULEHD *rp);
88 + static int cvtndx(VNDX vi, char *vs);
89 + static int nonplanar(int ac, char **av);
90 + static int putface(int ac, char **av);
91 + static int puttri(char *v1, char *v2, char *v3);
92 + static void freeverts(void);
93 + static int newv(double x, double y, double z);
94 + static int newvn(double x, double y, double z);
95 + static int newvt(double x, double y);
96 + static void syntax(char *er);
97  
98 < main(argc, argv)                /* read in .obj file and convert */
99 < int     argc;
100 < char    *argv[];
98 >
99 > int
100 > main(           /* read in .OBJ file and convert */
101 >        int     argc,
102 >        char    *argv[]
103 > )
104   {
105          int     donames = 0;
106          int     i;
# Line 110 | Line 122 | char   *argv[];
122                  default:
123                          goto userr;
124                  }
125 <        if (i > argc | i < argc-1)
125 >        if ((i > argc) | (i < argc-1))
126                  goto userr;
127          if (i == argc)
128                  inpfile = "<stdin>";
# Line 131 | Line 143 | char   *argv[];
143                  printargs(argc, argv, stdout);
144                  convert(stdin);
145          }
146 +        if (ndegen)
147 +                printf("# %d degenerate faces\n", ndegen);
148 +        if (n0norm)
149 +                printf("# %d invalid (zero) normals\n", n0norm);
150          exit(0);
151   userr:
152          fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n",
# Line 139 | Line 155 | userr:
155   }
156  
157  
158 < getnames(fp)                    /* get valid qualifier names */
159 < FILE    *fp;
158 > void
159 > getnames(                       /* get valid qualifier names */
160 >        FILE    *fp
161 > )
162   {
163          char    *argv[MAXARG];
164          int     argc;
165          ID      tmpid;
166 <        register int    i;
166 >        int     i;
167  
168 <        while (argc = getstmt(argv, fp))
168 >        while ( (argc = getstmt(argv, fp)) )
169                  switch (argv[0][0]) {
170                  case 'f':                               /* face */
171                          if (!argv[0][1])
# Line 188 | Line 206 | FILE   *fp;
206   }
207  
208  
209 < convert(fp)                     /* convert a T-mesh */
210 < FILE    *fp;
209 > void
210 > convert(                        /* convert an OBJ stream */
211 >        FILE    *fp
212 > )
213   {
214          char    *argv[MAXARG];
215          int     argc;
216          int     nstats, nunknown;
217 <        register int    i;
217 >        int     i;
218  
219          nstats = nunknown = 0;
220                                          /* scan until EOF */
221 <        while (argc = getstmt(argv, fp)) {
221 >        while ( (argc = getstmt(argv, fp)) ) {
222                  switch (argv[0][0]) {
223                  case 'v':               /* vertex */
224                          switch (argv[0][1]) {
# Line 240 | Line 260 | FILE   *fp;
260                                  if (!puttri(argv[1], argv[2], argv[3]))
261                                          syntax("Bad triangle");
262                                  break;
243                        case 4:
244                                if (!putquad(argv[1], argv[2],
245                                                argv[3], argv[4]))
246                                        syntax("Bad quad");
247                                break;
263                          default:
264                                  if (!putface(argc-1, argv+1))
265                                          syntax("Bad face");
# Line 262 | Line 277 | FILE   *fp;
277                                  if (!strcmp(argv[1], "off"))
278                                          mapname[0] = '\0';
279                                  else
280 <                                        sprintf(mapname, "%s.pic", argv[1]);
280 >                                        sprintf(mapname, "%s.hdr", argv[1]);
281                          } else
282                                  goto unknown;
283                          break;
# Line 278 | Line 293 | FILE   *fp;
293                                  goto unknown;
294                          for (i = 1; i < argc; i++)
295                                  strcpy(group[i-1], argv[i]);
296 <                        group[i-1][0] = '\0';
296 >                        group[argc-1][0] = '\0';
297                          break;
298                  case '#':               /* comment */
299                          printargs(argc, argv, stdout);
# Line 297 | Line 312 | FILE   *fp;
312  
313  
314   int
315 < getstmt(av, fp)                         /* read the next statement from fp */
316 < register char   *av[MAXARG];
317 < FILE    *fp;
315 > getstmt(                                /* read the next statement from fp */
316 >        char    *av[MAXARG],
317 >        FILE    *fp
318 > )
319   {
320 <        extern char     *fgetline();
321 <        static char     sbuf[MAXARG*10];
322 <        register char   *cp;
307 <        register int    i;
320 >        static char     sbuf[MAXARG*16];
321 >        char    *cp;
322 >        int     i;
323  
324          do {
325                  if (fgetline(cp=sbuf, sizeof(sbuf), fp) == NULL)
# Line 316 | Line 331 | FILE   *fp;
331                                          lineno++;
332                                  *cp++ = '\0';
333                          }
334 <                        if (!*cp || i >= MAXARG-1)
334 >                        if (!*cp)
335                                  break;
336 +                        if (i >= MAXARG-1) {
337 +                                fprintf(stderr,
338 +                        "warning: line %d: too many arguments (limit %d)\n",
339 +                                        lineno+1, MAXARG-1);
340 +                                break;
341 +                        }
342                          av[i++] = cp;
343                          while (*++cp && !isspace(*cp))
344                                  ;
# Line 331 | Line 352 | FILE   *fp;
352  
353  
354   char *
355 < getmtl()                                /* figure material for this face */
355 > getmtl(void)                            /* figure material for this face */
356   {
357 <        register RULEHD *rp = ourmapping;
357 >        RULEHD  *rp = ourmapping;
358  
359          if (rp == NULL) {               /* no rule set */
360                  if (matname[0])
# Line 357 | Line 378 | getmtl()                               /* figure material for this face */
378  
379  
380   char *
381 < getonm()                                /* invent a good name for object */
381 > getonm(void)                            /* invent a good name for object */
382   {
383          static char     name[64];
384 <        register char   *cp1, *cp2;
385 <        register int    i;
384 >        char    *cp1, *cp2;
385 >        int     i;
386                                          /* check for preset */
387          if (objname[0])
388                  return(objname);
# Line 372 | Line 393 | getonm()                               /* invent a good name for object */
393                  cp2 = group[i];
394                  if (cp1 > name)
395                          *cp1++ = '.';
396 <                while (*cp1 = *cp2++)
396 >                while ( (*cp1 = *cp2++) )
397                          if (++cp1 >= name+sizeof(name)-2) {
398                                  *cp1 = '\0';
399                                  return(name);
# Line 382 | Line 403 | getonm()                               /* invent a good name for object */
403   }
404  
405  
406 < matchrule(rp)                           /* check for a match on this rule */
407 < register RULEHD *rp;
406 > int
407 > matchrule(                              /* check for a match on this rule */
408 >        RULEHD  *rp
409 > )
410   {
411          ID      tmpid;
412          int     gotmatch;
413 <        register int    i;
413 >        int     i;
414  
415          if (rp->qflg & FL(Q_MTL)) {
416                  if (!matname[0])
# Line 433 | Line 456 | register RULEHD        *rp;
456   }
457  
458  
459 < cvtndx(vi, vs)                          /* convert vertex string to index */
460 < register VNDX   vi;
461 < register char   *vs;
459 > int
460 > cvtndx(                         /* convert vertex string to index */
461 >        VNDX    vi,
462 >        char    *vs
463 > )
464   {
465                                          /* get point */
466          vi[0] = atoi(vs);
# Line 476 | Line 501 | register char  *vs;
501                          return(0);
502          } else
503                  vi[2] = -1;
504 +                                        /* zero normal is not normal */
505 +        if (vi[2] >= 0 && DOT(vnlist[vi[2]],vnlist[vi[2]]) <= FTINY)
506 +                vi[2] = -1;
507          return(1);
508   }
509  
510  
511 < nonplanar(ac, av)                       /* are vertices non-planar? */
512 < register int    ac;
513 < register char   **av;
511 > int
512 > nonplanar(                      /* are vertices non-planar? */
513 >        int     ac,
514 >        char    **av
515 > )
516   {
517          VNDX    vi;
518 <        FLOAT   *p0, *p1;
518 >        RREAL   *p0, *p1;
519          FVECT   v1, v2, nsum, newn;
520          double  d;
521 <        register int    i;
521 >        int     i;
522  
523          if (!cvtndx(vi, av[0]))
524                  return(0);
525 <        if (vi[2] >= 0)
525 >        if (!flatten && vi[2] >= 0)
526                  return(1);              /* has interpolated normals */
527          if (ac < 4)
528                  return(0);              /* it's a triangle! */
# Line 531 | Line 561 | register char  **av;
561   }
562  
563  
564 < putface(ac, av)                         /* put out an N-sided polygon */
565 < int     ac;
566 < register char   **av;
564 > int
565 > putface(                                /* put out an N-sided polygon */
566 >        int     ac,
567 >        char    **av
568 > )
569   {
570          VNDX    vi;
571          char    *cp;
572 <        register int    i;
572 >        int     i;
573  
574 <        if (nonplanar(ac, av)) {        /* break into quads and triangles */
575 <                while (ac > 3) {
576 <                        if (!putquad(av[0], av[1], av[2], av[3]))
574 >        if (nonplanar(ac, av)) {        /* break into triangles */
575 >                while (ac > 2) {
576 >                        if (!puttri(av[0], av[1], av[2]))
577                                  return(0);
578 <                        ac -= 2;        /* remove two vertices & rotate */
578 >                        ac--;           /* remove vertex & rotate */
579                          cp = av[0];
580                          for (i = 0; i < ac-1; i++)
581 <                                av[i] = av[i+3];
581 >                                av[i] = av[i+2];
582                          av[i] = cp;
583                  }
552                if (ac == 3 && !puttri(av[0], av[1], av[2]))
553                        return(0);
584                  return(1);
585          }
586          if ((cp = getmtl()) == NULL)
# Line 566 | Line 596 | register char  **av;
596   }
597  
598  
599 < puttri(v1, v2, v3)                      /* put out a triangle */
600 < char    *v1, *v2, *v3;
599 > int
600 > puttri(                 /* put out a triangle */
601 >        char *v1,
602 >        char *v2,
603 >        char *v3
604 > )
605   {
606          char    *mod;
607          VNDX    v1i, v2i, v3i;
608          BARYCCM bvecs;
609 <        int     texOK, patOK;
609 >        RREAL   bcoor[3][3];
610 >        int     texOK = 0, patOK;
611 >        int     flatness;
612 >        int     i;
613  
614          if ((mod = getmtl()) == NULL)
615                  return(-1);
# Line 580 | Line 617 | char   *v1, *v2, *v3;
617          if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3))
618                  return(0);
619                                          /* compute barycentric coordinates */
620 <        texOK = !flatten && (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0);
620 >        if (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0)
621 >                flatness = flat_tri(vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]],
622 >                                vnlist[v1i[2]], vnlist[v2i[2]], vnlist[v3i[2]]);
623 >        else
624 >                flatness = ISFLAT;
625 >
626 >        switch (flatness) {
627 >        case DEGEN:                     /* zero area */
628 >                ndegen++;
629 >                return(-1);
630 >        case RVFLAT:                    /* reversed normals, but flat */
631 >        case ISFLAT:                    /* smoothing unnecessary */
632 >                texOK = 0;
633 >                break;
634 >        case RVBENT:                    /* reversed normals with smoothing */
635 >        case ISBENT:                    /* proper smoothing */
636 >                texOK = 1;
637 >                break;
638 >        }
639 >        if (flatten)
640 >                texOK = 0;
641   #ifdef TEXMAPS
642          patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0);
643   #else
# Line 589 | Line 646 | char   *v1, *v2, *v3;
646          if (texOK | patOK)
647                  if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]],
648                                  vlist[v3i[0]]) < 0)
649 <                        return(-1);
649 >                        texOK = patOK = 0;
650                                          /* put out texture (if any) */
651          if (texOK) {
652                  printf("\n%s texfunc %s\n", mod, TEXNAME);
653                  mod = TEXNAME;
654                  printf("4 dx dy dz %s\n", TCALNAME);
655 <                printf("0\n16 ");
656 <                put_baryc(&bvecs);
657 <                printf("\t%14.12g %14.12g %14.12g\n",
658 <                                vnlist[v1i[2]][0], vnlist[v2i[2]][0],
659 <                                vnlist[v3i[2]][0]);
660 <                printf("\t%14.12g %14.12g %14.12g\n",
661 <                                vnlist[v1i[2]][1], vnlist[v2i[2]][1],
605 <                                vnlist[v3i[2]][1]);
606 <                printf("\t%14.12g %14.12g %14.12g\n",
607 <                                vnlist[v1i[2]][2], vnlist[v2i[2]][2],
608 <                                vnlist[v3i[2]][2]);
655 >                printf("0\n");
656 >                for (i = 0; i < 3; i++) {
657 >                        bcoor[i][0] = vnlist[v1i[2]][i];
658 >                        bcoor[i][1] = vnlist[v2i[2]][i];
659 >                        bcoor[i][2] = vnlist[v3i[2]][i];
660 >                }
661 >                put_baryc(&bvecs, bcoor, 3);
662          }
663   #ifdef TEXMAPS
664                                          /* put out pattern (if any) */
# Line 613 | Line 666 | char   *v1, *v2, *v3;
666                  printf("\n%s colorpict %s\n", mod, PATNAME);
667                  mod = PATNAME;
668                  printf("7 noneg noneg noneg %s %s u v\n", mapname, TCALNAME);
669 <                printf("0\n13 ");
670 <                put_baryc(&bvecs);
671 <                printf("\t%f %f %f\n", vtlist[v1i[1]][0],
672 <                                vtlist[v2i[1]][0], vtlist[v3i[1]][0]);
673 <                printf("\t%f %f %f\n", vtlist[v1i[1]][1],
674 <                                vtlist[v2i[1]][1], vtlist[v3i[1]][1]);
669 >                printf("0\n");
670 >                for (i = 0; i < 2; i++) {
671 >                        bcoor[i][0] = vtlist[v1i[1]][i];
672 >                        bcoor[i][1] = vtlist[v2i[1]][i];
673 >                        bcoor[i][2] = vtlist[v3i[1]][i];
674 >                }
675 >                put_baryc(&bvecs, bcoor, 2);
676          }
677   #endif
678 <                                        /* put out triangle */
678 >                                        /* put out (reversed) triangle */
679          printf("\n%s polygon %s.%d\n", mod, getonm(), faceno);
680          printf("0\n0\n9\n");
681 <        pvect(vlist[v1i[0]]);
682 <        pvect(vlist[v2i[0]]);
683 <        pvect(vlist[v3i[0]]);
684 <
685 <        return(1);
686 < }
687 <
688 <
635 < int
636 < comp_baryc(bcm, v1, v2, v3)             /* compute barycentric vectors */
637 < register BARYCCM        *bcm;
638 < FLOAT   *v1, *v2, *v3;
639 < {
640 <        FLOAT   *vt;
641 <        FVECT   va, vab, vcb;
642 <        double  d;
643 <        int     ax0, ax1;
644 <        register int    i, j;
645 <                                        /* compute major axis */
646 <        for (i = 0; i < 3; i++) {
647 <                vab[i] = v1[i] - v2[i];
648 <                vcb[i] = v3[i] - v2[i];
681 >        if (flatness == RVFLAT || flatness == RVBENT) {
682 >                pvect(vlist[v3i[0]]);
683 >                pvect(vlist[v2i[0]]);
684 >                pvect(vlist[v1i[0]]);
685 >        } else {
686 >                pvect(vlist[v1i[0]]);
687 >                pvect(vlist[v2i[0]]);
688 >                pvect(vlist[v3i[0]]);
689          }
650        fcross(va, vab, vcb);
651        bcm->ax = ABS(va[0]) > ABS(va[1]) ? 0 : 1;
652        bcm->ax = ABS(va[bcm->ax]) > ABS(va[2]) ? bcm->ax : 2;
653        ax0 = (bcm->ax + 1) % 3;
654        ax1 = (bcm->ax + 2) % 3;
655        for (j = 0; j < 2; j++) {
656                vab[0] = v1[ax0] - v2[ax0];
657                vcb[0] = v3[ax0] - v2[ax0];
658                vab[1] = v1[ax1] - v2[ax1];
659                vcb[1] = v3[ax1] - v2[ax1];
660                d = vcb[0]*vcb[0] + vcb[1]*vcb[1];
661                if (d <= FTINY)
662                        return(-1);
663                d = (vcb[0]*vab[0]+vcb[1]*vab[1])/d;
664                va[0] = vab[0] - vcb[0]*d;
665                va[1] = vab[1] - vcb[1]*d;
666                d = va[0]*va[0] + va[1]*va[1];
667                if (d <= FTINY)
668                        return(-1);
669                bcm->tm[j][0] = va[0] /= d;
670                bcm->tm[j][1] = va[1] /= d;
671                bcm->tm[j][2] = -(v2[ax0]*va[0]+v2[ax1]*va[1]);
672                                        /* rotate vertices */
673                vt = v1;
674                v1 = v2;
675                v2 = v3;
676                v3 = vt;
677        }
678        return(0);
679 }
680
681
682 put_baryc(bcm)                          /* put barycentric coord. vectors */
683 register BARYCCM        *bcm;
684 {
685        printf("\t%d\n", bcm->ax);
686        printf("%14.8f %14.8f %14.8f\n",
687                                bcm->tm[0][0], bcm->tm[0][1], bcm->tm[0][2]);
688        printf("%14.8f %14.8f %14.8f\n",
689                                bcm->tm[1][0], bcm->tm[1][1], bcm->tm[1][2]);
690 }
691
692
693 putquad(p0, p1, p3, p2)                 /* put out a quadrilateral */
694 char  *p0, *p1, *p3, *p2;               /* names correspond to binary pos. */
695 {
696        VNDX  p0i, p1i, p2i, p3i;
697        FVECT  norm[4];
698        char  *mod, *name;
699        int  axis;
700        FVECT  v1, v2, vc1, vc2;
701        int  ok1, ok2;
702
703 #ifdef TEXMAPS
704        /* also should output texture index coordinates,
705         * which will require new .cal file
706         */
707 #endif
708        if ((mod = getmtl()) == NULL)
709                return(-1);
710        name = getonm();
711                                        /* get actual indices */
712        if (!cvtndx(p0i,p0) || !cvtndx(p1i,p1) ||
713                        !cvtndx(p2i,p2) || !cvtndx(p3i,p3))
714                return(0);
715                                        /* compute exact normals */
716        fvsum(v1, vlist[p1i[0]], vlist[p0i[0]], -1.0);
717        fvsum(v2, vlist[p2i[0]], vlist[p0i[0]], -1.0);
718        fcross(vc1, v1, v2);
719        ok1 = normalize(vc1) != 0.0;
720        fvsum(v1, vlist[p2i[0]], vlist[p3i[0]], -1.0);
721        fvsum(v2, vlist[p1i[0]], vlist[p3i[0]], -1.0);
722        fcross(vc2, v1, v2);
723        ok2 = normalize(vc2) != 0.0;
724        if (!(ok1 | ok2))
725                return(-1);
726                                        /* compute normal interpolation */
727        axis = norminterp(norm, p0i, p1i, p2i, p3i);
728
729                                        /* put out quadrilateral? */
730        if (ok1 & ok2 && fabs(fdot(vc1,vc2)) >= 1.0-FTINY) {
731                printf("\n%s ", mod);
732                if (axis != -1) {
733                        printf("texfunc %s\n", TEXNAME);
734                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
735                        printf("0\n13\t%d\n", axis);
736                        pvect(norm[0]);
737                        pvect(norm[1]);
738                        pvect(norm[2]);
739                        fvsum(v1, norm[3], vc1, -0.5);
740                        fvsum(v1, v1, vc2, -0.5);
741                        pvect(v1);
742                        printf("\n%s ", TEXNAME);
743                }
744                printf("polygon %s.%d\n", name, faceno);
745                printf("0\n0\n12\n");
746                pvect(vlist[p0i[0]]);
747                pvect(vlist[p1i[0]]);
748                pvect(vlist[p3i[0]]);
749                pvect(vlist[p2i[0]]);
750                return(1);
751        }
752                                        /* put out triangles? */
753        if (ok1) {
754                printf("\n%s ", mod);
755                if (axis != -1) {
756                        printf("texfunc %s\n", TEXNAME);
757                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
758                        printf("0\n13\t%d\n", axis);
759                        pvect(norm[0]);
760                        pvect(norm[1]);
761                        pvect(norm[2]);
762                        fvsum(v1, norm[3], vc1, -1.0);
763                        pvect(v1);
764                        printf("\n%s ", TEXNAME);
765                }
766                printf("polygon %s.%da\n", name, faceno);
767                printf("0\n0\n9\n");
768                pvect(vlist[p0i[0]]);
769                pvect(vlist[p1i[0]]);
770                pvect(vlist[p2i[0]]);
771        }
772        if (ok2) {
773                printf("\n%s ", mod);
774                if (axis != -1) {
775                        printf("texfunc %s\n", TEXNAME);
776                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
777                        printf("0\n13\t%d\n", axis);
778                        pvect(norm[0]);
779                        pvect(norm[1]);
780                        pvect(norm[2]);
781                        fvsum(v2, norm[3], vc2, -1.0);
782                        pvect(v2);
783                        printf("\n%s ", TEXNAME);
784                }
785                printf("polygon %s.%db\n", name, faceno);
786                printf("0\n0\n9\n");
787                pvect(vlist[p2i[0]]);
788                pvect(vlist[p1i[0]]);
789                pvect(vlist[p3i[0]]);
790        }
690          return(1);
691   }
692  
693  
694 < int
695 < norminterp(resmat, p0i, p1i, p2i, p3i)  /* compute normal interpolation */
797 < register FVECT  resmat[4];
798 < register VNDX  p0i, p1i, p2i, p3i;
694 > void
695 > freeverts(void)                 /* free all vertices */
696   {
800 #define u  ((ax+1)%3)
801 #define v  ((ax+2)%3)
802
803        register int  ax;
804        MAT4  eqnmat;
805        FVECT  v1;
806        register int  i, j;
807
808 #ifdef TEXMAPS
809        /* also check for texture indices */
810 #endif
811        if (flatten || !(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0))
812                return(-1);
813                                        /* find dominant axis */
814        VCOPY(v1, vnlist[p0i[2]]);
815        fvsum(v1, v1, vnlist[p1i[2]], 1.0);
816        fvsum(v1, v1, vnlist[p2i[2]], 1.0);
817        fvsum(v1, v1, vnlist[p3i[2]], 1.0);
818        ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1;
819        ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2;
820                                        /* assign equation matrix */
821        eqnmat[0][0] = vlist[p0i[0]][u]*vlist[p0i[0]][v];
822        eqnmat[0][1] = vlist[p0i[0]][u];
823        eqnmat[0][2] = vlist[p0i[0]][v];
824        eqnmat[0][3] = 1.0;
825        eqnmat[1][0] = vlist[p1i[0]][u]*vlist[p1i[0]][v];
826        eqnmat[1][1] = vlist[p1i[0]][u];
827        eqnmat[1][2] = vlist[p1i[0]][v];
828        eqnmat[1][3] = 1.0;
829        eqnmat[2][0] = vlist[p2i[0]][u]*vlist[p2i[0]][v];
830        eqnmat[2][1] = vlist[p2i[0]][u];
831        eqnmat[2][2] = vlist[p2i[0]][v];
832        eqnmat[2][3] = 1.0;
833        eqnmat[3][0] = vlist[p3i[0]][u]*vlist[p3i[0]][v];
834        eqnmat[3][1] = vlist[p3i[0]][u];
835        eqnmat[3][2] = vlist[p3i[0]][v];
836        eqnmat[3][3] = 1.0;
837                                        /* invert matrix (solve system) */
838        if (!invmat4(eqnmat, eqnmat))
839                return(-1);                     /* no solution */
840                                        /* compute result matrix */
841        for (j = 0; j < 4; j++)
842                for (i = 0; i < 3; i++)
843                        resmat[j][i] =  eqnmat[j][0]*vnlist[p0i[2]][i] +
844                                        eqnmat[j][1]*vnlist[p1i[2]][i] +
845                                        eqnmat[j][2]*vnlist[p2i[2]][i] +
846                                        eqnmat[j][3]*vnlist[p3i[2]][i];
847 #ifdef TEXMAPS
848        /* compute result matrix for texture indices */
849 #endif
850        return(ax);
851
852 #undef u
853 #undef v
854 }
855
856
857 freeverts()                     /* free all vertices */
858 {
697          if (nvs) {
698 <                free((char *)vlist);
698 >                free((void *)vlist);
699                  nvs = 0;
700          }
701          if (nvts) {
702 <                free((char *)vtlist);
702 >                free((void *)vtlist);
703                  nvts = 0;
704          }
705          if (nvns) {
706 <                free((char *)vnlist);
706 >                free((void *)vnlist);
707                  nvns = 0;
708          }
709   }
710  
711  
712   int
713 < newv(x, y, z)                   /* create a new vertex */
714 < double  x, y, z;
713 > newv(                   /* create a new vertex */
714 >        double  x,
715 >        double  y,
716 >        double  z
717 > )
718   {
719          if (!(nvs%CHUNKSIZ)) {          /* allocate next block */
720                  if (nvs == 0)
721                          vlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT));
722                  else
723 <                        vlist = (FVECT *)realloc((char *)vlist,
723 >                        vlist = (FVECT *)realloc((void *)vlist,
724                                          (nvs+CHUNKSIZ)*sizeof(FVECT));
725                  if (vlist == NULL) {
726                          fprintf(stderr,
# Line 896 | Line 737 | double x, y, z;
737  
738  
739   int
740 < newvn(x, y, z)                  /* create a new vertex normal */
741 < double  x, y, z;
740 > newvn(                  /* create a new vertex normal */
741 >        double  x,
742 >        double  y,
743 >        double  z
744 > )
745   {
746          if (!(nvns%CHUNKSIZ)) {         /* allocate next block */
747                  if (nvns == 0)
748                          vnlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT));
749                  else
750 <                        vnlist = (FVECT *)realloc((char *)vnlist,
750 >                        vnlist = (FVECT *)realloc((void *)vnlist,
751                                          (nvns+CHUNKSIZ)*sizeof(FVECT));
752                  if (vnlist == NULL) {
753                          fprintf(stderr,
# Line 915 | Line 759 | double x, y, z;
759          vnlist[nvns][0] = x;
760          vnlist[nvns][1] = y;
761          vnlist[nvns][2] = z;
762 <        if (normalize(vnlist[nvns]) == 0.0)
919 <                return(0);
762 >        n0norm += (normalize(vnlist[nvns]) == 0.0);
763          return(++nvns);
764   }
765  
766  
767   int
768 < newvt(x, y)                     /* create a new texture map vertex */
769 < double  x, y;
768 > newvt(                  /* create a new texture map vertex */
769 >        double  x,
770 >        double  y
771 > )
772   {
773          if (!(nvts%CHUNKSIZ)) {         /* allocate next block */
774                  if (nvts == 0)
775 <                        vtlist = (FLOAT (*)[2])malloc(CHUNKSIZ*2*sizeof(FLOAT));
775 >                        vtlist = (RREAL (*)[2])malloc(CHUNKSIZ*2*sizeof(RREAL));
776                  else
777 <                        vtlist = (FLOAT (*)[2])realloc((char *)vtlist,
778 <                                        (nvts+CHUNKSIZ)*2*sizeof(FLOAT));
777 >                        vtlist = (RREAL (*)[2])realloc((void *)vtlist,
778 >                                        (nvts+CHUNKSIZ)*2*sizeof(RREAL));
779                  if (vtlist == NULL) {
780                          fprintf(stderr,
781                          "Out of memory while allocating texture vertex %d\n",
# Line 945 | Line 790 | double x, y;
790   }
791  
792  
793 < syntax(er)                      /* report syntax error and exit */
794 < char    *er;
793 > void
794 > syntax(                 /* report syntax error and exit */
795 >        char    *er
796 > )
797   {
798          fprintf(stderr, "%s: Wavefront syntax error near line %d: %s\n",
799                          inpfile, lineno, er);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines