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.15 by greg, Wed Jun 22 12:35:59 1994 UTC

# Line 17 | Line 17 | static char SCCSid[] = "$SunId$ LBL";
17  
18   #include "trans.h"
19  
20 + #include "tmesh.h"
21 +
22   #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 */
# Line 37 | Line 35 | int    nvns;
35   FLOAT   (*vtlist)[2];           /* map vertex list */
36   int     nvts;
37  
40 typedef FLOAT   BARYCCM[3][4];  /* barycentric coordinate system */
41
38   typedef int     VNDX[3];        /* vertex index (point,map,normal) */
39  
40   #define CHUNKSIZ        256     /* vertex allocation chunk size */
# Line 130 | Line 126 | char   *argv[];
126          }
127          exit(0);
128   userr:
129 <        fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n] [file.obj]\n",
129 >        fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n",
130                          argv[0]);
131          exit(1);
132   }
# Line 237 | Line 233 | FILE   *fp;
233                                  if (!puttri(argv[1], argv[2], argv[3]))
234                                          syntax("Bad triangle");
235                                  break;
240                        case 4:
241                                if (!putquad(argv[1], argv[2],
242                                                argv[3], argv[4]))
243                                        syntax("Bad quad");
244                                break;
236                          default:
237                                  if (!putface(argc-1, argv+1))
238                                          syntax("Bad face");
# Line 440 | Line 431 | register char  *vs;
431                  if (vi[0]-- > nvs)
432                          return(0);
433          } else if (vi[0] < 0) {
434 <                vi[0] = nvs + vi[0];
434 >                vi[0] += nvs;
435                  if (vi[0] < 0)
436                          return(0);
437          } else
# Line 454 | Line 445 | register char  *vs;
445                  if (vi[1]-- > nvts)
446                          return(0);
447          } else if (vi[1] < 0) {
448 <                vi[1] = nvts + vi[1];
448 >                vi[1] += nvts;
449                  if (vi[1] < 0)
450                          return(0);
451          } else
# Line 468 | Line 459 | register char  *vs;
459                  if (vi[2]-- > nvns)
460                          return(0);
461          } else if (vi[2] < 0) {
462 <                vi[2] = nvns + vi[2];
462 >                vi[2] += nvns;
463                  if (vi[2] < 0)
464                          return(0);
465          } else
# Line 477 | Line 468 | register char  *vs;
468   }
469  
470  
471 < nonplanar(ac, av)                       /* are vertices are non-planar? */
471 > nonplanar(ac, av)                       /* are vertices non-planar? */
472   register int    ac;
473   register char   **av;
474   {
# Line 489 | Line 480 | register char  **av;
480  
481          if (!cvtndx(vi, av[0]))
482                  return(0);
483 <        if (vi[2] >= 0)
483 >        if (!flatten && vi[2] >= 0)
484                  return(1);              /* has interpolated normals */
485          if (ac < 4)
486                  return(0);              /* it's a triangle! */
# Line 536 | Line 527 | register char  **av;
527          char    *cp;
528          register int    i;
529  
530 <        if (nonplanar(ac, av)) {        /* break into quads and triangles */
531 <                while (ac > 3) {
532 <                        if (!putquad(av[0], av[1], av[2], av[3]))
530 >        if (nonplanar(ac, av)) {        /* break into triangles */
531 >                while (ac > 2) {
532 >                        if (!puttri(av[0], av[1], av[2]))
533                                  return(0);
534 <                        ac -= 2;        /* remove two vertices & rotate */
534 >                        ac--;           /* remove vertex & rotate */
535                          cp = av[0];
536                          for (i = 0; i < ac-1; i++)
537 <                                av[i] = av[i+3];
537 >                                av[i] = av[i+2];
538                          av[i] = cp;
539                  }
549                if (ac == 3 && !puttri(av[0], av[1], av[2]))
550                        return(0);
540                  return(1);
541          }
542          if ((cp = getmtl()) == NULL)
# Line 569 | Line 558 | char   *v1, *v2, *v3;
558          char    *mod;
559          VNDX    v1i, v2i, v3i;
560          BARYCCM bvecs;
561 +        FLOAT   bcoor[3][3];
562          int     texOK, patOK;
563 +        register int    i;
564  
565          if ((mod = getmtl()) == NULL)
566                  return(-1);
# Line 584 | Line 575 | char   *v1, *v2, *v3;
575          patOK = 0;
576   #endif
577          if (texOK | patOK)
578 <                if (comp_baryc(bvecs, vlist[v1i[0]], vlist[v2i[0]],
578 >                if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]],
579                                  vlist[v3i[0]]) < 0)
580                          return(-1);
581                                          /* put out texture (if any) */
# Line 592 | Line 583 | char   *v1, *v2, *v3;
583                  printf("\n%s texfunc %s\n", mod, TEXNAME);
584                  mod = TEXNAME;
585                  printf("4 dx dy dz %s\n", TCALNAME);
586 <                printf("0\n21\n");
587 <                put_baryc(bvecs);
588 <                printf("\t%14.12g %14.12g %14.12g\n",
589 <                                vnlist[v1i[2]][0], vnlist[v2i[2]][0],
590 <                                vnlist[v3i[2]][0]);
591 <                printf("\t%14.12g %14.12g %14.12g\n",
592 <                                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]);
586 >                printf("0\n");
587 >                for (i = 0; i < 3; i++) {
588 >                        bcoor[i][0] = vnlist[v1i[2]][i];
589 >                        bcoor[i][1] = vnlist[v2i[2]][i];
590 >                        bcoor[i][2] = vnlist[v3i[2]][i];
591 >                }
592 >                put_baryc(&bvecs, bcoor, 3);
593          }
594   #ifdef TEXMAPS
595                                          /* put out pattern (if any) */
# Line 610 | Line 597 | char   *v1, *v2, *v3;
597                  printf("\n%s colorpict %s\n", mod, PATNAME);
598                  mod = PATNAME;
599                  printf("7 noneg noneg noneg %s %s u v\n", mapname, TCALNAME);
600 <                printf("0\n18\n");
601 <                put_baryc(bvecs);
602 <                printf("\t%f %f %f\n", vtlist[v1i[1]][0],
603 <                                vtlist[v2i[1]][0], vtlist[v3i[1]][0]);
604 <                printf("\t%f %f %f\n", vtlist[v1i[1]][1],
605 <                                vtlist[v2i[1]][1], vtlist[v3i[1]][1]);
600 >                printf("0\n");
601 >                for (i = 0; i < 2; i++) {
602 >                        bcoor[i][0] = vtlist[v1i[1]][i];
603 >                        bcoor[i][1] = vtlist[v2i[1]][i];
604 >                        bcoor[i][2] = vtlist[v3i[1]][i];
605 >                }
606 >                put_baryc(&bvecs, bcoor, 2);
607          }
608   #endif
609                                          /* put out triangle */
# Line 626 | Line 614 | char   *v1, *v2, *v3;
614          pvect(vlist[v3i[0]]);
615  
616          return(1);
629 }
630
631
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;
666        }
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        }
780        return(1);
781 }
782
783
784 int
785 norminterp(resmat, p0i, p1i, p2i, p3i)  /* compute normal interpolation */
786 register FVECT  resmat[4];
787 register VNDX  p0i, p1i, p2i, p3i;
788 {
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
617   }
618  
619  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines