ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/obj2rad.c
(Generate patch)

Comparing ray/src/cv/obj2rad.c (file contents):
Revision 2.1 by greg, Tue Apr 12 17:12:42 1994 UTC vs.
Revision 2.22 by schorsch, Sat Nov 15 17:54:06 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1994 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Convert a Wavefront .obj file to Radiance format.
6   *
7 < * Currently, we support only polygonal geometry, and faces
8 < * must be either quads or triangles for smoothing to work.
9 < * Also, texture indices only work for triangles, though
10 < * I'm not sure they work correctly.
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.  (Taken out -- see TEXMAPS defines.)
11   */
12  
13 < #include "standard.h"
14 <
13 > #include <stdlib.h>
14 > #include <stdio.h>
15   #include <ctype.h>
16  
17 < #define VOIDID          "void"          /* this is defined in object.h */
17 > #include "rtmath.h"
18 > #include "rtio.h"
19 > #include "resolu.h"
20 > #include "trans.h"
21 > #include "tmesh.h"
22  
23 < #define TCALNAME        "tmesh.cal"     /* triangle interp. file */
23 < #define QCALNAME        "surf.cal"      /* quad interp. file */
23 >
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  
27 #define  ABS(x)         ((x)>=0 ? (x) : -(x))
28
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];           /* texture vertex list */
35 > RREAL   (*vtlist)[2];           /* map vertex list */
36   int     nvts;
37  
38 < typedef FLOAT   BARYCCM[3][4];  /* barycentric coordinate system */
38 > typedef int     VNDX[3];        /* vertex index (point,map,normal) */
39  
40 < typedef int     VNDX[3];        /* vertex index (point,texture,normal) */
40 > #define CHUNKSIZ        256     /* vertex allocation chunk size */
41  
42 #define CHUNKSIZ        128     /* vertex allocation chunk size */
43
42   #define MAXARG          64      /* maximum # arguments in a statement */
43  
44 < char    *defmat = VOIDID;       /* default (starting) material name */
45 < char    *defpat = "";           /* default (starting) picture name */
46 < char    *defobj = "F";          /* default (starting) object name */
44 >                                /* qualifiers */
45 > #define Q_MTL           0
46 > #define Q_MAP           1
47 > #define Q_GRP           2
48 > #define Q_OBJ           3
49 > #define Q_FAC           4
50 > #define NQUALS          5
51  
52 < char    picfile[128];           /* current picture file */
52 > char    *qname[NQUALS] = {
53 >        "Material",
54 >        "Map",
55 >        "Group",
56 >        "Object",
57 >        "Face",
58 > };
59 >
60 > QLIST   qlist = {NQUALS, qname};
61 >                                /* valid qualifier ids */
62 > IDLIST  qual[NQUALS];
63 >                                /* mapping rules */
64 > RULEHD  *ourmapping = NULL;
65 >
66 > char    *defmat = DEFMAT;       /* default (starting) material name */
67 > char    *defobj = DEFOBJ;       /* default (starting) object name */
68 >
69 > int     flatten = 0;            /* discard surface normal information */
70 >
71 > char    mapname[128];           /* current picture file */
72   char    matname[64];            /* current material name */
73 + char    group[16][32];          /* current group names */
74   char    objname[128];           /* current object name */
75 + char    *inpfile;               /* input file name */
76   int     lineno;                 /* current line number */
77 + int     faceno;                 /* current face number */
78  
79 < int     nfaces;                 /* total number of faces output */
79 > static void getnames(FILE *fp);
80 > static void convert(FILE *fp);
81 > static int getstmt(char *av[MAXARG], FILE *fp);
82 > static char * getmtl(void);
83 > static char * getonm(void);
84 > static int matchrule(RULEHD *rp);
85 > static int cvtndx(VNDX vi, char *vs);
86 > static int nonplanar(int ac, char **av);
87 > static int putface(int ac, char **av);
88 > static int puttri(char *v1, char *v2, char *v3);
89 > static void freeverts(void);
90 > static int newv(double x, double y, double z);
91 > static int newvn(double x, double y, double z);
92 > static int newvt(double x, double y);
93 > static void syntax(char *er);
94  
95  
96 < main(argc, argv)                /* read in T-mesh files and convert */
97 < int     argc;
98 < char    *argv[];
96 > int
97 > main(           /* read in .obj file and convert */
98 >        int     argc,
99 >        char    *argv[]
100 > )
101   {
102 <        FILE    *fp;
102 >        int     donames = 0;
103          int     i;
104  
105          for (i = 1; i < argc && argv[i][0] == '-'; i++)
# Line 67 | Line 107 | char   *argv[];
107                  case 'o':               /* object name */
108                          defobj = argv[++i];
109                          break;
110 <                case 'm':               /* default material */
111 <                        defmat = argv[++i];
110 >                case 'n':               /* just produce name list */
111 >                        donames++;
112                          break;
113 <                case 'p':               /* default picture */
114 <                        defpat = argv[++i];
113 >                case 'm':               /* use custom mapfile */
114 >                        ourmapping = getmapping(argv[++i], &qlist);
115                          break;
116 +                case 'f':               /* flatten surfaces */
117 +                        flatten++;
118 +                        break;
119                  default:
120 <                        fprintf(stderr,
78 <                        "Usage: %s [-o obj][-m mat][-p pic] [file ..]\n",
79 <                                        argv[0]);
80 <                        exit(1);
120 >                        goto userr;
121                  }
122 <        if (i >= argc)
123 <                convert("<stdin>", stdin);
124 <        else
125 <                for ( ; i < argc; i++) {
126 <                        if ((fp = fopen(argv[i], "r")) == NULL) {
127 <                                perror(argv[i]);
128 <                                exit(1);
122 >        if ((i > argc) | (i < argc-1))
123 >                goto userr;
124 >        if (i == argc)
125 >                inpfile = "<stdin>";
126 >        else if (freopen(inpfile=argv[i], "r", stdin) == NULL) {
127 >                fprintf(stderr, "%s: cannot open\n", inpfile);
128 >                exit(1);
129 >        }
130 >        if (donames) {                          /* scan for ids */
131 >                getnames(stdin);
132 >                printf("filename \"%s\"\n", inpfile);
133 >                printf("filetype \"Wavefront\"\n");
134 >                write_quals(&qlist, qual, stdout);
135 >                printf("qualifier %s begin\n", qlist.qual[Q_FAC]);
136 >                printf("[%d:%d]\n", 1, faceno);
137 >                printf("end\n");
138 >        } else {                                /* translate file */
139 >                printf("# ");
140 >                printargs(argc, argv, stdout);
141 >                convert(stdin);
142 >        }
143 >        exit(0);
144 > userr:
145 >        fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n",
146 >                        argv[0]);
147 >        exit(1);
148 > }
149 >
150 >
151 > void
152 > getnames(                       /* get valid qualifier names */
153 >        FILE    *fp
154 > )
155 > {
156 >        char    *argv[MAXARG];
157 >        int     argc;
158 >        ID      tmpid;
159 >        register int    i;
160 >
161 >        while ( (argc = getstmt(argv, fp)) )
162 >                switch (argv[0][0]) {
163 >                case 'f':                               /* face */
164 >                        if (!argv[0][1])
165 >                                faceno++;
166 >                        break;
167 >                case 'u':
168 >                        if (!strcmp(argv[0], "usemtl")) {       /* material */
169 >                                if (argc < 2)
170 >                                        break;          /* not fatal */
171 >                                tmpid.number = 0;
172 >                                tmpid.name = argv[1];
173 >                                findid(&qual[Q_MTL], &tmpid, 1);
174 >                        } else if (!strcmp(argv[0], "usemap")) {/* map */
175 >                                if (argc < 2 || !strcmp(argv[1], "off"))
176 >                                        break;          /* not fatal */
177 >                                tmpid.number = 0;
178 >                                tmpid.name = argv[1];
179 >                                findid(&qual[Q_MAP], &tmpid, 1);
180                          }
181 <                        convert(argv[i], fp);
182 <                        fclose(fp);
181 >                        break;
182 >                case 'o':               /* object name */
183 >                        if (argv[0][1] || argc < 2)
184 >                                break;
185 >                        tmpid.number = 0;
186 >                        tmpid.name = argv[1];
187 >                        findid(&qual[Q_OBJ], &tmpid, 1);
188 >                        break;
189 >                case 'g':               /* group name(s) */
190 >                        if (argv[0][1])
191 >                                break;
192 >                        tmpid.number = 0;
193 >                        for (i = 1; i < argc; i++) {
194 >                                tmpid.name = argv[i];
195 >                                findid(&qual[Q_GRP], &tmpid, 1);
196 >                        }
197 >                        break;
198                  }
93        exit(0);
199   }
200  
201  
202 < convert(fname, fp)              /* convert a T-mesh */
203 < char    *fname;
204 < FILE    *fp;
202 > void
203 > convert(                        /* convert a T-mesh */
204 >        FILE    *fp
205 > )
206   {
207          char    *argv[MAXARG];
208          int     argc;
209          int     nstats, nunknown;
210          register int    i;
105                                        /* start fresh */
106        freeverts();
107        strcpy(picfile, defpat);
108        strcpy(matname, defmat);
109        strcpy(objname, defobj);
110        lineno = 0;
111        nstats = nunknown = 0;
211  
212 <        printf("\n# Wavefront file read from: %s\n", fname);
212 >        nstats = nunknown = 0;
213                                          /* scan until EOF */
214 <        while (argc = getstmt(argv, fp)) {
214 >        while ( (argc = getstmt(argv, fp)) ) {
215                  switch (argv[0][0]) {
216                  case 'v':               /* vertex */
217                          switch (argv[0][1]) {
218                          case '\0':                      /* point */
219                                  if (badarg(argc-1,argv+1,"fff"))
220 <                                        syntax(fname, lineno, "Bad vertex");
220 >                                        syntax("Bad vertex");
221                                  newv(atof(argv[1]), atof(argv[2]),
222                                                  atof(argv[3]));
223                                  break;
# Line 126 | Line 225 | FILE   *fp;
225                                  if (argv[0][2])
226                                          goto unknown;
227                                  if (badarg(argc-1,argv+1,"fff"))
228 <                                        syntax(fname, lineno, "Bad normal");
228 >                                        syntax("Bad normal");
229                                  if (!newvn(atof(argv[1]), atof(argv[2]),
230                                                  atof(argv[3])))
231 <                                        syntax(fname, lineno, "Zero normal");
231 >                                        syntax("Zero normal");
232                                  break;
233 <                        case 't':                       /* texture */
233 >                        case 't':                       /* texture map */
234                                  if (argv[0][2])
235                                          goto unknown;
236                                  if (badarg(argc-1,argv+1,"ff"))
# Line 145 | Line 244 | FILE   *fp;
244                  case 'f':                               /* face */
245                          if (argv[0][1])
246                                  goto unknown;
247 +                        faceno++;
248                          switch (argc-1) {
249                          case 0: case 1: case 2:
250 <                                syntax(fname, lineno, "Too few vertices");
250 >                                syntax("Too few vertices");
251                                  break;
252                          case 3:
253                                  if (!puttri(argv[1], argv[2], argv[3]))
254 <                                        syntax(fname, lineno, "Bad triangle");
254 >                                        syntax("Bad triangle");
255                                  break;
156                        case 4:
157                                if (!putquad(argv[1], argv[2],
158                                                argv[3], argv[4]))
159                                        syntax(fname, lineno, "Bad quad");
160                                break;
256                          default:
257                                  if (!putface(argc-1, argv+1))
258 <                                        syntax(fname, lineno, "Bad face");
258 >                                        syntax("Bad face");
259                                  break;
260                          }
261                          break;
# Line 173 | Line 268 | FILE   *fp;
268                                  if (argc < 2)
269                                          break;          /* not fatal */
270                                  if (!strcmp(argv[1], "off"))
271 <                                        picfile[0] = '\0';
271 >                                        mapname[0] = '\0';
272                                  else
273 <                                        strcpy(picfile, argv[1]);
273 >                                        sprintf(mapname, "%s.pic", argv[1]);
274                          } else
275                                  goto unknown;
276                          break;
# Line 189 | Line 284 | FILE   *fp;
284                  case 'g':               /* group name(s) */
285                          if (argv[0][1])
286                                  goto unknown;
192                        if (argc < 2)
193                                break;          /* not fatal */
194                        objname[0] = '\0';
287                          for (i = 1; i < argc; i++)
288 <                                if (objname[0])
289 <                                        sprintf(objname+strlen(objname),
198 <                                                ".%s", argv[i]);
199 <                                else
200 <                                        strcpy(objname, argv[i]);
288 >                                strcpy(group[i-1], argv[i]);
289 >                        group[i-1][0] = '\0';
290                          break;
291                  case '#':               /* comment */
292 +                        printargs(argc, argv, stdout);
293                          break;
294                  default:;               /* something we don't deal with */
295                  unknown:
# Line 208 | Line 298 | FILE   *fp;
298                  }
299                  nstats++;
300          }
301 <        printf("# Done processing file: %s\n", fname);
301 >        printf("\n# Done processing file: %s\n", inpfile);
302          printf("# %d lines, %d statements, %d unrecognized\n",
303                          lineno, nstats, nunknown);
304   }
305  
306  
307   int
308 < getstmt(av, fp)                         /* read the next statement from fp */
309 < register char   *av[MAXARG];
310 < FILE    *fp;
308 > getstmt(                                /* read the next statement from fp */
309 >        register char   *av[MAXARG],
310 >        FILE    *fp
311 > )
312   {
313          extern char     *fgetline();
314          static char     sbuf[MAXARG*10];
# Line 246 | Line 337 | FILE   *fp;
337  
338          return(i);
339   }
249                
340  
341 < cvtndx(vi, vs)                          /* convert vertex string to index */
342 < register VNDX   vi;
343 < register char   *vs;
341 >
342 > char *
343 > getmtl(void)                            /* figure material for this face */
344   {
345 +        register RULEHD *rp = ourmapping;
346 +
347 +        if (rp == NULL) {               /* no rule set */
348 +                if (matname[0])
349 +                        return(matname);
350 +                if (group[0][0])
351 +                        return(group[0]);
352 +                return(defmat);
353 +        }
354 +                                        /* check for match */
355 +        do {
356 +                if (matchrule(rp)) {
357 +                        if (!strcmp(rp->mnam, VOIDID))
358 +                                return(NULL);   /* match is null */
359 +                        return(rp->mnam);
360 +                }
361 +                rp = rp->next;
362 +        } while (rp != NULL);
363 +                                        /* no match found */
364 +        return(NULL);
365 + }
366 +
367 +
368 + char *
369 + getonm(void)                            /* invent a good name for object */
370 + {
371 +        static char     name[64];
372 +        register char   *cp1, *cp2;
373 +        register int    i;
374 +                                        /* check for preset */
375 +        if (objname[0])
376 +                return(objname);
377 +        if (!group[0][0])
378 +                return(defobj);
379 +        cp1 = name;                     /* else make name out of groups */
380 +        for (i = 0; group[i][0]; i++) {
381 +                cp2 = group[i];
382 +                if (cp1 > name)
383 +                        *cp1++ = '.';
384 +                while ( (*cp1 = *cp2++) )
385 +                        if (++cp1 >= name+sizeof(name)-2) {
386 +                                *cp1 = '\0';
387 +                                return(name);
388 +                        }
389 +        }
390 +        return(name);
391 + }
392 +
393 +
394 + int
395 + matchrule(                              /* check for a match on this rule */
396 +        register RULEHD *rp
397 + )
398 + {
399 +        ID      tmpid;
400 +        int     gotmatch;
401 +        register int    i;
402 +
403 +        if (rp->qflg & FL(Q_MTL)) {
404 +                if (!matname[0])
405 +                        return(0);
406 +                tmpid.number = 0;
407 +                tmpid.name = matname;
408 +                if (!matchid(&tmpid, &idm(rp)[Q_MTL]))
409 +                        return(0);
410 +        }
411 +        if (rp->qflg & FL(Q_MAP)) {
412 +                if (!mapname[0])
413 +                        return(0);
414 +                tmpid.number = 0;
415 +                tmpid.name = mapname;
416 +                if (!matchid(&tmpid, &idm(rp)[Q_MAP]))
417 +                        return(0);
418 +        }
419 +        if (rp->qflg & FL(Q_GRP)) {
420 +                tmpid.number = 0;
421 +                gotmatch = 0;
422 +                for (i = 0; group[i][0]; i++) {
423 +                        tmpid.name = group[i];
424 +                        gotmatch |= matchid(&tmpid, &idm(rp)[Q_GRP]);
425 +                }
426 +                if (!gotmatch)
427 +                        return(0);
428 +        }
429 +        if (rp->qflg & FL(Q_OBJ)) {
430 +                if (!objname[0])
431 +                        return(0);
432 +                tmpid.number = 0;
433 +                tmpid.name = objname;
434 +                if (!matchid(&tmpid, &idm(rp)[Q_OBJ]))
435 +                        return(0);
436 +        }
437 +        if (rp->qflg & FL(Q_FAC)) {
438 +                tmpid.name = NULL;
439 +                tmpid.number = faceno;
440 +                if (!matchid(&tmpid, &idm(rp)[Q_FAC]))
441 +                        return(0);
442 +        }
443 +        return(1);
444 + }
445 +
446 +
447 + int
448 + cvtndx(                         /* convert vertex string to index */
449 +        register VNDX   vi,
450 +        register char   *vs
451 + )
452 + {
453                                          /* get point */
454          vi[0] = atoi(vs);
455          if (vi[0] > 0) {
456                  if (vi[0]-- > nvs)
457                          return(0);
458          } else if (vi[0] < 0) {
459 <                vi[0] = nvs + vi[0];
459 >                vi[0] += nvs;
460                  if (vi[0] < 0)
461                          return(0);
462          } else
463                  return(0);
464 <                                        /* get texture */
464 >                                        /* get map */
465          while (*vs)
466                  if (*vs++ == '/')
467                          break;
# Line 272 | Line 470 | register char  *vs;
470                  if (vi[1]-- > nvts)
471                          return(0);
472          } else if (vi[1] < 0) {
473 <                vi[1] = nvts + vi[1];
473 >                vi[1] += nvts;
474                  if (vi[1] < 0)
475                          return(0);
476          } else
# Line 286 | Line 484 | register char  *vs;
484                  if (vi[2]-- > nvns)
485                          return(0);
486          } else if (vi[2] < 0) {
487 <                vi[2] = nvns + vi[2];
487 >                vi[2] += nvns;
488                  if (vi[2] < 0)
489                          return(0);
490          } else
# Line 295 | Line 493 | register char  *vs;
493   }
494  
495  
496 < putface(ac, av)                         /* put out an N-sided polygon */
497 < register int    ac;
498 < register char   **av;
496 > int
497 > nonplanar(                      /* are vertices non-planar? */
498 >        register int    ac,
499 >        register char   **av
500 > )
501   {
502          VNDX    vi;
503 +        RREAL   *p0, *p1;
504 +        FVECT   v1, v2, nsum, newn;
505 +        double  d;
506 +        register int    i;
507  
508 <        printf("\n%s polygon %s.%d\n", matname, objname, ++nfaces);
508 >        if (!cvtndx(vi, av[0]))
509 >                return(0);
510 >        if (!flatten && vi[2] >= 0)
511 >                return(1);              /* has interpolated normals */
512 >        if (ac < 4)
513 >                return(0);              /* it's a triangle! */
514 >                                        /* set up */
515 >        p0 = vlist[vi[0]];
516 >        if (!cvtndx(vi, av[1]))
517 >                return(0);              /* error gets caught later */
518 >        nsum[0] = nsum[1] = nsum[2] = 0.;
519 >        p1 = vlist[vi[0]];
520 >        fvsum(v2, p1, p0, -1.0);
521 >        for (i = 2; i < ac; i++) {
522 >                VCOPY(v1, v2);
523 >                if (!cvtndx(vi, av[i]))
524 >                        return(0);
525 >                p1 = vlist[vi[0]];
526 >                fvsum(v2, p1, p0, -1.0);
527 >                fcross(newn, v1, v2);
528 >                if (normalize(newn) == 0.0) {
529 >                        if (i < 3)
530 >                                return(1);      /* can't deal with this */
531 >                        fvsum(nsum, nsum, nsum, 1./(i-2));
532 >                        continue;
533 >                }
534 >                d = fdot(newn,nsum);
535 >                if (d >= 0) {
536 >                        if (d < (1.0-FTINY)*(i-2))
537 >                                return(1);
538 >                        fvsum(nsum, nsum, newn, 1.0);
539 >                } else {
540 >                        if (d > -(1.0-FTINY)*(i-2))
541 >                                return(1);
542 >                        fvsum(nsum, nsum, newn, -1.0);
543 >                }
544 >        }
545 >        return(0);
546 > }
547 >
548 >
549 > int
550 > putface(                                /* put out an N-sided polygon */
551 >        int     ac,
552 >        register char   **av
553 > )
554 > {
555 >        VNDX    vi;
556 >        char    *cp;
557 >        register int    i;
558 >
559 >        if (nonplanar(ac, av)) {        /* break into triangles */
560 >                while (ac > 2) {
561 >                        if (!puttri(av[0], av[1], av[2]))
562 >                                return(0);
563 >                        ac--;           /* remove vertex & rotate */
564 >                        cp = av[0];
565 >                        for (i = 0; i < ac-1; i++)
566 >                                av[i] = av[i+2];
567 >                        av[i] = cp;
568 >                }
569 >                return(1);
570 >        }
571 >        if ((cp = getmtl()) == NULL)
572 >                return(-1);
573 >        printf("\n%s polygon %s.%d\n", cp, getonm(), faceno);
574          printf("0\n0\n%d\n", 3*ac);
575 <        while (ac--) {
576 <                if (!cvtndx(vi, *av++))
575 >        for (i = 0; i < ac; i++) {
576 >                if (!cvtndx(vi, av[i]))
577                          return(0);
578                  pvect(vlist[vi[0]]);
579          }
# Line 312 | Line 581 | register char  **av;
581   }
582  
583  
584 < puttri(v1, v2, v3)                      /* put out a triangle */
585 < char    *v1, *v2, *v3;
584 > int
585 > puttri(                 /* put out a triangle */
586 >        char *v1,
587 >        char *v2,
588 >        char *v3
589 > )
590   {
591 <        char    *mod = matname;
591 >        char    *mod;
592          VNDX    v1i, v2i, v3i;
593          BARYCCM bvecs;
594 +        RREAL   bcoor[3][3];
595 +        int     texOK = 0, patOK;
596 +        int     flatness;
597 +        register int    i;
598  
599 +        if ((mod = getmtl()) == NULL)
600 +                return(-1);
601 +
602          if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3))
603                  return(0);
604                                          /* compute barycentric coordinates */
605 <        if ((v1i[2]>=0&&v2i[2]>=0&&v3i[2]>=0) ||
606 <                        (v1i[1]>=0&&v2i[1]>=0&&v3i[1]>=0))
607 <                if (comp_baryc(bvecs, vlist[v1i[0]], vlist[v2i[0]],
605 >        if (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0)
606 >                flatness = flat_tri(vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]],
607 >                                vnlist[v1i[2]], vnlist[v2i[2]], vnlist[v3i[2]]);
608 >        else
609 >                flatness = ISFLAT;
610 >
611 >        switch (flatness) {
612 >        case DEGEN:                     /* zero area */
613 >                return(-1);
614 >        case RVFLAT:                    /* reversed normals, but flat */
615 >        case ISFLAT:                    /* smoothing unnecessary */
616 >                texOK = 0;
617 >                break;
618 >        case RVBENT:                    /* reversed normals with smoothing */
619 >        case ISBENT:                    /* proper smoothing */
620 >                texOK = 1;
621 >                break;
622 >        }
623 >        if (flatten)
624 >                texOK = 0;
625 > #ifdef TEXMAPS
626 >        patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0);
627 > #else
628 >        patOK = 0;
629 > #endif
630 >        if (texOK | patOK)
631 >                if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]],
632                                  vlist[v3i[0]]) < 0)
633 <                        return(0);
633 >                        return(-1);
634                                          /* put out texture (if any) */
635 <        if (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0) {
635 >        if (texOK) {
636                  printf("\n%s texfunc %s\n", mod, TEXNAME);
637                  mod = TEXNAME;
638                  printf("4 dx dy dz %s\n", TCALNAME);
639 <                printf("0\n21\n");
640 <                put_baryc(bvecs);
641 <                printf("\t%14.12g %14.12g %14.12g\n",
642 <                                vnlist[v1i[2]][0], vnlist[v2i[2]][0],
643 <                                vnlist[v3i[2]][0]);
644 <                printf("\t%14.12g %14.12g %14.12g\n",
645 <                                vnlist[v1i[2]][1], vnlist[v2i[2]][1],
342 <                                vnlist[v3i[2]][1]);
343 <                printf("\t%14.12g %14.12g %14.12g\n",
344 <                                vnlist[v1i[2]][2], vnlist[v2i[2]][2],
345 <                                vnlist[v3i[2]][2]);
639 >                printf("0\n");
640 >                for (i = 0; i < 3; i++) {
641 >                        bcoor[i][0] = vnlist[v1i[2]][i];
642 >                        bcoor[i][1] = vnlist[v2i[2]][i];
643 >                        bcoor[i][2] = vnlist[v3i[2]][i];
644 >                }
645 >                put_baryc(&bvecs, bcoor, 3);
646          }
647 + #ifdef TEXMAPS
648                                          /* put out pattern (if any) */
649 <        if (picfile[0] && v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0) {
649 >        if (patOK) {
650                  printf("\n%s colorpict %s\n", mod, PATNAME);
651                  mod = PATNAME;
652 <                printf("7 noneg noneg noneg %s %s u v\n", picfile, TCALNAME);
653 <                printf("0\n18\n");
654 <                put_baryc(bvecs);
655 <                printf("\t%f %f %f\n", vtlist[v1i[1]][0],
656 <                                vtlist[v2i[1]][0], vtlist[v3i[1]][0]);
657 <                printf("\t%f %f %f\n", vtlist[v1i[1]][1],
658 <                                vtlist[v2i[1]][1], vtlist[v3i[1]][1]);
652 >                printf("7 noneg noneg noneg %s %s u v\n", mapname, TCALNAME);
653 >                printf("0\n");
654 >                for (i = 0; i < 2; i++) {
655 >                        bcoor[i][0] = vtlist[v1i[1]][i];
656 >                        bcoor[i][1] = vtlist[v2i[1]][i];
657 >                        bcoor[i][2] = vtlist[v3i[1]][i];
658 >                }
659 >                put_baryc(&bvecs, bcoor, 2);
660          }
661 <                                        /* put out triangle */
662 <        printf("\n%s polygon %s.%d\n", matname, objname, ++nfaces);
661 > #endif
662 >                                        /* put out (reversed) triangle */
663 >        printf("\n%s polygon %s.%d\n", mod, getonm(), faceno);
664          printf("0\n0\n9\n");
665 <        pvect(vlist[v1i[0]]);
666 <        pvect(vlist[v2i[0]]);
667 <        pvect(vlist[v3i[0]]);
668 <
669 <        return(1);
670 < }
671 <
672 <
370 < int
371 < comp_baryc(bcm, v1, v2, v3)             /* compute barycentric vectors */
372 < register BARYCCM        bcm;
373 < FLOAT   *v1, *v2, *v3;
374 < {
375 <        FLOAT   *vt;
376 <        FVECT   va, vab, vcb;
377 <        double  d;
378 <        register int    i, j;
379 <
380 <        for (j = 0; j < 3; j++) {
381 <                for (i = 0; i < 3; i++) {
382 <                        vab[i] = v1[i] - v2[i];
383 <                        vcb[i] = v3[i] - v2[i];
384 <                }
385 <                d = DOT(vcb,vcb);
386 <                if (d <= FTINY)
387 <                        return(-1);
388 <                d = DOT(vcb,vab)/d;
389 <                for (i = 0; i < 3; i++)
390 <                        va[i] = vab[i] - vcb[i]*d;
391 <                d = DOT(va,va);
392 <                if (d <= FTINY)
393 <                        return(-1);
394 <                for (i = 0; i < 3; i++) {
395 <                        va[i] /= d;
396 <                        bcm[j][i] = va[i];
397 <                }
398 <                bcm[j][3] = -DOT(v2,va);
399 <                                        /* rotate vertices */
400 <                vt = v1;
401 <                v1 = v2;
402 <                v2 = v3;
403 <                v3 = vt;
665 >        if (flatness == RVFLAT || flatness == RVBENT) {
666 >                pvect(vlist[v3i[0]]);
667 >                pvect(vlist[v2i[0]]);
668 >                pvect(vlist[v1i[0]]);
669 >        } else {
670 >                pvect(vlist[v1i[0]]);
671 >                pvect(vlist[v2i[0]]);
672 >                pvect(vlist[v3i[0]]);
673          }
405        return(0);
406 }
407
408
409 put_baryc(bcm)                          /* put barycentric coord. vectors */
410 register BARYCCM        bcm;
411 {
412        register int    i;
413
414        for (i = 0; i < 3; i++)
415                printf("%14.8f %14.8f %14.8f %14.8f\n",
416                                bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]);
417 }
418
419
420 putquad(p0, p1, p2, p3)                 /* put out a quadrilateral */
421 char  *p0, *p1, *p2, *p3;
422 {
423        VNDX  p0i, p1i, p2i, p3i;
424        FVECT  norm[4];
425        int  axis;
426        FVECT  v1, v2, vc1, vc2;
427        int  ok1, ok2;
428                                        /* get actual indices */
429        if (!cvtndx(p0i,p0) || !cvtndx(p1i,p1) ||
430                        !cvtndx(p2i,p2) || !cvtndx(p3i,p3))
431                return(0);
432                                        /* compute exact normals */
433        fvsum(v1, vlist[p1i[0]], vlist[p0i[0]], -1.0);
434        fvsum(v2, vlist[p2i[0]], vlist[p0i[0]], -1.0);
435        fcross(vc1, v1, v2);
436        ok1 = normalize(vc1) != 0.0;
437        fvsum(v1, vlist[p2i[0]], vlist[p3i[0]], -1.0);
438        fvsum(v2, vlist[p1i[0]], vlist[p3i[0]], -1.0);
439        fcross(vc2, v1, v2);
440        ok2 = normalize(vc2) != 0.0;
441        if (!(ok1 | ok2))
442                return(0);
443                                        /* compute normal interpolation */
444        axis = norminterp(norm, p0i, p1i, p2i, p3i);
445
446                                        /* put out quadrilateral? */
447        if (ok1 & ok2 && fdot(vc1,vc2) >= 1.0-FTINY*FTINY) {
448                printf("\n%s ", matname);
449                if (axis != -1) {
450                        printf("texfunc %s\n", TEXNAME);
451                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
452                        printf("0\n13\t%d\n", axis);
453                        pvect(norm[0]);
454                        pvect(norm[1]);
455                        pvect(norm[2]);
456                        fvsum(v1, norm[3], vc1, -0.5);
457                        fvsum(v1, v1, vc2, -0.5);
458                        pvect(v1);
459                        printf("\n%s ", TEXNAME);
460                }
461                printf("polygon %s.%d\n", objname, ++nfaces);
462                printf("0\n0\n12\n");
463                pvect(vlist[p0i[0]]);
464                pvect(vlist[p1i[0]]);
465                pvect(vlist[p3i[0]]);
466                pvect(vlist[p2i[0]]);
467                return(1);
468        }
469                                        /* put out triangles? */
470        if (ok1) {
471                printf("\n%s ", matname);
472                if (axis != -1) {
473                        printf("texfunc %s\n", TEXNAME);
474                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
475                        printf("0\n13\t%d\n", axis);
476                        pvect(norm[0]);
477                        pvect(norm[1]);
478                        pvect(norm[2]);
479                        fvsum(v1, norm[3], vc1, -1.0);
480                        pvect(v1);
481                        printf("\n%s ", TEXNAME);
482                }
483                printf("polygon %s.%d\n", objname, ++nfaces);
484                printf("0\n0\n9\n");
485                pvect(vlist[p0i[0]]);
486                pvect(vlist[p1i[0]]);
487                pvect(vlist[p2i[0]]);
488        }
489        if (ok2) {
490                printf("\n%s ", matname);
491                if (axis != -1) {
492                        printf("texfunc %s\n", TEXNAME);
493                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
494                        printf("0\n13\t%d\n", axis);
495                        pvect(norm[0]);
496                        pvect(norm[1]);
497                        pvect(norm[2]);
498                        fvsum(v2, norm[3], vc2, -1.0);
499                        pvect(v2);
500                        printf("\n%s ", TEXNAME);
501                }
502                printf("polygon %s.%d\n", objname, ++nfaces);
503                printf("0\n0\n9\n");
504                pvect(vlist[p2i[0]]);
505                pvect(vlist[p1i[0]]);
506                pvect(vlist[p3i[0]]);
507        }
674          return(1);
675   }
676  
677  
678 < int
679 < norminterp(resmat, p0i, p1i, p2i, p3i)  /* compute normal interpolation */
514 < register FVECT  resmat[4];
515 < register VNDX  p0i, p1i, p2i, p3i;
678 > void
679 > freeverts(void)                 /* free all vertices */
680   {
517 #define u  ((ax+1)%3)
518 #define v  ((ax+2)%3)
519
520        register int  ax;
521        MAT4  eqnmat;
522        FVECT  v1;
523        register int  i, j;
524
525        if (!(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0))
526                return(-1);
527                                        /* find dominant axis */
528        VCOPY(v1, vnlist[p0i[2]]);
529        fvsum(v1, v1, vnlist[p1i[2]], 1.0);
530        fvsum(v1, v1, vnlist[p2i[2]], 1.0);
531        fvsum(v1, v1, vnlist[p3i[2]], 1.0);
532        ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1;
533        ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2;
534                                        /* assign equation matrix */
535        eqnmat[0][0] = vlist[p0i[0]][u]*vlist[p0i[0]][v];
536        eqnmat[0][1] = vlist[p0i[0]][u];
537        eqnmat[0][2] = vlist[p0i[0]][v];
538        eqnmat[0][3] = 1.0;
539        eqnmat[1][0] = vlist[p1i[0]][u]*vlist[p1i[0]][v];
540        eqnmat[1][1] = vlist[p1i[0]][u];
541        eqnmat[1][2] = vlist[p1i[0]][v];
542        eqnmat[1][3] = 1.0;
543        eqnmat[2][0] = vlist[p2i[0]][u]*vlist[p2i[0]][v];
544        eqnmat[2][1] = vlist[p2i[0]][u];
545        eqnmat[2][2] = vlist[p2i[0]][v];
546        eqnmat[2][3] = 1.0;
547        eqnmat[3][0] = vlist[p3i[0]][u]*vlist[p3i[0]][v];
548        eqnmat[3][1] = vlist[p3i[0]][u];
549        eqnmat[3][2] = vlist[p3i[0]][v];
550        eqnmat[3][3] = 1.0;
551                                        /* invert matrix (solve system) */
552        if (!invmat4(eqnmat, eqnmat))
553                return(-1);                     /* no solution */
554                                        /* compute result matrix */
555        for (j = 0; j < 4; j++)
556                for (i = 0; i < 3; i++)
557                        resmat[j][i] =  eqnmat[j][0]*vnlist[p0i[2]][i] +
558                                        eqnmat[j][1]*vnlist[p1i[2]][i] +
559                                        eqnmat[j][2]*vnlist[p2i[2]][i] +
560                                        eqnmat[j][3]*vnlist[p3i[2]][i];
561        return(ax);
562
563 #undef u
564 #undef v
565 }
566
567
568 freeverts()                     /* free all vertices */
569 {
681          if (nvs) {
682 <                free((char *)vlist);
682 >                free((void *)vlist);
683                  nvs = 0;
684          }
685          if (nvts) {
686 <                free((char *)vtlist);
686 >                free((void *)vtlist);
687                  nvts = 0;
688          }
689          if (nvns) {
690 <                free((char *)vnlist);
690 >                free((void *)vnlist);
691                  nvns = 0;
692          }
693   }
694  
695  
696   int
697 < newv(x, y, z)                   /* create a new vertex */
698 < double  x, y, z;
697 > newv(                   /* create a new vertex */
698 >        double  x,
699 >        double  y,
700 >        double  z
701 > )
702   {
703          if (!(nvs%CHUNKSIZ)) {          /* allocate next block */
704                  if (nvs == 0)
705                          vlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT));
706                  else
707 <                        vlist = (FVECT *)realloc((char *)vlist,
707 >                        vlist = (FVECT *)realloc((void *)vlist,
708                                          (nvs+CHUNKSIZ)*sizeof(FVECT));
709                  if (vlist == NULL) {
710                          fprintf(stderr,
# Line 607 | Line 721 | double x, y, z;
721  
722  
723   int
724 < newvn(x, y, z)                  /* create a new vertex normal */
725 < double  x, y, z;
724 > newvn(                  /* create a new vertex normal */
725 >        double  x,
726 >        double  y,
727 >        double  z
728 > )
729   {
730          if (!(nvns%CHUNKSIZ)) {         /* allocate next block */
731                  if (nvns == 0)
732                          vnlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT));
733                  else
734 <                        vnlist = (FVECT *)realloc((char *)vnlist,
734 >                        vnlist = (FVECT *)realloc((void *)vnlist,
735                                          (nvns+CHUNKSIZ)*sizeof(FVECT));
736                  if (vnlist == NULL) {
737                          fprintf(stderr,
# Line 633 | Line 750 | double x, y, z;
750  
751  
752   int
753 < newvt(x, y)                     /* create a new texture vertex */
754 < double  x, y;
753 > newvt(                  /* create a new texture map vertex */
754 >        double  x,
755 >        double  y
756 > )
757   {
758          if (!(nvts%CHUNKSIZ)) {         /* allocate next block */
759                  if (nvts == 0)
760 <                        vtlist = (FLOAT (*)[2])malloc(CHUNKSIZ*2*sizeof(FLOAT));
760 >                        vtlist = (RREAL (*)[2])malloc(CHUNKSIZ*2*sizeof(RREAL));
761                  else
762 <                        vtlist = (FLOAT (*)[2])realloc((char *)vtlist,
763 <                                        (nvts+CHUNKSIZ)*2*sizeof(FLOAT));
762 >                        vtlist = (RREAL (*)[2])realloc((void *)vtlist,
763 >                                        (nvts+CHUNKSIZ)*2*sizeof(RREAL));
764                  if (vtlist == NULL) {
765                          fprintf(stderr,
766                          "Out of memory while allocating texture vertex %d\n",
# Line 649 | Line 768 | double x, y;
768                          exit(1);
769                  }
770          }
771 <                                        /* assign new texture vertex */
771 >                                        /* assign new vertex */
772          vtlist[nvts][0] = x;
773          vtlist[nvts][1] = y;
774          return(++nvts);
775   }
776  
777  
778 < syntax(fn, ln, er)                      /* report syntax error and exit */
779 < char    *fn;
780 < int     ln;
781 < char    *er;
778 > void
779 > syntax(                 /* report syntax error and exit */
780 >        char    *er
781 > )
782   {
783          fprintf(stderr, "%s: Wavefront syntax error near line %d: %s\n",
784 <                        fn, ln, er);
784 >                        inpfile, lineno, er);
785          exit(1);
786   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines