ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/normal.c
Revision: 2.1
Committed: Tue Nov 12 17:09:11 1991 UTC (32 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.14: +0 -0 lines
Log Message:
updated revision number for release 2.0

File Contents

# Content
1 /* Copyright (c) 1991 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * normal.c - shading function for normal materials.
9 *
10 * 8/19/85
11 * 12/19/85 - added stuff for metals.
12 * 6/26/87 - improved specular model.
13 * 9/28/87 - added model for translucent materials.
14 */
15
16 #include "ray.h"
17
18 #include "otypes.h"
19
20 /*
21 * This routine uses portions of the reflection
22 * model described by Cook and Torrance.
23 * The computation of specular components has been simplified by
24 * numerous approximations and ommisions to improve speed.
25 * We orient the surface towards the incoming ray, so a single
26 * surface can be used to represent an infinitely thin object.
27 *
28 * Arguments for MAT_PLASTIC and MAT_METAL are:
29 * red grn blu specular-frac. facet-slope
30 *
31 * Arguments for MAT_TRANS are:
32 * red grn blu rspec rough trans tspec
33 */
34
35 #define BSPEC(m) (6.0) /* specularity parameter b */
36
37 extern double exp();
38
39 typedef struct {
40 OBJREC *mp; /* material pointer */
41 COLOR mcolor; /* color of this material */
42 COLOR scolor; /* color of specular component */
43 FVECT vrefl; /* vector in direction of reflected ray */
44 FVECT prdir; /* vector in transmitted direction */
45 double alpha2; /* roughness squared times 2 */
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;
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 / np->pdot;
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 always modified by material color.
112 */
113 /* roughness + source */
114 dtmp = np->alpha2 + omega/(2.0*PI);
115 /* gaussian */
116 dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
117 /* worth using? */
118 if (dtmp > FTINY) {
119 copycolor(ctmp, np->mcolor);
120 dtmp *= np->tspec * omega / np->pdot;
121 scalecolor(ctmp, dtmp);
122 addcolor(cval, ctmp);
123 }
124 }
125 }
126
127
128 m_normal(m, r) /* color a ray which hit something normal */
129 register OBJREC *m;
130 register RAY *r;
131 {
132 NORMDAT nd;
133 double transtest, transdist;
134 double dtmp;
135 COLOR ctmp;
136 register int i;
137
138 if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
139 objerror(m, USER, "bad # arguments");
140 /* easy shadow test */
141 if (r->crtype & SHADOW && m->otype != MAT_TRANS)
142 return;
143 nd.mp = m;
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 nd.alpha2 = m->oargs.farg[4];
150 nd.alpha2 *= 2.0 * nd.alpha2;
151 /* reorient if necessary */
152 if (r->rod < 0.0)
153 flipsurface(r);
154 /* get modifiers */
155 raytexture(r, m->omod);
156 nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
157 if (nd.pdot < .001)
158 nd.pdot = .001; /* non-zero for dirnorm() */
159 multcolor(nd.mcolor, r->pcol); /* modify material color */
160 transtest = 0;
161 /* get specular component */
162 nd.rspec = m->oargs.farg[3];
163
164 if (nd.rspec > FTINY) { /* has specular component */
165 /* compute specular color */
166 if (m->otype == MAT_METAL)
167 copycolor(nd.scolor, nd.mcolor);
168 else
169 setcolor(nd.scolor, 1.0, 1.0, 1.0);
170 scalecolor(nd.scolor, nd.rspec);
171 /* improved model */
172 dtmp = exp(-BSPEC(m)*nd.pdot);
173 for (i = 0; i < 3; i++)
174 colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
175 nd.rspec += (1.0-nd.rspec)*dtmp;
176 /* compute reflected ray */
177 for (i = 0; i < 3; i++)
178 nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
179
180 if (nd.alpha2 <= FTINY && !(r->crtype & SHADOW)) {
181 RAY lr;
182 if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
183 VCOPY(lr.rdir, nd.vrefl);
184 rayvalue(&lr);
185 multcolor(lr.rcol, nd.scolor);
186 addcolor(r->rcol, lr.rcol);
187 }
188 }
189 }
190 /* compute transmission */
191 if (m->otype == MAT_TRANS) {
192 nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
193 nd.tspec = nd.trans * m->oargs.farg[6];
194 nd.tdiff = nd.trans - nd.tspec;
195 if (r->crtype & SHADOW || DOT(r->pert,r->pert) <= FTINY*FTINY) {
196 VCOPY(nd.prdir, r->rdir);
197 transtest = 2;
198 } else {
199 for (i = 0; i < 3; i++) /* perturb direction */
200 nd.prdir[i] = r->rdir[i] - .75*r->pert[i];
201 normalize(nd.prdir);
202 }
203 } else
204 nd.tdiff = nd.tspec = nd.trans = 0.0;
205 /* transmitted ray */
206 if (nd.tspec > FTINY && nd.alpha2 <= FTINY) {
207 RAY lr;
208 if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
209 VCOPY(lr.rdir, nd.prdir);
210 rayvalue(&lr);
211 scalecolor(lr.rcol, nd.tspec);
212 multcolor(lr.rcol, nd.mcolor); /* modified by color */
213 addcolor(r->rcol, lr.rcol);
214 transtest *= bright(lr.rcol);
215 transdist = r->rot + lr.rt;
216 }
217 }
218 if (r->crtype & SHADOW) /* the rest is shadow */
219 return;
220 /* diffuse reflection */
221 nd.rdiff = 1.0 - nd.trans - nd.rspec;
222
223 if (nd.rdiff <= FTINY && nd.tdiff <= FTINY && nd.alpha2 <= FTINY)
224 return; /* purely specular */
225
226 if (nd.rdiff > FTINY) { /* ambient from this side */
227 ambient(ctmp, r);
228 if (nd.alpha2 <= FTINY)
229 scalecolor(ctmp, nd.rdiff);
230 else
231 scalecolor(ctmp, 1.0-nd.trans);
232 multcolor(ctmp, nd.mcolor); /* modified by material color */
233 addcolor(r->rcol, ctmp); /* add to returned color */
234 }
235 if (nd.tdiff > FTINY) { /* ambient from other side */
236 flipsurface(r);
237 ambient(ctmp, r);
238 if (nd.alpha2 <= FTINY)
239 scalecolor(ctmp, nd.tdiff);
240 else
241 scalecolor(ctmp, nd.trans);
242 multcolor(ctmp, nd.mcolor); /* modified by color */
243 addcolor(r->rcol, ctmp);
244 flipsurface(r);
245 }
246 /* add direct component */
247 direct(r, dirnorm, &nd);
248 /* check distance */
249 if (transtest > bright(r->rcol))
250 r->rt = transdist;
251 }