--- ray/src/rt/m_brdf.c 1991/08/12 08:20:53 1.16 +++ ray/src/rt/m_brdf.c 2004/09/09 15:40:02 2.22 @@ -1,25 +1,27 @@ -/* Copyright (c) 1991 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: m_brdf.c,v 2.22 2004/09/09 15:40:02 greg Exp $"; #endif - /* * Shading for materials with arbitrary BRDF's */ -#include "ray.h" +#include "copyright.h" +#include "ray.h" +#include "ambient.h" #include "data.h" - +#include "source.h" #include "otypes.h" +#include "rtotypes.h" +#include "func.h" /* * Arguments to this material include the color and specularity. * String arguments include the reflection function and files. * The BRDF is currently used just for the specular component to light * sources. Reflectance values or data coordinates are functions - * of the direction to the light source. + * of the direction to the light source. (Data modification functions + * are passed the source direction as args 2-4.) * We orient the surface towards the incoming ray, so a single * surface can be used to represent an infinitely thin object. * @@ -49,46 +51,53 @@ static char SCCSid[] = "$SunId$ LBL"; * rbrtd gbrtd bbrtd * funcfile transform * 0 - * 6+ red grn blu rspec trans tspec A7 .. + * 9+ rdf gdf bdf + * rdb gdb bdb + * rdt gdt bdt A10 .. * * In addition to the normal variables available to functions, * we define the following: * NxP, NyP, NzP - perturbed surface normal * RdotP - perturbed ray dot product - * CrP, CgP, CbP - perturbed material color + * CrP, CgP, CbP - perturbed material color (or pattern) */ -extern double funvalue(), varvalue(); -extern XF funcxf; - typedef struct { OBJREC *mp; /* material pointer */ RAY *pr; /* intersected ray */ DATARRAY *dp; /* data array for PDATA, MDATA or TDATA */ - COLOR mcolor; /* color of this material */ - double rspec; /* specular reflection */ - double rdiff; /* diffuse reflection */ - double trans; /* transmissivity */ - double tspec; /* specular transmission */ - double tdiff; /* diffuse transmission */ + COLOR mcolor; /* material (or pattern) color */ + COLOR rdiff; /* diffuse reflection */ + COLOR tdiff; /* diffuse transmission */ + double rspec; /* specular reflectance (1 for BRDTF) */ + double trans; /* transmissivity (.5 for BRDTF) */ + double tspec; /* specular transmittance (1 for BRDTF) */ FVECT pnorm; /* perturbed surface normal */ double pdot; /* perturbed dot product */ } BRDFDAT; /* BRDF material data */ -dirbrdf(cval, np, ldir, omega) /* compute source contribution */ -COLOR cval; /* returned coefficient */ -register BRDFDAT *np; /* material data */ -FVECT ldir; /* light source direction */ -double omega; /* light source size */ +static srcdirf_t dirbrdf; +static int setbrdfunc(BRDFDAT *np); + + +static void +dirbrdf( /* compute source contribution */ + COLOR cval, /* returned coefficient */ + void *nnp, /* material data */ + FVECT ldir, /* light source direction */ + double omega /* light source size */ +) { + register BRDFDAT *np = nnp; double ldot; double dtmp; COLOR ctmp; FVECT ldx; - double pt[MAXDIM]; + static double vldx[5], pt[MAXDIM]; register char **sa; register int i; +#define lddx (vldx+1) setcolor(cval, 0.0, 0.0, 0.0); @@ -96,26 +105,26 @@ double omega; /* light source size */ if (ldot <= FTINY && ldot >= -FTINY) return; /* too close to grazing */ + if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) return; /* wrong side */ - if (ldot > 0.0 && np->rdiff > FTINY) { + if (ldot > 0.0) { /* * Compute and add diffuse reflected component to returned * color. The diffuse reflected component will always be * modified by the color of the material. */ - copycolor(ctmp, np->mcolor); - dtmp = ldot * omega * np->rdiff / PI; + copycolor(ctmp, np->rdiff); + dtmp = ldot * omega / PI; scalecolor(ctmp, dtmp); addcolor(cval, ctmp); - } - if (ldot < 0.0 && np->tdiff > FTINY) { + } else { /* * Diffuse transmitted component. */ - copycolor(ctmp, np->mcolor); - dtmp = -ldot * omega * np->tdiff / PI; + copycolor(ctmp, np->tdiff); + dtmp = -ldot * omega / PI; scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } @@ -128,40 +137,46 @@ double omega; /* light source size */ /* transform light vector */ multv3(ldx, ldir, funcxf.xfm); for (i = 0; i < 3; i++) - ldx[i] /= funcxf.sca; + lddx[i] = ldx[i]/funcxf.sca; + lddx[3] = omega; /* compute BRTDF */ if (np->mp->otype == MAT_BRTDF) { - colval(ctmp,RED) = funvalue(sa[6], 3, ldx); + if (sa[6][0] == '0') /* special case */ + colval(ctmp,RED) = 0.0; + else + colval(ctmp,RED) = funvalue(sa[6], 4, lddx); if (!strcmp(sa[7],sa[6])) colval(ctmp,GRN) = colval(ctmp,RED); else - colval(ctmp,GRN) = funvalue(sa[7], 3, ldx); + colval(ctmp,GRN) = funvalue(sa[7], 4, lddx); if (!strcmp(sa[8],sa[6])) colval(ctmp,BLU) = colval(ctmp,RED); else if (!strcmp(sa[8],sa[7])) colval(ctmp,BLU) = colval(ctmp,GRN); else - colval(ctmp,BLU) = funvalue(sa[8], 3, ldx); + colval(ctmp,BLU) = funvalue(sa[8], 4, lddx); dtmp = bright(ctmp); } else if (np->dp == NULL) { - dtmp = funvalue(sa[0], 3, ldx); + dtmp = funvalue(sa[0], 4, lddx); setcolor(ctmp, dtmp, dtmp, dtmp); } else { for (i = 0; i < np->dp->nd; i++) - pt[i] = funvalue(sa[3+i], 3, ldx); - dtmp = datavalue(np->dp, pt); - dtmp = funvalue(sa[0], 1, &dtmp); + pt[i] = funvalue(sa[3+i], 4, lddx); + vldx[0] = datavalue(np->dp, pt); + dtmp = funvalue(sa[0], 5, vldx); setcolor(ctmp, dtmp, dtmp, dtmp); } - if (errno) - goto computerr; + if (errno == EDOM || errno == ERANGE) { + objerror(np->mp, WARNING, "compute error"); + return; + } if (dtmp <= FTINY) return; if (ldot > 0.0) { /* * Compute reflected non-diffuse component. */ - if (np->mp->otype == MAT_MFUNC || np->mp->otype == MAT_MDATA) + if ((np->mp->otype == MAT_MFUNC) | (np->mp->otype == MAT_MDATA)) multcolor(ctmp, np->mcolor); dtmp = ldot * omega * np->rspec; scalecolor(ctmp, dtmp); @@ -170,169 +185,255 @@ double omega; /* light source size */ /* * Compute transmitted non-diffuse component. */ - if (np->mp->otype == MAT_TFUNC || np->mp->otype == MAT_TDATA) + if ((np->mp->otype == MAT_TFUNC) | (np->mp->otype == MAT_TDATA)) multcolor(ctmp, np->mcolor); dtmp = -ldot * omega * np->tspec; scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } - return; -computerr: - objerror(np->mp, WARNING, "compute error"); - return; +#undef lddx } -m_brdf(m, r) /* color a ray which hit a BRDF material */ -register OBJREC *m; -register RAY *r; +extern int +m_brdf( /* color a ray that hit a BRDTfunc material */ + register OBJREC *m, + register RAY *r +) { - int minsa, minfa; + int hitfront = 1; BRDFDAT nd; + RAY sr; + double mirtest=0, mirdist=0; double transtest, transdist; + int hasrefl, hastrans; + int hastexture; COLOR ctmp; - double dtmp, tspect, rspecr; + FVECT vtmp; + double d; + register MFUNC *mf; register int i; /* check arguments */ - switch (m->otype) { - case MAT_PFUNC: case MAT_MFUNC: - minsa = 2; minfa = 4; break; - case MAT_PDATA: case MAT_MDATA: - minsa = 4; minfa = 4; break; - case MAT_TFUNC: - minsa = 2; minfa = 6; break; - case MAT_TDATA: - minsa = 4; minfa = 6; break; - case MAT_BRTDF: - minsa = 10; minfa = 6; break; - } - if (m->oargs.nsargs < minsa || m->oargs.nfargs < minfa) + if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9)) objerror(m, USER, "bad # arguments"); nd.mp = m; nd.pr = r; - /* get specular component */ - nd.rspec = m->oargs.farg[3]; - /* compute transmission */ - if (m->otype == MAT_TFUNC || m->otype == MAT_TDATA - || m->otype == MAT_BRTDF) { - nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec); - nd.tspec = nd.trans * m->oargs.farg[5]; - nd.tdiff = nd.trans - nd.tspec; - } else - nd.tdiff = nd.tspec = nd.trans = 0.0; - /* early shadow check */ - if (r->crtype & SHADOW && (m->otype != MAT_BRTDF || nd.tspec <= FTINY)) - return; - /* diffuse reflection */ - nd.rdiff = 1.0 - nd.trans - nd.rspec; - /* get material color */ - setcolor(nd.mcolor, m->oargs.farg[0], - m->oargs.farg[1], - m->oargs.farg[2]); - /* fix orientation */ - if (r->rod < 0.0) - flipsurface(r); + /* dummy values */ + nd.rspec = nd.tspec = 1.0; + nd.trans = 0.5; + /* diffuse reflectance */ + if (r->rod > 0.0) + setcolor(nd.rdiff, m->oargs.farg[0], + m->oargs.farg[1], + m->oargs.farg[2]); + else + setcolor(nd.rdiff, m->oargs.farg[3], + m->oargs.farg[4], + m->oargs.farg[5]); + /* diffuse transmittance */ + setcolor(nd.tdiff, m->oargs.farg[6], + m->oargs.farg[7], + m->oargs.farg[8]); /* get modifiers */ raytexture(r, m->omod); - nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ - multcolor(nd.mcolor, r->pcol); /* modify material color */ - transtest = 0; - /* load auxiliary files */ - if (m->otype == MAT_PDATA || m->otype == MAT_MDATA - || m->otype == MAT_TDATA) { - nd.dp = getdata(m->oargs.sarg[1]); - for (i = 3; i < m->oargs.nsargs; i++) - if (m->oargs.sarg[i][0] == '-') - break; - if (i-3 != nd.dp->nd) - objerror(m, USER, "dimension error"); - funcfile(m->oargs.sarg[2]); - } else if (m->otype == MAT_BRTDF) { - nd.dp = NULL; - funcfile(m->oargs.sarg[9]); + hastexture = DOT(r->pert,r->pert) > FTINY*FTINY; + if (hastexture) { /* perturb normal */ + nd.pdot = raynormal(nd.pnorm, r); } else { - nd.dp = NULL; - funcfile(m->oargs.sarg[1]); + VCOPY(nd.pnorm, r->ron); + nd.pdot = r->rod; } - /* set special variables */ - setbrdfunc(&nd); + if (r->rod < 0.0) { /* orient perturbed values */ + nd.pdot = -nd.pdot; + for (i = 0; i < 3; i++) { + nd.pnorm[i] = -nd.pnorm[i]; + r->pert[i] = -r->pert[i]; + } + hitfront = 0; + } + copycolor(nd.mcolor, r->pcol); /* get pattern color */ + multcolor(nd.rdiff, nd.mcolor); /* modify diffuse values */ + multcolor(nd.tdiff, nd.mcolor); + hasrefl = bright(nd.rdiff) > FTINY; + hastrans = bright(nd.tdiff) > FTINY; + /* load cal file */ + nd.dp = NULL; + mf = getfunc(m, 9, 0x3f, 0); /* compute transmitted ray */ - tspect = 0.; - if (m->otype == MAT_BRTDF && nd.tspec > FTINY) { - RAY sr; - errno = 0; - setcolor(ctmp, varvalue(m->oargs.sarg[3]), - varvalue(m->oargs.sarg[4]), - varvalue(m->oargs.sarg[5])); - scalecolor(ctmp, nd.trans); - if (errno) - objerror(m, WARNING, "compute error"); - else if ((tspect = bright(ctmp)) > FTINY && - rayorigin(&sr, r, TRANS, tspect) == 0) { - if (!(r->crtype & SHADOW) && - DOT(r->pert,r->pert) > FTINY*FTINY) { - for (i = 0; i < 3; i++) /* perturb direction */ - sr.rdir[i] = r->rdir[i] - - .75*r->pert[i]; - normalize(sr.rdir); - } else { + setbrdfunc(&nd); + errno = 0; + setcolor(ctmp, evalue(mf->ep[3]), + evalue(mf->ep[4]), + evalue(mf->ep[5])); + if (errno == EDOM || errno == ERANGE) + objerror(m, WARNING, "compute error"); + else if (rayorigin(&sr, r, TRANS, bright(ctmp)) == 0) { + if (!(r->crtype & SHADOW) && hastexture) { + for (i = 0; i < 3; i++) /* perturb direction */ + sr.rdir[i] = r->rdir[i] - .75*r->pert[i]; + if (normalize(sr.rdir) == 0.0) { + objerror(m, WARNING, "illegal perturbation"); VCOPY(sr.rdir, r->rdir); - transtest = 2; } - rayvalue(&sr); - multcolor(sr.rcol, ctmp); - addcolor(r->rcol, sr.rcol); - transtest *= bright(sr.rcol); + } else { + VCOPY(sr.rdir, r->rdir); + } + rayvalue(&sr); + multcolor(sr.rcol, ctmp); + addcolor(r->rcol, sr.rcol); + if (!hastexture) { + transtest = 2.0*bright(sr.rcol); transdist = r->rot + sr.rt; } } if (r->crtype & SHADOW) /* the rest is shadow */ - return; + return(1); /* compute reflected ray */ - rspecr = 0.; - if (m->otype == MAT_BRTDF && nd.rspec > FTINY) { - RAY sr; - errno = 0; - setcolor(ctmp, varvalue(m->oargs.sarg[0]), - varvalue(m->oargs.sarg[1]), - varvalue(m->oargs.sarg[2])); - if (errno) - objerror(m, WARNING, "compute error"); - else if ((rspecr = bright(ctmp)) > FTINY && - rayorigin(&sr, r, REFLECTED, rspecr) == 0) { - for (i = 0; i < 3; i++) - sr.rdir[i] = r->rdir[i] + - 2.0*nd.pdot*nd.pnorm[i]; - rayvalue(&sr); - multcolor(sr.rcol, ctmp); - addcolor(r->rcol, sr.rcol); + setbrdfunc(&nd); + errno = 0; + setcolor(ctmp, evalue(mf->ep[0]), + evalue(mf->ep[1]), + evalue(mf->ep[2])); + if (errno == EDOM || errno == ERANGE) + objerror(m, WARNING, "compute error"); + else if (rayorigin(&sr, r, REFLECTED, bright(ctmp)) == 0) { + for (i = 0; i < 3; i++) + sr.rdir[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; + rayvalue(&sr); + multcolor(sr.rcol, ctmp); + addcolor(r->rcol, sr.rcol); + if (!hastexture && r->ro != NULL && isflat(r->ro->otype)) { + mirtest = 2.0*bright(sr.rcol); + mirdist = r->rot + sr.rt; } } /* compute ambient */ - if ((dtmp = 1.0-nd.trans-rspecr) > FTINY) { - ambient(ctmp, r); - scalecolor(ctmp, dtmp); + if (hasrefl) { + if (!hitfront) + flipsurface(r); + ambient(ctmp, r, nd.pnorm); + multcolor(ctmp, nd.rdiff); + addcolor(r->rcol, ctmp); /* add to returned color */ + if (!hitfront) + flipsurface(r); + } + if (hastrans) { /* from other side */ + if (hitfront) + flipsurface(r); + vtmp[0] = -nd.pnorm[0]; + vtmp[1] = -nd.pnorm[1]; + vtmp[2] = -nd.pnorm[2]; + ambient(ctmp, r, vtmp); + multcolor(ctmp, nd.tdiff); + addcolor(r->rcol, ctmp); + if (hitfront) + flipsurface(r); + } + if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0') + direct(r, dirbrdf, &nd); /* add direct component */ + + d = bright(r->rcol); /* set effective distance */ + if (transtest > d) + r->rt = transdist; + else if (mirtest > d) + r->rt = mirdist; + + return(1); +} + + + +extern int +m_brdf2( /* color a ray that hit a BRDF material */ + register OBJREC *m, + register RAY *r +) +{ + BRDFDAT nd; + COLOR ctmp; + FVECT vtmp; + double dtmp; + /* always a shadow */ + if (r->crtype & SHADOW) + return(1); + /* check arguments */ + if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs < + ((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4))) + objerror(m, USER, "bad # arguments"); + /* check for back side */ + if (r->rod < 0.0) { + if (!backvis && m->otype != MAT_TFUNC + && m->otype != MAT_TDATA) { + raytrans(r); + return(1); + } + raytexture(r, m->omod); + flipsurface(r); /* reorient if backvis */ + } else + raytexture(r, m->omod); + + nd.mp = m; + nd.pr = r; + /* get material color */ + setcolor(nd.mcolor, m->oargs.farg[0], + m->oargs.farg[1], + m->oargs.farg[2]); + /* get specular component */ + nd.rspec = m->oargs.farg[3]; + /* compute transmittance */ + if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) { + nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec); + nd.tspec = nd.trans * m->oargs.farg[5]; + dtmp = nd.trans - nd.tspec; + setcolor(nd.tdiff, dtmp, dtmp, dtmp); + } else { + nd.tspec = nd.trans = 0.0; + setcolor(nd.tdiff, 0.0, 0.0, 0.0); + } + /* compute reflectance */ + dtmp = 1.0 - nd.trans - nd.rspec; + setcolor(nd.rdiff, dtmp, dtmp, dtmp); + nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ + multcolor(nd.mcolor, r->pcol); /* modify material color */ + multcolor(nd.rdiff, nd.mcolor); + multcolor(nd.tdiff, nd.mcolor); + /* load auxiliary files */ + if (hasdata(m->otype)) { + nd.dp = getdata(m->oargs.sarg[1]); + getfunc(m, 2, 0, 0); + } else { + nd.dp = NULL; + getfunc(m, 1, 0, 0); + } + /* compute ambient */ + if (nd.trans < 1.0-FTINY) { + ambient(ctmp, r, nd.pnorm); + scalecolor(ctmp, 1.0-nd.trans); multcolor(ctmp, nd.mcolor); /* modified by material color */ addcolor(r->rcol, ctmp); /* add to returned color */ } - if ((dtmp = nd.trans-tspect) > FTINY) { /* from other side */ + if (nd.trans > FTINY) { /* from other side */ flipsurface(r); - ambient(ctmp, r); - scalecolor(ctmp, dtmp); + vtmp[0] = -nd.pnorm[0]; + vtmp[1] = -nd.pnorm[1]; + vtmp[2] = -nd.pnorm[2]; + ambient(ctmp, r, vtmp); + scalecolor(ctmp, nd.trans); multcolor(ctmp, nd.mcolor); addcolor(r->rcol, ctmp); flipsurface(r); } /* add direct component */ direct(r, dirbrdf, &nd); - /* check distance */ - if (transtest > bright(r->rcol)) - r->rt = transdist; + + return(1); } -setbrdfunc(np) /* set up brdf function and variables */ -register BRDFDAT *np; +static int +setbrdfunc( /* set up brdf function and variables */ + register BRDFDAT *np +) { FVECT vec;