ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
Revision: 2.3
Committed: Wed Mar 4 16:52:13 1992 UTC (32 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +19 -6 lines
Log Message:
improved assignment and use of minarad and maxarad

File Contents

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