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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines