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.34 by greg, Fri Jan 5 16:33:36 2024 UTC vs.
Revision 2.36 by greg, Fri May 23 17:02:07 2025 UTC

# Line 20 | Line 20 | static const char      RCSid[] = "$Id$";
20  
21   #define invert          (xf_context != NULL && xf_context->rev)
22  
23 + #define SGEN_DEF        "spec*"
24 + #define SGEN_RS         "rs_spec*"
25 + #define SGEN_TD         "td_spec*"
26 + #define SGEN_TS         "ts_spec*"
27 +
28 + char    void_str[] = "void";            /* global VOIDID */
29 + char    sgen_str[16] = SGEN_DEF;        /* generic specular */
30 +
31   double  glowdist = FHUGE;               /* glow test distance */
32  
33   double  emult = 1.;                     /* emitter multiplier */
34  
35   FILE    *matfp;                         /* material output file */
36  
37 + int     dospectra = 0;                  /* output spectral colors? */
38  
39 +
40   extern int r_comment(int ac, char **av);
41   extern int r_color(int ac, char **av);
42   extern int r_cone(int ac, char **av);
# Line 41 | Line 51 | extern char * object(void);
51   extern char * addarg(char *op, char *arg);
52   extern void do_tri(char *mat, C_VERTEX *cv1, C_VERTEX *cv2, C_VERTEX *cv3, int iv);
53   extern void cvtcolor(COLOR radrgb, C_COLOR *ciec, double intensity);
54 + extern int color_clash(int e1, int e2);
55 + extern int isgrey(COLOR rgb);
56 + extern void putrgbpat(char *pnm, COLOR rgb);
57   extern char * specolor(COLOR radrgb, C_COLOR *ciec, double intensity);
58  
59  
# Line 107 | Line 120 | main(
120                          }
121                          printf(" %s", argv[i]);
122                          break;
123 +                case 's':                       /* spectral color output? */
124 +                        dospectra = !dospectra;
125 +                        break;
126                  default:
127                          goto userr;
128                  }
# Line 132 | Line 148 | main(
148                  }
149          exit(0);
150   userr:
151 <        fprintf(stderr, "Usage: %s [-g dist][-e mult][-m matf] [file.mgf] ..\n",
151 >        fprintf(stderr, "Usage: %s [-s][-g dist][-e mult][-m matf] [file.mgf] ..\n",
152                          argv[0]);
153          exit(1);
154   }
# Line 515 | Line 531 | char *
531   material(void)                  /* get (and print) current material */
532   {
533          char    *mname = "mat";
534 +        C_COLOR *refclr = NULL;
535          char    *pname;
536          COLOR   radrgb, c2;
537          double  d;
# Line 572 | Line 589 | material(void)                 /* get (and print) current material *
589                                  c_cmaterial->nr);
590                  return(mname);
591          }
592 +                                        /* check for WGMDfunc */
593 +        if (((c_cmaterial->rs > .02) & (c_cmaterial->ts > .02) &&
594 +                        fabs(c_cmaterial->rs_a - c_cmaterial->ts_a) > .02) ||
595 +                        ((c_cmaterial->rs > .05) & (c_cmaterial->rd > .05) &&
596 +                                !c_isgrey(&c_cmaterial->rs_c) &&
597 +                                !c_equiv(&c_cmaterial->rd_c, &c_cmaterial->rs_c)) ||
598 +                        color_clash(MG_E_TS, MG_E_TD) ||
599 +                        color_clash(MG_E_TD, MG_E_RD) ||
600 +                        color_clash(MG_E_RD, MG_E_TS)) {
601 +                COLOR   rs_rgb, ts_rgb, td_rgb; /* separate modifier paths */
602 +                char    rs_pname[128], ts_pname[128], td_pname[128];
603 +                strcpy(sgen_str, SGEN_RS);
604 +                strcpy(rs_pname, specolor(rs_rgb, &c_cmaterial->rs_c, c_cmaterial->rs));
605 +                strcpy(sgen_str, SGEN_TS);
606 +                strcpy(ts_pname, specolor(ts_rgb, &c_cmaterial->ts_c, c_cmaterial->ts));
607 +                strcpy(sgen_str, SGEN_TD);
608 +                strcpy(td_pname, specolor(td_rgb, &c_cmaterial->td_c, c_cmaterial->td));
609 +                strcpy(sgen_str, SGEN_DEF);
610 +                pname = specolor(radrgb, &c_cmaterial->rd_c, c_cmaterial->rd);
611 +                if (!strcmp(rs_pname, void_str) && !isgrey(rs_rgb)) {
612 +                        putrgbpat(strcpy(rs_pname,"rs_rgb*"), rs_rgb);
613 +                        colval(rs_rgb,GRN) = 1;
614 +                }
615 +                if (!strcmp(ts_pname, void_str) && !isgrey(ts_rgb)) {
616 +                        putrgbpat(strcpy(ts_pname,"ts_rgb*"), ts_rgb);
617 +                        colval(ts_rgb,GRN) = 1;
618 +                }
619 +                fprintf(matfp, "\n%s WGMDfunc %s\n", pname, mname);
620 +                fprintf(matfp, "13\t%s %f %f %f\n", rs_pname, colval(rs_rgb,GRN),
621 +                                c_cmaterial->rs_a, c_cmaterial->rs_a);
622 +                fprintf(matfp, "\t%s %f %f %f\n", ts_pname, colval(ts_rgb,GRN),
623 +                                c_cmaterial->ts_a, c_cmaterial->ts_a);
624 +                fprintf(matfp, "\t%s\n\t0 0 1 .\n0\n", td_pname);
625 +                fprintf(matfp, "9\t%f %f %f\n", colval(radrgb,RED),
626 +                                colval(radrgb,GRN), colval(radrgb,BLU));
627 +                fprintf(matfp, "\t%f %f %f\n", colval(radrgb,RED),
628 +                                colval(radrgb,GRN), colval(radrgb,BLU));
629 +                fprintf(matfp, "\t%f %f %f\n", colval(td_rgb,RED),
630 +                                colval(td_rgb,GRN), colval(td_rgb,BLU));
631 +                if (c_cmaterial->sided)
632 +                        putsided(mname);
633 +                return(mname);
634 +        }
635                                          /* check for trans */
636 <        if (c_cmaterial->td > .01 || c_cmaterial->ts > .01) {
636 >        if (c_cmaterial->td + c_cmaterial->ts > .01) {
637                  double  a5, a6;
638                                                  /* average colors */
639                  d = c_cmaterial->rd + c_cmaterial->td + c_cmaterial->ts;
# Line 649 | Line 709 | cvtcolor(      /* convert a CIE XYZ color to RGB */
709   {
710          COLOR   ciexyz;
711  
712 +        if (intensity <= 0) {
713 +                setcolor(radrgb, 0, 0, 0);
714 +                return;
715 +        }
716          c_ccvt(ciec, C_CSXY);           /* get xy representation */
717          ciexyz[1] = intensity;
718          ciexyz[0] = ciec->cx/ciec->cy*ciexyz[1];
# Line 657 | Line 721 | cvtcolor(      /* convert a CIE XYZ color to RGB */
721   }
722  
723  
724 + int color_clash(        /* do non-zero material components clash? */
725 +        int e1,
726 +        int e2
727 + )
728 + {
729 +        C_COLOR *c1;
730 +
731 +        if (e1 == e2)
732 +                return(0);
733 +        switch (e1) {
734 +        case MG_E_RD:
735 +                if (c_cmaterial->rd <= .01) return(0);
736 +                c1 = &c_cmaterial->rd_c;
737 +                break;
738 +        case MG_E_RS:
739 +                if (c_cmaterial->rs <= .01) return(0);
740 +                c1 = &c_cmaterial->rs_c;
741 +                break;
742 +        case MG_E_TD:
743 +                if (c_cmaterial->td <= .01) return(0);
744 +                c1 = &c_cmaterial->td_c;
745 +                break;
746 +        case MG_E_TS:
747 +                if (c_cmaterial->ts <= .01) return(0);
748 +                c1 = &c_cmaterial->ts_c;
749 +                break;
750 +        default:
751 +                return(0);
752 +        }
753 +        switch (e2) {
754 +        case MG_E_RD:
755 +                if (c_cmaterial->rd <= .01) return(0);
756 +                return(!c_equiv(c1, &c_cmaterial->rd_c));
757 +        case MG_E_RS:
758 +                if (c_cmaterial->rs <= .01) return(0);
759 +                return(!c_equiv(c1, &c_cmaterial->rs_c));
760 +        case MG_E_TD:
761 +                if (c_cmaterial->td <= .01) return(0);
762 +                return(!c_equiv(c1, &c_cmaterial->td_c));
763 +        case MG_E_TS:
764 +                if (c_cmaterial->ts <= .01) return(0);
765 +                return(!c_equiv(c1, &c_cmaterial->ts_c));
766 +        }
767 +        return(0);
768 + }
769 +
770 +
771   static int      /* new spectrum definition? */
772   newspecdef(C_COLOR *spc)
773   {
# Line 688 | Line 799 | specolor(      /* check if color has spectra and output acc
799          double  mult;
800          int     cbeg, cend, i;
801  
802 <        if (!(clr->flags & C_CDSPEC)) {         /* not defined spectrally? */
802 >        if (!dospectra | !(clr->flags & C_CDSPEC) | (intensity <= FTINY)) {
803                  cvtcolor(radrgb, clr, intensity);
804 <                return("void");
804 >                return(void_str);               /* just use RGB */
805          }
806          setcolor(radrgb, intensity, intensity, intensity);
807          for (cbeg = 0; cbeg < C_CNSS; cbeg++)   /* trim zeros off beginning */
808                  if (clr->ssamp[cbeg])
809                          break;
810          if (cbeg >= C_CNSS)                     /* should never happen! */
811 <                return("void");
811 >                return(void_str);
812          if (clr->client_data != NULL) {         /* get name if available */
813                  strcpy(spname, (char *)clr->client_data);
814                  strcat(spname, "*");            /* make sure it's special */
815                  if (!newspecdef(clr))           /* output already? */
816                          return(spname);
817          } else
818 <                strcpy(spname, "spec*");
818 >                strcpy(spname, sgen_str);
819          c_ccvt(clr, C_CSEFF);                   /* else output spectrum prim */
820          for (cend = 0; !clr->ssamp[C_CNSS-1-cend]; cend++)
821                  ;                               /* trim zeros off end */
# Line 718 | Line 829 | specolor(      /* check if color has spectra and output acc
829          }
830          fputc('\n', matfp);
831          return(spname);
832 + }
833 +
834 +
835 + int
836 + isgrey(                         /* is RGB close match to grey? */
837 +        COLOR rgb
838 + )
839 + {
840 +        const double    yv = bright(rgb);
841 +        double          diff2 = 0;
842 +        int             i;
843 +
844 +        for (i = 3; i--; ) {
845 +                double  d = yv - colval(rgb,i);
846 +                diff2 += d*d;
847 +        }
848 +        return(diff2 <= yv*yv*0.0025);
849 + }
850 +
851 +
852 + void
853 + putrgbpat(                      /* put out RGB pattern with given name */
854 +        char *pnm,
855 +        COLOR rgb
856 + )
857 + {
858 +        fprintf(matfp, "\nvoid colorfunc %s\n", pnm);
859 +        fprintf(matfp, "4 %f %f %f .\n0\n0\n", colval(rgb,RED),
860 +                        colval(rgb,GRN), colval(rgb,BLU));
861   }
862  
863  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines