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.10 by greg, Wed Jun 15 12:29:55 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 struct {
41        int     ax;             /* major axis */
42        FLOAT   tm[2][3];       /* transformation */
43 } BARYCCM;
44
35   typedef int     VNDX[3];        /* vertex index (point,map,normal) */
36  
37   #define CHUNKSIZ        256     /* vertex allocation chunk size */
# Line 240 | Line 230 | FILE   *fp;
230                                  if (!puttri(argv[1], argv[2], argv[3]))
231                                          syntax("Bad triangle");
232                                  break;
243                        case 4:
244                                if (!putquad(argv[1], argv[2],
245                                                argv[3], argv[4]))
246                                        syntax("Bad quad");
247                                break;
233                          default:
234                                  if (!putface(argc-1, argv+1))
235                                          syntax("Bad face");
# Line 492 | 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 539 | 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                  }
552                if (ac == 3 && !puttri(av[0], av[1], av[2]))
553                        return(0);
537                  return(1);
538          }
539          if ((cp = getmtl()) == NULL)
# Line 572 | 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 580 | 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 = !flatten && (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
# Line 595 | 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\n16 ");
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],
605 <                                vnlist[v3i[2]][1]);
606 <                printf("\t%14.12g %14.12g %14.12g\n",
607 <                                vnlist[v1i[2]][2], vnlist[v2i[2]][2],
608 <                                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 613 | 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\n13 ");
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 <
635 < int
636 < comp_baryc(bcm, v1, v2, v3)             /* compute barycentric vectors */
637 < register BARYCCM        *bcm;
638 < FLOAT   *v1, *v2, *v3;
639 < {
640 <        FLOAT   *vt;
641 <        FVECT   va, vab, vcb;
642 <        double  d;
643 <        int     ax0, ax1;
644 <        register int    i, j;
645 <                                        /* compute major axis */
646 <        for (i = 0; i < 3; i++) {
647 <                vab[i] = v1[i] - v2[i];
648 <                vcb[i] = v3[i] - v2[i];
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          }
650        fcross(va, vab, vcb);
651        bcm->ax = ABS(va[0]) > ABS(va[1]) ? 0 : 1;
652        bcm->ax = ABS(va[bcm->ax]) > ABS(va[2]) ? bcm->ax : 2;
653        ax0 = (bcm->ax + 1) % 3;
654        ax1 = (bcm->ax + 2) % 3;
655        for (j = 0; j < 2; j++) {
656                vab[0] = v1[ax0] - v2[ax0];
657                vcb[0] = v3[ax0] - v2[ax0];
658                vab[1] = v1[ax1] - v2[ax1];
659                vcb[1] = v3[ax1] - v2[ax1];
660                d = vcb[0]*vcb[0] + vcb[1]*vcb[1];
661                if (d <= FTINY)
662                        return(-1);
663                d = (vcb[0]*vab[0]+vcb[1]*vab[1])/d;
664                va[0] = vab[0] - vcb[0]*d;
665                va[1] = vab[1] - vcb[1]*d;
666                d = va[0]*va[0] + va[1]*va[1];
667                if (d <= FTINY)
668                        return(-1);
669                bcm->tm[j][0] = va[0] /= d;
670                bcm->tm[j][1] = va[1] /= d;
671                bcm->tm[j][2] = -(v2[ax0]*va[0]+v2[ax1]*va[1]);
672                                        /* rotate vertices */
673                vt = v1;
674                v1 = v2;
675                v2 = v3;
676                v3 = vt;
677        }
678        return(0);
679 }
680
681
682 put_baryc(bcm)                          /* put barycentric coord. vectors */
683 register BARYCCM        *bcm;
684 {
685        printf("\t%d\n", bcm->ax);
686        printf("%14.8f %14.8f %14.8f\n",
687                                bcm->tm[0][0], bcm->tm[0][1], bcm->tm[0][2]);
688        printf("%14.8f %14.8f %14.8f\n",
689                                bcm->tm[1][0], bcm->tm[1][1], bcm->tm[1][2]);
690 }
691
692
693 putquad(p0, p1, p3, p2)                 /* put out a quadrilateral */
694 char  *p0, *p1, *p3, *p2;               /* names correspond to binary pos. */
695 {
696        VNDX  p0i, p1i, p2i, p3i;
697        FVECT  norm[4];
698        char  *mod, *name;
699        int  axis;
700        FVECT  v1, v2, vc1, vc2;
701        int  ok1, ok2;
702
703 #ifdef TEXMAPS
704        /* also should output texture index coordinates,
705         * which will require new .cal file
706         */
707 #endif
708        if ((mod = getmtl()) == NULL)
709                return(-1);
710        name = getonm();
711                                        /* get actual indices */
712        if (!cvtndx(p0i,p0) || !cvtndx(p1i,p1) ||
713                        !cvtndx(p2i,p2) || !cvtndx(p3i,p3))
714                return(0);
715                                        /* compute exact normals */
716        fvsum(v1, vlist[p1i[0]], vlist[p0i[0]], -1.0);
717        fvsum(v2, vlist[p2i[0]], vlist[p0i[0]], -1.0);
718        fcross(vc1, v1, v2);
719        ok1 = normalize(vc1) != 0.0;
720        fvsum(v1, vlist[p2i[0]], vlist[p3i[0]], -1.0);
721        fvsum(v2, vlist[p1i[0]], vlist[p3i[0]], -1.0);
722        fcross(vc2, v1, v2);
723        ok2 = normalize(vc2) != 0.0;
724        if (!(ok1 | ok2))
725                return(-1);
726                                        /* compute normal interpolation */
727        axis = norminterp(norm, p0i, p1i, p2i, p3i);
728
729                                        /* put out quadrilateral? */
730        if (ok1 & ok2 && fabs(fdot(vc1,vc2)) >= 1.0-FTINY) {
731                printf("\n%s ", mod);
732                if (axis != -1) {
733                        printf("texfunc %s\n", TEXNAME);
734                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
735                        printf("0\n13\t%d\n", axis);
736                        pvect(norm[0]);
737                        pvect(norm[1]);
738                        pvect(norm[2]);
739                        fvsum(v1, norm[3], vc1, -0.5);
740                        fvsum(v1, v1, vc2, -0.5);
741                        pvect(v1);
742                        printf("\n%s ", TEXNAME);
743                }
744                printf("polygon %s.%d\n", name, faceno);
745                printf("0\n0\n12\n");
746                pvect(vlist[p0i[0]]);
747                pvect(vlist[p1i[0]]);
748                pvect(vlist[p3i[0]]);
749                pvect(vlist[p2i[0]]);
750                return(1);
751        }
752                                        /* put out triangles? */
753        if (ok1) {
754                printf("\n%s ", mod);
755                if (axis != -1) {
756                        printf("texfunc %s\n", TEXNAME);
757                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
758                        printf("0\n13\t%d\n", axis);
759                        pvect(norm[0]);
760                        pvect(norm[1]);
761                        pvect(norm[2]);
762                        fvsum(v1, norm[3], vc1, -1.0);
763                        pvect(v1);
764                        printf("\n%s ", TEXNAME);
765                }
766                printf("polygon %s.%da\n", name, faceno);
767                printf("0\n0\n9\n");
768                pvect(vlist[p0i[0]]);
769                pvect(vlist[p1i[0]]);
770                pvect(vlist[p2i[0]]);
771        }
772        if (ok2) {
773                printf("\n%s ", mod);
774                if (axis != -1) {
775                        printf("texfunc %s\n", TEXNAME);
776                        printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME);
777                        printf("0\n13\t%d\n", axis);
778                        pvect(norm[0]);
779                        pvect(norm[1]);
780                        pvect(norm[2]);
781                        fvsum(v2, norm[3], vc2, -1.0);
782                        pvect(v2);
783                        printf("\n%s ", TEXNAME);
784                }
785                printf("polygon %s.%db\n", name, faceno);
786                printf("0\n0\n9\n");
787                pvect(vlist[p2i[0]]);
788                pvect(vlist[p1i[0]]);
789                pvect(vlist[p3i[0]]);
790        }
638          return(1);
639   }
640  
641  
795 int
796 norminterp(resmat, p0i, p1i, p2i, p3i)  /* compute normal interpolation */
797 register FVECT  resmat[4];
798 register VNDX  p0i, p1i, p2i, p3i;
799 {
800 #define u  ((ax+1)%3)
801 #define v  ((ax+2)%3)
802
803        register int  ax;
804        MAT4  eqnmat;
805        FVECT  v1;
806        register int  i, j;
807
808 #ifdef TEXMAPS
809        /* also check for texture indices */
810 #endif
811        if (flatten || !(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0))
812                return(-1);
813                                        /* find dominant axis */
814        VCOPY(v1, vnlist[p0i[2]]);
815        fvsum(v1, v1, vnlist[p1i[2]], 1.0);
816        fvsum(v1, v1, vnlist[p2i[2]], 1.0);
817        fvsum(v1, v1, vnlist[p3i[2]], 1.0);
818        ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1;
819        ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2;
820                                        /* assign equation matrix */
821        eqnmat[0][0] = vlist[p0i[0]][u]*vlist[p0i[0]][v];
822        eqnmat[0][1] = vlist[p0i[0]][u];
823        eqnmat[0][2] = vlist[p0i[0]][v];
824        eqnmat[0][3] = 1.0;
825        eqnmat[1][0] = vlist[p1i[0]][u]*vlist[p1i[0]][v];
826        eqnmat[1][1] = vlist[p1i[0]][u];
827        eqnmat[1][2] = vlist[p1i[0]][v];
828        eqnmat[1][3] = 1.0;
829        eqnmat[2][0] = vlist[p2i[0]][u]*vlist[p2i[0]][v];
830        eqnmat[2][1] = vlist[p2i[0]][u];
831        eqnmat[2][2] = vlist[p2i[0]][v];
832        eqnmat[2][3] = 1.0;
833        eqnmat[3][0] = vlist[p3i[0]][u]*vlist[p3i[0]][v];
834        eqnmat[3][1] = vlist[p3i[0]][u];
835        eqnmat[3][2] = vlist[p3i[0]][v];
836        eqnmat[3][3] = 1.0;
837                                        /* invert matrix (solve system) */
838        if (!invmat4(eqnmat, eqnmat))
839                return(-1);                     /* no solution */
840                                        /* compute result matrix */
841        for (j = 0; j < 4; j++)
842                for (i = 0; i < 3; i++)
843                        resmat[j][i] =  eqnmat[j][0]*vnlist[p0i[2]][i] +
844                                        eqnmat[j][1]*vnlist[p1i[2]][i] +
845                                        eqnmat[j][2]*vnlist[p2i[2]][i] +
846                                        eqnmat[j][3]*vnlist[p3i[2]][i];
847 #ifdef TEXMAPS
848        /* compute result matrix for texture indices */
849 #endif
850        return(ax);
851
852 #undef u
853 #undef v
854 }
855
856
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