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

Comparing ray/src/cv/obj2rad.c (file contents):
Revision 2.6 by greg, Thu Apr 14 04:29:10 1994 UTC vs.
Revision 2.18 by greg, Tue Mar 4 01:42:29 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.  Non-planar
8   * faces are broken rather haphazardly into triangles.
9   * Also, texture map indices only work for triangles, though
10 < * I'm not sure they work correctly.
10 > * I'm not sure they work correctly.  (Taken out -- see TEXMAPS defines.)
11   */
12  
13   #include "standard.h"
14  
15   #include "trans.h"
16  
17 + #include "tmesh.h"
18 +
19   #include <ctype.h>
20  
22 #define TCALNAME        "tmesh.cal"     /* triangle interp. file */
23 #define QCALNAME        "surf.cal"      /* quad interp. file */
21   #define PATNAME         "M-pat"         /* mesh pattern name (reused) */
22   #define TEXNAME         "M-nor"         /* mesh texture name (reused) */
23   #define DEFOBJ          "unnamed"       /* default object name */
24   #define DEFMAT          "white"         /* default material name */
25  
29 #define  ABS(x)         ((x)>=0 ? (x) : -(x))
30
26   #define pvect(v)        printf("%18.12g %18.12g %18.12g\n",(v)[0],(v)[1],(v)[2])
27  
28   FVECT   *vlist;                 /* our vertex list */
# Line 37 | Line 32 | int    nvns;
32   FLOAT   (*vtlist)[2];           /* map vertex list */
33   int     nvts;
34  
40 typedef FLOAT   BARYCCM[3][4];  /* barycentric coordinate system */
41
35   typedef int     VNDX[3];        /* vertex index (point,map,normal) */
36  
37   #define CHUNKSIZ        256     /* vertex allocation chunk size */
# Line 70 | Line 63 | RULEHD *ourmapping = NULL;
63   char    *defmat = DEFMAT;       /* default (starting) material name */
64   char    *defobj = DEFOBJ;       /* default (starting) object name */
65  
66 + int     flatten = 0;            /* discard surface normal information */
67 +
68   char    *getmtl(), *getonm();
69  
70   char    mapname[128];           /* current picture file */
# Line 99 | Line 94 | char   *argv[];
94                  case 'm':               /* use custom mapfile */
95                          ourmapping = getmapping(argv[++i], &qlist);
96                          break;
97 +                case 'f':               /* flatten surfaces */
98 +                        flatten++;
99 +                        break;
100                  default:
101                          goto userr;
102                  }
# Line 125 | Line 123 | char   *argv[];
123          }
124          exit(0);
125   userr:
126 <        fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n] [file.obj]\n",
126 >        fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n",
127                          argv[0]);
128          exit(1);
129   }
# Line 232 | Line 230 | FILE   *fp;
230                                  if (!puttri(argv[1], argv[2], argv[3]))
231                                          syntax("Bad triangle");
232                                  break;
235                        case 4:
236                                if (!putquad(argv[1], argv[2],
237                                                argv[3], argv[4]))
238                                        syntax("Bad quad");
239                                break;
233                          default:
234                                  if (!putface(argc-1, argv+1))
235                                          syntax("Bad face");
# Line 254 | Line 247 | FILE   *fp;
247                                  if (!strcmp(argv[1], "off"))
248                                          mapname[0] = '\0';
249                                  else
250 <                                        strcpy(mapname, argv[1]);
250 >                                        sprintf(mapname, "%s.pic", argv[1]);
251                          } else
252                                  goto unknown;
253                          break;
# Line 273 | Line 266 | FILE   *fp;
266                          group[i-1][0] = '\0';
267                          break;
268                  case '#':               /* comment */
269 +                        printargs(argc, argv, stdout);
270                          break;
271                  default:;               /* something we don't deal with */
272                  unknown:
# Line 434 | Line 428 | register char  *vs;
428                  if (vi[0]-- > nvs)
429                          return(0);
430          } else if (vi[0] < 0) {
431 <                vi[0] = nvs + vi[0];
431 >                vi[0] += nvs;
432                  if (vi[0] < 0)
433                          return(0);
434          } else
# Line 448 | Line 442 | register char  *vs;
442                  if (vi[1]-- > nvts)
443                          return(0);
444          } else if (vi[1] < 0) {
445 <                vi[1] = nvts + vi[1];
445 >                vi[1] += nvts;
446                  if (vi[1] < 0)
447                          return(0);
448          } else
# Line 462 | Line 456 | register char  *vs;
456                  if (vi[2]-- > nvns)
457                          return(0);
458          } else if (vi[2] < 0) {
459 <                vi[2] = nvns + vi[2];
459 >                vi[2] += nvns;
460                  if (vi[2] < 0)
461                          return(0);
462          } else
# Line 471 | Line 465 | register char  *vs;
465   }
466  
467  
468 < nonplanar(ac, av)                       /* are vertices are non-planar? */
468 > nonplanar(ac, av)                       /* are vertices non-planar? */
469   register int    ac;
470   register char   **av;
471   {
# Line 483 | Line 477 | register char  **av;
477  
478          if (!cvtndx(vi, av[0]))
479                  return(0);
480 <        if (vi[2] >= 0)
480 >        if (!flatten && vi[2] >= 0)
481                  return(1);              /* has interpolated normals */
482          if (ac < 4)
483                  return(0);              /* it's a triangle! */
# Line 527 | Line 521 | int    ac;
521   register char   **av;
522   {
523          VNDX    vi;
524 <        char    *mod;
524 >        char    *cp;
525          register int    i;
526  
527 <        if (nonplanar(ac, av)) {        /* break into quads and triangles */
528 <                while (ac > 3) {
529 <                        if (!putquad(av[0], av[1], av[2], av[3]))
527 >        if (nonplanar(ac, av)) {        /* break into triangles */
528 >                while (ac > 2) {
529 >                        if (!puttri(av[0], av[1], av[2]))
530                                  return(0);
531 <                                        /* remove two vertices & rotate */
532 <                        av[ac -= 2] = av[0];
533 <                        for (i = 0; i <= ac; i++)
531 >                        ac--;           /* remove vertex & rotate */
532 >                        cp = av[0];
533 >                        for (i = 0; i < ac-1; i++)
534                                  av[i] = av[i+2];
535 +                        av[i] = cp;
536                  }
542                if (ac == 3 && !puttri(av[0], av[1], av[2]))
543                        return(0);
537                  return(1);
538          }
539 <        if ((mod = getmtl()) == NULL)
539 >        if ((cp = getmtl()) == NULL)
540                  return(-1);
541 <        printf("\n%s polygon %s.%d\n", mod, getonm(), faceno);
541 >        printf("\n%s polygon %s.%d\n", cp, getonm(), faceno);
542          printf("0\n0\n%d\n", 3*ac);
543          for (i = 0; i < ac; i++) {
544                  if (!cvtndx(vi, av[i]))
# Line 562 | Line 555 | char   *v1, *v2, *v3;
555          char    *mod;
556          VNDX    v1i, v2i, v3i;
557          BARYCCM bvecs;
558 +        FLOAT   bcoor[3][3];
559          int     texOK, patOK;
560 +        int     flatness;
561 +        register int    i;
562  
563          if ((mod = getmtl()) == NULL)
564                  return(-1);
# Line 570 | Line 566 | char   *v1, *v2, *v3;
566          if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3))
567                  return(0);
568                                          /* compute barycentric coordinates */
569 <        texOK = (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0);
569 >        if (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0)
570 >                flatness = flat_tri(vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]],
571 >                                vnlist[v1i[2]], vnlist[v2i[2]], vnlist[v3i[2]]);
572 >        else
573 >                flatness = ISFLAT;
574 >
575 >        switch (flatness) {
576 >        case DEGEN:                     /* zero area */
577 >                return(-1);
578 >        case RVFLAT:                    /* reversed normals, but flat */
579 >        case ISFLAT:                    /* smoothing unnecessary */
580 >                texOK = 0;
581 >                break;
582 >        case RVBENT:                    /* reversed normals with smoothing */
583 >        case ISBENT:                    /* proper smoothing */
584 >                texOK = 1;
585 >                break;
586 >        }
587 >        if (flatten)
588 >                texOK = 0;
589 > #ifdef TEXMAPS
590          patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0);
591 + #else
592 +        patOK = 0;
593 + #endif
594          if (texOK | patOK)
595 <                if (comp_baryc(bvecs, vlist[v1i[0]], vlist[v2i[0]],
595 >                if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]],
596                                  vlist[v3i[0]]) < 0)
597                          return(-1);
598                                          /* put out texture (if any) */
# Line 581 | Line 600 | char   *v1, *v2, *v3;
600                  printf("\n%s texfunc %s\n", mod, TEXNAME);
601                  mod = TEXNAME;
602                  printf("4 dx dy dz %s\n", TCALNAME);
603 <                printf("0\n21\n");
604 <                put_baryc(bvecs);
605 <                printf("\t%14.12g %14.12g %14.12g\n",
606 <                                vnlist[v1i[2]][0], vnlist[v2i[2]][0],
607 <                                vnlist[v3i[2]][0]);
608 <                printf("\t%14.12g %14.12g %14.12g\n",
609 <                                vnlist[v1i[2]][1], vnlist[v2i[2]][1],
591 <                                vnlist[v3i[2]][1]);
592 <                printf("\t%14.12g %14.12g %14.12g\n",
593 <                                vnlist[v1i[2]][2], vnlist[v2i[2]][2],
594 <                                vnlist[v3i[2]][2]);
603 >                printf("0\n");
604 >                for (i = 0; i < 3; i++) {
605 >                        bcoor[i][0] = vnlist[v1i[2]][i];
606 >                        bcoor[i][1] = vnlist[v2i[2]][i];
607 >                        bcoor[i][2] = vnlist[v3i[2]][i];
608 >                }
609 >                put_baryc(&bvecs, bcoor, 3);
610          }
611 + #ifdef TEXMAPS
612                                          /* put out pattern (if any) */
613          if (patOK) {
614                  printf("\n%s colorpict %s\n", mod, PATNAME);
615                  mod = PATNAME;
616                  printf("7 noneg noneg noneg %s %s u v\n", mapname, TCALNAME);
617 <                printf("0\n18\n");
618 <                put_baryc(bvecs);
619 <                printf("\t%f %f %f\n", vtlist[v1i[1]][0],
620 <                                vtlist[v2i[1]][0], vtlist[v3i[1]][0]);
621 <                printf("\t%f %f %f\n", vtlist[v1i[1]][1],
622 <                                vtlist[v2i[1]][1], vtlist[v3i[1]][1]);
617 >                printf("0\n");
618 >                for (i = 0; i < 2; i++) {
619 >                        bcoor[i][0] = vtlist[v1i[1]][i];
620 >                        bcoor[i][1] = vtlist[v2i[1]][i];
621 >                        bcoor[i][2] = vtlist[v3i[1]][i];
622 >                }
623 >                put_baryc(&bvecs, bcoor, 2);
624          }
625 <                                        /* put out triangle */
625 > #endif
626 >                                        /* put out (reversed) triangle */
627          printf("\n%s polygon %s.%d\n", mod, getonm(), faceno);
628          printf("0\n0\n9\n");
629 <        pvect(vlist[v1i[0]]);
630 <        pvect(vlist[v2i[0]]);
631 <        pvect(vlist[v3i[0]]);
632 <
633 <        return(1);
634 < }
635 <
636 <
619 < int
620 < comp_baryc(bcm, v1, v2, v3)             /* compute barycentric vectors */
621 < register BARYCCM        bcm;
622 < FLOAT   *v1, *v2, *v3;
623 < {
624 <        FLOAT   *vt;
625 <        FVECT   va, vab, vcb;
626 <        double  d;
627 <        register int    i, j;
628 <
629 <        for (j = 0; j < 3; j++) {
630 <                for (i = 0; i < 3; i++) {
631 <                        vab[i] = v1[i] - v2[i];
632 <                        vcb[i] = v3[i] - v2[i];
633 <                }
634 <                d = DOT(vcb,vcb);
635 <                if (d <= FTINY)
636 <                        return(-1);
637 <                d = DOT(vcb,vab)/d;
638 <                for (i = 0; i < 3; i++)
639 <                        va[i] = vab[i] - vcb[i]*d;
640 <                d = DOT(va,va);
641 <                if (d <= FTINY)
642 <                        return(-1);
643 <                for (i = 0; i < 3; i++) {
644 <                        va[i] /= d;
645 <                        bcm[j][i] = va[i];
646 <                }
647 <                bcm[j][3] = -DOT(v2,va);
648 <                                        /* rotate vertices */
649 <                vt = v1;
650 <                v1 = v2;
651 <                v2 = v3;
652 <                v3 = vt;
629 >        if (flatness == RVFLAT || flatness == RVBENT) {
630 >                pvect(vlist[v3i[0]]);
631 >                pvect(vlist[v2i[0]]);
632 >                pvect(vlist[v1i[0]]);
633 >        } else {
634 >                pvect(vlist[v1i[0]]);
635 >                pvect(vlist[v2i[0]]);
636 >                pvect(vlist[v3i[0]]);
637          }
654        return(0);
655 }
656
657
658 put_baryc(bcm)                          /* put barycentric coord. vectors */
659 register BARYCCM        bcm;
660 {
661        register int    i;
662
663        for (i = 0; i < 3; i++)
664                printf("%14.8f %14.8f %14.8f %14.8f\n",
665                                bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]);
666 }
667
668
669 putquad(p0, p1, p3, p2)                 /* put out a quadrilateral */
670 char  *p0, *p1, *p3, *p2;               /* names correspond to binary pos. */
671 {
672        VNDX  p0i, p1i, p2i, p3i;
673        FVECT  norm[4];
674        char  *mod, *name;
675        int  axis;
676        FVECT  v1, v2, vc1, vc2;
677        int  ok1, ok2;
678
679        if ((mod = getmtl()) == NULL)
680                return(-1);
681        name = getonm();
682                                        /* get actual indices */
683        if (!cvtndx(p0i,p0) || !cvtndx(p1i,p1) ||
684                        !cvtndx(p2i,p2) || !cvtndx(p3i,p3))
685                return(0);
686                                        /* compute exact normals */
687        fvsum(v1, vlist[p1i[0]], vlist[p0i[0]], -1.0);
688        fvsum(v2, vlist[p2i[0]], vlist[p0i[0]], -1.0);
689        fcross(vc1, v1, v2);
690        ok1 = normalize(vc1) != 0.0;
691        fvsum(v1, vlist[p2i[0]], vlist[p3i[0]], -1.0);
692        fvsum(v2, vlist[p1i[0]], vlist[p3i[0]], -1.0);
693        fcross(vc2, v1, v2);
694        ok2 = normalize(vc2) != 0.0;
695        if (!(ok1 | ok2))
696                return(-1);
697                                        /* compute normal interpolation */
698        axis = norminterp(norm, p0i, p1i, p2i, p3i);
699
700                                        /* put out quadrilateral? */
701        if (ok1 & ok2 && fabs(fdot(vc1,vc2)) >= 1.0-FTINY) {
702                printf("\n%s ", mod);
703                if (axis != -1) {
704                        printf("texfunc %s\n", TEXNAME);
705                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
706                        printf("0\n13\t%d\n", axis);
707                        pvect(norm[0]);
708                        pvect(norm[1]);
709                        pvect(norm[2]);
710                        fvsum(v1, norm[3], vc1, -0.5);
711                        fvsum(v1, v1, vc2, -0.5);
712                        pvect(v1);
713                        printf("\n%s ", TEXNAME);
714                }
715                printf("polygon %s.%d\n", name, faceno);
716                printf("0\n0\n12\n");
717                pvect(vlist[p0i[0]]);
718                pvect(vlist[p1i[0]]);
719                pvect(vlist[p3i[0]]);
720                pvect(vlist[p2i[0]]);
721                return(1);
722        }
723                                        /* put out triangles? */
724        if (ok1) {
725                printf("\n%s ", mod);
726                if (axis != -1) {
727                        printf("texfunc %s\n", TEXNAME);
728                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
729                        printf("0\n13\t%d\n", axis);
730                        pvect(norm[0]);
731                        pvect(norm[1]);
732                        pvect(norm[2]);
733                        fvsum(v1, norm[3], vc1, -1.0);
734                        pvect(v1);
735                        printf("\n%s ", TEXNAME);
736                }
737                printf("polygon %s.%da\n", name, faceno);
738                printf("0\n0\n9\n");
739                pvect(vlist[p0i[0]]);
740                pvect(vlist[p1i[0]]);
741                pvect(vlist[p2i[0]]);
742        }
743        if (ok2) {
744                printf("\n%s ", mod);
745                if (axis != -1) {
746                        printf("texfunc %s\n", TEXNAME);
747                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
748                        printf("0\n13\t%d\n", axis);
749                        pvect(norm[0]);
750                        pvect(norm[1]);
751                        pvect(norm[2]);
752                        fvsum(v2, norm[3], vc2, -1.0);
753                        pvect(v2);
754                        printf("\n%s ", TEXNAME);
755                }
756                printf("polygon %s.%db\n", name, faceno);
757                printf("0\n0\n9\n");
758                pvect(vlist[p2i[0]]);
759                pvect(vlist[p1i[0]]);
760                pvect(vlist[p3i[0]]);
761        }
638          return(1);
639   }
640  
641  
766 int
767 norminterp(resmat, p0i, p1i, p2i, p3i)  /* compute normal interpolation */
768 register FVECT  resmat[4];
769 register VNDX  p0i, p1i, p2i, p3i;
770 {
771 #define u  ((ax+1)%3)
772 #define v  ((ax+2)%3)
773
774        register int  ax;
775        MAT4  eqnmat;
776        FVECT  v1;
777        register int  i, j;
778
779        if (!(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0))
780                return(-1);
781                                        /* find dominant axis */
782        VCOPY(v1, vnlist[p0i[2]]);
783        fvsum(v1, v1, vnlist[p1i[2]], 1.0);
784        fvsum(v1, v1, vnlist[p2i[2]], 1.0);
785        fvsum(v1, v1, vnlist[p3i[2]], 1.0);
786        ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1;
787        ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2;
788                                        /* assign equation matrix */
789        eqnmat[0][0] = vlist[p0i[0]][u]*vlist[p0i[0]][v];
790        eqnmat[0][1] = vlist[p0i[0]][u];
791        eqnmat[0][2] = vlist[p0i[0]][v];
792        eqnmat[0][3] = 1.0;
793        eqnmat[1][0] = vlist[p1i[0]][u]*vlist[p1i[0]][v];
794        eqnmat[1][1] = vlist[p1i[0]][u];
795        eqnmat[1][2] = vlist[p1i[0]][v];
796        eqnmat[1][3] = 1.0;
797        eqnmat[2][0] = vlist[p2i[0]][u]*vlist[p2i[0]][v];
798        eqnmat[2][1] = vlist[p2i[0]][u];
799        eqnmat[2][2] = vlist[p2i[0]][v];
800        eqnmat[2][3] = 1.0;
801        eqnmat[3][0] = vlist[p3i[0]][u]*vlist[p3i[0]][v];
802        eqnmat[3][1] = vlist[p3i[0]][u];
803        eqnmat[3][2] = vlist[p3i[0]][v];
804        eqnmat[3][3] = 1.0;
805                                        /* invert matrix (solve system) */
806        if (!invmat4(eqnmat, eqnmat))
807                return(-1);                     /* no solution */
808                                        /* compute result matrix */
809        for (j = 0; j < 4; j++)
810                for (i = 0; i < 3; i++)
811                        resmat[j][i] =  eqnmat[j][0]*vnlist[p0i[2]][i] +
812                                        eqnmat[j][1]*vnlist[p1i[2]][i] +
813                                        eqnmat[j][2]*vnlist[p2i[2]][i] +
814                                        eqnmat[j][3]*vnlist[p3i[2]][i];
815        return(ax);
816
817 #undef u
818 #undef v
819 }
820
821
642   freeverts()                     /* free all vertices */
643   {
644          if (nvs) {
645 <                free((char *)vlist);
645 >                free((void *)vlist);
646                  nvs = 0;
647          }
648          if (nvts) {
649 <                free((char *)vtlist);
649 >                free((void *)vtlist);
650                  nvts = 0;
651          }
652          if (nvns) {
653 <                free((char *)vnlist);
653 >                free((void *)vnlist);
654                  nvns = 0;
655          }
656   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines