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

Comparing ray/src/cv/rad2mgf.c (file contents):
Revision 2.1 by greg, Thu Jul 7 17:30:33 1994 UTC vs.
Revision 2.5 by greg, Mon Aug 15 16:29:22 1994 UTC

# Line 26 | Line 26 | LUTAB  rmats = LU_SINIT(free,NULL);            /* defined materia
26  
27   LUTAB   rdispatch = LU_SINIT(NULL,NULL);        /* function dispatch table */
28  
29 < char    curmat[80];             /* current material */
29 > char    curmat[80];                             /* current material */
30 > char    curobj[128] = "Untitled";               /* current object name */
31  
32 < double  unit_mult = 1.;         /* units multiplier */
32 > double  unit_mult = 1.;                         /* units multiplier */
33  
34 + #define hasmult         (unit_mult < .999 || unit_mult > 1.001)
35  
36 + /*
37 + * Stuff for tracking and reusing vertices:
38 + */
39 +
40 + char    VKFMT[] = "%+1.9e %+1.9e %+1.9e";
41 + #define VKLEN           64
42 +
43 + #define mkvkey(k,v)     sprintf(k, VKFMT, (v)[0], (v)[1], (v)[2])
44 +
45 + #define NVERTS          256
46 +
47 + long    clock;          /* incremented at each vertex request */
48 +
49 + struct vert {
50 +        long    lused;          /* when last used (0 if unassigned) */
51 +        FVECT   p;              /* track point position only */
52 + } vert[NVERTS];         /* our vertex cache */
53 +
54 + LUTAB   vertab = LU_SINIT(free,NULL);   /* our vertex lookup table */
55 +
56 +
57   main(argc, argv)
58   int     argc;
59   char    **argv;
# Line 85 | Line 108 | char   *inp;
108          register int    c;
109  
110          if (inp == NULL) {
111 <                inp = "the standard input";
111 >                inp = "standard input";
112                  fp = stdin;
113          } else if (inp[0] == '!') {
114                  if ((fp = popen(inp+1, "r")) == NULL) {
# Line 209 | Line 232 | char   *id;
232   {
233          if (!strcmp(id, curmat))        /* already set? */
234                  return;
235 +        if (!strcmp(id, VOIDID))        /* cannot set */
236 +                return;
237          printf("m %s\n", id);
238          strcpy(curmat, id);
239   }
240  
241  
242 + setobj(id)                      /* set object name to this one */
243 + char    *id;
244 + {
245 +        register char   *cp, *cp2;
246 +        char    *end = NULL;
247 +        int     diff = 0;
248 +                                /* use all but final suffix */
249 +        for (cp = id; *cp; cp++)
250 +                if (*cp == '.')
251 +                        end = cp;
252 +        if (end == NULL)
253 +                end = cp;
254 +                                /* copy to current object */
255 +        for (cp = id, cp2 = curobj; cp < end; *cp2++ = *cp++)
256 +                diff += *cp != *cp2;
257 +        if (!diff && !*cp2)
258 +                return;
259 +        *cp2 = '\0';
260 +        fputs("o\no ", stdout);
261 +        puts(curobj);
262 + }
263 +
264 +
265   init()                  /* initialize dispatch table and output */
266   {
267 +        lu_init(&vertab, NVERTS);
268          lu_init(&rdispatch, 22);
269          add2dispatch("polygon", o_face);
270          add2dispatch("cone", o_cone);
# Line 239 | Line 288 | init()                 /* initialize dispatch table and output */
288          add2dispatch("glow", o_light);
289          add2dispatch("illum", o_illum);
290          puts("# The following was converted from Radiance scene input");
291 <        if (unit_mult < .999 || unit_mult > 1.001)
291 >        if (hasmult)
292                  printf("xf -s %.4e\n", unit_mult);
293 +        printf("o %s\n", curobj);
294   }
295  
296  
297   uninit()                        /* mark end of MGF file */
298   {
299 <        if (unit_mult < .999 || unit_mult > 1.001)
299 >        puts("o");
300 >        if (hasmult)
301                  puts("xf");
302          puts("# End of data converted from Radiance scene input");
303          lu_done(&rdispatch);
304          lu_done(&rmats);
305 +        lu_done(&vertab);
306   }
307  
308  
309 + clrverts()                      /* clear vertex table */
310 + {
311 +        register int    i;
312 +
313 +        lu_done(&vertab);
314 +        for (i = 0; i < NVERTS; i++)
315 +                vert[i].lused = 0;
316 +        lu_init(&vertab, NVERTS);
317 + }
318 +
319 +
320   add2dispatch(name, func)        /* add function to dispatch table */
321   char    *name;
322   int     (*func)();
# Line 271 | Line 334 | int    (*func)();
334   }
335  
336  
274 char    VKFMT[] = "%+1.9e %+1.9e %+1.9e";
275 #define VKLEN           64
276
277 #define mkvkey(k,v)     sprintf(k, VKFMT, (v)[0], (v)[1], (v)[2])
278
279 #define NVERTS          256
280
281 long    clock;          /* incremented at each vertex request */
282
283 struct vert {
284        long    lused;          /* when last used (0 if unassigned) */
285        FVECT   p;              /* track point position only */
286 } vert[NVERTS];
287
288 LUTAB   vertab = LU_SINIT(free,NULL);   /* our vertex lookup table */
289
290
337   char *
338 < getvertid(vp)                   /* get/set vertex ID for this point */
338 > getvertid(vname, vp)            /* get/set vertex ID for this point */
339 > char    *vname;
340   FVECT   vp;
341   {
295        static char     vname[6];
342          char    vkey[VKLEN];
343          register LUENT  *lp;
344          register int    i, vndx;
345  
300        if (!vertab.tsiz && !lu_init(&vertab, NVERTS))
301                goto memerr;
346          clock++;                        /* increment counter */
347          mkvkey(vkey, vp);
348          if ((lp = lu_find(&vertab, vkey)) == NULL)
# Line 319 | Line 363 | FVECT  vp;
363                          mkvkey(vkey, vert[vndx].p);
364                          lu_delete(&vertab, vkey);
365                  }
366 <                vert[vndx].lused = clock;                       /* assign it */
367 <                VCOPY(vert[vndx].p, vp);
324 <                printf("v v%d =\np %.15g %.15g %.15g\n",        /* print it */
366 >                VCOPY(vert[vndx].p, vp);                        /* assign it */
367 >                printf("v v%d =\n\tp %.15g %.15g %.15g\n",      /* print it */
368                                  vndx, vp[0], vp[1], vp[2]);
369                  lp->data = (char *)&vert[vndx];                 /* set it */
370          } else
371                  vndx = (struct vert *)lp->data - vert;
372 +        vert[vndx].lused = clock;               /* record this use */
373          sprintf(vname, "v%d", vndx);
374          return(vname);
375   memerr:
# Line 340 | Line 384 | char   *mod, *typ, *id;
384   FUNARGS *fa;
385   {
386          char    entbuf[512];
387 <        register char   *cp1, *cp2;
387 >        register char   *cp;
388          register int    i;
389  
390          if (fa->nfargs < 9 | fa->nfargs % 3)
391                  return(-1);
392          setmat(mod);
393 <        printf("o %s\n", id);
394 <        cp1 = entbuf;
395 <        *cp1++ = 'f';
393 >        setobj(id);
394 >        cp = entbuf;
395 >        *cp++ = 'f';
396          for (i = 0; i < fa->nfargs; i += 3) {
397 <                cp2 = getvertid(fa->farg + i);
398 <                *cp1++ = ' ';
399 <                while ((*cp1 = *cp2++))
400 <                        cp1++;
397 >                *cp++ = ' ';
398 >                getvertid(cp, fa->farg + i);
399 >                while (*cp)
400 >                        cp++;
401          }
402          puts(entbuf);
359        puts("o");
403          return(0);
404   }
405  
# Line 366 | Line 409 | o_cone(mod, typ, id, fa)       /* print out a cone */
409   char    *mod, *typ, *id;
410   register FUNARGS        *fa;
411   {
412 +        char    v1[6], v2[6];
413 +
414          if (fa->nfargs != 8)
415                  return(-1);
416          setmat(mod);
417 <        printf("o %s\n", id);
418 <        printf("v cv1 =\np %.12g %.12g %.12g\n",
419 <                        fa->farg[0], fa->farg[1], fa->farg[2]);
375 <        printf("v cv2 =\np %.12g %.12g %.12g\n",
376 <                        fa->farg[3], fa->farg[4], fa->farg[5]);
417 >        setobj(id);
418 >        getvertid(v1, fa->farg);
419 >        getvertid(v2, fa->farg + 3);
420          if (typ[1] == 'u')                      /* cup -> inverted cone */
421 <                printf("cone cv1 %.12g cv2 %.12g\n",
422 <                                -fa->farg[6], -fa->farg[7]);
421 >                printf("cone %s %.12g %s %.12g\n",
422 >                                v1, -fa->farg[6], v2, -fa->farg[7]);
423          else
424 <                printf("cone cv1 %.12g cv2 %.12g\n",
425 <                                fa->farg[6], fa->farg[7]);
383 <        puts("o");
424 >                printf("cone %s %.12g %s %.12g\n",
425 >                                v1, fa->farg[6], v2, fa->farg[7]);
426          return(0);
427   }
428  
# Line 390 | Line 432 | o_sphere(mod, typ, id, fa)     /* print out a sphere */
432   char    *mod, *typ, *id;
433   register FUNARGS        *fa;
434   {
435 +        char    cent[6];
436 +
437          if (fa->nfargs != 4)
438                  return(-1);
439          setmat(mod);
440 <        printf("o %s\n", id);
441 <        printf("v cent =\np %.12g %.12g %.12g\n",
442 <                        fa->farg[0], fa->farg[1], fa->farg[2]);
399 <        printf("sph cent %.12g\n", typ[0]=='b' ? -fa->farg[3] : fa->farg[3]);
400 <        puts("o");
440 >        setobj(id);
441 >        printf("sph %s %.12g\n", getvertid(cent, fa->farg),
442 >                        typ[0]=='b' ? -fa->farg[3] : fa->farg[3]);
443          return(0);
444   }
445  
# Line 407 | Line 449 | o_cylinder(mod, typ, id, fa)   /* print out a cylinder *
449   char    *mod, *typ, *id;
450   register FUNARGS        *fa;
451   {
452 +        char    v1[6], v2[6];
453 +
454          if (fa->nfargs != 7)
455                  return(-1);
456          setmat(mod);
457 <        printf("o %s\n", id);
458 <        printf("v cv1 =\np %.12g %.12g %.12g\n",
459 <                        fa->farg[0], fa->farg[1], fa->farg[2]);
460 <        printf("v cv2 =\np %.12g %.12g %.12g\n",
461 <                        fa->farg[3], fa->farg[4], fa->farg[5]);
418 <        printf("cyl cv1 %.12g cv2\n",
419 <                        typ[0]=='t' ? -fa->farg[6] : fa->farg[6]);
420 <        puts("o");
457 >        setobj(id);
458 >        getvertid(v1, fa->farg);
459 >        getvertid(v2, fa->farg + 3);
460 >        printf("cyl %s %.12g %s\n", v1,
461 >                        typ[0]=='t' ? -fa->farg[6] : fa->farg[6], v2);
462          return(0);
463   }
464  
# Line 430 | Line 471 | register FUNARGS       *fa;
471          if (fa->nfargs != 8)
472                  return(-1);
473          setmat(mod);
474 <        printf("o %s\n", id);
475 <        printf("v cent =\np %.12g %.12g %.12g\n",
474 >        setobj(id);
475 >        printf("v cent =\n\tp %.12g %.12g %.12g\n",
476                          fa->farg[0], fa->farg[1], fa->farg[2]);
477 <        printf("n %.12g %.12g %.12g\n",
477 >        printf("\tn %.12g %.12g %.12g\n",
478                          fa->farg[3], fa->farg[4], fa->farg[5]);
479          if (fa->farg[6] < fa->farg[7])
480                  printf("ring cent %.12g %.12g\n",
# Line 441 | Line 482 | register FUNARGS       *fa;
482          else
483                  printf("ring cent %.12g %.12g\n",
484                                  fa->farg[7], fa->farg[6]);
444        puts("o");
485          return(0);
486   }
487  
# Line 451 | Line 491 | o_instance(mod, typ, id, fa)   /* convert an instance */
491   char    *mod, *typ, *id;
492   FUNARGS *fa;
493   {
494 <        return(0);              /* this is too damned difficult! */
494 >        register int    i;
495 >        register char   *cp;
496 >        char    *start = NULL, *end = NULL;
497 >        /*
498 >         * We don't really know how to do this, so we just create
499 >         * a reference to an undefined MGF file and it's the user's
500 >         * responsibility to create this file and put the appropriate
501 >         * stuff into it.
502 >         */
503 >        if (fa->nsargs < 1)
504 >                return(-1);
505 >        setmat(mod);                    /* only works if surfaces are void */
506 >        setobj(id);
507 >        for (cp = fa->sarg[0]; *cp; cp++)       /* construct MGF file name */
508 >                if (*cp == '/')
509 >                        start = cp+1;
510 >                else if (*cp == '.')
511 >                        end = cp;
512 >        if (start == NULL)
513 >                start = fa->sarg[0];
514 >        if (end == NULL || start >= end)
515 >                end = cp;
516 >        fputs("i ", stdout);                    /* print include entity */
517 >        for (cp = start; cp < end; cp++)
518 >                putchar(*cp);
519 >        fputs(".mgf", stdout);                  /* add MGF suffix */
520 >        for (i = 1; i < fa->nsargs; i++) {      /* add transform */
521 >                putchar(' ');
522 >                fputs(fa->sarg[i], stdout);
523 >        }
524 >        putchar('\n');
525 >        clrverts();                     /* vertex id's no longer reliable */
526 >        return(0);
527   }
528  
529  
# Line 475 | Line 547 | FUNARGS        *fa;
547          }
548                                          /* else create invisible material */
549          newmat(id, NULL);
550 <        puts("ts 1 0");
550 >        puts("\tts 1 0");
551          return(0);
552   }
553  
# Line 493 | Line 565 | register FUNARGS       *fa;
565          newmat(id, NULL);
566          rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
567          rgb_cie(cxyz, rrgb);
568 <        puts("c");                              /* put diffuse component */
568 >        puts("\tc");                            /* put diffuse component */
569          d = cxyz[0] + cxyz[1] + cxyz[2];
570          if (d > FTINY)
571 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
572 <        printf("rd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
573 <        puts("c");                              /* put specular component */
574 <        printf("rs %.4f %.4f\n", fa->farg[3],
575 <                        typ[7]=='2' ? .5*(fa->farg[4] + fa->farg[5]) :
576 <                                        fa->farg[4]);
571 >                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
572 >        printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
573 >        if (fa->farg[3] > FTINY) {              /* put specular component */
574 >                puts("\tc");
575 >                printf("\trs %.4f %.4f\n", fa->farg[3],
576 >                                typ[7]=='2' ? .5*(fa->farg[4] + fa->farg[5]) :
577 >                                                fa->farg[4]);
578 >        }
579          return(0);
580   }
581  
# Line 519 | Line 593 | register FUNARGS       *fa;
593          newmat(id, NULL);
594          rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
595          rgb_cie(cxyz, rrgb);
596 <        puts("c");                              /* put diffuse component */
596 >        puts("\tc");                            /* put diffuse component */
597          d = cxyz[0] + cxyz[1] + cxyz[2];
598          if (d > FTINY)
599 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
600 <        printf("rd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
599 >                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
600 >        printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
601                                                  /* put specular component */
602 <        printf("rs %.4f %.4f\n", cxyz[1]*fa->farg[3],
602 >        printf("\trs %.4f %.4f\n", cxyz[1]*fa->farg[3],
603                          typ[5]=='2' ? .5*(fa->farg[4] + fa->farg[5]) :
604                                          fa->farg[4]);
605          return(0);
# Line 549 | Line 623 | register FUNARGS       *fa;
623          F = (1. - nrfr)/(1. + nrfr);            /* use normal incidence */
624          F *= F;
625          for (i = 0; i < 3; i++) {
626 <                rrgb[i] = (1. - F)*(1. - F)/(1. - F*F*fa->farg[i]*fa->farg[i]);
553 <                trgb[i] = F * (1. + (1. - 2.*F)*fa->farg[i]) /
626 >                trgb[i] = fa->farg[i] * (1. - F)*(1. - F) /
627                                  (1. - F*F*fa->farg[i]*fa->farg[i]);
628 +                rrgb[i] = F * (1. + (1. - 2.*F)*fa->farg[i]) /
629 +                                (1. - F*F*fa->farg[i]*fa->farg[i]);
630          }
631          rgb_cie(cxyz, rrgb);                    /* put reflected component */
632 <        puts("c");
632 >        puts("\tc");
633          d = cxyz[0] + cxyz[1] + cxyz[2];
634          if (d > FTINY)
635 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
636 <        printf("rs %.4f 0\n", cxyz[1]);
635 >                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
636 >        printf("\trs %.4f 0\n", cxyz[1]);
637          rgb_cie(cxyz, trgb);                    /* put transmitted component */
638 <        puts("c");
638 >        puts("\tc");
639          d = cxyz[0] + cxyz[1] + cxyz[2];
640          if (d > FTINY)
641 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
642 <        printf("ts %.4f 0\n", cxyz[1]);
641 >                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
642 >        printf("\tts %.4f 0\n", cxyz[1]);
643          return(0);
644   }
645  
# Line 586 | Line 661 | register FUNARGS       *fa;
661          newmat(id, NULL);
662          rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
663          rgb_cie(cxyz, rrgb);
664 <        puts("c");                              /* put specular component */
664 >        puts("\tc");                            /* put specular component */
665          d = cxyz[0] + cxyz[1] + cxyz[2];
666          if (d > FTINY)
667 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
668 <        printf("rs %.4f 0\n", cxyz[1]);
667 >                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
668 >        printf("\trs %.4f 0\n", cxyz[1]);
669          return(0);
670   }
671  
# Line 619 | Line 694 | register FUNARGS       *fa;
694          newmat(id, NULL);
695          rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
696          rgb_cie(cxyz, rrgb);
697 <        puts("c");                              /* put transmitted diffuse */
697 >        puts("\tc");                            /* put transmitted diffuse */
698          d = cxyz[0] + cxyz[1] + cxyz[2];
699          if (d > FTINY)
700 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
701 <        printf("td %.4f\n", cxyz[1]*trans*(1. - fa->farg[3])*(1. - tspec));
700 >                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
701 >        printf("\ttd %.4f\n", cxyz[1]*trans*(1. - fa->farg[3])*(1. - tspec));
702                                                  /* put transmitted specular */
703 <        printf("ts %.4f %.4f\n", cxyz[1]*trans*tspec*(1. - fa->farg[3]), rough);
703 >        printf("\tts %.4f %.4f\n", cxyz[1]*trans*tspec*(1. - fa->farg[3]), rough);
704                                                  /* put reflected diffuse */
705 <        printf("rd %.4f\n", cxyz[1]*(1. - fa->farg[3])*(1. - trans));
706 <        puts("c");                              /* put reflected specular */
707 <        printf("rs %.4f %.4f\n", fa->farg[3], rough);
705 >        printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3])*(1. - trans));
706 >        puts("\tc");                            /* put reflected specular */
707 >        printf("\trs %.4f %.4f\n", fa->farg[3], rough);
708          return(0);
709   }
710  
# Line 648 | Line 723 | register FUNARGS       *fa;
723          rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
724          rgb_cie(cxyz, rrgb);
725          d = cxyz[0] + cxyz[1] + cxyz[2];
726 <        puts("c");
726 >        puts("\tc");
727          if (d > FTINY)
728 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
729 <        printf("ed %.4g\n", cxyz[1]);
728 >                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
729 >        printf("\ted %.4g\n", cxyz[1]*WHTEFFICACY);
730          return(0);
731   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines