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.7 by greg, Thu Apr 14 13:36:18 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   *
# Line 17 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
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 435 | 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 449 | 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 463 | 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 472 | 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 484 | 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 531 | Line 524 | register char  **av;
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 <                        ac -= 2;        /* remove two vertices & rotate */
531 >                        ac--;           /* remove vertex & rotate */
532                          cp = av[0];
533                          for (i = 0; i < ac-1; i++)
534 <                                av[i] = av[i+3];
534 >                                av[i] = av[i+2];
535                          av[i] = cp;
536                  }
544                if (ac == 3 && !puttri(av[0], av[1], av[2]))
545                        return(0);
537                  return(1);
538          }
539          if ((cp = getmtl()) == NULL)
# Line 564 | 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 572 | 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 587 | 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],
597 <                                vnlist[v3i[2]][1]);
598 <                printf("\t%14.12g %14.12g %14.12g\n",
599 <                                vnlist[v1i[2]][2], vnlist[v2i[2]][2],
600 <                                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) */
# Line 605 | Line 614 | char   *v1, *v2, *v3;
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   #endif
626 <                                        /* put out triangle */
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 <
627 < int
628 < comp_baryc(bcm, v1, v2, v3)             /* compute barycentric vectors */
629 < register BARYCCM        bcm;
630 < FLOAT   *v1, *v2, *v3;
631 < {
632 <        FLOAT   *vt;
633 <        FVECT   va, vab, vcb;
634 <        double  d;
635 <        register int    i, j;
636 <
637 <        for (j = 0; j < 3; j++) {
638 <                for (i = 0; i < 3; i++) {
639 <                        vab[i] = v1[i] - v2[i];
640 <                        vcb[i] = v3[i] - v2[i];
641 <                }
642 <                d = DOT(vcb,vcb);
643 <                if (d <= FTINY)
644 <                        return(-1);
645 <                d = DOT(vcb,vab)/d;
646 <                for (i = 0; i < 3; i++)
647 <                        va[i] = vab[i] - vcb[i]*d;
648 <                d = DOT(va,va);
649 <                if (d <= FTINY)
650 <                        return(-1);
651 <                for (i = 0; i < 3; i++) {
652 <                        va[i] /= d;
653 <                        bcm[j][i] = va[i];
654 <                }
655 <                bcm[j][3] = -DOT(v2,va);
656 <                                        /* rotate vertices */
657 <                vt = v1;
658 <                v1 = v2;
659 <                v2 = v3;
660 <                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          }
662        return(0);
663 }
664
665
666 put_baryc(bcm)                          /* put barycentric coord. vectors */
667 register BARYCCM        bcm;
668 {
669        register int    i;
670
671        for (i = 0; i < 3; i++)
672                printf("%14.8f %14.8f %14.8f %14.8f\n",
673                                bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]);
674 }
675
676
677 putquad(p0, p1, p3, p2)                 /* put out a quadrilateral */
678 char  *p0, *p1, *p3, *p2;               /* names correspond to binary pos. */
679 {
680        VNDX  p0i, p1i, p2i, p3i;
681        FVECT  norm[4];
682        char  *mod, *name;
683        int  axis;
684        FVECT  v1, v2, vc1, vc2;
685        int  ok1, ok2;
686
687 #ifdef TEXMAPS
688        /* also should output texture index coordinates,
689         * which will require new .cal file
690         */
691 #endif
692        if ((mod = getmtl()) == NULL)
693                return(-1);
694        name = getonm();
695                                        /* get actual indices */
696        if (!cvtndx(p0i,p0) || !cvtndx(p1i,p1) ||
697                        !cvtndx(p2i,p2) || !cvtndx(p3i,p3))
698                return(0);
699                                        /* compute exact normals */
700        fvsum(v1, vlist[p1i[0]], vlist[p0i[0]], -1.0);
701        fvsum(v2, vlist[p2i[0]], vlist[p0i[0]], -1.0);
702        fcross(vc1, v1, v2);
703        ok1 = normalize(vc1) != 0.0;
704        fvsum(v1, vlist[p2i[0]], vlist[p3i[0]], -1.0);
705        fvsum(v2, vlist[p1i[0]], vlist[p3i[0]], -1.0);
706        fcross(vc2, v1, v2);
707        ok2 = normalize(vc2) != 0.0;
708        if (!(ok1 | ok2))
709                return(-1);
710                                        /* compute normal interpolation */
711        axis = norminterp(norm, p0i, p1i, p2i, p3i);
712
713                                        /* put out quadrilateral? */
714        if (ok1 & ok2 && fabs(fdot(vc1,vc2)) >= 1.0-FTINY) {
715                printf("\n%s ", mod);
716                if (axis != -1) {
717                        printf("texfunc %s\n", TEXNAME);
718                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
719                        printf("0\n13\t%d\n", axis);
720                        pvect(norm[0]);
721                        pvect(norm[1]);
722                        pvect(norm[2]);
723                        fvsum(v1, norm[3], vc1, -0.5);
724                        fvsum(v1, v1, vc2, -0.5);
725                        pvect(v1);
726                        printf("\n%s ", TEXNAME);
727                }
728                printf("polygon %s.%d\n", name, faceno);
729                printf("0\n0\n12\n");
730                pvect(vlist[p0i[0]]);
731                pvect(vlist[p1i[0]]);
732                pvect(vlist[p3i[0]]);
733                pvect(vlist[p2i[0]]);
734                return(1);
735        }
736                                        /* put out triangles? */
737        if (ok1) {
738                printf("\n%s ", mod);
739                if (axis != -1) {
740                        printf("texfunc %s\n", TEXNAME);
741                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
742                        printf("0\n13\t%d\n", axis);
743                        pvect(norm[0]);
744                        pvect(norm[1]);
745                        pvect(norm[2]);
746                        fvsum(v1, norm[3], vc1, -1.0);
747                        pvect(v1);
748                        printf("\n%s ", TEXNAME);
749                }
750                printf("polygon %s.%da\n", name, faceno);
751                printf("0\n0\n9\n");
752                pvect(vlist[p0i[0]]);
753                pvect(vlist[p1i[0]]);
754                pvect(vlist[p2i[0]]);
755        }
756        if (ok2) {
757                printf("\n%s ", mod);
758                if (axis != -1) {
759                        printf("texfunc %s\n", TEXNAME);
760                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
761                        printf("0\n13\t%d\n", axis);
762                        pvect(norm[0]);
763                        pvect(norm[1]);
764                        pvect(norm[2]);
765                        fvsum(v2, norm[3], vc2, -1.0);
766                        pvect(v2);
767                        printf("\n%s ", TEXNAME);
768                }
769                printf("polygon %s.%db\n", name, faceno);
770                printf("0\n0\n9\n");
771                pvect(vlist[p2i[0]]);
772                pvect(vlist[p1i[0]]);
773                pvect(vlist[p3i[0]]);
774        }
638          return(1);
639   }
640  
641  
779 int
780 norminterp(resmat, p0i, p1i, p2i, p3i)  /* compute normal interpolation */
781 register FVECT  resmat[4];
782 register VNDX  p0i, p1i, p2i, p3i;
783 {
784 #define u  ((ax+1)%3)
785 #define v  ((ax+2)%3)
786
787        register int  ax;
788        MAT4  eqnmat;
789        FVECT  v1;
790        register int  i, j;
791
792 #ifdef TEXMAPS
793        /* also check for texture indices */
794 #endif
795        if (!(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0))
796                return(-1);
797                                        /* find dominant axis */
798        VCOPY(v1, vnlist[p0i[2]]);
799        fvsum(v1, v1, vnlist[p1i[2]], 1.0);
800        fvsum(v1, v1, vnlist[p2i[2]], 1.0);
801        fvsum(v1, v1, vnlist[p3i[2]], 1.0);
802        ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1;
803        ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2;
804                                        /* assign equation matrix */
805        eqnmat[0][0] = vlist[p0i[0]][u]*vlist[p0i[0]][v];
806        eqnmat[0][1] = vlist[p0i[0]][u];
807        eqnmat[0][2] = vlist[p0i[0]][v];
808        eqnmat[0][3] = 1.0;
809        eqnmat[1][0] = vlist[p1i[0]][u]*vlist[p1i[0]][v];
810        eqnmat[1][1] = vlist[p1i[0]][u];
811        eqnmat[1][2] = vlist[p1i[0]][v];
812        eqnmat[1][3] = 1.0;
813        eqnmat[2][0] = vlist[p2i[0]][u]*vlist[p2i[0]][v];
814        eqnmat[2][1] = vlist[p2i[0]][u];
815        eqnmat[2][2] = vlist[p2i[0]][v];
816        eqnmat[2][3] = 1.0;
817        eqnmat[3][0] = vlist[p3i[0]][u]*vlist[p3i[0]][v];
818        eqnmat[3][1] = vlist[p3i[0]][u];
819        eqnmat[3][2] = vlist[p3i[0]][v];
820        eqnmat[3][3] = 1.0;
821                                        /* invert matrix (solve system) */
822        if (!invmat4(eqnmat, eqnmat))
823                return(-1);                     /* no solution */
824                                        /* compute result matrix */
825        for (j = 0; j < 4; j++)
826                for (i = 0; i < 3; i++)
827                        resmat[j][i] =  eqnmat[j][0]*vnlist[p0i[2]][i] +
828                                        eqnmat[j][1]*vnlist[p1i[2]][i] +
829                                        eqnmat[j][2]*vnlist[p2i[2]][i] +
830                                        eqnmat[j][3]*vnlist[p3i[2]][i];
831 #ifdef TEXMAPS
832        /* compute result matrix for texture indices */
833 #endif
834        return(ax);
835
836 #undef u
837 #undef v
838 }
839
840
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