15 |
|
|
16 |
|
#include "ray.h" |
17 |
|
|
18 |
– |
#include "source.h" |
19 |
– |
|
18 |
|
#include "otypes.h" |
19 |
|
|
20 |
|
/* |
34 |
|
|
35 |
|
#define BSPEC(m) (6.0) /* specularity parameter b */ |
36 |
|
|
37 |
+ |
extern double exp(); |
38 |
|
|
39 |
< |
m_normal(m, r) /* color a ray which hit something normal */ |
40 |
< |
register OBJREC *m; |
41 |
< |
register RAY *r; |
43 |
< |
{ |
44 |
< |
double exp(); |
39 |
> |
typedef struct { |
40 |
> |
OBJREC *mp; /* material pointer */ |
41 |
> |
RAY *pr; /* intersected ray */ |
42 |
|
COLOR mcolor; /* color of this material */ |
43 |
|
COLOR scolor; /* color of specular component */ |
44 |
|
FVECT vrefl; /* vector in direction of reflected ray */ |
45 |
|
double alpha2; /* roughness squared times 2 */ |
49 |
– |
RAY lr; /* ray to illumination source */ |
46 |
|
double rdiff, rspec; /* reflected specular, diffuse */ |
47 |
|
double trans; /* transmissivity */ |
48 |
|
double tdiff, tspec; /* transmitted specular, diffuse */ |
49 |
|
FVECT pnorm; /* perturbed surface normal */ |
50 |
|
double pdot; /* perturbed dot product */ |
51 |
+ |
} NORMDAT; /* normal material data */ |
52 |
+ |
|
53 |
+ |
|
54 |
+ |
dirnorm(cval, np, ldir, omega) /* compute source contribution */ |
55 |
+ |
COLOR cval; /* returned coefficient */ |
56 |
+ |
register NORMDAT *np; /* material data */ |
57 |
+ |
FVECT ldir; /* light source direction */ |
58 |
+ |
double omega; /* light source size */ |
59 |
+ |
{ |
60 |
|
double ldot; |
56 |
– |
double omega; |
61 |
|
double dtmp; |
62 |
|
COLOR ctmp; |
63 |
+ |
|
64 |
+ |
setcolor(cval, 0.0, 0.0, 0.0); |
65 |
+ |
|
66 |
+ |
ldot = DOT(np->pnorm, ldir); |
67 |
+ |
|
68 |
+ |
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
69 |
+ |
return; /* wrong side */ |
70 |
+ |
|
71 |
+ |
if (ldot > FTINY && 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 (ldot > FTINY && np->rspec > FTINY && np->alpha2 > FTINY) { |
83 |
+ |
/* |
84 |
+ |
* Compute specular reflection coefficient using |
85 |
+ |
* gaussian distribution model. |
86 |
+ |
*/ |
87 |
+ |
/* roughness + source */ |
88 |
+ |
dtmp = np->alpha2 + omega/(2.0*PI); |
89 |
+ |
/* gaussian */ |
90 |
+ |
dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
91 |
+ |
/* worth using? */ |
92 |
+ |
if (dtmp > FTINY) { |
93 |
+ |
copycolor(ctmp, np->scolor); |
94 |
+ |
dtmp *= omega; |
95 |
+ |
scalecolor(ctmp, dtmp); |
96 |
+ |
addcolor(cval, ctmp); |
97 |
+ |
} |
98 |
+ |
} |
99 |
+ |
if (ldot < -FTINY && np->tdiff > FTINY) { |
100 |
+ |
/* |
101 |
+ |
* Compute diffuse transmission. |
102 |
+ |
*/ |
103 |
+ |
copycolor(ctmp, np->mcolor); |
104 |
+ |
dtmp = -ldot * omega * np->tdiff / PI; |
105 |
+ |
scalecolor(ctmp, dtmp); |
106 |
+ |
addcolor(cval, ctmp); |
107 |
+ |
} |
108 |
+ |
if (ldot < -FTINY && np->tspec > FTINY && np->alpha2 > FTINY) { |
109 |
+ |
/* |
110 |
+ |
* Compute specular transmission. Specular transmission |
111 |
+ |
* is unaffected by material color. |
112 |
+ |
*/ |
113 |
+ |
/* roughness + source */ |
114 |
+ |
dtmp = np->alpha2 + omega/(2.0*PI); |
115 |
+ |
/* gaussian */ |
116 |
+ |
dtmp = exp((DOT(np->pr->rdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
117 |
+ |
/* worth using? */ |
118 |
+ |
if (dtmp > FTINY) { |
119 |
+ |
dtmp *= np->tspec * omega; |
120 |
+ |
setcolor(ctmp, dtmp, dtmp, dtmp); |
121 |
+ |
addcolor(cval, ctmp); |
122 |
+ |
} |
123 |
+ |
} |
124 |
+ |
} |
125 |
+ |
|
126 |
+ |
|
127 |
+ |
m_normal(m, r) /* color a ray which hit something normal */ |
128 |
+ |
register OBJREC *m; |
129 |
+ |
register RAY *r; |
130 |
+ |
{ |
131 |
+ |
NORMDAT nd; |
132 |
+ |
double dtmp; |
133 |
+ |
COLOR ctmp; |
134 |
|
register int i; |
135 |
|
|
136 |
|
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
138 |
|
/* easy shadow test */ |
139 |
|
if (r->crtype & SHADOW && m->otype != MAT_TRANS) |
140 |
|
return; |
141 |
+ |
nd.mp = m; |
142 |
+ |
nd.pr = r; |
143 |
|
/* get material color */ |
144 |
< |
setcolor(mcolor, m->oargs.farg[0], |
144 |
> |
setcolor(nd.mcolor, m->oargs.farg[0], |
145 |
|
m->oargs.farg[1], |
146 |
|
m->oargs.farg[2]); |
147 |
|
/* get roughness */ |
148 |
< |
alpha2 = m->oargs.farg[4]; |
149 |
< |
alpha2 *= 2.0 * alpha2; |
148 |
> |
nd.alpha2 = m->oargs.farg[4]; |
149 |
> |
nd.alpha2 *= 2.0 * nd.alpha2; |
150 |
|
/* reorient if necessary */ |
151 |
|
if (r->rod < 0.0) |
152 |
|
flipsurface(r); |
153 |
|
/* get modifiers */ |
154 |
|
raytexture(r, m->omod); |
155 |
< |
pdot = raynormal(pnorm, r); /* perturb normal */ |
156 |
< |
multcolor(mcolor, r->pcol); /* modify material color */ |
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 |
< |
rspec = m->oargs.farg[3]; |
159 |
> |
nd.rspec = m->oargs.farg[3]; |
160 |
|
|
161 |
< |
if (rspec > FTINY) { /* has specular component */ |
161 |
> |
if (nd.rspec > FTINY) { /* has specular component */ |
162 |
|
/* compute specular color */ |
163 |
|
if (m->otype == MAT_METAL) |
164 |
< |
copycolor(scolor, mcolor); |
164 |
> |
copycolor(nd.scolor, nd.mcolor); |
165 |
|
else |
166 |
< |
setcolor(scolor, 1.0, 1.0, 1.0); |
167 |
< |
scalecolor(scolor, rspec); |
166 |
> |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
167 |
> |
scalecolor(nd.scolor, nd.rspec); |
168 |
|
/* improved model */ |
169 |
< |
dtmp = exp(-BSPEC(m)*pdot); |
169 |
> |
dtmp = exp(-BSPEC(m)*nd.pdot); |
170 |
|
for (i = 0; i < 3; i++) |
171 |
< |
colval(scolor,i) += (1.0-colval(scolor,i))*dtmp; |
172 |
< |
rspec += (1.0-rspec)*dtmp; |
171 |
> |
colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; |
172 |
> |
nd.rspec += (1.0-nd.rspec)*dtmp; |
173 |
|
/* compute reflected ray */ |
174 |
|
for (i = 0; i < 3; i++) |
175 |
< |
vrefl[i] = r->rdir[i] + 2.0*pdot*pnorm[i]; |
175 |
> |
nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; |
176 |
|
|
177 |
< |
if (alpha2 <= FTINY && !(r->crtype & SHADOW)) |
178 |
< |
if (rayorigin(&lr, r, REFLECTED, rspec) == 0) { |
179 |
< |
VCOPY(lr.rdir, vrefl); |
177 |
> |
if (nd.alpha2 <= FTINY && !(r->crtype & SHADOW)) { |
178 |
> |
RAY lr; |
179 |
> |
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
180 |
> |
VCOPY(lr.rdir, nd.vrefl); |
181 |
|
rayvalue(&lr); |
182 |
< |
multcolor(lr.rcol, scolor); |
182 |
> |
multcolor(lr.rcol, nd.scolor); |
183 |
|
addcolor(r->rcol, lr.rcol); |
184 |
+ |
if (nd.rspec > 0.5 && m->omod == OVOID) |
185 |
+ |
r->rt = r->rot + lr.rt; |
186 |
|
} |
187 |
+ |
} |
188 |
|
} |
189 |
< |
|
189 |
> |
/* compute transmission */ |
190 |
|
if (m->otype == MAT_TRANS) { |
191 |
< |
trans = m->oargs.farg[5]*(1.0 - rspec); |
192 |
< |
tspec = trans * m->oargs.farg[6]; |
193 |
< |
tdiff = trans - tspec; |
191 |
> |
nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); |
192 |
> |
nd.tspec = nd.trans * m->oargs.farg[6]; |
193 |
> |
nd.tdiff = nd.trans - nd.tspec; |
194 |
|
} else |
195 |
< |
tdiff = tspec = trans = 0.0; |
195 |
> |
nd.tdiff = nd.tspec = nd.trans = 0.0; |
196 |
|
/* transmitted ray */ |
197 |
< |
if (tspec > FTINY && alpha2 <= FTINY) |
198 |
< |
if (rayorigin(&lr, r, TRANS, tspec) == 0) { |
197 |
> |
if (nd.tspec > FTINY && nd.alpha2 <= FTINY) { |
198 |
> |
RAY lr; |
199 |
> |
if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { |
200 |
|
VCOPY(lr.rdir, r->rdir); |
201 |
|
rayvalue(&lr); |
202 |
< |
scalecolor(lr.rcol, tspec); |
202 |
> |
scalecolor(lr.rcol, nd.tspec); |
203 |
|
addcolor(r->rcol, lr.rcol); |
204 |
+ |
if (nd.tspec > .5) |
205 |
+ |
r->rt = r->rot + lr.rt; |
206 |
|
} |
207 |
+ |
} |
208 |
|
if (r->crtype & SHADOW) /* the rest is shadow */ |
209 |
|
return; |
210 |
|
/* diffuse reflection */ |
211 |
< |
rdiff = 1.0 - trans - rspec; |
211 |
> |
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
212 |
|
|
213 |
< |
if (rdiff <= FTINY && tdiff <= FTINY && alpha2 <= FTINY) |
213 |
> |
if (nd.rdiff <= FTINY && nd.tdiff <= FTINY && nd.alpha2 <= FTINY) |
214 |
|
return; /* purely specular */ |
215 |
|
|
216 |
< |
ambient(ctmp, r); /* compute ambient component */ |
217 |
< |
scalecolor(ctmp, 1.0-trans); /* from this side */ |
218 |
< |
multcolor(ctmp, mcolor); /* modified by material color */ |
219 |
< |
addcolor(r->rcol, ctmp); /* add to returned color */ |
220 |
< |
|
221 |
< |
if (trans > FTINY) { /* ambient from other side */ |
216 |
> |
if (nd.rdiff > FTINY) { /* ambient from this side */ |
217 |
> |
ambient(ctmp, r); |
218 |
> |
if (nd.alpha2 <= FTINY) |
219 |
> |
scalecolor(ctmp, nd.rdiff); |
220 |
> |
else |
221 |
> |
scalecolor(ctmp, 1.0-nd.trans); |
222 |
> |
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
223 |
> |
addcolor(r->rcol, ctmp); /* add to returned color */ |
224 |
> |
} |
225 |
> |
if (nd.tdiff > FTINY) { /* ambient from other side */ |
226 |
|
flipsurface(r); |
227 |
< |
scalecolor(ctmp, trans); |
228 |
< |
multcolor(ctmp, mcolor); |
227 |
> |
ambient(ctmp, r); |
228 |
> |
if (nd.alpha2 <= FTINY) |
229 |
> |
scalecolor(ctmp, nd.tdiff); |
230 |
> |
else |
231 |
> |
scalecolor(ctmp, nd.trans); |
232 |
> |
multcolor(ctmp, nd.mcolor); |
233 |
|
addcolor(r->rcol, ctmp); |
234 |
|
flipsurface(r); |
235 |
|
} |
236 |
< |
|
237 |
< |
for (i = 0; i < nsources; i++) { /* add specular and diffuse */ |
144 |
< |
|
145 |
< |
if ((omega = srcray(&lr, r, i)) == 0.0) |
146 |
< |
continue; /* bad source */ |
147 |
< |
|
148 |
< |
ldot = DOT(pnorm, lr.rdir); |
149 |
< |
|
150 |
< |
if (ldot < 0.0 ? trans <= FTINY : trans >= 1.0-FTINY) |
151 |
< |
continue; /* wrong side */ |
152 |
< |
|
153 |
< |
rayvalue(&lr); /* compute light ray value */ |
154 |
< |
|
155 |
< |
if (intens(lr.rcol) <= FTINY) |
156 |
< |
continue; /* didn't hit light source */ |
157 |
< |
|
158 |
< |
if (ldot > FTINY && rdiff > FTINY) { |
159 |
< |
/* |
160 |
< |
* Compute and add diffuse component to returned color. |
161 |
< |
* The diffuse component will always be modified by the |
162 |
< |
* color of the material. |
163 |
< |
*/ |
164 |
< |
copycolor(ctmp, lr.rcol); |
165 |
< |
dtmp = ldot * omega * rdiff / PI; |
166 |
< |
scalecolor(ctmp, dtmp); |
167 |
< |
multcolor(ctmp, mcolor); |
168 |
< |
addcolor(r->rcol, ctmp); |
169 |
< |
} |
170 |
< |
if (ldot > FTINY && rspec > FTINY && alpha2 > FTINY) { |
171 |
< |
/* |
172 |
< |
* Compute specular reflection coefficient using |
173 |
< |
* gaussian distribution model. |
174 |
< |
*/ |
175 |
< |
/* roughness + source */ |
176 |
< |
dtmp = alpha2 + omega/(2.0*PI); |
177 |
< |
/* gaussian */ |
178 |
< |
dtmp = exp((DOT(vrefl,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp; |
179 |
< |
/* worth using? */ |
180 |
< |
if (dtmp > FTINY) { |
181 |
< |
copycolor(ctmp, lr.rcol); |
182 |
< |
dtmp *= omega; |
183 |
< |
scalecolor(ctmp, dtmp); |
184 |
< |
multcolor(ctmp, scolor); |
185 |
< |
addcolor(r->rcol, ctmp); |
186 |
< |
} |
187 |
< |
} |
188 |
< |
if (ldot < -FTINY && tdiff > FTINY) { |
189 |
< |
/* |
190 |
< |
* Compute diffuse transmission. |
191 |
< |
*/ |
192 |
< |
copycolor(ctmp, lr.rcol); |
193 |
< |
dtmp = -ldot * omega * tdiff / PI; |
194 |
< |
scalecolor(ctmp, dtmp); |
195 |
< |
multcolor(ctmp, mcolor); |
196 |
< |
addcolor(r->rcol, ctmp); |
197 |
< |
} |
198 |
< |
if (ldot < -FTINY && tspec > FTINY && alpha2 > FTINY) { |
199 |
< |
/* |
200 |
< |
* Compute specular transmission. |
201 |
< |
*/ |
202 |
< |
/* roughness + source */ |
203 |
< |
dtmp = alpha2 + omega/(2.0*PI); |
204 |
< |
/* gaussian */ |
205 |
< |
dtmp = exp((DOT(r->rdir,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp; |
206 |
< |
/* worth using? */ |
207 |
< |
if (dtmp > FTINY) { |
208 |
< |
copycolor(ctmp, lr.rcol); |
209 |
< |
dtmp *= tspec * omega; |
210 |
< |
scalecolor(ctmp, dtmp); |
211 |
< |
addcolor(r->rcol, ctmp); |
212 |
< |
} |
213 |
< |
} |
214 |
< |
} |
236 |
> |
/* add direct component */ |
237 |
> |
direct(r, dirnorm, &nd); |
238 |
|
} |