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.14 by greg, Wed Jun 15 19:06:04 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 */
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  
28 #define  ABS(x)         ((x)>=0 ? (x) : -(x))
29
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 36 | Line 32 | int    nvns;
32   FLOAT   (*vtlist)[2];           /* map vertex list */
33   int     nvts;
34  
39 typedef struct {
40        int     ax;             /* major axis */
41        FLOAT   tm[2][3];       /* transformation */
42 } BARYCCM;
43
35   typedef int     VNDX[3];        /* vertex index (point,map,normal) */
36  
37   #define CHUNKSIZ        256     /* vertex allocation chunk size */
# Line 566 | Line 557 | char   *v1, *v2, *v3;
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)
# Line 574 | 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 612 | Line 623 | char   *v1, *v2, *v3;
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 <
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 >        }
638          return(1);
639   }
640  
641  
626 int
627 comp_baryc(bcm, v1, v2, v3)             /* compute barycentric vectors */
628 register BARYCCM        *bcm;
629 FLOAT   *v1, *v2, *v3;
630 {
631        FLOAT   *vt;
632        FVECT   va, vab, vcb;
633        double  d;
634        int     ax0, ax1;
635        register int    i, j;
636                                        /* compute major axis */
637        for (i = 0; i < 3; i++) {
638                vab[i] = v1[i] - v2[i];
639                vcb[i] = v3[i] - v2[i];
640        }
641        fcross(va, vab, vcb);
642        bcm->ax = ABS(va[0]) > ABS(va[1]) ? 0 : 1;
643        bcm->ax = ABS(va[bcm->ax]) > ABS(va[2]) ? bcm->ax : 2;
644        ax0 = (bcm->ax + 1) % 3;
645        ax1 = (bcm->ax + 2) % 3;
646        for (j = 0; j < 2; j++) {
647                vab[0] = v1[ax0] - v2[ax0];
648                vcb[0] = v3[ax0] - v2[ax0];
649                vab[1] = v1[ax1] - v2[ax1];
650                vcb[1] = v3[ax1] - v2[ax1];
651                d = vcb[0]*vcb[0] + vcb[1]*vcb[1];
652                if (d <= FTINY)
653                        return(-1);
654                d = (vcb[0]*vab[0]+vcb[1]*vab[1])/d;
655                va[0] = vab[0] - vcb[0]*d;
656                va[1] = vab[1] - vcb[1]*d;
657                d = va[0]*va[0] + va[1]*va[1];
658                if (d <= FTINY)
659                        return(-1);
660                bcm->tm[j][0] = va[0] /= d;
661                bcm->tm[j][1] = va[1] /= d;
662                bcm->tm[j][2] = -(v2[ax0]*va[0]+v2[ax1]*va[1]);
663                                        /* rotate vertices */
664                vt = v1;
665                v1 = v2;
666                v2 = v3;
667                v3 = vt;
668        }
669        return(0);
670 }
671
672
673 put_baryc(bcm, com, n)                  /* put barycentric coord. vectors */
674 register BARYCCM        *bcm;
675 register FLOAT  com[][3];
676 int     n;
677 {
678        double  a, b;
679        register int    i, j;
680
681        printf("%d\t%d\n", 1+3*n, bcm->ax);
682        for (i = 0; i < n; i++) {
683                a = com[i][0] - com[i][2];
684                b = com[i][1] - com[i][2];
685                printf("%14.8f %14.8f %14.8f\n",
686                        bcm->tm[0][0]*a + bcm->tm[1][0]*b,
687                        bcm->tm[0][1]*a + bcm->tm[1][1]*b,
688                        bcm->tm[0][2]*a + bcm->tm[1][2]*b + com[i][2]);
689        }
690 }
691
692
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