--- ray/src/rt/m_brdf.c 2004/03/30 16:13:01 2.21 +++ ray/src/rt/m_brdf.c 2010/10/25 22:57:45 2.27 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: m_brdf.c,v 2.21 2004/03/30 16:13:01 schorsch Exp $"; +static const char RCSid[] = "$Id: m_brdf.c,v 2.27 2010/10/25 22:57:45 greg Exp $"; #endif /* * Shading for materials with arbitrary BRDF's @@ -129,7 +129,7 @@ dirbrdf( /* compute source contribution */ addcolor(cval, ctmp); } if (ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY) - return; /* no specular component */ + return; /* diffuse only */ /* set up function */ setbrdfunc(np); sa = np->mp->oargs.sarg; @@ -145,7 +145,9 @@ dirbrdf( /* compute source contribution */ colval(ctmp,RED) = 0.0; else colval(ctmp,RED) = funvalue(sa[6], 4, lddx); - if (!strcmp(sa[7],sa[6])) + if (sa[7][0] == '0') + colval(ctmp,GRN) = 0.0; + else if (!strcmp(sa[7],sa[6])) colval(ctmp,GRN) = colval(ctmp,RED); else colval(ctmp,GRN) = funvalue(sa[7], 4, lddx); @@ -166,7 +168,7 @@ dirbrdf( /* compute source contribution */ dtmp = funvalue(sa[0], 5, vldx); setcolor(ctmp, dtmp, dtmp, dtmp); } - if (errno == EDOM || errno == ERANGE) { + if ((errno == EDOM) | (errno == ERANGE)) { objerror(np->mp, WARNING, "compute error"); return; } @@ -204,10 +206,13 @@ m_brdf( /* color a ray that hit a BRDTfunc material int hitfront = 1; BRDFDAT nd; RAY sr; + double mirtest=0, mirdist=0; double transtest, transdist; int hasrefl, hastrans; + int hastexture; COLOR ctmp; FVECT vtmp; + double d; register MFUNC *mf; register int i; /* check arguments */ @@ -233,7 +238,13 @@ m_brdf( /* color a ray that hit a BRDTfunc material m->oargs.farg[8]); /* get modifiers */ raytexture(r, m->omod); - nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ + hastexture = DOT(r->pert,r->pert) > FTINY*FTINY; + if (hastexture) { /* perturb normal */ + nd.pdot = raynormal(nd.pnorm, r); + } else { + VCOPY(nd.pnorm, r->ron); + nd.pdot = r->rod; + } if (r->rod < 0.0) { /* orient perturbed values */ nd.pdot = -nd.pdot; for (i = 0; i < 3; i++) { @@ -252,32 +263,30 @@ m_brdf( /* color a ray that hit a BRDTfunc material mf = getfunc(m, 9, 0x3f, 0); /* compute transmitted ray */ setbrdfunc(&nd); - transtest = 0; - transdist = r->rot; errno = 0; setcolor(ctmp, evalue(mf->ep[3]), evalue(mf->ep[4]), evalue(mf->ep[5])); - if (errno == EDOM || errno == ERANGE) + if ((errno == EDOM) | (errno == ERANGE)) objerror(m, WARNING, "compute error"); - else if (rayorigin(&sr, r, TRANS, bright(ctmp)) == 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]; + else if (rayorigin(&sr, TRANS, r, ctmp) == 0) { + if (!(r->crtype & SHADOW) && hastexture) { + /* perturb direction */ + VSUM(sr.rdir, r->rdir, r->pert, -.75); if (normalize(sr.rdir) == 0.0) { objerror(m, WARNING, "illegal perturbation"); VCOPY(sr.rdir, r->rdir); } } else { VCOPY(sr.rdir, r->rdir); - transtest = 2; } rayvalue(&sr); - multcolor(sr.rcol, ctmp); + multcolor(sr.rcol, sr.rcoef); addcolor(r->rcol, sr.rcol); - transtest *= bright(sr.rcol); - transdist = r->rot + sr.rt; + if (!hastexture) { + transtest = 2.0*bright(sr.rcol); + transdist = r->rot + sr.rt; + } } if (r->crtype & SHADOW) /* the rest is shadow */ return(1); @@ -287,21 +296,25 @@ m_brdf( /* color a ray that hit a BRDTfunc material setcolor(ctmp, evalue(mf->ep[0]), evalue(mf->ep[1]), evalue(mf->ep[2])); - if (errno == EDOM || errno == ERANGE) + 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]; + else if (rayorigin(&sr, REFLECTED, r, ctmp) == 0) { + VSUM(sr.rdir, r->rdir, nd.pnorm, 2.*nd.pdot); + checknorm(sr.rdir); rayvalue(&sr); - multcolor(sr.rcol, ctmp); + multcolor(sr.rcol, sr.rcoef); 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 (hasrefl) { if (!hitfront) flipsurface(r); - ambient(ctmp, r, nd.pnorm); - multcolor(ctmp, nd.rdiff); + copycolor(ctmp, nd.rdiff); + multambient(ctmp, r, nd.pnorm); addcolor(r->rcol, ctmp); /* add to returned color */ if (!hitfront) flipsurface(r); @@ -312,17 +325,20 @@ m_brdf( /* color a ray that hit a BRDTfunc material vtmp[0] = -nd.pnorm[0]; vtmp[1] = -nd.pnorm[1]; vtmp[2] = -nd.pnorm[2]; - ambient(ctmp, r, vtmp); - multcolor(ctmp, nd.tdiff); + copycolor(ctmp, nd.tdiff); + multambient(ctmp, r, vtmp); addcolor(r->rcol, ctmp); if (hitfront) flipsurface(r); } if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0') direct(r, dirbrdf, &nd); /* add direct component */ - /* check distance */ - if (transtest > bright(r->rcol)) + + d = bright(r->rcol); /* set effective distance */ + if (transtest > d) r->rt = transdist; + else if (mirtest > d) + r->rt = mirdist; return(1); } @@ -393,9 +409,9 @@ m_brdf2( /* color a ray that hit a BRDF material */ } /* compute ambient */ if (nd.trans < 1.0-FTINY) { - ambient(ctmp, r, nd.pnorm); + copycolor(ctmp, nd.mcolor); /* modified by material color */ scalecolor(ctmp, 1.0-nd.trans); - multcolor(ctmp, nd.mcolor); /* modified by material color */ + multambient(ctmp, r, nd.pnorm); addcolor(r->rcol, ctmp); /* add to returned color */ } if (nd.trans > FTINY) { /* from other side */ @@ -403,9 +419,9 @@ m_brdf2( /* color a ray that hit a BRDF material */ vtmp[0] = -nd.pnorm[0]; vtmp[1] = -nd.pnorm[1]; vtmp[2] = -nd.pnorm[2]; - ambient(ctmp, r, vtmp); + copycolor(ctmp, nd.mcolor); scalecolor(ctmp, nd.trans); - multcolor(ctmp, nd.mcolor); + multambient(ctmp, r, vtmp); addcolor(r->rcol, ctmp); flipsurface(r); }