--- ray/src/rt/m_brdf.c 1995/05/17 17:31:39 2.12 +++ ray/src/rt/m_brdf.c 2003/03/05 16:16:53 2.18 @@ -1,13 +1,12 @@ -/* 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.18 2003/03/05 16:16:53 greg Exp $"; #endif - /* * Shading for materials with arbitrary BRDF's */ +#include "copyright.h" + #include "ray.h" #include "data.h" @@ -16,8 +15,6 @@ static char SCCSid[] = "$SunId$ LBL"; #include "func.h" -extern int backvis; /* back faces visible? */ - /* * Arguments to this material include the color and specularity. * String arguments include the reflection function and files. @@ -80,6 +77,7 @@ typedef struct { } BRDFDAT; /* BRDF material data */ +static void dirbrdf(cval, np, ldir, omega) /* compute source contribution */ COLOR cval; /* returned coefficient */ register BRDFDAT *np; /* material data */ @@ -162,7 +160,7 @@ double omega; /* light source size */ dtmp = funvalue(sa[0], 5, vldx); setcolor(ctmp, dtmp, dtmp, dtmp); } - if (errno) { + if (errno == EDOM || errno == ERANGE) { objerror(np->mp, WARNING, "compute error"); return; } @@ -191,15 +189,18 @@ double omega; /* light source size */ } -m_brdf(m, r) /* color a ray which hit a BRDTF material */ +int +m_brdf(m, r) /* color a ray that hit a BRDTfunc material */ register OBJREC *m; register RAY *r; { + int hitfront = 1; BRDFDAT nd; RAY sr; double transtest, transdist; int hasrefl, hastrans; COLOR ctmp; + FVECT vtmp; register MFUNC *mf; register int i; /* check arguments */ @@ -223,7 +224,7 @@ register RAY *r; setcolor(nd.tdiff, m->oargs.farg[6], m->oargs.farg[7], m->oargs.farg[8]); - /* get modifiers */ + /* get modifiers */ raytexture(r, m->omod); nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ if (r->rod < 0.0) { /* orient perturbed values */ @@ -232,6 +233,7 @@ register RAY *r; 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 */ @@ -249,7 +251,7 @@ register RAY *r; setcolor(ctmp, evalue(mf->ep[3]), evalue(mf->ep[4]), evalue(mf->ep[5])); - if (errno) + if (errno == EDOM || errno == ERANGE) objerror(m, WARNING, "compute error"); else if (rayorigin(&sr, r, TRANS, bright(ctmp)) == 0) { if (!(r->crtype & SHADOW) && @@ -278,7 +280,7 @@ register RAY *r; setcolor(ctmp, evalue(mf->ep[0]), evalue(mf->ep[1]), evalue(mf->ep[2])); - if (errno) + 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++) @@ -289,21 +291,24 @@ register RAY *r; } /* compute ambient */ if (hasrefl) { - if (nd.pdot < 0.0) + if (!hitfront) flipsurface(r); - ambient(ctmp, r); + ambient(ctmp, r, nd.pnorm); multcolor(ctmp, nd.rdiff); addcolor(r->rcol, ctmp); /* add to returned color */ - if (nd.pdot < 0.0) + if (!hitfront) flipsurface(r); } if (hastrans) { /* from other side */ - if (nd.pdot > 0.0) + if (hitfront) flipsurface(r); - ambient(ctmp, 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 (nd.pdot > 0.0) + if (hitfront) flipsurface(r); } if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0') @@ -317,12 +322,14 @@ register RAY *r; -m_brdf2(m, r) /* color a ray which hit a BRDF material */ +int +m_brdf2(m, r) /* 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) @@ -331,6 +338,18 @@ register RAY *r; 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 */ @@ -352,17 +371,6 @@ register RAY *r; /* compute reflectance */ dtmp = 1.0 - nd.trans - nd.rspec; setcolor(nd.rdiff, dtmp, dtmp, dtmp); - /* check for back side */ - if (r->rod < 0.0) { - if (!backvis && m->otype != MAT_TFUNC - && m->otype != MAT_TDATA) { - raytrans(r); - return(1); - } - flipsurface(r); /* reorient if backvis */ - } - /* get modifiers */ - raytexture(r, m->omod); nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ multcolor(nd.mcolor, r->pcol); /* modify material color */ multcolor(nd.rdiff, nd.mcolor); @@ -377,14 +385,17 @@ register RAY *r; } /* compute ambient */ if (nd.trans < 1.0-FTINY) { - ambient(ctmp, r); + 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 (nd.trans > FTINY) { /* from other side */ flipsurface(r); - ambient(ctmp, r); + 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); @@ -397,6 +408,7 @@ register RAY *r; } +int setbrdfunc(np) /* set up brdf function and variables */ register BRDFDAT *np; {