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.11 by greg, Wed Jun 15 12:50:37 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 486 | 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 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 = !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 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\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],
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\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 <
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  
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        int     ax0, ax1;
636        register int    i, j;
637                                        /* compute major axis */
638        for (i = 0; i < 3; i++) {
639                vab[i] = v1[i] - v2[i];
640                vcb[i] = v3[i] - v2[i];
641        }
642        fcross(va, vab, vcb);
643        bcm->ax = ABS(va[0]) > ABS(va[1]) ? 0 : 1;
644        bcm->ax = ABS(va[bcm->ax]) > ABS(va[2]) ? bcm->ax : 2;
645        ax0 = (bcm->ax + 1) % 3;
646        ax1 = (bcm->ax + 2) % 3;
647        for (j = 0; j < 2; j++) {
648                vab[0] = v1[ax0] - v2[ax0];
649                vcb[0] = v3[ax0] - v2[ax0];
650                vab[1] = v1[ax1] - v2[ax1];
651                vcb[1] = v3[ax1] - v2[ax1];
652                d = vcb[0]*vcb[0] + vcb[1]*vcb[1];
653                if (d <= FTINY)
654                        return(-1);
655                d = (vcb[0]*vab[0]+vcb[1]*vab[1])/d;
656                va[0] = vab[0] - vcb[0]*d;
657                va[1] = vab[1] - vcb[1]*d;
658                d = va[0]*va[0] + va[1]*va[1];
659                if (d <= FTINY)
660                        return(-1);
661                bcm->tm[j][0] = va[0] /= d;
662                bcm->tm[j][1] = va[1] /= d;
663                bcm->tm[j][2] = -(v2[ax0]*va[0]+v2[ax1]*va[1]);
664                                        /* rotate vertices */
665                vt = v1;
666                v1 = v2;
667                v2 = v3;
668                v3 = vt;
669        }
670        return(0);
671 }
672
673
674 put_baryc(bcm)                          /* put barycentric coord. vectors */
675 register BARYCCM        *bcm;
676 {
677        printf("\t%d\n", bcm->ax);
678        printf("%14.8f %14.8f %14.8f\n",
679                                bcm->tm[0][0], bcm->tm[0][1], bcm->tm[0][2]);
680        printf("%14.8f %14.8f %14.8f\n",
681                                bcm->tm[1][0], bcm->tm[1][1], bcm->tm[1][2]);
682 }
683
684
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