ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
Revision: 2.17
Committed: Tue Jan 26 09:08:20 1993 UTC (31 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.16: +7 -11 lines
Log Message:
took out ambfname again

File Contents

# User Rev Content
1 greg 2.15 /* Copyright (c) 1993 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    
11     #include "ray.h"
12    
13     #include "octree.h"
14    
15 greg 1.8 #include "otypes.h"
16    
17 greg 1.14 #include "ambient.h"
18    
19 greg 1.1 #include "random.h"
20    
21 greg 2.12 #define OCTSCALE 0.5 /* ceil((valid rad.)/(cube size)) */
22 greg 1.1
23 greg 1.14 typedef struct ambtree {
24 greg 2.12 AMBVAL *alist; /* ambient value list */
25     struct ambtree *kid; /* 8 child nodes */
26 greg 1.14 } AMBTREE; /* ambient octree */
27    
28 greg 1.1 extern CUBE thescene; /* contains space boundaries */
29    
30 greg 2.12 #define MAXASET 511 /* maximum number of elements in ambient set */
31     OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */
32 greg 1.1
33 greg 2.12 double maxarad; /* maximum ambient radius */
34     double minarad; /* minimum ambient radius */
35 greg 1.1
36 greg 2.12 static AMBTREE atrunk; /* our ambient trunk node */
37 greg 1.1
38     static FILE *ambfp = NULL; /* ambient file pointer */
39 greg 2.16 static int nunflshed = 0; /* number of unflushed ambient values */
40 greg 1.1
41 greg 2.12 #define AMBFLUSH (BUFSIZ/AMBVALSIZ)
42 greg 2.6
43 greg 2.12 #define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL))
44 greg 1.1
45 greg 2.12 #define newambtree() (AMBTREE *)calloc(8, sizeof(AMBTREE))
46 greg 1.1
47 greg 2.7 extern long ftell(), lseek();
48 greg 2.16 static int initambfile(), avsave(), avinsert();
49 greg 1.1
50 greg 2.7
51 greg 2.3 setambres(ar) /* set ambient resolution */
52     int ar;
53     {
54     /* set min & max radii */
55     if (ar <= 0) {
56     minarad = 0.0;
57     maxarad = thescene.cusize / 2.0;
58     } else {
59     minarad = thescene.cusize / ar;
60     maxarad = 16.0 * minarad; /* heuristic */
61     if (maxarad > thescene.cusize / 2.0)
62     maxarad = thescene.cusize / 2.0;
63     }
64 greg 2.4 if (maxarad <= FTINY)
65     maxarad = .001;
66 greg 2.3 }
67    
68    
69 greg 1.1 setambient(afile) /* initialize calculation */
70     char *afile;
71     {
72 greg 2.9 long headlen;
73 greg 2.12 AMBVAL amb;
74 greg 2.3 /* init ambient limits */
75     setambres(ambres);
76     /* open ambient file */
77 greg 2.17 if (afile != NULL) {
78 greg 1.1 if ((ambfp = fopen(afile, "r+")) != NULL) {
79 greg 2.6 initambfile(0);
80 greg 2.9 headlen = ftell(ambfp);
81 greg 2.5 while (readambval(&amb, ambfp))
82 greg 1.1 avinsert(&amb, &atrunk, thescene.cuorg,
83     thescene.cusize);
84     /* align */
85 greg 2.9 fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
86 greg 2.10 } else if ((ambfp = fopen(afile, "w+")) != NULL)
87 greg 2.6 initambfile(1);
88     else {
89 greg 1.1 sprintf(errmsg, "cannot open ambient file \"%s\"",
90     afile);
91     error(SYSTEM, errmsg);
92 greg 1.8 }
93 greg 2.16 nunflshed++; /* lie */
94     ambsync();
95     }
96 greg 1.8 }
97    
98    
99     ambnotify(obj) /* record new modifier */
100 greg 2.12 OBJECT obj;
101 greg 1.8 {
102 greg 1.11 static int hitlimit = 0;
103 greg 2.12 register OBJREC *o = objptr(obj);
104 greg 1.8 register char **amblp;
105    
106 greg 1.11 if (hitlimit || !ismodifier(o->otype))
107 greg 1.8 return;
108     for (amblp = amblist; *amblp != NULL; amblp++)
109     if (!strcmp(o->oname, *amblp)) {
110 greg 1.11 if (ambset[0] >= MAXASET) {
111     error(WARNING, "too many modifiers in ambient list");
112     hitlimit++;
113     return; /* should this be fatal? */
114     }
115 greg 1.8 insertelem(ambset, obj);
116     return;
117 greg 1.1 }
118     }
119    
120    
121     ambient(acol, r) /* compute ambient component for ray */
122     COLOR acol;
123     register RAY *r;
124     {
125     static int rdepth = 0; /* ambient recursion */
126 greg 2.12 double d;
127 greg 1.1
128     if (ambdiv <= 0) /* no ambient calculation */
129     goto dumbamb;
130     /* check number of bounces */
131 greg 1.16 if (rdepth >= ambounce)
132 greg 1.1 goto dumbamb;
133     /* check ambient list */
134     if (ambincl != -1 && r->ro != NULL &&
135     ambincl != inset(ambset, r->ro->omod))
136     goto dumbamb;
137    
138     if (ambacc <= FTINY) { /* no ambient storage */
139 greg 1.16 rdepth++;
140     d = doambient(acol, r, r->rweight, NULL, NULL);
141     rdepth--;
142     if (d == 0.0)
143 greg 1.1 goto dumbamb;
144 greg 1.16 return;
145 greg 1.1 }
146     /* get ambient value */
147     setcolor(acol, 0.0, 0.0, 0.0);
148 greg 1.16 d = sumambient(acol, r, rdepth,
149     &atrunk, thescene.cuorg, thescene.cusize);
150     if (d > FTINY)
151     scalecolor(acol, 1.0/d);
152     else {
153     d = makeambient(acol, r, rdepth++);
154     rdepth--;
155     }
156     if (d > FTINY)
157     return;
158 greg 1.1 dumbamb: /* return global value */
159     copycolor(acol, ambval);
160     }
161    
162    
163     double
164 greg 1.16 sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */
165 greg 1.1 COLOR acol;
166     register RAY *r;
167 greg 1.16 int al;
168 greg 2.12 AMBTREE *at;
169 greg 1.1 FVECT c0;
170 greg 2.12 double s;
171 greg 1.1 {
172 greg 2.12 double d, e1, e2, wt, wsum;
173 greg 1.1 COLOR ct;
174     FVECT ck0;
175     int i;
176     register int j;
177 greg 2.12 register AMBVAL *av;
178 greg 1.7 /* do this node */
179 greg 1.1 wsum = 0.0;
180     for (av = at->alist; av != NULL; av = av->next) {
181     /*
182 greg 1.16 * Ambient level test.
183 greg 1.1 */
184 greg 1.16 if (av->lvl > al || av->weight < r->rweight-FTINY)
185 greg 1.1 continue;
186     /*
187     * Ambient radius test.
188     */
189     e1 = 0.0;
190     for (j = 0; j < 3; j++) {
191     d = av->pos[j] - r->rop[j];
192     e1 += d * d;
193     }
194     e1 /= av->rad * av->rad;
195     if (e1 > ambacc*ambacc*1.21)
196     continue;
197     /*
198     * Normal direction test.
199     */
200     e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight;
201     if (e2 < 0.0) e2 = 0.0;
202     if (e1 + e2 > ambacc*ambacc*1.21)
203     continue;
204     /*
205     * Ray behind test.
206     */
207     d = 0.0;
208     for (j = 0; j < 3; j++)
209     d += (r->rop[j] - av->pos[j]) *
210     (av->dir[j] + r->ron[j]);
211 greg 1.18 if (d*0.5 < -minarad*ambacc-.001)
212 greg 1.1 continue;
213     /*
214     * Jittering final test reduces image artifacts.
215     */
216     wt = sqrt(e1) + sqrt(e2);
217 greg 2.2 wt *= .9 + .2*urand(9015+samplendx);
218 greg 1.6 if (wt > ambacc)
219 greg 1.1 continue;
220     if (wt <= 1e-3)
221     wt = 1e3;
222     else
223     wt = 1.0 / wt;
224     wsum += wt;
225 greg 1.15 extambient(ct, av, r->rop, r->ron);
226 greg 1.1 scalecolor(ct, wt);
227     addcolor(acol, ct);
228 greg 1.7 }
229     if (at->kid == NULL)
230     return(wsum);
231     /* do children */
232     s *= 0.5;
233     for (i = 0; i < 8; i++) {
234     for (j = 0; j < 3; j++) {
235     ck0[j] = c0[j];
236     if (1<<j & i)
237     ck0[j] += s;
238     if (r->rop[j] < ck0[j] - OCTSCALE*s)
239     break;
240     if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
241     break;
242     }
243     if (j == 3)
244 greg 1.16 wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
245 greg 1.1 }
246     return(wsum);
247     }
248    
249    
250     double
251 greg 1.16 makeambient(acol, r, al) /* make a new ambient value */
252 greg 1.1 COLOR acol;
253     register RAY *r;
254 greg 1.16 int al;
255 greg 1.1 {
256 greg 2.12 AMBVAL amb;
257 greg 1.14 FVECT gp, gd;
258 greg 1.16 /* compute weight */
259     amb.weight = pow(AVGREFL, (double)al);
260 greg 1.17 if (r->rweight < 0.2*amb.weight) /* heuristic */
261 greg 1.16 amb.weight = r->rweight;
262     /* compute ambient */
263     amb.rad = doambient(acol, r, amb.weight, gp, gd);
264 greg 1.1 if (amb.rad == 0.0)
265     return(0.0);
266     /* store it */
267     VCOPY(amb.pos, r->rop);
268     VCOPY(amb.dir, r->ron);
269 greg 1.16 amb.lvl = al;
270 greg 1.1 copycolor(amb.val, acol);
271 greg 1.14 VCOPY(amb.gpos, gp);
272     VCOPY(amb.gdir, gd);
273 greg 1.1 /* insert into tree */
274 greg 2.7 avsave(&amb); /* and save to file */
275 greg 1.1 return(amb.rad);
276 greg 1.15 }
277    
278    
279     extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */
280     COLOR cr;
281 greg 2.12 register AMBVAL *ap;
282 greg 1.15 FVECT pv, nv;
283     {
284     FVECT v1, v2;
285     register int i;
286 greg 2.12 double d;
287 greg 1.15
288     d = 1.0; /* zeroeth order */
289     /* gradient due to translation */
290     for (i = 0; i < 3; i++)
291     d += ap->gpos[i]*(pv[i]-ap->pos[i]);
292     /* gradient due to rotation */
293     VCOPY(v1, ap->dir);
294     fcross(v2, v1, nv);
295     d += DOT(ap->gdir, v2);
296     if (d <= 0.0) {
297     setcolor(cr, 0.0, 0.0, 0.0);
298     return;
299     }
300     copycolor(cr, ap->val);
301     scalecolor(cr, d);
302 greg 1.1 }
303    
304    
305     static
306 greg 2.9 initambfile(creat) /* initialize ambient file */
307     int creat;
308     {
309     extern char *progname, *octname, VersionID[];
310    
311 greg 2.12 #ifdef MSDOS
312     setmode(fileno(ambfp), O_BINARY);
313     #endif
314 greg 2.9 setbuf(ambfp, bmalloc(BUFSIZ));
315     if (creat) { /* new file */
316     fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
317     progname, colval(ambval,RED),
318     colval(ambval,GRN), colval(ambval,BLU),
319     ambounce, ambacc);
320     fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
321     ambdiv, ambssamp, ambres,
322     octname==NULL ? "" : octname);
323     fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
324     fputformat(AMBFMT, ambfp);
325     putc('\n', ambfp);
326     putambmagic(ambfp);
327 greg 2.17 } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
328     error(USER, "bad ambient file");
329 greg 2.9 }
330    
331    
332     static
333 greg 2.7 avsave(av) /* insert and save an ambient value */
334 greg 2.12 AMBVAL *av;
335 greg 1.1 {
336 greg 2.7 avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
337 greg 1.1 if (ambfp == NULL)
338     return;
339 greg 2.5 if (writambval(av, ambfp) < 0)
340 greg 1.1 goto writerr;
341 greg 2.16 if (++nunflshed >= AMBFLUSH)
342 greg 2.7 if (ambsync() == EOF)
343 greg 1.1 goto writerr;
344     return;
345     writerr:
346 greg 2.17 error(SYSTEM, "error writing ambient file");
347 greg 1.1 }
348    
349    
350     static
351     avinsert(aval, at, c0, s) /* insert ambient value in a tree */
352 greg 2.12 AMBVAL *aval;
353 greg 1.1 register AMBTREE *at;
354     FVECT c0;
355 greg 2.12 double s;
356 greg 1.1 {
357     FVECT ck0;
358     int branch;
359 greg 2.12 register AMBVAL *av;
360 greg 1.1 register int i;
361    
362     if ((av = newambval()) == NULL)
363     goto memerr;
364 greg 1.9 copystruct(av, aval);
365 greg 1.1 VCOPY(ck0, c0);
366     while (s*(OCTSCALE/2) > av->rad*ambacc) {
367     if (at->kid == NULL)
368     if ((at->kid = newambtree()) == NULL)
369     goto memerr;
370     s *= 0.5;
371     branch = 0;
372     for (i = 0; i < 3; i++)
373     if (av->pos[i] > ck0[i] + s) {
374     ck0[i] += s;
375     branch |= 1 << i;
376     }
377     at = at->kid + branch;
378     }
379     av->next = at->alist;
380     at->alist = av;
381     return;
382     memerr:
383     error(SYSTEM, "out of memory in avinsert");
384 greg 2.7 }
385    
386    
387 greg 2.12 #ifdef NIX
388 greg 2.10
389 greg 2.16 int
390 greg 2.10 ambsync() /* flush ambient file */
391     {
392 greg 2.16 if (nunflshed == 0)
393     return(0);
394     nunflshed = 0;
395 greg 2.10 return(fflush(ambfp));
396     }
397    
398     #else
399    
400 greg 2.16 int
401 greg 2.7 ambsync() /* synchronize ambient file */
402     {
403     static FILE *ambinp = NULL;
404 greg 2.15 static long lastpos = -1;
405 greg 2.7 struct flock fls;
406 greg 2.15 long flen;
407 greg 2.12 AMBVAL avs;
408 greg 2.7 register int n;
409 greg 2.16
410     if (nunflshed == 0)
411     return(0);
412 greg 2.7 /* gain exclusive access */
413     fls.l_type = F_WRLCK;
414     fls.l_whence = 0;
415     fls.l_start = 0L;
416     fls.l_len = 0L;
417     if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
418     error(SYSTEM, "cannot lock ambient file");
419 greg 2.15 if (lastpos < 0) /* initializing */
420     goto syncend;
421 greg 2.7 /* see if file has grown */
422 greg 2.15 if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
423     error(SYSTEM, "cannot seek on ambient file");
424     if (n = flen - lastpos) { /* file has grown */
425 greg 2.17 if (ambinp == NULL) { /* use duplicate filedes */
426     ambinp = fdopen(dup(fileno(ambfp)), "r");
427 greg 2.14 if (ambinp == NULL)
428 greg 2.17 error(SYSTEM, "fdopen failed in ambsync");
429 greg 2.14 }
430 greg 2.15 if (fseek(ambinp, lastpos, 0) < 0)
431     error(SYSTEM, "fseek failed in ambsync");
432     while (n >= AMBVALSIZ) { /* load contributed values */
433 greg 2.7 readambval(&avs, ambinp);
434     avinsert(&avs,&atrunk,thescene.cuorg,thescene.cusize);
435 greg 2.15 n -= AMBVALSIZ;
436     }
437     if (n) /* alignment */
438 greg 2.10 lseek(fileno(ambfp), flen-n, 0);
439 greg 2.7 }
440 greg 2.15 syncend:
441 greg 2.7 n = fflush(ambfp); /* calls write() at last */
442 greg 2.15 lastpos = lseek(fileno(ambfp), 0L, 1);
443 greg 2.7 fls.l_type = F_UNLCK; /* release file */
444     fcntl(fileno(ambfp), F_SETLKW, &fls);
445 greg 2.16 nunflshed = 0;
446 greg 2.7 return(n);
447 greg 1.1 }
448 greg 2.10
449     #endif