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.5 by greg, Mon Aug 15 16:29:22 1994 UTC vs.
Revision 2.23 by schorsch, Sat Nov 15 17:54:06 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 Radiance scene description to MGF
6   */
7  
8 < #include <stdio.h>
8 > #include <ctype.h>
9   #include <string.h>
10 < #include "fvect.h"
10 > #include <stdio.h>
11 >
12 > #include "platform.h"
13 > #include "rtmath.h"
14 > #include "rtio.h"
15 > #include "rtprocess.h"
16   #include "object.h"
17   #include "color.h"
18   #include "lookup.h"
19  
20 < int     o_face(), o_cone(), o_sphere(), o_ring(), o_cylinder();
19 < int     o_instance(), o_source(), o_illum();
20 < int     o_plastic(), o_metal(), o_glass(), o_mirror(), o_trans(), o_light();
20 > #define C_1SIDEDTHICK   0.005
21  
22 extern void     free();
23 extern char     *malloc();
22  
23   LUTAB   rmats = LU_SINIT(free,NULL);            /* defined material table */
24  
# Line 37 | Line 35 | double unit_mult = 1.;                         /* units multiplier */
35   * Stuff for tracking and reusing vertices:
36   */
37  
38 < char    VKFMT[] = "%+1.9e %+1.9e %+1.9e";
38 > char    VKFMT[] = "%+16.9e %+16.9e %+16.9e";
39   #define VKLEN           64
40  
41   #define mkvkey(k,v)     sprintf(k, VKFMT, (v)[0], (v)[1], (v)[2])
42  
43   #define NVERTS          256
44  
45 < long    clock;          /* incremented at each vertex request */
45 > long    vclock;         /* incremented at each vertex request */
46  
47   struct vert {
48          long    lused;          /* when last used (0 if unassigned) */
# Line 53 | Line 51 | struct vert {
51  
52   LUTAB   vertab = LU_SINIT(free,NULL);   /* our vertex lookup table */
53  
54 + static void rad2mgf(char *inp);
55 + static void cvtprim(char *inp, char *mod, char *typ, char *id, FUNARGS *fa);
56 + static void newmat(char *id, char *alias);
57 + static void setmat(char *id);
58 + static void setobj(char *id);
59 + static void init(void);
60 + static void uninit(void);
61 + static void clrverts(void);
62 + static void add2dispatch(char *name, int (*func)());
63 + static char *getvertid(char *vname, FVECT vp);
64 + static int o_unsupported(char *mod, char *typ, char *id, FUNARGS *fa);
65 + static int o_face(char *mod, char *typ, char *id, FUNARGS *fa);
66 + static int o_cone(char *mod, char *typ, char *id, FUNARGS *fa);
67 + static int o_sphere(char *mod, char *typ, char *id, FUNARGS *fa);
68 + static int o_cylinder(char *mod, char *typ, char *id, FUNARGS *fa);
69 + static int o_ring(char *mod, char *typ, char *id, FUNARGS *fa);
70 + static int o_instance(char *mod, char *typ, char *id, FUNARGS *fa);
71 + static int o_illum(char *mod, char *typ, char *id, FUNARGS *fa);
72 + static int o_plastic(char *mod, char *typ, char *id, FUNARGS *fa);
73 + static int o_metal(char *mod, char *typ, char *id, FUNARGS *fa);
74 + static int o_glass(char *mod, char *typ, char *id, FUNARGS *fa);
75 + static int o_dielectric(char *mod, char *typ, char *id, FUNARGS *fa);
76 + static int o_mirror(char *mod, char *typ, char *id, FUNARGS *fa);
77 + static int o_trans(char *mod, char *typ, char *id, FUNARGS *fa);
78 + static int o_light(char *mod, char *typ, char *id, FUNARGS *fa);
79  
80 < main(argc, argv)
81 < int     argc;
82 < char    **argv;
80 >
81 > int
82 > main(
83 >        int     argc,
84 >        char    **argv
85 > )
86   {
87          int     i;
88  
# Line 80 | Line 106 | char   **argv;
106                                  goto unkopt;
107                          }
108                          break;
109 +                default:
110 +                        goto unkopt;
111                  }
112          init();
113          if (i >= argc)
# Line 95 | Line 123 | unkopt:
123   }
124  
125  
126 < rad2mgf(inp)            /* convert a Radiance file to MGF */
127 < char    *inp;
126 > void
127 > rad2mgf(                /* convert a Radiance file to MGF */
128 >        char    *inp
129 > )
130   {
131   #define mod     buf
132   #define typ     (buf+128)
# Line 174 | Line 204 | char   *inp;
204   }
205  
206  
207 < cvtprim(inp, mod, typ, id, fa)  /* process Radiance primitive */
208 < char    *inp, *mod, *typ, *id;
209 < FUNARGS *fa;
207 > void
208 > cvtprim(        /* process Radiance primitive */
209 >        char    *inp,
210 >        char    *mod,
211 >        char    *typ,
212 >        char    *id,
213 >        FUNARGS *fa
214 > )
215   {
216          int     (*df)();
217  
218          df = (int (*)())lu_find(&rdispatch, typ)->data;
219          if (df != NULL) {                               /* convert */
220                  if ((*df)(mod, typ, id, fa) < 0) {
221 <                        fprintf(stderr, "%s: bad %s \"%s\"\n", typ, id);
221 >                        fprintf(stderr, "%s: bad %s \"%s\"\n", "rat2mgf", typ, id);
222                          exit(1);
223                  }
224 <        } else if (lu_find(&rmats, mod)->data != NULL)  /* make alias */
225 <                newmat(id, mod);
224 >        } else {                                        /* unsupported */
225 >                o_unsupported(mod, typ, id, fa);
226 >                if (lu_find(&rmats, mod)->data != NULL) /* make alias */
227 >                        newmat(id, mod);
228 >        }
229   }
230  
231  
232 < newmat(id, alias)               /* add a modifier to the alias list */
233 < char    *id;
234 < char    *alias;
232 > void
233 > newmat(         /* add a modifier to the alias list */
234 >        char    *id,
235 >        char    *alias
236 > )
237   {
238          register LUENT  *lp, *lpa;
239  
# Line 227 | Line 267 | memerr:
267   }
268  
269  
270 < setmat(id)                      /* set material to this one */
271 < char    *id;
270 > void
271 > setmat(                 /* set material to this one */
272 >        char    *id
273 > )
274   {
275          if (!strcmp(id, curmat))        /* already set? */
276                  return;
# Line 239 | Line 281 | char   *id;
281   }
282  
283  
284 < setobj(id)                      /* set object name to this one */
285 < char    *id;
284 > void
285 > setobj(                 /* set object name to this one */
286 >        char    *id
287 > )
288   {
289          register char   *cp, *cp2;
290          char    *end = NULL;
# Line 252 | Line 296 | char   *id;
296          if (end == NULL)
297                  end = cp;
298                                  /* copy to current object */
299 <        for (cp = id, cp2 = curobj; cp < end; *cp2++ = *cp++)
299 >        cp2 = curobj;
300 >        if (!isalpha(*id)) {    /* start with letter */
301 >                diff = *cp2 != 'O';
302 >                *cp2++ = 'O';
303 >        }
304 >        for (cp = id; cp < end; *cp2++ = *cp++) {
305 >                if ((*cp < '!') | (*cp > '~'))  /* limit to visible chars */
306 >                        *cp = '?';
307                  diff += *cp != *cp2;
308 +        }
309          if (!diff && !*cp2)
310                  return;
311          *cp2 = '\0';
# Line 262 | Line 314 | char   *id;
314   }
315  
316  
317 < init()                  /* initialize dispatch table and output */
317 > void
318 > init(void)                      /* initialize dispatch table and output */
319   {
320          lu_init(&vertab, NVERTS);
321          lu_init(&rdispatch, 22);
# Line 275 | Line 328 | init()                 /* initialize dispatch table and output */
328          add2dispatch("tube", o_cylinder);
329          add2dispatch("ring", o_ring);
330          add2dispatch("instance", o_instance);
331 +        add2dispatch("mesh", o_instance);
332          add2dispatch("plastic", o_plastic);
333          add2dispatch("plastic2", o_plastic);
334          add2dispatch("metal", o_metal);
335          add2dispatch("metal2", o_metal);
336          add2dispatch("glass", o_glass);
337 +        add2dispatch("dielectric", o_dielectric);
338          add2dispatch("trans", o_trans);
339          add2dispatch("trans2", o_trans);
340          add2dispatch("mirror", o_mirror);
# Line 287 | Line 342 | init()                 /* initialize dispatch table and output */
342          add2dispatch("spotlight", o_light);
343          add2dispatch("glow", o_light);
344          add2dispatch("illum", o_illum);
345 <        puts("# The following was converted from Radiance scene input");
345 >        puts("# The following was converted from RADIANCE scene input");
346          if (hasmult)
347                  printf("xf -s %.4e\n", unit_mult);
348          printf("o %s\n", curobj);
349   }
350  
351  
352 < uninit()                        /* mark end of MGF file */
352 > void
353 > uninit(void)                    /* mark end of MGF file */
354   {
355          puts("o");
356          if (hasmult)
357                  puts("xf");
358 <        puts("# End of data converted from Radiance scene input");
358 >        puts("# End of data converted from RADIANCE scene input");
359          lu_done(&rdispatch);
360          lu_done(&rmats);
361          lu_done(&vertab);
362   }
363  
364  
365 < clrverts()                      /* clear vertex table */
365 > void
366 > clrverts(void)                  /* clear vertex table */
367   {
368          register int    i;
369  
# Line 317 | Line 374 | clrverts()                     /* clear vertex table */
374   }
375  
376  
377 < add2dispatch(name, func)        /* add function to dispatch table */
378 < char    *name;
379 < int     (*func)();
377 > void
378 > add2dispatch(   /* add function to dispatch table */
379 >        char    *name,
380 >        int     (*func)()
381 > )
382   {
383          register LUENT  *lp;
384  
# Line 335 | Line 394 | int    (*func)();
394  
395  
396   char *
397 < getvertid(vname, vp)            /* get/set vertex ID for this point */
398 < char    *vname;
399 < FVECT   vp;
397 > getvertid(              /* get/set vertex ID for this point */
398 >        char    *vname,
399 >        FVECT   vp
400 > )
401   {
402 <        char    vkey[VKLEN];
402 >        static char     vkey[VKLEN];
403          register LUENT  *lp;
404          register int    i, vndx;
405  
406 <        clock++;                        /* increment counter */
406 >        vclock++;                       /* increment counter */
407          mkvkey(vkey, vp);
408          if ((lp = lu_find(&vertab, vkey)) == NULL)
409                  goto memerr;
# Line 369 | Line 429 | FVECT  vp;
429                  lp->data = (char *)&vert[vndx];                 /* set it */
430          } else
431                  vndx = (struct vert *)lp->data - vert;
432 <        vert[vndx].lused = clock;               /* record this use */
432 >        vert[vndx].lused = vclock;              /* record this use */
433          sprintf(vname, "v%d", vndx);
434          return(vname);
435   memerr:
# Line 379 | Line 439 | memerr:
439  
440  
441   int
442 < o_face(mod, typ, id, fa)                /* print out a polygon */
443 < char    *mod, *typ, *id;
444 < FUNARGS *fa;
442 > o_unsupported(          /* mark unsupported primitive */
443 >        char    *mod,
444 >        char    *typ,
445 >        char    *id,
446 >        FUNARGS *fa
447 > )
448   {
449 <        char    entbuf[512];
449 >        register int    i;
450 >
451 >        fputs("\n# Unsupported RADIANCE primitive:\n", stdout);
452 >        printf("# %s %s %s", mod, typ, id);
453 >        printf("\n# %d", fa->nsargs);
454 >        for (i = 0; i < fa->nsargs; i++)
455 >                printf(" %s", fa->sarg[i]);
456 > #ifdef IARGS
457 >        printf("\n# %d", fa->niargs);
458 >        for (i = 0; i < fa->niargs; i++)
459 >                printf(" %ld", fa->iarg[i]);
460 > #else
461 >        fputs("\n# 0", stdout);
462 > #endif
463 >        printf("\n# %d", fa->nfargs);
464 >        for (i = 0; i < fa->nfargs; i++)
465 >                printf(" %g", fa->farg[i]);
466 >        fputs("\n\n", stdout);
467 >        return(0);
468 > }
469 >
470 >
471 > int
472 > o_face(         /* print out a polygon */
473 >        char    *mod,
474 >        char    *typ,
475 >        char    *id,
476 >        FUNARGS *fa
477 > )
478 > {
479 >        char    entbuf[2048], *linestart;
480          register char   *cp;
481          register int    i;
482  
483 <        if (fa->nfargs < 9 | fa->nfargs % 3)
483 >        if ((fa->nfargs < 9) | (fa->nfargs % 3))
484                  return(-1);
485          setmat(mod);
486          setobj(id);
487 <        cp = entbuf;
487 >        cp = linestart = entbuf;
488          *cp++ = 'f';
489          for (i = 0; i < fa->nfargs; i += 3) {
490                  *cp++ = ' ';
491 +                if (cp - linestart > 72) {
492 +                        *cp++ = '\\'; *cp++ = '\n';
493 +                        linestart = cp;
494 +                        *cp++ = ' '; *cp++ = ' ';
495 +                }
496                  getvertid(cp, fa->farg + i);
497                  while (*cp)
498                          cp++;
# Line 405 | Line 503 | FUNARGS        *fa;
503  
504  
505   int
506 < o_cone(mod, typ, id, fa)        /* print out a cone */
507 < char    *mod, *typ, *id;
508 < register FUNARGS        *fa;
506 > o_cone( /* print out a cone */
507 >        char    *mod,
508 >        char    *typ,
509 >        char    *id,
510 >        register FUNARGS        *fa
511 > )
512   {
513          char    v1[6], v2[6];
514  
# Line 428 | Line 529 | register FUNARGS       *fa;
529  
530  
531   int
532 < o_sphere(mod, typ, id, fa)      /* print out a sphere */
533 < char    *mod, *typ, *id;
534 < register FUNARGS        *fa;
532 > o_sphere(       /* print out a sphere */
533 >        char    *mod,
534 >        char    *typ,
535 >        char    *id,
536 >        register FUNARGS        *fa
537 > )
538   {
539          char    cent[6];
540  
# Line 445 | Line 549 | register FUNARGS       *fa;
549  
550  
551   int
552 < o_cylinder(mod, typ, id, fa)    /* print out a cylinder */
553 < char    *mod, *typ, *id;
554 < register FUNARGS        *fa;
552 > o_cylinder(     /* print out a cylinder */
553 >        char    *mod,
554 >        char    *typ,
555 >        char    *id,
556 >        register FUNARGS        *fa
557 > )
558   {
559          char    v1[6], v2[6];
560  
# Line 464 | Line 571 | register FUNARGS       *fa;
571  
572  
573   int
574 < o_ring(mod, typ, id, fa)        /* print out a ring */
575 < char    *mod, *typ, *id;
576 < register FUNARGS        *fa;
574 > o_ring( /* print out a ring */
575 >        char    *mod,
576 >        char    *typ,
577 >        char    *id,
578 >        register FUNARGS        *fa
579 > )
580   {
581          if (fa->nfargs != 8)
582                  return(-1);
# Line 487 | Line 597 | register FUNARGS       *fa;
597  
598  
599   int
600 < o_instance(mod, typ, id, fa)    /* convert an instance */
601 < char    *mod, *typ, *id;
602 < FUNARGS *fa;
600 > o_instance(     /* convert an instance (or mesh) */
601 >        char    *mod,
602 >        char    *typ,
603 >        char    *id,
604 >        FUNARGS *fa
605 > )
606   {
607          register int    i;
608          register char   *cp;
# Line 528 | Line 641 | FUNARGS        *fa;
641  
642  
643   int
644 < o_source(mod, typ, id, fa)      /* convert a source */
645 < char    *mod, *typ, *id;
646 < FUNARGS *fa;
644 > o_illum(        /* convert an illum material */
645 >        char    *mod,
646 >        char    *typ,
647 >        char    *id,
648 >        FUNARGS *fa
649 > )
650   {
535        return(0);              /* there is no MGF equivalent! */
536 }
537
538
539 int
540 o_illum(mod, typ, id, fa)       /* convert an illum material */
541 char    *mod, *typ, *id;
542 FUNARGS *fa;
543 {
651          if (fa->nsargs == 1 && strcmp(fa->sarg[0], VOIDID)) {
652                  newmat(id, fa->sarg[0]);        /* just create alias */
653                  return(0);
# Line 553 | Line 660 | FUNARGS        *fa;
660  
661  
662   int
663 < o_plastic(mod, typ, id, fa)     /* convert a plastic material */
664 < char    *mod, *typ, *id;
665 < register FUNARGS        *fa;
663 > o_plastic(      /* convert a plastic material */
664 >        char    *mod,
665 >        char    *typ,
666 >        char    *id,
667 >        register FUNARGS        *fa
668 > )
669   {
670          COLOR   cxyz, rrgb;
671          double  d;
# Line 581 | Line 691 | register FUNARGS       *fa;
691  
692  
693   int
694 < o_metal(mod, typ, id, fa)       /* convert a metal material */
695 < char    *mod, *typ, *id;
696 < register FUNARGS        *fa;
694 > o_metal(        /* convert a metal material */
695 >        char    *mod,
696 >        char    *typ,
697 >        char    *id,
698 >        register FUNARGS        *fa
699 > )
700   {
701          COLOR   cxyz, rrgb;
702          double  d;
# Line 607 | Line 720 | register FUNARGS       *fa;
720  
721  
722   int
723 < o_glass(mod, typ, id, fa)       /* convert a glass material */
724 < char    *mod, *typ, *id;
725 < register FUNARGS        *fa;
723 > o_glass(        /* convert a glass material */
724 >        char    *mod,
725 >        char    *typ,
726 >        char    *id,
727 >        register FUNARGS        *fa
728 > )
729   {
730          COLOR   cxyz, rrgb, trgb;
731          double  nrfr = 1.52, F, d;
# Line 620 | Line 736 | register FUNARGS       *fa;
736          newmat(id, NULL);
737          if (fa->nfargs == 4)
738                  nrfr = fa->farg[3];
739 +        printf("\tir %f 0\n", nrfr);
740          F = (1. - nrfr)/(1. + nrfr);            /* use normal incidence */
741          F *= F;
742          for (i = 0; i < 3; i++) {
# Line 645 | Line 762 | register FUNARGS       *fa;
762  
763  
764   int
765 < o_mirror(mod, typ, id, fa)      /* convert a mirror material */
766 < char    *mod, *typ, *id;
767 < register FUNARGS        *fa;
765 > o_dielectric(   /* convert a dielectric material */
766 >        char    *mod,
767 >        char    *typ,
768 >        char    *id,
769 >        register FUNARGS        *fa
770 > )
771   {
772 +        COLOR   cxyz, trgb;
773 +        double  F, d;
774 +        register int    i;
775 +
776 +        if (fa->nfargs != 5)
777 +                return(-1);
778 +        newmat(id, NULL);
779 +        F = (1. - fa->farg[3])/(1. + fa->farg[3]);      /* normal incidence */
780 +        F *= F;
781 +        for (i = 0; i < 3; i++)
782 +                trgb[i] = (1. - F)*pow(fa->farg[i], C_1SIDEDTHICK/unit_mult);
783 +        printf("\tir %f 0\n", fa->farg[3]);     /* put index of refraction */
784 +        printf("\tsides 1\n");
785 +        puts("\tc");                            /* put reflected component */
786 +        printf("\trs %.4f 0\n", F);
787 +        rgb_cie(cxyz, trgb);                    /* put transmitted component */
788 +        puts("\tc");
789 +        d = cxyz[0] + cxyz[1] + cxyz[2];
790 +        if (d > FTINY)
791 +                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
792 +        printf("\tts %.4f 0\n", cxyz[1]);
793 +        return(0);
794 + }
795 +
796 +
797 + int
798 + o_mirror(       /* convert a mirror material */
799 +        char    *mod,
800 +        char    *typ,
801 +        char    *id,
802 +        register FUNARGS        *fa
803 + )
804 + {
805          COLOR   cxyz, rrgb;
806          double  d;
807  
# Line 671 | Line 824 | register FUNARGS       *fa;
824  
825  
826   int
827 < o_trans(mod, typ, id, fa)       /* convert a trans material */
828 < char    *mod, *typ, *id;
829 < register FUNARGS        *fa;
827 > o_trans(        /* convert a trans material */
828 >        char    *mod,
829 >        char    *typ,
830 >        char    *id,
831 >        register FUNARGS        *fa
832 > )
833   {
834          COLOR   cxyz, rrgb;
835          double  rough, trans, tspec, d;
# Line 710 | Line 866 | register FUNARGS       *fa;
866  
867  
868   int
869 < o_light(mod, typ, id, fa)               /* convert a light type */
870 < char    *mod, *typ, *id;
871 < register FUNARGS        *fa;
869 > o_light(                /* convert a light type */
870 >        char    *mod,
871 >        char    *typ,
872 >        char    *id,
873 >        register FUNARGS        *fa
874 > )
875   {
876          COLOR   cxyz, rrgb;
877          double  d;
# Line 726 | Line 885 | register FUNARGS       *fa;
885          puts("\tc");
886          if (d > FTINY)
887                  printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
888 <        printf("\ted %.4g\n", cxyz[1]*WHTEFFICACY);
888 >        printf("\ted %.4g\n", cxyz[1]*(PI*WHTEFFICACY));
889          return(0);
890   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines