ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/tmesh2rad.c
(Generate patch)

Comparing ray/src/cv/tmesh2rad.c (file contents):
Revision 2.1 by greg, Tue Feb 15 15:58:03 1994 UTC vs.
Revision 2.8 by greg, Wed Jun 22 12:35:58 1994 UTC

# Line 5 | Line 5 | static char SCCSid[] = "$SunId$ LBL";
5   #endif
6  
7   /*
8 < * Convert a triangle mesh into a Radiance description.
8 > * Convert a trianglular mesh into a Radiance description.
9   *
10   * Unlike most other converters, we have defined a file
11 < * format for the mesh ourselves.  It contains eight types,
11 > * format for the input ourselves.  The format contains eight types,
12   * each of which is identified by a single letter.  These are:
13   *
14   *      # comment               = a comment.  Continues until end of line.
15   *      v id Px Py Pz           = a vertex.  The id must be an integer.
16   *      n Nx Ny Nz              = a normal.  Corresponds to most recent vertex.
17   *      i Iu Iv                 = an index.  Corresponds to most recent vertex.
18 < *      p picture               = a picture.  Used as a pattern what follows.
18 > *      p picture               = a picture.  Used as a pattern for following.
19   *      m material              = a material name.  Used for what follows.
20 + *      o object                = an object name.  Used for what follows.
21   *      t id1 id2 id3           = a triangle.
22   *
23   * Only the 't' type results in any output.  The others merely set values
# Line 26 | Line 27 | static char SCCSid[] = "$SunId$ LBL";
27   * only makes sense for a mesh which is to be put into an octree for
28   * instancing.)  Using a pattern requires that each vertex have an
29   * associated index value for generating the colorpict primitive.
30 + * Likewise, an interpolated surface normal also requires that each
31 + * vertex of the triangle have an associated normal vector.
32 + * It is not necessary for the normal vectors to have unit length.
33   */
34  
35   #include "standard.h"
36  
37 < #define CALNAME         "tmesh.cal"     /* the name of our auxiliary file */
34 < #define PATNAME         "tpat"          /* triangle pattern name (reused) */
35 < #define TEXNAME         "tnor"          /* triangle texture name (reused) */
37 > #include "tmesh.h"
38  
39 + #define VOIDID          "void"          /* this is defined in object.h */
40 +
41 + #define PATNAME         "T-pat"         /* triangle pattern name (reused) */
42 + #define TEXNAME         "T-nor"         /* triangle texture name (reused) */
43 +
44   #define V_DEFINED       01              /* this vertex is defined */
45   #define V_HASNORM       02              /* vertex has surface normal */
46   #define V_HASINDX       04              /* vertex has index */
# Line 48 | Line 55 | typedef struct {
55   VERTEX  *vlist = NULL;          /* our vertex list */
56   int     nverts = 0;             /* number of vertices in our list */
57  
58 < typedef FLOAT   BARYCCM[3][4];
58 > #define novert(i)       ((i)<0|(i)>=nverts || !(vlist[i].flags&V_DEFINED))
59  
53 #define novert(i)       ((i)<0|(i)>=nverts||!(vlist[i].flags&V_DEFINED))
54
60   #define CHUNKSIZ        128     /* vertex allocation chunk size */
61  
62   extern VERTEX   *vnew();        /* allocate a vertex (never freed) */
63  
64 + char    *defmat = VOIDID;       /* default (starting) material name */
65 + char    *defpat = "";           /* default (starting) picture name */
66 + char    *defobj = "T";          /* default (starting) object name */
67  
68 +
69   main(argc, argv)                /* read in T-mesh files and convert */
70   int     argc;
71   char    *argv[];
# Line 64 | Line 73 | char   *argv[];
73          FILE    *fp;
74          int     i;
75  
76 <        if (argc == 1)
76 >        for (i = 1; i < argc && argv[i][0] == '-'; i++)
77 >                switch (argv[i][1]) {
78 >                case 'o':               /* object name */
79 >                        defobj = argv[++i];
80 >                        break;
81 >                case 'm':               /* default material */
82 >                        defmat = argv[++i];
83 >                        break;
84 >                case 'p':               /* default picture */
85 >                        defpat = argv[++i];
86 >                        break;
87 >                default:
88 >                        fprintf(stderr,
89 >                        "Usage: %s [-o obj][-m mat][-p pic] [file ..]\n",
90 >                                        argv[0]);
91 >                        exit(1);
92 >                }
93 >        if (i >= argc)
94                  convert("<stdin>", stdin);
95          else
96 <                for (i = 1; i < argc; i++) {
96 >                for ( ; i < argc; i++) {
97                          if ((fp = fopen(argv[i], "r")) == NULL) {
98                                  perror(argv[i]);
99                                  exit(1);
# Line 83 | Line 109 | convert(fname, fp)             /* convert a T-mesh */
109   char    *fname;
110   FILE    *fp;
111   {
112 <        char    typ[2];
112 >        char    typ[4];
113          int     id[3];
114          double  vec[3];
115          char    picfile[128];
116          char    matname[64];
117 <        char    *err;
118 <        register int    c;
119 <        register VERTEX *lastv = NULL;
117 >        char    objname[64];
118 >        register int    i;
119 >        register VERTEX *lastv;
120 >                                        /* start fresh */
121 >        i = nverts;
122 >        lastv = vlist;
123 >        while (i--)
124 >                (lastv++)->flags = 0;
125 >        lastv = NULL;
126 >        strcpy(picfile, defpat);
127 >        strcpy(matname, defmat);
128 >        strcpy(objname, defobj);
129  
130 <        picfile[0] = '\0';
131 <        strcpy(matname, "void");
97 <
130 >        printf("\n## T-mesh read from: %s\n", fname);
131 >                                        /* scan until EOF */
132          while (fscanf(fp, "%1s", typ) == 1)
133                  switch (typ[0]) {
134                  case 'v':               /* vertex */
# Line 108 | Line 142 | FILE   *fp;
142                                  syntax(fname, fp, "Bad triangle");
143                          if (novert(id[0]) | novert(id[1]) | novert(id[2]))
144                                  syntax(fname, fp, "Undefined triangle vertex");
145 <                        triangle(picfile, matname, &vlist[id[0]],
145 >                        triangle(picfile, matname, objname, &vlist[id[0]],
146                                          &vlist[id[1]], &vlist[id[2]]);
147                          break;
148                  case 'n':               /* surface normal */
# Line 133 | Line 167 | FILE   *fp;
167                          lastv->ndx[1] = vec[1];
168                          lastv->flags |= V_HASINDX;
169                          break;
170 +                case 'o':               /* object name */
171 +                        if (fscanf(fp, "%s", objname) != 1)
172 +                                syntax(fname, fp, "Bad object name");
173 +                        break;
174                  case 'm':               /* material */
175                          if (fscanf(fp, "%s", matname) != 1)
176                                  syntax(fname, fp, "Bad material");
177                          if (matname[0] == '-' && !matname[1])
178 <                                strcpy(matname, "void");
178 >                                strcpy(matname, VOIDID);
179                          break;
180                  case 'p':               /* picture */
181                          if (fscanf(fp, "%s", picfile) != 1)
# Line 146 | Line 184 | FILE   *fp;
184                                  picfile[0] = '\0';
185                          break;
186                  case '#':               /* comment */
187 <                        while ((c = getc(fp)) != EOF && c != '\n')
188 <                                ;
187 >                        fputs("\n#", stdout);
188 >                        while ((i = getc(fp)) != EOF) {
189 >                                putchar(i);
190 >                                if (i == '\n')
191 >                                        break;
192 >                        }
193                          break;
194                  default:
195                          syntax(fname, fp, "Unknown type");
# Line 156 | Line 198 | FILE   *fp;
198   }
199  
200  
201 < triangle(pn, mn, v1, v2, v3)            /* put out a triangle */
202 < char    *pn, *mn;
201 > triangle(pn, mod, obj, v1, v2, v3)      /* put out a triangle */
202 > char    *pn, *mod, *obj;
203   register VERTEX *v1, *v2, *v3;
204   {
205          static int      ntri = 0;
164        char    *mod = mn;
206          BARYCCM bvecs;
207 +        FLOAT   bvm[3][3];
208 +        register int    i;
209                                          /* compute barycentric coordinates */
210          if (v1->flags & v2->flags & v3->flags & (V_HASINDX|V_HASNORM))
211 <                if (comp_baryc(bvecs, v1->pos, v2->pos, v3->pos) < 0)
211 >                if (comp_baryc(&bvecs, v1->pos, v2->pos, v3->pos) < 0)
212                          return;
213                                          /* put out texture (if any) */
214          if (v1->flags & v2->flags & v3->flags & V_HASNORM) {
215                  printf("\n%s texfunc %s\n", mod, TEXNAME);
216                  mod = TEXNAME;
217 <                printf("4 dx dy dz %s\n", CALNAME);
218 <                printf("0\n21\n");
219 <                put_baryc(bvecs);
220 <                printf("\t%f %f %f\n", v1->nor[0], v1->nor[1], v1->nor[2]);
221 <                printf("\t%f %f %f\n", v2->nor[0], v2->nor[1], v2->nor[2]);
222 <                printf("\t%f %f %f\n", v3->nor[0], v3->nor[1], v3->nor[2]);
217 >                printf("4 dx dy dz %s\n", TCALNAME);
218 >                printf("0\n");
219 >                for (i = 0; i < 3; i++) {
220 >                        bvm[i][0] = v1->nor[i];
221 >                        bvm[i][1] = v2->nor[i];
222 >                        bvm[i][2] = v3->nor[i];
223 >                }
224 >                put_baryc(&bvecs, bvm, 3);
225          }
226                                          /* put out pattern (if any) */
227          if (*pn && (v1->flags & v2->flags & v3->flags & V_HASINDX)) {
228                  printf("\n%s colorpict %s\n", mod, PATNAME);
229                  mod = PATNAME;
230 <                printf("7 noneg noneg noneg %s %s u v\n", pn, CALNAME);
231 <                printf("0\n18\n");
232 <                put_baryc(bvecs);
233 <                printf("\t%f %f\n", v1->ndx[0], v1->ndx[1]);
234 <                printf("\t%f %f\n", v2->ndx[0], v2->ndx[1]);
235 <                printf("\t%f %f\n", v3->ndx[0], v3->ndx[1]);
230 >                printf("7 noneg noneg noneg %s %s u v\n", pn, TCALNAME);
231 >                printf("0\n");
232 >                for (i = 0; i < 2; i++) {
233 >                        bvm[i][0] = v1->ndx[i];
234 >                        bvm[i][1] = v2->ndx[i];
235 >                        bvm[i][2] = v3->ndx[i];
236 >                }
237 >                put_baryc(&bvecs, bvm, 2);
238          }
239                                          /* put out triangle */
240 <        printf("\n%s polygon t%d\n", mod, ++ntri);
240 >        printf("\n%s polygon %s.%d\n", mod, obj, ++ntri);
241          printf("0\n0\n9\n");
242          printf("%18.12g %18.12g %18.12g\n", v1->pos[0],v1->pos[1],v1->pos[2]);
243          printf("%18.12g %18.12g %18.12g\n", v2->pos[0],v2->pos[1],v2->pos[2]);
# Line 198 | Line 245 | register VERTEX        *v1, *v2, *v3;
245   }
246  
247  
201 int
202 comp_baryc(bcm, v1, v2, v3)             /* compute barycentric vectors */
203 register BARYCCM        bcm;
204 FVECT   v1, v2, v3;
205 {
206        FLOAT   *vt;
207        FVECT   va, vab, vcb;
208        double  d;
209        register int    i, j;
210
211        for (j = 0; j < 3; j++) {
212                for (i = 0; i < 3; i++) {
213                        vab[i] = v1[i] - v2[i];
214                        vcb[i] = v3[i] - v2[i];
215                }
216                d = DOT(vcb,vcb);
217                if (d <= FTINY)
218                        return(-1);
219                d = DOT(vcb,vab)/d;
220                for (i = 0; i < 3; i++)
221                        va[i] = vab[i] - vcb[i]*d;
222                d = DOT(va,va);
223                if (d <= FTINY)
224                        return(-1);
225                for (i = 0; i < 3; i++) {
226                        va[i] /= d;
227                        bcm[j][i] = va[i];
228                }
229                bcm[j][3] = -DOT(v2,va);
230                                        /* rotate vertices */
231                vt = v1;
232                v1 = v2;
233                v2 = v3;
234                v3 = vt;
235        }
236        return(0);
237 }
238
239
240 put_baryc(bcm)                          /* put barycentric coord. vectors */
241 register BARYCCM        bcm;
242 {
243        register int    i;
244
245        for (i = 0; i < 3; i++)
246                printf("%14.8f %14.8f %14.8f %14.8f\n",
247                                bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]);
248 }
249
250
248   VERTEX *
249   vnew(id, x, y, z)                       /* create a new vertex */
250   register int    id;
# Line 255 | Line 252 | double x, y, z;
252   {
253          register int    i;
254  
255 <        if (id > nverts) {              /* get some more */
255 >        if (id >= nverts) {             /* get some more */
256                  i = nverts;
257 <                nverts = CHUNKSIZ*((id%CHUNKSIZ)+1);
257 >                nverts = CHUNKSIZ*((id/CHUNKSIZ)+1);
258                  if (vlist == NULL)
259                          vlist = (VERTEX *)malloc(nverts*sizeof(VERTEX));
260                  else
# Line 268 | Line 265 | double x, y, z;
265                          "Out of memory while allocating vertex %d\n", id);
266                          exit(1);
267                  }
268 <                while (i < nverts)              /* clear them */
268 >                while (i < nverts)              /* clear what's new */
269                          vlist[i++].flags = 0;
270          }
271 <                                        /* assign it */
271 >                                        /* assign new vertex */
272          vlist[id].pos[0] = x;
273          vlist[id].pos[1] = y;
274          vlist[id].pos[2] = z;
# Line 292 | Line 289 | char   *er;
289          int     lineno;
290  
291          if (fp == stdin)
292 <                fprintf(stderr, "%s: syntax error: %s\n", fn, er);
292 >                fprintf(stderr, "%s: T-mesh format error: %s\n", fn, er);
293          else {
294                  cpos = ftell(fp);
295                  fseek(fp, 0L, 0);
# Line 303 | Line 300 | char   *er;
300                          if (c == '\n')
301                                  lineno++;
302                  }
303 <                fprintf(stderr, "%s: syntax error at line %d: %s\n",
303 >                fprintf(stderr, "%s: T-mesh format error at line %d: %s\n",
304                                  fn, lineno, er);
305          }
306          exit(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines