ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
Revision: 2.2
Committed: Wed Jan 15 10:36:42 1992 UTC (32 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +1 -1 lines
Log Message:
changed jittering test to use urand

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 * ambient.c - routines dealing with ambient (inter-reflected) component.
9 *
10 * The macro AMBFLUSH (if defined) is the number of ambient values
11 * to wait before flushing to the ambient file.
12 *
13 * 5/9/86
14 */
15
16 #include "ray.h"
17
18 #include "octree.h"
19
20 #include "otypes.h"
21
22 #include "ambient.h"
23
24 #include "random.h"
25
26 #define OCTSCALE 0.5 /* ceil((valid rad.)/(cube size)) */
27
28 typedef struct ambtree {
29 AMBVAL *alist; /* ambient value list */
30 struct ambtree *kid; /* 8 child nodes */
31 } AMBTREE; /* ambient octree */
32
33 extern CUBE thescene; /* contains space boundaries */
34
35 #define MAXASET 511 /* maximum number of elements in ambient set */
36 OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */
37
38 double maxarad; /* maximum ambient radius */
39 double minarad; /* minimum ambient radius */
40
41 static AMBTREE atrunk; /* our ambient trunk node */
42
43 static FILE *ambfp = NULL; /* ambient file pointer */
44
45 #define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL))
46
47 #define newambtree() (AMBTREE *)calloc(8, sizeof(AMBTREE))
48
49
50 setambient(afile) /* initialize calculation */
51 char *afile;
52 {
53 long ftell();
54 AMBVAL amb;
55
56 maxarad = thescene.cusize / 2.0; /* maximum radius */
57 /* minimum radius */
58 minarad = ambres > 0 ? thescene.cusize/ambres : 0.0;
59
60 /* open ambient file */
61 if (afile != NULL)
62 if ((ambfp = fopen(afile, "r+")) != NULL) {
63 while (fread((char *)&amb,sizeof(AMBVAL),1,ambfp) == 1)
64 avinsert(&amb, &atrunk, thescene.cuorg,
65 thescene.cusize);
66 /* align */
67 fseek(ambfp, -(ftell(ambfp)%sizeof(AMBVAL)), 1);
68 } else if ((ambfp = fopen(afile, "w")) == NULL) {
69 sprintf(errmsg, "cannot open ambient file \"%s\"",
70 afile);
71 error(SYSTEM, errmsg);
72 }
73 }
74
75
76 ambnotify(obj) /* record new modifier */
77 OBJECT obj;
78 {
79 static int hitlimit = 0;
80 register OBJREC *o = objptr(obj);
81 register char **amblp;
82
83 if (hitlimit || !ismodifier(o->otype))
84 return;
85 for (amblp = amblist; *amblp != NULL; amblp++)
86 if (!strcmp(o->oname, *amblp)) {
87 if (ambset[0] >= MAXASET) {
88 error(WARNING, "too many modifiers in ambient list");
89 hitlimit++;
90 return; /* should this be fatal? */
91 }
92 insertelem(ambset, obj);
93 return;
94 }
95 }
96
97
98 ambient(acol, r) /* compute ambient component for ray */
99 COLOR acol;
100 register RAY *r;
101 {
102 static int rdepth = 0; /* ambient recursion */
103 double d;
104
105 if (ambdiv <= 0) /* no ambient calculation */
106 goto dumbamb;
107 /* check number of bounces */
108 if (rdepth >= ambounce)
109 goto dumbamb;
110 /* check ambient list */
111 if (ambincl != -1 && r->ro != NULL &&
112 ambincl != inset(ambset, r->ro->omod))
113 goto dumbamb;
114
115 if (ambacc <= FTINY) { /* no ambient storage */
116 rdepth++;
117 d = doambient(acol, r, r->rweight, NULL, NULL);
118 rdepth--;
119 if (d == 0.0)
120 goto dumbamb;
121 return;
122 }
123 /* get ambient value */
124 setcolor(acol, 0.0, 0.0, 0.0);
125 d = sumambient(acol, r, rdepth,
126 &atrunk, thescene.cuorg, thescene.cusize);
127 if (d > FTINY)
128 scalecolor(acol, 1.0/d);
129 else {
130 d = makeambient(acol, r, rdepth++);
131 rdepth--;
132 }
133 if (d > FTINY)
134 return;
135 dumbamb: /* return global value */
136 copycolor(acol, ambval);
137 }
138
139
140 double
141 sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */
142 COLOR acol;
143 register RAY *r;
144 int al;
145 AMBTREE *at;
146 FVECT c0;
147 double s;
148 {
149 extern double sqrt();
150 double d, e1, e2, wt, wsum;
151 COLOR ct;
152 FVECT ck0;
153 int i;
154 register int j;
155 register AMBVAL *av;
156 /* do this node */
157 wsum = 0.0;
158 for (av = at->alist; av != NULL; av = av->next) {
159 /*
160 * Ambient level test.
161 */
162 if (av->lvl > al || av->weight < r->rweight-FTINY)
163 continue;
164 /*
165 * Ambient radius test.
166 */
167 e1 = 0.0;
168 for (j = 0; j < 3; j++) {
169 d = av->pos[j] - r->rop[j];
170 e1 += d * d;
171 }
172 e1 /= av->rad * av->rad;
173 if (e1 > ambacc*ambacc*1.21)
174 continue;
175 /*
176 * Normal direction test.
177 */
178 e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight;
179 if (e2 < 0.0) e2 = 0.0;
180 if (e1 + e2 > ambacc*ambacc*1.21)
181 continue;
182 /*
183 * Ray behind test.
184 */
185 d = 0.0;
186 for (j = 0; j < 3; j++)
187 d += (r->rop[j] - av->pos[j]) *
188 (av->dir[j] + r->ron[j]);
189 if (d*0.5 < -minarad*ambacc-.001)
190 continue;
191 /*
192 * Jittering final test reduces image artifacts.
193 */
194 wt = sqrt(e1) + sqrt(e2);
195 wt *= .9 + .2*urand(9015+samplendx);
196 if (wt > ambacc)
197 continue;
198 if (wt <= 1e-3)
199 wt = 1e3;
200 else
201 wt = 1.0 / wt;
202 wsum += wt;
203 extambient(ct, av, r->rop, r->ron);
204 scalecolor(ct, wt);
205 addcolor(acol, ct);
206 }
207 if (at->kid == NULL)
208 return(wsum);
209 /* do children */
210 s *= 0.5;
211 for (i = 0; i < 8; i++) {
212 for (j = 0; j < 3; j++) {
213 ck0[j] = c0[j];
214 if (1<<j & i)
215 ck0[j] += s;
216 if (r->rop[j] < ck0[j] - OCTSCALE*s)
217 break;
218 if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
219 break;
220 }
221 if (j == 3)
222 wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
223 }
224 return(wsum);
225 }
226
227
228 double
229 makeambient(acol, r, al) /* make a new ambient value */
230 COLOR acol;
231 register RAY *r;
232 int al;
233 {
234 AMBVAL amb;
235 FVECT gp, gd;
236 /* compute weight */
237 amb.weight = pow(AVGREFL, (double)al);
238 if (r->rweight < 0.2*amb.weight) /* heuristic */
239 amb.weight = r->rweight;
240 /* compute ambient */
241 amb.rad = doambient(acol, r, amb.weight, gp, gd);
242 if (amb.rad == 0.0)
243 return(0.0);
244 /* store it */
245 VCOPY(amb.pos, r->rop);
246 VCOPY(amb.dir, r->ron);
247 amb.lvl = al;
248 copycolor(amb.val, acol);
249 VCOPY(amb.gpos, gp);
250 VCOPY(amb.gdir, gd);
251 /* insert into tree */
252 avinsert(&amb, &atrunk, thescene.cuorg, thescene.cusize);
253 avsave(&amb); /* write to file */
254 return(amb.rad);
255 }
256
257
258 extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */
259 COLOR cr;
260 register AMBVAL *ap;
261 FVECT pv, nv;
262 {
263 FVECT v1, v2;
264 register int i;
265 double d;
266
267 d = 1.0; /* zeroeth order */
268 /* gradient due to translation */
269 for (i = 0; i < 3; i++)
270 d += ap->gpos[i]*(pv[i]-ap->pos[i]);
271 /* gradient due to rotation */
272 VCOPY(v1, ap->dir);
273 fcross(v2, v1, nv);
274 d += DOT(ap->gdir, v2);
275 if (d <= 0.0) {
276 setcolor(cr, 0.0, 0.0, 0.0);
277 return;
278 }
279 copycolor(cr, ap->val);
280 scalecolor(cr, d);
281 }
282
283
284 static
285 avsave(av) /* save an ambient value */
286 AMBVAL *av;
287 {
288 #ifdef AMBFLUSH
289 static int nunflshed = 0;
290 #endif
291 if (ambfp == NULL)
292 return;
293 if (fwrite((char *)av, sizeof(AMBVAL), 1, ambfp) != 1)
294 goto writerr;
295 #ifdef AMBFLUSH
296 if (++nunflshed >= AMBFLUSH) {
297 if (fflush(ambfp) == EOF)
298 goto writerr;
299 nunflshed = 0;
300 }
301 #endif
302 return;
303 writerr:
304 error(SYSTEM, "error writing ambient file");
305 }
306
307
308 static
309 avinsert(aval, at, c0, s) /* insert ambient value in a tree */
310 AMBVAL *aval;
311 register AMBTREE *at;
312 FVECT c0;
313 double s;
314 {
315 FVECT ck0;
316 int branch;
317 register AMBVAL *av;
318 register int i;
319
320 if ((av = newambval()) == NULL)
321 goto memerr;
322 copystruct(av, aval);
323 VCOPY(ck0, c0);
324 while (s*(OCTSCALE/2) > av->rad*ambacc) {
325 if (at->kid == NULL)
326 if ((at->kid = newambtree()) == NULL)
327 goto memerr;
328 s *= 0.5;
329 branch = 0;
330 for (i = 0; i < 3; i++)
331 if (av->pos[i] > ck0[i] + s) {
332 ck0[i] += s;
333 branch |= 1 << i;
334 }
335 at = at->kid + branch;
336 }
337 av->next = at->alist;
338 at->alist = av;
339 return;
340 memerr:
341 error(SYSTEM, "out of memory in avinsert");
342 }