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 |
extern double funvalue(), varvalue(); |
38 |
|
39 |
typedef struct { |
40 |
OBJREC *mp; /* material pointer */ |
41 |
RAY *pr; /* intersected ray */ |
42 |
DATARRAY *dp; /* data array for PDATA or MDATA */ |
43 |
COLOR mcolor; /* color of this material */ |
44 |
COLOR scolor; /* color of specular component */ |
45 |
double rspec; /* specular reflection */ |
46 |
double rdiff; /* diffuse reflection */ |
47 |
FVECT pnorm; /* perturbed surface normal */ |
48 |
double pdot; /* perturbed dot product */ |
49 |
} BRDFDAT; /* BRDF material data */ |
50 |
|
51 |
|
52 |
dirbrdf(cval, np, ldir, omega) /* compute source contribution */ |
53 |
COLOR cval; /* returned coefficient */ |
54 |
register BRDFDAT *np; /* material data */ |
55 |
FVECT ldir; /* light source direction */ |
56 |
double omega; /* light source size */ |
57 |
{ |
58 |
double ldot; |
59 |
double dtmp; |
60 |
COLOR ctmp; |
61 |
double pt[MAXDIM]; |
62 |
register int i; |
63 |
|
64 |
setcolor(cval, 0.0, 0.0, 0.0); |
65 |
|
66 |
ldot = DOT(np->pnorm, ldir); |
67 |
|
68 |
if (ldot < 0.0) |
69 |
return; /* wrong side */ |
70 |
|
71 |
if (np->rdiff > FTINY) { |
72 |
/* |
73 |
* Compute and add diffuse reflected component to returned |
74 |
* color. The diffuse reflected component will always be |
75 |
* modified by the color of the material. |
76 |
*/ |
77 |
copycolor(ctmp, np->mcolor); |
78 |
dtmp = ldot * omega * np->rdiff / PI; |
79 |
scalecolor(ctmp, dtmp); |
80 |
addcolor(cval, ctmp); |
81 |
} |
82 |
if (np->rspec > FTINY) { |
83 |
/* |
84 |
* Compute specular component. |
85 |
*/ |
86 |
setfunc(np->mp, np->pr); |
87 |
errno = 0; |
88 |
if (np->dp == NULL) |
89 |
dtmp = funvalue(np->mp->oargs.sarg[0], 3, ldir); |
90 |
else { |
91 |
for (i = 0; i < np->dp->nd; i++) |
92 |
pt[i] = funvalue(np->mp->oargs.sarg[3+i], |
93 |
3, ldir); |
94 |
dtmp = datavalue(np->dp, pt); |
95 |
dtmp = funvalue(np->mp->oargs.sarg[0], 1, &dtmp); |
96 |
} |
97 |
if (errno) |
98 |
goto computerr; |
99 |
if (dtmp > FTINY) { |
100 |
copycolor(ctmp, np->scolor); |
101 |
dtmp *= ldot * omega; |
102 |
scalecolor(ctmp, dtmp); |
103 |
addcolor(cval, ctmp); |
104 |
} |
105 |
} |
106 |
return; |
107 |
computerr: |
108 |
objerror(np->mp, WARNING, "compute error"); |
109 |
return; |
110 |
} |
111 |
|
112 |
|
113 |
m_brdf(m, r) /* color a ray which hit a BRDF material */ |
114 |
register OBJREC *m; |
115 |
register RAY *r; |
116 |
{ |
117 |
BRDFDAT nd; |
118 |
double dtmp; |
119 |
COLOR ctmp; |
120 |
register int i; |
121 |
|
122 |
if (m->oargs.nsargs < 2 || m->oargs.nfargs < 4) |
123 |
objerror(m, USER, "bad # arguments"); |
124 |
/* easy shadow test */ |
125 |
if (r->crtype & SHADOW) |
126 |
return; |
127 |
nd.mp = m; |
128 |
nd.pr = r; |
129 |
/* load auxiliary files */ |
130 |
if (m->otype == MAT_PDATA || m->otype == MAT_MDATA) { |
131 |
nd.dp = getdata(m->oargs.sarg[1]); |
132 |
for (i = 3; i < m->oargs.nsargs; i++) |
133 |
if (m->oargs.sarg[i][0] == '-') |
134 |
break; |
135 |
if (i-3 != nd.dp->nd) |
136 |
objerror(m, USER, "dimension error"); |
137 |
if (!fundefined(m->oargs.sarg[3])) |
138 |
loadfunc(m->oargs.sarg[2]); |
139 |
} else { |
140 |
nd.dp = NULL; |
141 |
if (!fundefined(m->oargs.sarg[0])) |
142 |
loadfunc(m->oargs.sarg[1]); |
143 |
} |
144 |
/* get material color */ |
145 |
setcolor(nd.mcolor, m->oargs.farg[0], |
146 |
m->oargs.farg[1], |
147 |
m->oargs.farg[2]); |
148 |
/* get roughness */ |
149 |
if (r->rod < 0.0) |
150 |
flipsurface(r); |
151 |
/* get modifiers */ |
152 |
raytexture(r, m->omod); |
153 |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
154 |
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
155 |
r->rt = r->rot; /* default ray length */ |
156 |
/* get specular component */ |
157 |
nd.rspec = m->oargs.farg[3]; |
158 |
|
159 |
if (nd.rspec > FTINY) { /* has specular component */ |
160 |
/* compute specular color */ |
161 |
if (m->otype == MAT_MFUNC || m->otype == MAT_MDATA) |
162 |
copycolor(nd.scolor, nd.mcolor); |
163 |
else |
164 |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
165 |
scalecolor(nd.scolor, nd.rspec); |
166 |
} |
167 |
/* diffuse reflection */ |
168 |
nd.rdiff = 1.0 - nd.rspec; |
169 |
/* compute ambient */ |
170 |
if (nd.rdiff > FTINY) { |
171 |
ambient(ctmp, r); |
172 |
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
173 |
addcolor(r->rcol, ctmp); /* add to returned color */ |
174 |
} |
175 |
/* add direct component */ |
176 |
direct(r, dirbrdf, &nd); |
177 |
} |