| 1 |
greg |
1.1 |
/* Copyright (c) 1990 Regents of the University of California */
|
| 2 |
|
|
|
| 3 |
|
|
#ifndef lint
|
| 4 |
|
|
static char SCCSid[] = "$SunId$ LBL";
|
| 5 |
|
|
#endif
|
| 6 |
|
|
|
| 7 |
|
|
/*
|
| 8 |
|
|
* Shading for materials with arbitrary BRDF's
|
| 9 |
|
|
*/
|
| 10 |
|
|
|
| 11 |
|
|
#include "ray.h"
|
| 12 |
|
|
|
| 13 |
|
|
#include "data.h"
|
| 14 |
|
|
|
| 15 |
|
|
#include "otypes.h"
|
| 16 |
|
|
|
| 17 |
|
|
/*
|
| 18 |
|
|
* Arguments to this material include the color and specularity.
|
| 19 |
|
|
* String arguments include the reflection function and files.
|
| 20 |
|
|
* The BRDF is currently used just for the specular component to light
|
| 21 |
|
|
* sources. Reflectance values or data coordinates are functions
|
| 22 |
|
|
* of the direction to the light source.
|
| 23 |
|
|
* We orient the surface towards the incoming ray, so a single
|
| 24 |
|
|
* surface can be used to represent an infinitely thin object.
|
| 25 |
|
|
*
|
| 26 |
|
|
* Arguments for MAT_PFUNC and MAT_MFUNC are:
|
| 27 |
|
|
* 2+ func funcfile transform ..
|
| 28 |
|
|
* 0
|
| 29 |
|
|
* 4+ red grn blu specularity args ..
|
| 30 |
|
|
*
|
| 31 |
|
|
* Arguments for MAT_PDATA and MAT_MDATA are:
|
| 32 |
|
|
* 4+ func datafile funcfile v0 .. transform ..
|
| 33 |
|
|
* 0
|
| 34 |
|
|
* 4+ red grn blu specularity args ..
|
| 35 |
|
|
*/
|
| 36 |
|
|
|
| 37 |
greg |
1.2 |
extern double funvalue(), varvalue();
|
| 38 |
|
|
|
| 39 |
greg |
1.1 |
#define BSPEC(m) (6.0) /* specular parameter b */
|
| 40 |
|
|
|
| 41 |
|
|
typedef struct {
|
| 42 |
|
|
OBJREC *mp; /* material pointer */
|
| 43 |
|
|
RAY *pr; /* intersected ray */
|
| 44 |
|
|
DATARRAY *dp; /* data array for PDATA or MDATA */
|
| 45 |
|
|
COLOR mcolor; /* color of this material */
|
| 46 |
|
|
COLOR scolor; /* color of specular component */
|
| 47 |
|
|
double rspec; /* specular reflection */
|
| 48 |
|
|
double rdiff; /* diffuse reflection */
|
| 49 |
|
|
FVECT pnorm; /* perturbed surface normal */
|
| 50 |
|
|
double pdot; /* perturbed dot product */
|
| 51 |
|
|
} BRDFDAT; /* BRDF material data */
|
| 52 |
|
|
|
| 53 |
|
|
|
| 54 |
|
|
dirbrdf(cval, np, ldir, omega) /* compute source contribution */
|
| 55 |
|
|
COLOR cval; /* returned coefficient */
|
| 56 |
|
|
register BRDFDAT *np; /* material data */
|
| 57 |
|
|
FVECT ldir; /* light source direction */
|
| 58 |
|
|
double omega; /* light source size */
|
| 59 |
|
|
{
|
| 60 |
|
|
double ldot;
|
| 61 |
|
|
double dtmp;
|
| 62 |
|
|
COLOR ctmp;
|
| 63 |
|
|
double pt[MAXDIM];
|
| 64 |
|
|
register int i;
|
| 65 |
|
|
|
| 66 |
|
|
setcolor(cval, 0.0, 0.0, 0.0);
|
| 67 |
|
|
|
| 68 |
|
|
ldot = DOT(np->pnorm, ldir);
|
| 69 |
|
|
|
| 70 |
|
|
if (ldot < 0.0)
|
| 71 |
|
|
return; /* wrong side */
|
| 72 |
|
|
|
| 73 |
|
|
if (np->rdiff > FTINY) {
|
| 74 |
|
|
/*
|
| 75 |
|
|
* Compute and add diffuse reflected component to returned
|
| 76 |
|
|
* color. The diffuse reflected component will always be
|
| 77 |
|
|
* modified by the color of the material.
|
| 78 |
|
|
*/
|
| 79 |
|
|
copycolor(ctmp, np->mcolor);
|
| 80 |
|
|
dtmp = ldot * omega * np->rdiff / PI;
|
| 81 |
|
|
scalecolor(ctmp, dtmp);
|
| 82 |
|
|
addcolor(cval, ctmp);
|
| 83 |
|
|
}
|
| 84 |
|
|
if (np->rspec > FTINY) {
|
| 85 |
|
|
/*
|
| 86 |
|
|
* Compute specular component.
|
| 87 |
|
|
*/
|
| 88 |
|
|
setfunc(np->mp, np->pr);
|
| 89 |
|
|
errno = 0;
|
| 90 |
|
|
if (np->dp == NULL)
|
| 91 |
|
|
dtmp = funvalue(np->mp->oargs.sarg[0], 3, ldir);
|
| 92 |
|
|
else {
|
| 93 |
|
|
for (i = 0; i < np->dp->nd; i++)
|
| 94 |
|
|
pt[i] = funvalue(np->mp->oargs.sarg[3+i],
|
| 95 |
|
|
3, ldir);
|
| 96 |
|
|
dtmp = datavalue(np->dp, pt);
|
| 97 |
|
|
dtmp = funvalue(np->mp->oargs.sarg[0], 1, &dtmp);
|
| 98 |
|
|
}
|
| 99 |
|
|
if (errno)
|
| 100 |
|
|
goto computerr;
|
| 101 |
|
|
if (dtmp > FTINY) {
|
| 102 |
|
|
copycolor(ctmp, np->scolor);
|
| 103 |
|
|
dtmp *= ldot * omega;
|
| 104 |
|
|
scalecolor(ctmp, dtmp);
|
| 105 |
|
|
addcolor(cval, ctmp);
|
| 106 |
|
|
}
|
| 107 |
|
|
}
|
| 108 |
|
|
return;
|
| 109 |
|
|
computerr:
|
| 110 |
|
|
objerror(np->mp, WARNING, "compute error");
|
| 111 |
|
|
return;
|
| 112 |
|
|
}
|
| 113 |
|
|
|
| 114 |
|
|
|
| 115 |
|
|
m_brdf(m, r) /* color a ray which hit a BRDF material */
|
| 116 |
|
|
register OBJREC *m;
|
| 117 |
|
|
register RAY *r;
|
| 118 |
|
|
{
|
| 119 |
|
|
BRDFDAT nd;
|
| 120 |
|
|
double dtmp;
|
| 121 |
|
|
COLOR ctmp;
|
| 122 |
|
|
register int i;
|
| 123 |
|
|
|
| 124 |
|
|
if (m->oargs.nsargs < 2 || m->oargs.nfargs < 4)
|
| 125 |
|
|
objerror(m, USER, "bad # arguments");
|
| 126 |
|
|
/* easy shadow test */
|
| 127 |
|
|
if (r->crtype & SHADOW)
|
| 128 |
|
|
return;
|
| 129 |
|
|
nd.mp = m;
|
| 130 |
|
|
nd.pr = r;
|
| 131 |
|
|
/* load auxiliary files */
|
| 132 |
|
|
if (m->otype == MAT_PDATA || m->otype == MAT_MDATA) {
|
| 133 |
|
|
nd.dp = getdata(m->oargs.sarg[1]);
|
| 134 |
|
|
for (i = 3; i < m->oargs.nsargs; i++)
|
| 135 |
|
|
if (m->oargs.sarg[i][0] == '-')
|
| 136 |
|
|
break;
|
| 137 |
|
|
if (i-3 != nd.dp->nd)
|
| 138 |
|
|
objerror(m, USER, "dimension error");
|
| 139 |
|
|
if (!fundefined(m->oargs.sarg[3]))
|
| 140 |
|
|
loadfunc(m->oargs.sarg[2]);
|
| 141 |
|
|
} else {
|
| 142 |
|
|
nd.dp = NULL;
|
| 143 |
|
|
if (!fundefined(m->oargs.sarg[0]))
|
| 144 |
|
|
loadfunc(m->oargs.sarg[1]);
|
| 145 |
|
|
}
|
| 146 |
|
|
/* get material color */
|
| 147 |
|
|
setcolor(nd.mcolor, m->oargs.farg[0],
|
| 148 |
|
|
m->oargs.farg[1],
|
| 149 |
|
|
m->oargs.farg[2]);
|
| 150 |
|
|
/* get roughness */
|
| 151 |
|
|
if (r->rod < 0.0)
|
| 152 |
|
|
flipsurface(r);
|
| 153 |
|
|
/* get modifiers */
|
| 154 |
|
|
raytexture(r, m->omod);
|
| 155 |
|
|
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
|
| 156 |
|
|
multcolor(nd.mcolor, r->pcol); /* modify material color */
|
| 157 |
|
|
r->rt = r->rot; /* default ray length */
|
| 158 |
|
|
/* get specular component */
|
| 159 |
|
|
nd.rspec = m->oargs.farg[3];
|
| 160 |
|
|
|
| 161 |
|
|
if (nd.rspec > FTINY) { /* has specular component */
|
| 162 |
|
|
/* compute specular color */
|
| 163 |
|
|
if (m->otype == MAT_MFUNC || m->otype == MAT_MDATA)
|
| 164 |
|
|
copycolor(nd.scolor, nd.mcolor);
|
| 165 |
|
|
else
|
| 166 |
|
|
setcolor(nd.scolor, 1.0, 1.0, 1.0);
|
| 167 |
|
|
scalecolor(nd.scolor, nd.rspec);
|
| 168 |
|
|
/* improved model */
|
| 169 |
|
|
dtmp = exp(-BSPEC(m)*nd.pdot);
|
| 170 |
|
|
for (i = 0; i < 3; i++)
|
| 171 |
|
|
colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
|
| 172 |
|
|
nd.rspec += (1.0-nd.rspec)*dtmp;
|
| 173 |
|
|
}
|
| 174 |
|
|
/* diffuse reflection */
|
| 175 |
|
|
nd.rdiff = 1.0 - nd.rspec;
|
| 176 |
|
|
/* compute ambient */
|
| 177 |
|
|
if (nd.rdiff > FTINY) {
|
| 178 |
|
|
ambient(ctmp, r);
|
| 179 |
|
|
multcolor(ctmp, nd.mcolor); /* modified by material color */
|
| 180 |
|
|
addcolor(r->rcol, ctmp); /* add to returned color */
|
| 181 |
|
|
}
|
| 182 |
|
|
/* add direct component */
|
| 183 |
|
|
direct(r, dirbrdf, &nd);
|
| 184 |
|
|
}
|