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.8 by greg, Tue Jun 14 14:30:38 1994 UTC vs.
Revision 2.24 by greg, Tue Jun 24 02:01:19 2008 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   *
# 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 FLOAT   BARYCCM[3][4];  /* barycentric coordinate system */
38 > int     ndegen = 0;             /* count of degenerate faces */
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 72 | Line 70 | char   *defobj = DEFOBJ;       /* default (starting) object na
70  
71   int     flatten = 0;            /* discard surface normal information */
72  
75 char    *getmtl(), *getonm();
76
73   char    mapname[128];           /* current picture file */
74   char    matname[64];            /* current material name */
75   char    group[16][32];          /* current group names */
# Line 82 | Line 78 | 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 107 | Line 121 | char   *argv[];
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 128 | Line 142 | char   *argv[];
142                  printargs(argc, argv, stdout);
143                  convert(stdin);
144          }
145 +        if (ndegen)
146 +                printf("# %d degenerate faces\n", ndegen);
147          exit(0);
148   userr:
149 <        fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n] [file.obj]\n",
149 >        fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n",
150                          argv[0]);
151          exit(1);
152   }
153  
154  
155 < getnames(fp)                    /* get valid qualifier names */
156 < FILE    *fp;
155 > void
156 > getnames(                       /* get valid qualifier names */
157 >        FILE    *fp
158 > )
159   {
160          char    *argv[MAXARG];
161          int     argc;
162          ID      tmpid;
163          register int    i;
164  
165 <        while (argc = getstmt(argv, fp))
165 >        while ( (argc = getstmt(argv, fp)) )
166                  switch (argv[0][0]) {
167                  case 'f':                               /* face */
168                          if (!argv[0][1])
# Line 185 | Line 203 | FILE   *fp;
203   }
204  
205  
206 < convert(fp)                     /* convert a T-mesh */
207 < FILE    *fp;
206 > void
207 > convert(                        /* convert a T-mesh */
208 >        FILE    *fp
209 > )
210   {
211          char    *argv[MAXARG];
212          int     argc;
# Line 195 | Line 215 | FILE   *fp;
215  
216          nstats = nunknown = 0;
217                                          /* scan until EOF */
218 <        while (argc = getstmt(argv, fp)) {
218 >        while ( (argc = getstmt(argv, fp)) ) {
219                  switch (argv[0][0]) {
220                  case 'v':               /* vertex */
221                          switch (argv[0][1]) {
# Line 237 | Line 257 | FILE   *fp;
257                                  if (!puttri(argv[1], argv[2], argv[3]))
258                                          syntax("Bad triangle");
259                                  break;
240                        case 4:
241                                if (!putquad(argv[1], argv[2],
242                                                argv[3], argv[4]))
243                                        syntax("Bad quad");
244                                break;
260                          default:
261                                  if (!putface(argc-1, argv+1))
262                                          syntax("Bad face");
# Line 294 | Line 309 | FILE   *fp;
309  
310  
311   int
312 < getstmt(av, fp)                         /* read the next statement from fp */
313 < register char   *av[MAXARG];
314 < FILE    *fp;
312 > getstmt(                                /* read the next statement from fp */
313 >        register char   *av[MAXARG],
314 >        FILE    *fp
315 > )
316   {
317 <        extern char     *fgetline();
302 <        static char     sbuf[MAXARG*10];
317 >        static char     sbuf[MAXARG*16];
318          register char   *cp;
319          register int    i;
320  
# Line 313 | Line 328 | FILE   *fp;
328                                          lineno++;
329                                  *cp++ = '\0';
330                          }
331 <                        if (!*cp || i >= MAXARG-1)
331 >                        if (!*cp)
332                                  break;
333 +                        if (i >= MAXARG-1) {
334 +                                fprintf(stderr,
335 +                        "warning: line %d: too many arguments (limit %d)\n",
336 +                                        lineno+1, MAXARG-1);
337 +                                break;
338 +                        }
339                          av[i++] = cp;
340                          while (*++cp && !isspace(*cp))
341                                  ;
# Line 328 | Line 349 | FILE   *fp;
349  
350  
351   char *
352 < getmtl()                                /* figure material for this face */
352 > getmtl(void)                            /* figure material for this face */
353   {
354          register RULEHD *rp = ourmapping;
355  
# Line 354 | Line 375 | getmtl()                               /* figure material for this face */
375  
376  
377   char *
378 < getonm()                                /* invent a good name for object */
378 > getonm(void)                            /* invent a good name for object */
379   {
380          static char     name[64];
381          register char   *cp1, *cp2;
# Line 369 | Line 390 | getonm()                               /* invent a good name for object */
390                  cp2 = group[i];
391                  if (cp1 > name)
392                          *cp1++ = '.';
393 <                while (*cp1 = *cp2++)
393 >                while ( (*cp1 = *cp2++) )
394                          if (++cp1 >= name+sizeof(name)-2) {
395                                  *cp1 = '\0';
396                                  return(name);
# Line 379 | Line 400 | getonm()                               /* invent a good name for object */
400   }
401  
402  
403 < matchrule(rp)                           /* check for a match on this rule */
404 < register RULEHD *rp;
403 > int
404 > matchrule(                              /* check for a match on this rule */
405 >        register RULEHD *rp
406 > )
407   {
408          ID      tmpid;
409          int     gotmatch;
# Line 430 | Line 453 | register RULEHD        *rp;
453   }
454  
455  
456 < cvtndx(vi, vs)                          /* convert vertex string to index */
457 < register VNDX   vi;
458 < register char   *vs;
456 > int
457 > cvtndx(                         /* convert vertex string to index */
458 >        register VNDX   vi,
459 >        register char   *vs
460 > )
461   {
462                                          /* get point */
463          vi[0] = atoi(vs);
# Line 440 | Line 465 | register char  *vs;
465                  if (vi[0]-- > nvs)
466                          return(0);
467          } else if (vi[0] < 0) {
468 <                vi[0] = nvs + vi[0];
468 >                vi[0] += nvs;
469                  if (vi[0] < 0)
470                          return(0);
471          } else
# Line 454 | Line 479 | register char  *vs;
479                  if (vi[1]-- > nvts)
480                          return(0);
481          } else if (vi[1] < 0) {
482 <                vi[1] = nvts + vi[1];
482 >                vi[1] += nvts;
483                  if (vi[1] < 0)
484                          return(0);
485          } else
# Line 468 | Line 493 | register char  *vs;
493                  if (vi[2]-- > nvns)
494                          return(0);
495          } else if (vi[2] < 0) {
496 <                vi[2] = nvns + vi[2];
496 >                vi[2] += nvns;
497                  if (vi[2] < 0)
498                          return(0);
499          } else
# Line 477 | Line 502 | register char  *vs;
502   }
503  
504  
505 < nonplanar(ac, av)                       /* are vertices are non-planar? */
506 < register int    ac;
507 < register char   **av;
505 > int
506 > nonplanar(                      /* are vertices non-planar? */
507 >        register int    ac,
508 >        register char   **av
509 > )
510   {
511          VNDX    vi;
512 <        FLOAT   *p0, *p1;
512 >        RREAL   *p0, *p1;
513          FVECT   v1, v2, nsum, newn;
514          double  d;
515          register int    i;
516  
517          if (!cvtndx(vi, av[0]))
518                  return(0);
519 <        if (vi[2] >= 0)
519 >        if (!flatten && vi[2] >= 0)
520                  return(1);              /* has interpolated normals */
521          if (ac < 4)
522                  return(0);              /* it's a triangle! */
# Line 528 | Line 555 | register char  **av;
555   }
556  
557  
558 < putface(ac, av)                         /* put out an N-sided polygon */
559 < int     ac;
560 < register char   **av;
558 > int
559 > putface(                                /* put out an N-sided polygon */
560 >        int     ac,
561 >        register char   **av
562 > )
563   {
564          VNDX    vi;
565          char    *cp;
566          register int    i;
567  
568 <        if (nonplanar(ac, av)) {        /* break into quads and triangles */
569 <                while (ac > 3) {
570 <                        if (!putquad(av[0], av[1], av[2], av[3]))
568 >        if (nonplanar(ac, av)) {        /* break into triangles */
569 >                while (ac > 2) {
570 >                        if (!puttri(av[0], av[1], av[2]))
571                                  return(0);
572 <                        ac -= 2;        /* remove two vertices & rotate */
572 >                        ac--;           /* remove vertex & rotate */
573                          cp = av[0];
574                          for (i = 0; i < ac-1; i++)
575 <                                av[i] = av[i+3];
575 >                                av[i] = av[i+2];
576                          av[i] = cp;
577                  }
549                if (ac == 3 && !puttri(av[0], av[1], av[2]))
550                        return(0);
578                  return(1);
579          }
580          if ((cp = getmtl()) == NULL)
581 <                return(-1);
581 >                return(0);
582          printf("\n%s polygon %s.%d\n", cp, getonm(), faceno);
583          printf("0\n0\n%d\n", 3*ac);
584          for (i = 0; i < ac; i++) {
# Line 563 | Line 590 | register char  **av;
590   }
591  
592  
593 < puttri(v1, v2, v3)                      /* put out a triangle */
594 < char    *v1, *v2, *v3;
593 > int
594 > puttri(                 /* put out a triangle */
595 >        char *v1,
596 >        char *v2,
597 >        char *v3
598 > )
599   {
600          char    *mod;
601          VNDX    v1i, v2i, v3i;
602          BARYCCM bvecs;
603 <        int     texOK, patOK;
603 >        RREAL   bcoor[3][3];
604 >        int     texOK = 0, patOK;
605 >        int     flatness;
606 >        register int    i;
607  
608          if ((mod = getmtl()) == NULL)
609 <                return(-1);
609 >                return(0);
610  
611          if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3))
612                  return(0);
613                                          /* compute barycentric coordinates */
614 <        texOK = !flatten && (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0);
614 >        if (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0)
615 >                flatness = flat_tri(vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]],
616 >                                vnlist[v1i[2]], vnlist[v2i[2]], vnlist[v3i[2]]);
617 >        else
618 >                flatness = ISFLAT;
619 >
620 >        switch (flatness) {
621 >        case DEGEN:                     /* zero area */
622 >                ndegen++;
623 >                return(-1);
624 >        case RVFLAT:                    /* reversed normals, but flat */
625 >        case ISFLAT:                    /* smoothing unnecessary */
626 >                texOK = 0;
627 >                break;
628 >        case RVBENT:                    /* reversed normals with smoothing */
629 >        case ISBENT:                    /* proper smoothing */
630 >                texOK = 1;
631 >                break;
632 >        }
633 >        if (flatten)
634 >                texOK = 0;
635   #ifdef TEXMAPS
636          patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0);
637   #else
638          patOK = 0;
639   #endif
640          if (texOK | patOK)
641 <                if (comp_baryc(bvecs, vlist[v1i[0]], vlist[v2i[0]],
641 >                if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]],
642                                  vlist[v3i[0]]) < 0)
643 <                        return(-1);
643 >                        texOK = patOK = 0;
644                                          /* put out texture (if any) */
645          if (texOK) {
646                  printf("\n%s texfunc %s\n", mod, TEXNAME);
647                  mod = TEXNAME;
648                  printf("4 dx dy dz %s\n", TCALNAME);
649 <                printf("0\n21\n");
650 <                put_baryc(bvecs);
651 <                printf("\t%14.12g %14.12g %14.12g\n",
652 <                                vnlist[v1i[2]][0], vnlist[v2i[2]][0],
653 <                                vnlist[v3i[2]][0]);
654 <                printf("\t%14.12g %14.12g %14.12g\n",
655 <                                vnlist[v1i[2]][1], vnlist[v2i[2]][1],
602 <                                vnlist[v3i[2]][1]);
603 <                printf("\t%14.12g %14.12g %14.12g\n",
604 <                                vnlist[v1i[2]][2], vnlist[v2i[2]][2],
605 <                                vnlist[v3i[2]][2]);
649 >                printf("0\n");
650 >                for (i = 0; i < 3; i++) {
651 >                        bcoor[i][0] = vnlist[v1i[2]][i];
652 >                        bcoor[i][1] = vnlist[v2i[2]][i];
653 >                        bcoor[i][2] = vnlist[v3i[2]][i];
654 >                }
655 >                put_baryc(&bvecs, bcoor, 3);
656          }
657   #ifdef TEXMAPS
658                                          /* put out pattern (if any) */
# Line 610 | Line 660 | char   *v1, *v2, *v3;
660                  printf("\n%s colorpict %s\n", mod, PATNAME);
661                  mod = PATNAME;
662                  printf("7 noneg noneg noneg %s %s u v\n", mapname, TCALNAME);
663 <                printf("0\n18\n");
664 <                put_baryc(bvecs);
665 <                printf("\t%f %f %f\n", vtlist[v1i[1]][0],
666 <                                vtlist[v2i[1]][0], vtlist[v3i[1]][0]);
667 <                printf("\t%f %f %f\n", vtlist[v1i[1]][1],
668 <                                vtlist[v2i[1]][1], vtlist[v3i[1]][1]);
663 >                printf("0\n");
664 >                for (i = 0; i < 2; i++) {
665 >                        bcoor[i][0] = vtlist[v1i[1]][i];
666 >                        bcoor[i][1] = vtlist[v2i[1]][i];
667 >                        bcoor[i][2] = vtlist[v3i[1]][i];
668 >                }
669 >                put_baryc(&bvecs, bcoor, 2);
670          }
671   #endif
672 <                                        /* put out triangle */
672 >                                        /* put out (reversed) triangle */
673          printf("\n%s polygon %s.%d\n", mod, getonm(), faceno);
674          printf("0\n0\n9\n");
675 <        pvect(vlist[v1i[0]]);
676 <        pvect(vlist[v2i[0]]);
677 <        pvect(vlist[v3i[0]]);
678 <
679 <        return(1);
680 < }
681 <
682 <
632 < int
633 < comp_baryc(bcm, v1, v2, v3)             /* compute barycentric vectors */
634 < register BARYCCM        bcm;
635 < FLOAT   *v1, *v2, *v3;
636 < {
637 <        FLOAT   *vt;
638 <        FVECT   va, vab, vcb;
639 <        double  d;
640 <        register int    i, j;
641 <
642 <        for (j = 0; j < 3; j++) {
643 <                for (i = 0; i < 3; i++) {
644 <                        vab[i] = v1[i] - v2[i];
645 <                        vcb[i] = v3[i] - v2[i];
646 <                }
647 <                d = DOT(vcb,vcb);
648 <                if (d <= FTINY)
649 <                        return(-1);
650 <                d = DOT(vcb,vab)/d;
651 <                for (i = 0; i < 3; i++)
652 <                        va[i] = vab[i] - vcb[i]*d;
653 <                d = DOT(va,va);
654 <                if (d <= FTINY)
655 <                        return(-1);
656 <                for (i = 0; i < 3; i++) {
657 <                        va[i] /= d;
658 <                        bcm[j][i] = va[i];
659 <                }
660 <                bcm[j][3] = -DOT(v2,va);
661 <                                        /* rotate vertices */
662 <                vt = v1;
663 <                v1 = v2;
664 <                v2 = v3;
665 <                v3 = vt;
675 >        if (flatness == RVFLAT || flatness == RVBENT) {
676 >                pvect(vlist[v3i[0]]);
677 >                pvect(vlist[v2i[0]]);
678 >                pvect(vlist[v1i[0]]);
679 >        } else {
680 >                pvect(vlist[v1i[0]]);
681 >                pvect(vlist[v2i[0]]);
682 >                pvect(vlist[v3i[0]]);
683          }
667        return(0);
668 }
669
670
671 put_baryc(bcm)                          /* put barycentric coord. vectors */
672 register BARYCCM        bcm;
673 {
674        register int    i;
675
676        for (i = 0; i < 3; i++)
677                printf("%14.8f %14.8f %14.8f %14.8f\n",
678                                bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]);
679 }
680
681
682 putquad(p0, p1, p3, p2)                 /* put out a quadrilateral */
683 char  *p0, *p1, *p3, *p2;               /* names correspond to binary pos. */
684 {
685        VNDX  p0i, p1i, p2i, p3i;
686        FVECT  norm[4];
687        char  *mod, *name;
688        int  axis;
689        FVECT  v1, v2, vc1, vc2;
690        int  ok1, ok2;
691
692 #ifdef TEXMAPS
693        /* also should output texture index coordinates,
694         * which will require new .cal file
695         */
696 #endif
697        if ((mod = getmtl()) == NULL)
698                return(-1);
699        name = getonm();
700                                        /* get actual indices */
701        if (!cvtndx(p0i,p0) || !cvtndx(p1i,p1) ||
702                        !cvtndx(p2i,p2) || !cvtndx(p3i,p3))
703                return(0);
704                                        /* compute exact normals */
705        fvsum(v1, vlist[p1i[0]], vlist[p0i[0]], -1.0);
706        fvsum(v2, vlist[p2i[0]], vlist[p0i[0]], -1.0);
707        fcross(vc1, v1, v2);
708        ok1 = normalize(vc1) != 0.0;
709        fvsum(v1, vlist[p2i[0]], vlist[p3i[0]], -1.0);
710        fvsum(v2, vlist[p1i[0]], vlist[p3i[0]], -1.0);
711        fcross(vc2, v1, v2);
712        ok2 = normalize(vc2) != 0.0;
713        if (!(ok1 | ok2))
714                return(-1);
715                                        /* compute normal interpolation */
716        axis = norminterp(norm, p0i, p1i, p2i, p3i);
717
718                                        /* put out quadrilateral? */
719        if (ok1 & ok2 && fabs(fdot(vc1,vc2)) >= 1.0-FTINY) {
720                printf("\n%s ", mod);
721                if (axis != -1) {
722                        printf("texfunc %s\n", TEXNAME);
723                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
724                        printf("0\n13\t%d\n", axis);
725                        pvect(norm[0]);
726                        pvect(norm[1]);
727                        pvect(norm[2]);
728                        fvsum(v1, norm[3], vc1, -0.5);
729                        fvsum(v1, v1, vc2, -0.5);
730                        pvect(v1);
731                        printf("\n%s ", TEXNAME);
732                }
733                printf("polygon %s.%d\n", name, faceno);
734                printf("0\n0\n12\n");
735                pvect(vlist[p0i[0]]);
736                pvect(vlist[p1i[0]]);
737                pvect(vlist[p3i[0]]);
738                pvect(vlist[p2i[0]]);
739                return(1);
740        }
741                                        /* put out triangles? */
742        if (ok1) {
743                printf("\n%s ", mod);
744                if (axis != -1) {
745                        printf("texfunc %s\n", TEXNAME);
746                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
747                        printf("0\n13\t%d\n", axis);
748                        pvect(norm[0]);
749                        pvect(norm[1]);
750                        pvect(norm[2]);
751                        fvsum(v1, norm[3], vc1, -1.0);
752                        pvect(v1);
753                        printf("\n%s ", TEXNAME);
754                }
755                printf("polygon %s.%da\n", name, faceno);
756                printf("0\n0\n9\n");
757                pvect(vlist[p0i[0]]);
758                pvect(vlist[p1i[0]]);
759                pvect(vlist[p2i[0]]);
760        }
761        if (ok2) {
762                printf("\n%s ", mod);
763                if (axis != -1) {
764                        printf("texfunc %s\n", TEXNAME);
765                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
766                        printf("0\n13\t%d\n", axis);
767                        pvect(norm[0]);
768                        pvect(norm[1]);
769                        pvect(norm[2]);
770                        fvsum(v2, norm[3], vc2, -1.0);
771                        pvect(v2);
772                        printf("\n%s ", TEXNAME);
773                }
774                printf("polygon %s.%db\n", name, faceno);
775                printf("0\n0\n9\n");
776                pvect(vlist[p2i[0]]);
777                pvect(vlist[p1i[0]]);
778                pvect(vlist[p3i[0]]);
779        }
684          return(1);
685   }
686  
687  
688 < int
689 < norminterp(resmat, p0i, p1i, p2i, p3i)  /* compute normal interpolation */
786 < register FVECT  resmat[4];
787 < register VNDX  p0i, p1i, p2i, p3i;
688 > void
689 > freeverts(void)                 /* free all vertices */
690   {
789 #define u  ((ax+1)%3)
790 #define v  ((ax+2)%3)
791
792        register int  ax;
793        MAT4  eqnmat;
794        FVECT  v1;
795        register int  i, j;
796
797 #ifdef TEXMAPS
798        /* also check for texture indices */
799 #endif
800        if (flatten || !(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0))
801                return(-1);
802                                        /* find dominant axis */
803        VCOPY(v1, vnlist[p0i[2]]);
804        fvsum(v1, v1, vnlist[p1i[2]], 1.0);
805        fvsum(v1, v1, vnlist[p2i[2]], 1.0);
806        fvsum(v1, v1, vnlist[p3i[2]], 1.0);
807        ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1;
808        ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2;
809                                        /* assign equation matrix */
810        eqnmat[0][0] = vlist[p0i[0]][u]*vlist[p0i[0]][v];
811        eqnmat[0][1] = vlist[p0i[0]][u];
812        eqnmat[0][2] = vlist[p0i[0]][v];
813        eqnmat[0][3] = 1.0;
814        eqnmat[1][0] = vlist[p1i[0]][u]*vlist[p1i[0]][v];
815        eqnmat[1][1] = vlist[p1i[0]][u];
816        eqnmat[1][2] = vlist[p1i[0]][v];
817        eqnmat[1][3] = 1.0;
818        eqnmat[2][0] = vlist[p2i[0]][u]*vlist[p2i[0]][v];
819        eqnmat[2][1] = vlist[p2i[0]][u];
820        eqnmat[2][2] = vlist[p2i[0]][v];
821        eqnmat[2][3] = 1.0;
822        eqnmat[3][0] = vlist[p3i[0]][u]*vlist[p3i[0]][v];
823        eqnmat[3][1] = vlist[p3i[0]][u];
824        eqnmat[3][2] = vlist[p3i[0]][v];
825        eqnmat[3][3] = 1.0;
826                                        /* invert matrix (solve system) */
827        if (!invmat4(eqnmat, eqnmat))
828                return(-1);                     /* no solution */
829                                        /* compute result matrix */
830        for (j = 0; j < 4; j++)
831                for (i = 0; i < 3; i++)
832                        resmat[j][i] =  eqnmat[j][0]*vnlist[p0i[2]][i] +
833                                        eqnmat[j][1]*vnlist[p1i[2]][i] +
834                                        eqnmat[j][2]*vnlist[p2i[2]][i] +
835                                        eqnmat[j][3]*vnlist[p3i[2]][i];
836 #ifdef TEXMAPS
837        /* compute result matrix for texture indices */
838 #endif
839        return(ax);
840
841 #undef u
842 #undef v
843 }
844
845
846 freeverts()                     /* free all vertices */
847 {
691          if (nvs) {
692 <                free((char *)vlist);
692 >                free((void *)vlist);
693                  nvs = 0;
694          }
695          if (nvts) {
696 <                free((char *)vtlist);
696 >                free((void *)vtlist);
697                  nvts = 0;
698          }
699          if (nvns) {
700 <                free((char *)vnlist);
700 >                free((void *)vnlist);
701                  nvns = 0;
702          }
703   }
704  
705  
706   int
707 < newv(x, y, z)                   /* create a new vertex */
708 < double  x, y, z;
707 > newv(                   /* create a new vertex */
708 >        double  x,
709 >        double  y,
710 >        double  z
711 > )
712   {
713          if (!(nvs%CHUNKSIZ)) {          /* allocate next block */
714                  if (nvs == 0)
715                          vlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT));
716                  else
717 <                        vlist = (FVECT *)realloc((char *)vlist,
717 >                        vlist = (FVECT *)realloc((void *)vlist,
718                                          (nvs+CHUNKSIZ)*sizeof(FVECT));
719                  if (vlist == NULL) {
720                          fprintf(stderr,
# Line 885 | Line 731 | double x, y, z;
731  
732  
733   int
734 < newvn(x, y, z)                  /* create a new vertex normal */
735 < double  x, y, z;
734 > newvn(                  /* create a new vertex normal */
735 >        double  x,
736 >        double  y,
737 >        double  z
738 > )
739   {
740          if (!(nvns%CHUNKSIZ)) {         /* allocate next block */
741                  if (nvns == 0)
742                          vnlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT));
743                  else
744 <                        vnlist = (FVECT *)realloc((char *)vnlist,
744 >                        vnlist = (FVECT *)realloc((void *)vnlist,
745                                          (nvns+CHUNKSIZ)*sizeof(FVECT));
746                  if (vnlist == NULL) {
747                          fprintf(stderr,
# Line 911 | Line 760 | double x, y, z;
760  
761  
762   int
763 < newvt(x, y)                     /* create a new texture map vertex */
764 < double  x, y;
763 > newvt(                  /* create a new texture map vertex */
764 >        double  x,
765 >        double  y
766 > )
767   {
768          if (!(nvts%CHUNKSIZ)) {         /* allocate next block */
769                  if (nvts == 0)
770 <                        vtlist = (FLOAT (*)[2])malloc(CHUNKSIZ*2*sizeof(FLOAT));
770 >                        vtlist = (RREAL (*)[2])malloc(CHUNKSIZ*2*sizeof(RREAL));
771                  else
772 <                        vtlist = (FLOAT (*)[2])realloc((char *)vtlist,
773 <                                        (nvts+CHUNKSIZ)*2*sizeof(FLOAT));
772 >                        vtlist = (RREAL (*)[2])realloc((void *)vtlist,
773 >                                        (nvts+CHUNKSIZ)*2*sizeof(RREAL));
774                  if (vtlist == NULL) {
775                          fprintf(stderr,
776                          "Out of memory while allocating texture vertex %d\n",
# Line 934 | Line 785 | double x, y;
785   }
786  
787  
788 < syntax(er)                      /* report syntax error and exit */
789 < char    *er;
788 > void
789 > syntax(                 /* report syntax error and exit */
790 >        char    *er
791 > )
792   {
793          fprintf(stderr, "%s: Wavefront syntax error near line %d: %s\n",
794                          inpfile, lineno, er);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines