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.6 by greg, Fri Sep 2 16:21:00 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 57 | Line 80 | char   **argv;
80                                  goto unkopt;
81                          }
82                          break;
83 +                default:
84 +                        goto unkopt;
85                  }
86          init();
87          if (i >= argc)
# Line 85 | Line 110 | char   *inp;
110          register int    c;
111  
112          if (inp == NULL) {
113 <                inp = "the standard input";
113 >                inp = "standard input";
114                  fp = stdin;
115          } else if (inp[0] == '!') {
116                  if ((fp = popen(inp+1, "r")) == NULL) {
# Line 209 | Line 234 | char   *id;
234   {
235          if (!strcmp(id, curmat))        /* already set? */
236                  return;
237 +        if (!strcmp(id, VOIDID))        /* cannot set */
238 +                return;
239          printf("m %s\n", id);
240          strcpy(curmat, id);
241   }
242  
243  
244 + setobj(id)                      /* set object name to this one */
245 + char    *id;
246 + {
247 +        register char   *cp, *cp2;
248 +        char    *end = NULL;
249 +        int     diff = 0;
250 +                                /* use all but final suffix */
251 +        for (cp = id; *cp; cp++)
252 +                if (*cp == '.')
253 +                        end = cp;
254 +        if (end == NULL)
255 +                end = cp;
256 +                                /* copy to current object */
257 +        for (cp = id, cp2 = curobj; cp < end; *cp2++ = *cp++)
258 +                diff += *cp != *cp2;
259 +        if (!diff && !*cp2)
260 +                return;
261 +        *cp2 = '\0';
262 +        fputs("o\no ", stdout);
263 +        puts(curobj);
264 + }
265 +
266 +
267   init()                  /* initialize dispatch table and output */
268   {
269 +        lu_init(&vertab, NVERTS);
270          lu_init(&rdispatch, 22);
271          add2dispatch("polygon", o_face);
272          add2dispatch("cone", o_cone);
# Line 239 | Line 290 | init()                 /* initialize dispatch table and output */
290          add2dispatch("glow", o_light);
291          add2dispatch("illum", o_illum);
292          puts("# The following was converted from Radiance scene input");
293 <        if (unit_mult < .999 || unit_mult > 1.001)
293 >        if (hasmult)
294                  printf("xf -s %.4e\n", unit_mult);
295 +        printf("o %s\n", curobj);
296   }
297  
298  
299   uninit()                        /* mark end of MGF file */
300   {
301 <        if (unit_mult < .999 || unit_mult > 1.001)
301 >        puts("o");
302 >        if (hasmult)
303                  puts("xf");
304          puts("# End of data converted from Radiance scene input");
305          lu_done(&rdispatch);
306          lu_done(&rmats);
307 +        lu_done(&vertab);
308   }
309  
310  
311 + clrverts()                      /* clear vertex table */
312 + {
313 +        register int    i;
314 +
315 +        lu_done(&vertab);
316 +        for (i = 0; i < NVERTS; i++)
317 +                vert[i].lused = 0;
318 +        lu_init(&vertab, NVERTS);
319 + }
320 +
321 +
322   add2dispatch(name, func)        /* add function to dispatch table */
323   char    *name;
324   int     (*func)();
# Line 271 | Line 336 | int    (*func)();
336   }
337  
338  
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
339   char *
340 < getvertid(vp)                   /* get/set vertex ID for this point */
340 > getvertid(vname, vp)            /* get/set vertex ID for this point */
341 > char    *vname;
342   FVECT   vp;
343   {
295        static char     vname[6];
344          char    vkey[VKLEN];
345          register LUENT  *lp;
346          register int    i, vndx;
347  
300        if (!vertab.tsiz && !lu_init(&vertab, NVERTS))
301                goto memerr;
348          clock++;                        /* increment counter */
349          mkvkey(vkey, vp);
350          if ((lp = lu_find(&vertab, vkey)) == NULL)
# Line 319 | Line 365 | FVECT  vp;
365                          mkvkey(vkey, vert[vndx].p);
366                          lu_delete(&vertab, vkey);
367                  }
368 <                vert[vndx].lused = clock;                       /* assign it */
369 <                VCOPY(vert[vndx].p, vp);
324 <                printf("v v%d =\np %.15g %.15g %.15g\n",        /* print it */
368 >                VCOPY(vert[vndx].p, vp);                        /* assign it */
369 >                printf("v v%d =\n\tp %.15g %.15g %.15g\n",      /* print it */
370                                  vndx, vp[0], vp[1], vp[2]);
371                  lp->data = (char *)&vert[vndx];                 /* set it */
372          } else
373                  vndx = (struct vert *)lp->data - vert;
374 +        vert[vndx].lused = clock;               /* record this use */
375          sprintf(vname, "v%d", vndx);
376          return(vname);
377   memerr:
# Line 340 | Line 386 | char   *mod, *typ, *id;
386   FUNARGS *fa;
387   {
388          char    entbuf[512];
389 <        register char   *cp1, *cp2;
389 >        register char   *cp;
390          register int    i;
391  
392          if (fa->nfargs < 9 | fa->nfargs % 3)
393                  return(-1);
394          setmat(mod);
395 <        printf("o %s\n", id);
396 <        cp1 = entbuf;
397 <        *cp1++ = 'f';
395 >        setobj(id);
396 >        cp = entbuf;
397 >        *cp++ = 'f';
398          for (i = 0; i < fa->nfargs; i += 3) {
399 <                cp2 = getvertid(fa->farg + i);
400 <                *cp1++ = ' ';
401 <                while ((*cp1 = *cp2++))
402 <                        cp1++;
399 >                *cp++ = ' ';
400 >                getvertid(cp, fa->farg + i);
401 >                while (*cp)
402 >                        cp++;
403          }
404          puts(entbuf);
359        puts("o");
405          return(0);
406   }
407  
# Line 366 | Line 411 | o_cone(mod, typ, id, fa)       /* print out a cone */
411   char    *mod, *typ, *id;
412   register FUNARGS        *fa;
413   {
414 +        char    v1[6], v2[6];
415 +
416          if (fa->nfargs != 8)
417                  return(-1);
418          setmat(mod);
419 <        printf("o %s\n", id);
420 <        printf("v cv1 =\np %.12g %.12g %.12g\n",
421 <                        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]);
419 >        setobj(id);
420 >        getvertid(v1, fa->farg);
421 >        getvertid(v2, fa->farg + 3);
422          if (typ[1] == 'u')                      /* cup -> inverted cone */
423 <                printf("cone cv1 %.12g cv2 %.12g\n",
424 <                                -fa->farg[6], -fa->farg[7]);
423 >                printf("cone %s %.12g %s %.12g\n",
424 >                                v1, -fa->farg[6], v2, -fa->farg[7]);
425          else
426 <                printf("cone cv1 %.12g cv2 %.12g\n",
427 <                                fa->farg[6], fa->farg[7]);
383 <        puts("o");
426 >                printf("cone %s %.12g %s %.12g\n",
427 >                                v1, fa->farg[6], v2, fa->farg[7]);
428          return(0);
429   }
430  
# Line 390 | Line 434 | o_sphere(mod, typ, id, fa)     /* print out a sphere */
434   char    *mod, *typ, *id;
435   register FUNARGS        *fa;
436   {
437 +        char    cent[6];
438 +
439          if (fa->nfargs != 4)
440                  return(-1);
441          setmat(mod);
442 <        printf("o %s\n", id);
443 <        printf("v cent =\np %.12g %.12g %.12g\n",
444 <                        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");
442 >        setobj(id);
443 >        printf("sph %s %.12g\n", getvertid(cent, fa->farg),
444 >                        typ[0]=='b' ? -fa->farg[3] : fa->farg[3]);
445          return(0);
446   }
447  
# Line 407 | Line 451 | o_cylinder(mod, typ, id, fa)   /* print out a cylinder *
451   char    *mod, *typ, *id;
452   register FUNARGS        *fa;
453   {
454 +        char    v1[6], v2[6];
455 +
456          if (fa->nfargs != 7)
457                  return(-1);
458          setmat(mod);
459 <        printf("o %s\n", id);
460 <        printf("v cv1 =\np %.12g %.12g %.12g\n",
461 <                        fa->farg[0], fa->farg[1], fa->farg[2]);
462 <        printf("v cv2 =\np %.12g %.12g %.12g\n",
463 <                        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");
459 >        setobj(id);
460 >        getvertid(v1, fa->farg);
461 >        getvertid(v2, fa->farg + 3);
462 >        printf("cyl %s %.12g %s\n", v1,
463 >                        typ[0]=='t' ? -fa->farg[6] : fa->farg[6], v2);
464          return(0);
465   }
466  
# Line 430 | Line 473 | register FUNARGS       *fa;
473          if (fa->nfargs != 8)
474                  return(-1);
475          setmat(mod);
476 <        printf("o %s\n", id);
477 <        printf("v cent =\np %.12g %.12g %.12g\n",
476 >        setobj(id);
477 >        printf("v cent =\n\tp %.12g %.12g %.12g\n",
478                          fa->farg[0], fa->farg[1], fa->farg[2]);
479 <        printf("n %.12g %.12g %.12g\n",
479 >        printf("\tn %.12g %.12g %.12g\n",
480                          fa->farg[3], fa->farg[4], fa->farg[5]);
481          if (fa->farg[6] < fa->farg[7])
482                  printf("ring cent %.12g %.12g\n",
# Line 441 | Line 484 | register FUNARGS       *fa;
484          else
485                  printf("ring cent %.12g %.12g\n",
486                                  fa->farg[7], fa->farg[6]);
444        puts("o");
487          return(0);
488   }
489  
# Line 451 | Line 493 | o_instance(mod, typ, id, fa)   /* convert an instance */
493   char    *mod, *typ, *id;
494   FUNARGS *fa;
495   {
496 <        return(0);              /* this is too damned difficult! */
496 >        register int    i;
497 >        register char   *cp;
498 >        char    *start = NULL, *end = NULL;
499 >        /*
500 >         * We don't really know how to do this, so we just create
501 >         * a reference to an undefined MGF file and it's the user's
502 >         * responsibility to create this file and put the appropriate
503 >         * stuff into it.
504 >         */
505 >        if (fa->nsargs < 1)
506 >                return(-1);
507 >        setmat(mod);                    /* only works if surfaces are void */
508 >        setobj(id);
509 >        for (cp = fa->sarg[0]; *cp; cp++)       /* construct MGF file name */
510 >                if (*cp == '/')
511 >                        start = cp+1;
512 >                else if (*cp == '.')
513 >                        end = cp;
514 >        if (start == NULL)
515 >                start = fa->sarg[0];
516 >        if (end == NULL || start >= end)
517 >                end = cp;
518 >        fputs("i ", stdout);                    /* print include entity */
519 >        for (cp = start; cp < end; cp++)
520 >                putchar(*cp);
521 >        fputs(".mgf", stdout);                  /* add MGF suffix */
522 >        for (i = 1; i < fa->nsargs; i++) {      /* add transform */
523 >                putchar(' ');
524 >                fputs(fa->sarg[i], stdout);
525 >        }
526 >        putchar('\n');
527 >        clrverts();                     /* vertex id's no longer reliable */
528 >        return(0);
529   }
530  
531  
# Line 475 | Line 549 | FUNARGS        *fa;
549          }
550                                          /* else create invisible material */
551          newmat(id, NULL);
552 <        puts("ts 1 0");
552 >        puts("\tts 1 0");
553          return(0);
554   }
555  
# Line 493 | Line 567 | register FUNARGS       *fa;
567          newmat(id, NULL);
568          rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
569          rgb_cie(cxyz, rrgb);
570 <        puts("c");                              /* put diffuse component */
570 >        puts("\tc");                            /* put diffuse component */
571          d = cxyz[0] + cxyz[1] + cxyz[2];
572          if (d > FTINY)
573 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
574 <        printf("rd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
575 <        puts("c");                              /* put specular component */
576 <        printf("rs %.4f %.4f\n", fa->farg[3],
577 <                        typ[7]=='2' ? .5*(fa->farg[4] + fa->farg[5]) :
578 <                                        fa->farg[4]);
573 >                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
574 >        printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
575 >        if (fa->farg[3] > FTINY) {              /* put specular component */
576 >                puts("\tc");
577 >                printf("\trs %.4f %.4f\n", fa->farg[3],
578 >                                typ[7]=='2' ? .5*(fa->farg[4] + fa->farg[5]) :
579 >                                                fa->farg[4]);
580 >        }
581          return(0);
582   }
583  
# Line 519 | Line 595 | register FUNARGS       *fa;
595          newmat(id, NULL);
596          rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
597          rgb_cie(cxyz, rrgb);
598 <        puts("c");                              /* put diffuse component */
598 >        puts("\tc");                            /* put diffuse component */
599          d = cxyz[0] + cxyz[1] + cxyz[2];
600          if (d > FTINY)
601 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
602 <        printf("rd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
601 >                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
602 >        printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
603                                                  /* put specular component */
604 <        printf("rs %.4f %.4f\n", cxyz[1]*fa->farg[3],
604 >        printf("\trs %.4f %.4f\n", cxyz[1]*fa->farg[3],
605                          typ[5]=='2' ? .5*(fa->farg[4] + fa->farg[5]) :
606                                          fa->farg[4]);
607          return(0);
# Line 549 | Line 625 | register FUNARGS       *fa;
625          F = (1. - nrfr)/(1. + nrfr);            /* use normal incidence */
626          F *= F;
627          for (i = 0; i < 3; i++) {
628 <                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]) /
628 >                trgb[i] = fa->farg[i] * (1. - F)*(1. - F) /
629                                  (1. - F*F*fa->farg[i]*fa->farg[i]);
630 +                rrgb[i] = F * (1. + (1. - 2.*F)*fa->farg[i]) /
631 +                                (1. - F*F*fa->farg[i]*fa->farg[i]);
632          }
633          rgb_cie(cxyz, rrgb);                    /* put reflected component */
634 <        puts("c");
634 >        puts("\tc");
635          d = cxyz[0] + cxyz[1] + cxyz[2];
636          if (d > FTINY)
637 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
638 <        printf("rs %.4f 0\n", cxyz[1]);
637 >                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
638 >        printf("\trs %.4f 0\n", cxyz[1]);
639          rgb_cie(cxyz, trgb);                    /* put transmitted component */
640 <        puts("c");
640 >        puts("\tc");
641          d = cxyz[0] + cxyz[1] + cxyz[2];
642          if (d > FTINY)
643 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
644 <        printf("ts %.4f 0\n", cxyz[1]);
643 >                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
644 >        printf("\tts %.4f 0\n", cxyz[1]);
645          return(0);
646   }
647  
# Line 586 | Line 663 | register FUNARGS       *fa;
663          newmat(id, NULL);
664          rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
665          rgb_cie(cxyz, rrgb);
666 <        puts("c");                              /* put specular component */
666 >        puts("\tc");                            /* put specular component */
667          d = cxyz[0] + cxyz[1] + cxyz[2];
668          if (d > FTINY)
669 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
670 <        printf("rs %.4f 0\n", cxyz[1]);
669 >                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
670 >        printf("\trs %.4f 0\n", cxyz[1]);
671          return(0);
672   }
673  
# Line 619 | Line 696 | register FUNARGS       *fa;
696          newmat(id, NULL);
697          rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
698          rgb_cie(cxyz, rrgb);
699 <        puts("c");                              /* put transmitted diffuse */
699 >        puts("\tc");                            /* put transmitted diffuse */
700          d = cxyz[0] + cxyz[1] + cxyz[2];
701          if (d > FTINY)
702 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
703 <        printf("td %.4f\n", cxyz[1]*trans*(1. - fa->farg[3])*(1. - tspec));
702 >                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
703 >        printf("\ttd %.4f\n", cxyz[1]*trans*(1. - fa->farg[3])*(1. - tspec));
704                                                  /* put transmitted specular */
705 <        printf("ts %.4f %.4f\n", cxyz[1]*trans*tspec*(1. - fa->farg[3]), rough);
705 >        printf("\tts %.4f %.4f\n", cxyz[1]*trans*tspec*(1. - fa->farg[3]), rough);
706                                                  /* put reflected diffuse */
707 <        printf("rd %.4f\n", cxyz[1]*(1. - fa->farg[3])*(1. - trans));
708 <        puts("c");                              /* put reflected specular */
709 <        printf("rs %.4f %.4f\n", fa->farg[3], rough);
707 >        printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3])*(1. - trans));
708 >        puts("\tc");                            /* put reflected specular */
709 >        printf("\trs %.4f %.4f\n", fa->farg[3], rough);
710          return(0);
711   }
712  
# Line 648 | Line 725 | register FUNARGS       *fa;
725          rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
726          rgb_cie(cxyz, rrgb);
727          d = cxyz[0] + cxyz[1] + cxyz[2];
728 <        puts("c");
728 >        puts("\tc");
729          if (d > FTINY)
730 <                printf("cxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
731 <        printf("ed %.4g\n", cxyz[1]);
730 >                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
731 >        printf("\ted %.4g\n", cxyz[1]*WHTEFFICACY);
732          return(0);
733   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines