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

Comparing ray/src/cv/mgf2rad.c (file contents):
Revision 2.1 by greg, Wed Jun 22 15:33:03 1994 UTC vs.
Revision 2.24 by gwlarson, Wed Jun 9 14:06:00 1999 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1994 Regents of the University of California */
1 > /* Copyright (c) 1996 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 17 | Line 17 | static char SCCSid[] = "$SunId$ LBL";
17  
18   #define putv(v)         printf("%18.12g %18.12g %18.12g\n",(v)[0],(v)[1],(v)[2])
19  
20 < #define isgrey(cxy)     ((cxy)->cx > .31 && (cxy)->cx < .35 && \
21 <                        (cxy)->cy > .31 && (cxy)->cy < .35)
20 > #define invert          (xf_context != NULL && xf_context->rev)
21  
22 < #define is0vect(v)      ((v)[0] == 0. && (v)[1] == 0. && (v)[2] == 0.)
22 > double  glowdist = FHUGE;               /* glow test distance */
23  
24 < #define BIGFLT          1e8
24 > double  emult = 1.;                     /* emitter multiplier */
25  
26 < double  glowdist = 1.5*BIGFLT;          /* glow test distance */
26 > FILE    *matfp;                         /* material output file */
27  
29 double  emult = 1.;                     /* emmitter multiplier */
30
28   int     r_comment(), r_cone(), r_cyl(), r_face(), r_ies(), r_ring(), r_sph();
29   char    *material(), *object(), *addarg();
30  
# Line 36 | Line 33 | main(argc, argv)               /* convert files to stdout */
33   int     argc;
34   char    *argv[];
35   {
36 <        int     i, rv;
36 >        int     i;
37 >
38 >        matfp = stdout;
39 >                                /* print out parser version */
40 >        printf("## Translated from MGF Version %d.%d\n", MG_VMAJOR, MG_VMINOR);
41                                  /* initialize dispatch table */
42 <        mg_ehand[MG_E_COMMENT] = r_comment;
43 <        mg_ehand[MG_E_COLOR] = c_hcolor;
44 <        mg_ehand[MG_E_CONE] = r_cone;
45 <        mg_ehand[MG_E_CXY] = c_hcolor;
46 <        mg_ehand[MG_E_CYL] = r_cyl;
47 <        mg_ehand[MG_E_ED] = c_hmaterial;
48 <        mg_ehand[MG_E_FACE] = r_face;
49 <        mg_ehand[MG_E_IES] = r_ies;
50 <        mg_ehand[MG_E_MATERIAL] = c_hmaterial;
51 <        mg_ehand[MG_E_NORMAL] = c_hvertex;
52 <        mg_ehand[MG_E_OBJECT] = obj_handler;
53 <        mg_ehand[MG_E_POINT] = c_hvertex;
54 <        mg_ehand[MG_E_RD] = c_hmaterial;
55 <        mg_ehand[MG_E_RING] = r_ring;
56 <        mg_ehand[MG_E_RS] = c_hmaterial;
57 <        mg_ehand[MG_E_SPH] = r_sph;
58 <        mg_ehand[MG_E_TD] = c_hmaterial;
59 <        mg_ehand[MG_E_TS] = c_hmaterial;
60 <        mg_ehand[MG_E_VERTEX] = c_hvertex;
61 <        mg_ehand[MG_E_XF] = xf_handler;
42 >        mg_ehand[MG_E_COMMENT] = r_comment;     /* we pass comments */
43 >        mg_ehand[MG_E_COLOR] = c_hcolor;        /* they get color */
44 >        mg_ehand[MG_E_CONE] = r_cone;           /* we do cones */
45 >        mg_ehand[MG_E_CMIX] = c_hcolor;         /* they mix colors */
46 >        mg_ehand[MG_E_CSPEC] = c_hcolor;        /* they get spectra */
47 >        mg_ehand[MG_E_CXY] = c_hcolor;          /* they get chromaticities */
48 >        mg_ehand[MG_E_CCT] = c_hcolor;          /* they get color temp's */
49 >        mg_ehand[MG_E_CYL] = r_cyl;             /* we do cylinders */
50 >        mg_ehand[MG_E_ED] = c_hmaterial;        /* they get emission */
51 >        mg_ehand[MG_E_FACE] = r_face;           /* we do faces */
52 >        mg_ehand[MG_E_IES] = r_ies;             /* we do IES files */
53 >        mg_ehand[MG_E_IR] = c_hmaterial;        /* they get refractive index */
54 >        mg_ehand[MG_E_MATERIAL] = c_hmaterial;  /* they get materials */
55 >        mg_ehand[MG_E_NORMAL] = c_hvertex;      /* they get normals */
56 >        mg_ehand[MG_E_OBJECT] = obj_handler;    /* they track object names */
57 >        mg_ehand[MG_E_POINT] = c_hvertex;       /* they get points */
58 >        mg_ehand[MG_E_RD] = c_hmaterial;        /* they get diffuse refl. */
59 >        mg_ehand[MG_E_RING] = r_ring;           /* we do rings */
60 >        mg_ehand[MG_E_RS] = c_hmaterial;        /* they get specular refl. */
61 >        mg_ehand[MG_E_SIDES] = c_hmaterial;     /* they get # sides */
62 >        mg_ehand[MG_E_SPH] = r_sph;             /* we do spheres */
63 >        mg_ehand[MG_E_TD] = c_hmaterial;        /* they get diffuse trans. */
64 >        mg_ehand[MG_E_TS] = c_hmaterial;        /* they get specular trans. */
65 >        mg_ehand[MG_E_VERTEX] = c_hvertex;      /* they get vertices */
66 >        mg_ehand[MG_E_XF] = xf_handler;         /* they track transforms */
67          mg_init();              /* initialize the parser */
68 <                                        /* get options & print header */
68 >                                        /* get our options & print header */
69          printf("## %s", argv[0]);
70          for (i = 1; i < argc && argv[i][0] == '-'; i++) {
71                  printf(" %s", argv[i]);
72                  switch (argv[i][1]) {
73                  case 'g':                       /* glow distance (meters) */
74 <                        if (argv[i][2] || badarg(argc-i, argv+i, "f"))
74 >                        if (argv[i][2] || badarg(argc-i-1, argv+i+1, "f"))
75                                  goto userr;
76                          glowdist = atof(argv[++i]);
77                          printf(" %s", argv[i]);
78                          break;
79                  case 'e':                       /* emitter multiplier */
80 <                        if (argv[i][2] || badarg(argc-i, argv+i, "f"))
80 >                        if (argv[i][2] || badarg(argc-i-1, argv+i+1, "f"))
81                                  goto userr;
82                          emult = atof(argv[++i]);
83                          printf(" %s", argv[i]);
84                          break;
85 +                case 'm':                       /* materials file */
86 +                        matfp = fopen(argv[++i], "a");
87 +                        if (matfp == NULL) {
88 +                                fprintf(stderr, "%s: cannot append\n", argv[i]);
89 +                                exit(1);
90 +                        }
91 +                        printf(" %s", argv[i]);
92 +                        break;
93                  default:
94                          goto userr;
95                  }
96          }
97          putchar('\n');
98          if (i == argc) {                /* convert stdin */
99 <                if ((rv = mg_load(NULL)) != MG_OK)
99 >                if (mg_load(NULL) != MG_OK)
100                          exit(1);
101 +                if (mg_nunknown)
102 +                        printf("## %s: %u unknown entities\n",
103 +                                        argv[0], mg_nunknown);
104          } else                          /* convert each file */
105                  for ( ; i < argc; i++) {
106                          printf("## %s %s ##############################\n",
107                                          argv[0], argv[i]);
108 <                        if ((rv = mg_load(argv[i])) != MG_OK)
108 >                        if (mg_load(argv[i]) != MG_OK)
109                                  exit(1);
110 +                        if (mg_nunknown) {
111 +                                printf("## %s %s: %u unknown entities\n",
112 +                                                argv[0], argv[i], mg_nunknown);
113 +                                mg_nunknown = 0;
114 +                        }
115                  }
116          exit(0);
117   userr:
118 <        fprintf(stderr, "Usage: %s [-g dist][-m mult] [file.mgf] ..\n",
118 >        fprintf(stderr, "Usage: %s [-g dist][-e mult][-m matf] [file.mgf] ..\n",
119                          argv[0]);
120          exit(1);
121   }
# Line 104 | Line 126 | r_comment(ac, av)              /* repeat a comment verbatim */
126   register int    ac;
127   register char   **av;
128   {
129 <        fputs("\n#", stdout);   /* use Radiance comment character */
130 <        while (--ac) {
129 >        putchar('#');           /* use Radiance comment character */
130 >        while (--ac) {                  /* pass through verbatim */
131                  putchar(' ');
132                  fputs(*++av, stdout);
133          }
# Line 125 | Line 147 | char   **av;
147          C_VERTEX        *cv1, *cv2;
148          FVECT   p1, p2;
149          int     inv;
150 <
150 >                                        /* check argument count and type */
151          if (ac != 5)
152                  return(MG_EARGC);
153          if (!isflt(av[2]) || !isflt(av[4]))
154                  return(MG_ETYPE);
155 +                                        /* get the endpoint vertices */
156          if ((cv1 = c_getvert(av[1])) == NULL ||
157                          (cv2 = c_getvert(av[3])) == NULL)
158                  return(MG_EUNDEF);
159 <        xf_xfmpoint(p1, cv1->p);
159 >        xf_xfmpoint(p1, cv1->p);        /* transform endpoints */
160          xf_xfmpoint(p2, cv2->p);
161 <        r1 = xf_scale(atof(av[2]));
161 >        r1 = xf_scale(atof(av[2]));     /* scale radii */
162          r2 = xf_scale(atof(av[4]));
163 <        inv = r1 < 0.;
164 <        if (r1 == 0.) {
163 >        inv = r1 < 0.;                  /* check for inverted cone */
164 >        if (r1 == 0.) {                 /* check for illegal radii */
165                  if (r2 == 0.)
166                          return(MG_EILL);
167                  inv = r2 < 0.;
# Line 148 | Line 171 | char   **av;
171                  r1 = -r1;
172                  r2 = -r2;
173          }
174 <        if ((mat = material()) == NULL)
174 >        if ((mat = material()) == NULL) /* get material */
175                  return(MG_EBADMAT);
176 +                                        /* spit the sucker out */
177          printf("\n%s %s %sc%d\n", mat, inv ? "cup" : "cone",
178                          object(), ++ncones);
179          printf("0\n0\n8\n");
# Line 171 | Line 195 | char   **av;
195          C_VERTEX        *cv1, *cv2;
196          FVECT   p1, p2;
197          int     inv;
198 <
198 >                                        /* check argument count and type */
199          if (ac != 4)
200                  return(MG_EARGC);
201          if (!isflt(av[2]))
202                  return(MG_ETYPE);
203 +                                        /* get the endpoint vertices */
204          if ((cv1 = c_getvert(av[1])) == NULL ||
205                          (cv2 = c_getvert(av[3])) == NULL)
206                  return(MG_EUNDEF);
207 <        xf_xfmpoint(p1, cv1->p);
207 >        xf_xfmpoint(p1, cv1->p);        /* transform endpoints */
208          xf_xfmpoint(p2, cv2->p);
209 <        rad = xf_scale(atof(av[2]));
210 <        if ((inv = rad < 0.))
209 >        rad = xf_scale(atof(av[2]));    /* scale radius */
210 >        if ((inv = rad < 0.))           /* check for inverted cylinder */
211                  rad = -rad;
212 <        if ((mat = material()) == NULL)
212 >        if ((mat = material()) == NULL) /* get material */
213                  return(MG_EBADMAT);
214 +                                        /* spit out the primitive */
215          printf("\n%s %s %scy%d\n", mat, inv ? "tube" : "cylinder",
216                          object(), ++ncyls);
217          printf("0\n0\n7\n");
# Line 207 | Line 233 | char   **av;
233          C_VERTEX        *cv;
234          FVECT   cent;
235          int     inv;
236 <
236 >                                        /* check argument count and type */
237          if (ac != 3)
238                  return(MG_EARGC);
239          if (!isflt(av[2]))
240                  return(MG_ETYPE);
241 <        if ((cv = c_getvert(av[1])) == NULL)
241 >        if ((cv = c_getvert(av[1])) == NULL)    /* get center vertex */
242                  return(MG_EUNDEF);
243 <        xf_xfmpoint(cent, cv->p);
244 <        rad = xf_scale(atof(av[2]));
245 <        if ((inv = rad < 0.))
243 >        xf_xfmpoint(cent, cv->p);               /* transform center */
244 >        rad = xf_scale(atof(av[2]));            /* scale radius */
245 >        if ((inv = rad < 0.))                   /* check for inversion */
246                  rad = -rad;
247 <        if ((mat = material()) == NULL)
247 >        if ((mat = material()) == NULL)         /* get material */
248                  return(MG_EBADMAT);
249 +                                                /* spit out primitive */
250          printf("\n%s %s %ss%d\n", mat, inv ? "bubble" : "sphere",
251                          object(), ++nsphs);
252          printf("0\n0\n4 %18.12g %18.12g %18.12g %18.12g\n",
# Line 238 | Line 265 | char   **av;
265          double  r1, r2;
266          C_VERTEX        *cv;
267          FVECT   cent, norm;
268 <
268 >                                        /* check argument count and type */
269          if (ac != 4)
270                  return(MG_EARGC);
271          if (!isflt(av[2]) || !isflt(av[3]))
272                  return(MG_ETYPE);
273 <        if ((cv = c_getvert(av[1])) == NULL)
273 >        if ((cv = c_getvert(av[1])) == NULL)    /* get center vertex */
274                  return(MG_EUNDEF);
275 <        if (is0vect(cv->n))
275 >        if (is0vect(cv->n))                     /* make sure we have normal */
276                  return(MG_EILL);
277 <        xf_xfmpoint(cent, cv->p);
278 <        xf_rotvect(norm, cv->n);
279 <        r1 = xf_scale(atof(av[2]));
277 >        xf_xfmpoint(cent, cv->p);               /* transform center */
278 >        xf_rotvect(norm, cv->n);                /* rotate normal */
279 >        r1 = xf_scale(atof(av[2]));             /* scale radii */
280          r2 = xf_scale(atof(av[3]));
281          if (r1 < 0. | r2 <= r1)
282                  return(MG_EILL);
283 <        if ((mat = material()) == NULL)
283 >        if ((mat = material()) == NULL)         /* get material */
284                  return(MG_EBADMAT);
285 +                                                /* spit out primitive */
286          printf("\n%s ring %sr%d\n", mat, object(), ++nrings);
287          printf("0\n0\n8\n");
288          putv(cent);
# Line 275 | Line 303 | char   **av;
303          register C_VERTEX       *cv;
304          FVECT   v;
305          int     rv;
306 <
306 >                                        /* check argument count and type */
307          if (ac < 4)
308                  return(MG_EARGC);
309 <        if ((mat = material()) == NULL)
309 >        if ((mat = material()) == NULL) /* get material */
310                  return(MG_EBADMAT);
311 <        if (ac < 5) {                           /* check for surface normals */
311 >        if (ac <= 5) {                          /* check for smoothing */
312 >                C_VERTEX        *cva[5];
313                  for (i = 1; i < ac; i++) {
314 <                        if ((cv = c_getvert(av[i])) == NULL)
314 >                        if ((cva[i-1] = c_getvert(av[i])) == NULL)
315                                  return(MG_EUNDEF);
316 <                        if (is0vect(cv->n))
316 >                        if (is0vect(cva[i-1]->n))
317                                  break;
318                  }
319 <                if (i == ac) {                  /* break into triangles */
320 <                        do_tri(mat, av[1], av[2], av[3]);
319 >                if (i == ac) {
320 >                        i = flat_tri(cva[0]->p, cva[1]->p, cva[2]->p,
321 >                                        cva[0]->n, cva[1]->n, cva[2]->n);
322 >                        if (i < 0)
323 >                                return(MG_OK);  /* degenerate (error?) */
324 >                }
325 >                if (!i) {                       /* smoothed triangles */
326 >                        do_tri(mat, cva[0], cva[1], cva[2]);
327                          if (ac == 5)
328 <                                do_tri(mat, av[3], av[4], av[1]);
328 >                                do_tri(mat, cva[2], cva[3], cva[0]);
329                          return(MG_OK);
330                  }
331          }
332 +                                        /* spit out unsmoothed primitive */
333          printf("\n%s polygon %sf%d\n", mat, object(), ++nfaces);
334          printf("0\n0\n%d\n", 3*(ac-1));
335 <        for (i = 1; i < ac; i++) {
336 <                if ((cv = c_getvert(av[i])) == NULL)
335 >        for (i = 1; i < ac; i++) {      /* get, transform, print each vertex */
336 >                if ((cv = c_getvert(av[invert ? ac-i : i])) == NULL)
337                          return(MG_EUNDEF);
338                  xf_xfmpoint(v, cv->p);
339                  putv(v);
# Line 306 | Line 342 | char   **av;
342   }
343  
344  
345 + int
346   r_ies(ac, av)                           /* convert an IES luminaire file */
347   int     ac;
348   char    **av;
349   {
350          int     xa0 = 2;
351 <        char    combuf[72];
351 >        char    combuf[128];
352          char    fname[48];
353          char    *oname;
354          register char   *op;
355          register int    i;
356 <
356 >                                        /* check argument count */
357          if (ac < 2)
358                  return(MG_EARGC);
359 <        (void)strcpy(combuf, "ies2rad");
360 <        op = combuf + 7;
361 <        if (ac-xa0 >= 2 && !strcmp(av[xa0], "-m")) {
362 <                if (!isflt(av[xa0+1]))
326 <                        return(MG_ETYPE);
327 <                op = addarg(addarg(op, "-m"), av[xa0+1]);
328 <                xa0 += 2;
329 <        }
330 <        if (access(av[1], 0) == -1)
331 <                return(MG_ENOFILE);
332 <        *op++ = ' ';                    /* IES filename goes last */
333 <        (void)strcpy(op, av[1]);
334 <        system(combuf);                 /* run ies2rad */
335 <                                        /* now let's find the output file */
336 <        if ((op = strrchr(av[1], '/')) == NULL)
359 >                                        /* construct output file name */
360 >        if ((op = strrchr(av[1], '/')) != NULL)
361 >                op++;
362 >        else
363                  op = av[1];
364          (void)strcpy(fname, op);
365          if ((op = strrchr(fname, '.')) == NULL)
366                  op = fname + strlen(fname);
367          (void)strcpy(op, ".rad");
368 <        if (access(fname, 0) == -1)
369 <                return(MG_EINCL);
370 <                                        /* put out xform command */
371 <        printf("\n!xform");
368 >                                        /* see if we need to run ies2rad */
369 >        if (access(fname, 0) == -1) {
370 >                (void)strcpy(combuf, "ies2rad");/* build ies2rad command */
371 >                op = combuf + 7;                /* get -m option (first) */
372 >                if (ac-xa0 >= 2 && !strcmp(av[xa0], "-m")) {
373 >                        if (!isflt(av[xa0+1]))
374 >                                return(MG_ETYPE);
375 >                        op = addarg(addarg(op, "-m"), av[xa0+1]);
376 >                        xa0 += 2;
377 >                }
378 >                *op++ = ' ';                    /* build IES filename */
379 >                i = 0;
380 >                if (mg_file != NULL &&
381 >                                (oname = strrchr(mg_file->fname,'/')) != NULL) {
382 >                        i = oname - mg_file->fname + 1;
383 >                        (void)strcpy(op, mg_file->fname);
384 >                }
385 >                (void)strcpy(op+i, av[1]);
386 >                if (access(op, 0) == -1)        /* check for file existence */
387 >                        return(MG_ENOFILE);
388 >                system(combuf);                 /* run ies2rad */
389 >                if (access(fname, 0) == -1)     /* check success */
390 >                        return(MG_EINCL);
391 >        }
392 >        printf("\n!xform");                     /* put out xform command */
393          oname = object();
394 <        if (*oname)
395 <                printf(" -n %s", oname);
394 >        if (*oname) {
395 >                printf(" -n ");
396 >                for (op = oname; op[1]; op++)   /* remove trailing separator */
397 >                        putchar(*op);
398 >        }
399          for (i = xa0; i < ac; i++)
400                  printf(" %s", av[i]);
401          if (ac > xa0 && xf_argc > 0)
# Line 357 | Line 407 | char   **av;
407   }
408  
409  
410 < do_tri(mat, vn1, vn2, vn3)              /* put out smoothed triangle */
411 < char    *mat, *vn1, *vn2, *vn3;
410 > do_tri(mat, cv1, cv2, cv3)              /* put out smoothed triangle */
411 > char    *mat;
412 > C_VERTEX        *cv1, *cv2, *cv3;
413   {
414          static int      ntris;
364        char    *mod = mat;
415          BARYCCM bvecs;
416          FLOAT   bcoor[3][3];
417 <        C_VERTEX        *cv1, *cv2, *cv3;
417 >        C_VERTEX        *cvt;
418          FVECT   v1, v2, v3;
419          FVECT   n1, n2, n3;
420          register int    i;
421 <                        /* the following is repeat code, so assume it's OK */
422 <        cv1 = c_getvert(vn1);
423 <        cv2 = c_getvert(vn2);
424 <        cv3 = c_getvert(vn3);
421 >
422 >        if (invert) {                   /* swap vertex order if inverted */
423 >                cvt = cv1;
424 >                cv1 = cv3;
425 >                cv3 = cvt;
426 >        }
427          xf_xfmpoint(v1, cv1->p);
428          xf_xfmpoint(v2, cv2->p);
429          xf_xfmpoint(v3, cv3->p);
430 <        if (comp_baryc(&bvecs, v1, v2, v3) == 0) {
431 <                printf("\n%s texfunc T-nor\n", mod);
432 <                mod = "T-nor";
433 <                printf("4 dx dy dz %s\n0\n", TCALNAME);
434 <                xf_rotvect(n1, cv1->n);
435 <                xf_rotvect(n2, cv2->n);
436 <                xf_rotvect(n3, cv3->n);
437 <                for (i = 0; i < 3; i++) {
438 <                        bcoor[i][0] = n1[i];
439 <                        bcoor[i][1] = n2[i];
440 <                        bcoor[i][2] = n3[i];
441 <                }
390 <                put_baryc(&bvecs, bcoor, 3);
430 >                                        /* compute barycentric coords. */
431 >        if (comp_baryc(&bvecs, v1, v2, v3) < 0)
432 >                return;                         /* degenerate triangle! */
433 >        printf("\n%s texfunc T-nor\n", mat);    /* put out texture */
434 >        printf("4 dx dy dz %s\n0\n", TCALNAME);
435 >        xf_rotvect(n1, cv1->n);
436 >        xf_rotvect(n2, cv2->n);
437 >        xf_rotvect(n3, cv3->n);
438 >        for (i = 0; i < 3; i++) {
439 >                bcoor[i][0] = n1[i];
440 >                bcoor[i][1] = n2[i];
441 >                bcoor[i][2] = n3[i];
442          }
443 <        printf("\n%s polygon %st%d\n", mod, object(), ++ntris);
443 >        put_baryc(&bvecs, bcoor, 3);
444 >                                                /* put out triangle */
445 >        printf("\nT-nor polygon %st%d\n", object(), ++ntris);
446          printf("0\n0\n9\n");
447          putv(v1);
448          putv(v2);
# Line 405 | Line 458 | material()                     /* get (and print) current material */
458          double  d;
459          register int    i;
460  
461 <        if (c_cmaterial->name != NULL)
462 <                mname = c_cmaterial->name;
461 >        if (c_cmname != NULL)
462 >                mname = c_cmname;
463          if (!c_cmaterial->clock)
464                  return(mname);          /* already current */
465                                  /* else update output */
466          c_cmaterial->clock = 0;
467          if (c_cmaterial->ed > .1) {     /* emitter */
468                  cvtcolor(radrgb, &c_cmaterial->ed_c,
469 <                                emult*c_cmaterial->ed/WHTEFFICACY);
470 <                if (glowdist < BIGFLT) {        /* do a glow */
471 <                        printf("\nvoid glow %s\n0\n0\n", mname);
472 <                        printf("4 %f %f %f %f\n", colval(radrgb,RED),
469 >                                emult*c_cmaterial->ed/(PI*WHTEFFICACY));
470 >                if (glowdist < FHUGE) {         /* do a glow */
471 >                        fprintf(matfp, "\nvoid glow %s\n0\n0\n", mname);
472 >                        fprintf(matfp, "4 %f %f %f %f\n", colval(radrgb,RED),
473                                          colval(radrgb,GRN),
474                                          colval(radrgb,BLU), glowdist);
475                  } else {
476 <                        printf("\nvoid light %s\n0\n0\n", mname);
477 <                        printf("3 %f %f %f\n", colval(radrgb,RED),
476 >                        fprintf(matfp, "\nvoid light %s\n0\n0\n", mname);
477 >                        fprintf(matfp, "3 %f %f %f\n", colval(radrgb,RED),
478                                          colval(radrgb,GRN),
479                                          colval(radrgb,BLU));
480                  }
# Line 429 | Line 482 | material()                     /* get (and print) current material */
482          }
483          d = c_cmaterial->rd + c_cmaterial->td +
484                          c_cmaterial->rs + c_cmaterial->ts;
485 <        if (d <= 0. | d >= 1.)
485 >        if (d < 0. | d > 1.)
486                  return(NULL);
487 <        if (c_cmaterial->td > .01 || c_cmaterial->ts > .01) {   /* trans */
487 >                                        /* check for glass/dielectric */
488 >        if (c_cmaterial->nr > 1.1 &&
489 >                        c_cmaterial->ts > .25 && c_cmaterial->rs <= .125 &&
490 >                        c_cmaterial->td <= .01 && c_cmaterial->rd <= .01 &&
491 >                        c_cmaterial->rs_a <= .01 && c_cmaterial->ts_a <= .01) {
492 >                cvtcolor(radrgb, &c_cmaterial->ts_c,
493 >                                c_cmaterial->ts + c_cmaterial->rs);
494 >                if (c_cmaterial->sided) {               /* dielectric */
495 >                        colval(radrgb,RED) = pow(colval(radrgb,RED),
496 >                                                        1./C_1SIDEDTHICK);
497 >                        colval(radrgb,GRN) = pow(colval(radrgb,GRN),
498 >                                                        1./C_1SIDEDTHICK);
499 >                        colval(radrgb,BLU) = pow(colval(radrgb,BLU),
500 >                                                        1./C_1SIDEDTHICK);
501 >                        fprintf(matfp, "\nvoid dielectric %s\n0\n0\n", mname);
502 >                        fprintf(matfp, "5 %g %g %g %f 0\n", colval(radrgb,RED),
503 >                                        colval(radrgb,GRN), colval(radrgb,BLU),
504 >                                        c_cmaterial->nr);
505 >                        return(mname);
506 >                }
507 >                                                        /* glass */
508 >                fprintf(matfp, "\nvoid glass %s\n0\n0\n", mname);
509 >                fprintf(matfp, "4 %f %f %f %f\n", colval(radrgb,RED),
510 >                                colval(radrgb,GRN), colval(radrgb,BLU),
511 >                                c_cmaterial->nr);
512 >                return(mname);
513 >                }
514 >                                        /* check for trans */
515 >        if (c_cmaterial->td > .01 || c_cmaterial->ts > .01) {
516                  double  ts, a5, a6;
517  
518 <                ts = sqrt(c_cmaterial->ts);     /* because we use 2 sides */
518 >                if (c_cmaterial->sided) {
519 >                        ts = sqrt(c_cmaterial->ts);     /* approximate */
520 >                        a5 = .5;
521 >                } else {
522 >                        ts = c_cmaterial->ts;
523 >                        a5 = 1.;
524 >                }
525                                                  /* average colors */
526                  d = c_cmaterial->rd + c_cmaterial->td + ts;
527                  cvtcolor(radrgb, &c_cmaterial->rd_c, c_cmaterial->rd/d);
# Line 444 | Line 531 | material()                     /* get (and print) current material */
531                  addcolor(radrgb, c2);
532                  if (c_cmaterial->rs + ts > .0001)
533                          a5 = (c_cmaterial->rs*c_cmaterial->rs_a +
534 <                                        ts*.5*c_cmaterial->ts_a) /
534 >                                        ts*a5*c_cmaterial->ts_a) /
535                                          (c_cmaterial->rs + ts);
536                  a6 = (c_cmaterial->td + ts) /
537                                  (c_cmaterial->rd + c_cmaterial->td + ts);
538 <                if (a6 < .999) {
538 >                if (a6 < .999)
539                          d = c_cmaterial->rd/(1. - c_cmaterial->rs)/(1. - a6);
540 <                        scalecolor(radrgb, d);
541 <                }
542 <                printf("\nvoid trans %s\n0\n0\n", mname);
543 <                printf("7 %f %f %f\n", colval(radrgb,RED),
540 >                else
541 >                        d = c_cmaterial->td + ts;
542 >                scalecolor(radrgb, d);
543 >                fprintf(matfp, "\nvoid trans %s\n0\n0\n", mname);
544 >                fprintf(matfp, "7 %f %f %f\n", colval(radrgb,RED),
545                                  colval(radrgb,GRN), colval(radrgb,BLU));
546 <                printf("\t%f %f %f %f\n", c_cmaterial->rs, a5, a6,
546 >                fprintf(matfp, "\t%f %f %f %f\n", c_cmaterial->rs, a5, a6,
547                                  ts/(ts + c_cmaterial->td));
548                  return(mname);
549          }
550 <        if (c_cmaterial->rs < .01 || isgrey(&c_cmaterial->rs_c)) { /* plastic */
551 <                if (c_cmaterial->rs > .999)
552 <                        cvtcolor(radrgb, &c_cmaterial->rd_c, 1.);
465 <                else
466 <                        cvtcolor(radrgb, &c_cmaterial->rd_c,
550 >                                        /* check for plastic */
551 >        if (c_cmaterial->rs < .1) {
552 >                cvtcolor(radrgb, &c_cmaterial->rd_c,
553                                          c_cmaterial->rd/(1.-c_cmaterial->rs));
554 <                printf("\nvoid plastic %s\n0\n0\n", mname);
555 <                printf("5 %f %f %f %f %f\n", colval(radrgb,RED),
554 >                fprintf(matfp, "\nvoid plastic %s\n0\n0\n", mname);
555 >                fprintf(matfp, "5 %f %f %f %f %f\n", colval(radrgb,RED),
556                                  colval(radrgb,GRN), colval(radrgb,BLU),
557                                  c_cmaterial->rs, c_cmaterial->rs_a);
558                  return(mname);
559          }
560                                          /* else it's metal */
561 <        d = c_cmaterial->rd + c_cmaterial->rs;  /* average colors */
562 <        cvtcolor(radrgb, &c_cmaterial->rd_c, c_cmaterial->rd/d);
563 <        cvtcolor(c2, &c_cmaterial->rs_c, c_cmaterial->rs/d);
561 >                                                /* average colors */
562 >        cvtcolor(radrgb, &c_cmaterial->rd_c, c_cmaterial->rd);
563 >        cvtcolor(c2, &c_cmaterial->rs_c, c_cmaterial->rs);
564          addcolor(radrgb, c2);
565 <        if (c_cmaterial->rs < .999) {
566 <                d = c_cmaterial->rd/(1. - c_cmaterial->rs);
481 <                scalecolor(radrgb, d);
482 <        }
483 <        printf("\nvoid metal %s\n0\n0\n", mname);
484 <        printf("5 %f %f %f %f %f\n", colval(radrgb,RED),
565 >        fprintf(matfp, "\nvoid metal %s\n0\n0\n", mname);
566 >        fprintf(matfp, "5 %f %f %f %f %f\n", colval(radrgb,RED),
567                          colval(radrgb,GRN), colval(radrgb,BLU),
568 <                        c_cmaterial->rs, c_cmaterial->rs_a);
568 >                        c_cmaterial->rs/(c_cmaterial->rd + c_cmaterial->rs),
569 >                        c_cmaterial->rs_a);
570          return(mname);
571   }
572  
573  
574 < cvtcolor(radrgb, ciec, intensity)       /* convert a CIE color to Radiance */
574 > cvtcolor(radrgb, ciec, intensity)       /* convert a CIE XYZ color to RGB */
575   COLOR   radrgb;
576   register C_COLOR        *ciec;
577   double  intensity;
578   {
579          static COLOR    ciexyz;
580  
581 +        c_ccvt(ciec, C_CSXY);           /* get xy representation */
582          ciexyz[1] = intensity;
583          ciexyz[0] = ciec->cx/ciec->cy*ciexyz[1];
584          ciexyz[2] = ciexyz[1]*(1./ciec->cy - 1.) - ciexyz[0];
# Line 509 | Line 593 | object()                       /* return current object name */
593          register int    i;
594          register char   *cp;
595          int     len;
596 <
596 >                                                /* tracked by obj_handler */
597          i = obj_nnames - sizeof(objbuf)/16;
598          if (i < 0)
599                  i = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines