ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
Revision: 1.14
Committed: Fri Jun 7 10:04:29 1991 UTC (32 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.13: +13 -225 lines
Log Message:
created ambient.h and ambcomp.c files

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 wsum;
104
105 rdepth++; /* increment level */
106
107 if (ambdiv <= 0) /* no ambient calculation */
108 goto dumbamb;
109 /* check number of bounces */
110 if (rdepth > ambounce)
111 goto dumbamb;
112 /* check ambient list */
113 if (ambincl != -1 && r->ro != NULL &&
114 ambincl != inset(ambset, r->ro->omod))
115 goto dumbamb;
116
117 if (ambacc <= FTINY) { /* no ambient storage */
118 if (doambient(acol, r, NULL, NULL) == 0.0)
119 goto dumbamb;
120 goto done;
121 }
122 /* get ambient value */
123 setcolor(acol, 0.0, 0.0, 0.0);
124 wsum = sumambient(acol, r, &atrunk, thescene.cuorg, thescene.cusize);
125 if (wsum > FTINY)
126 scalecolor(acol, 1.0/wsum);
127 else if (makeambient(acol, r) == 0.0)
128 goto dumbamb;
129 goto done;
130
131 dumbamb: /* return global value */
132 copycolor(acol, ambval);
133 done: /* must finish here! */
134 rdepth--;
135 }
136
137
138 double
139 sumambient(acol, r, at, c0, s) /* get interpolated ambient value */
140 COLOR acol;
141 register RAY *r;
142 AMBTREE *at;
143 FVECT c0;
144 double s;
145 {
146 extern double sqrt();
147 double d, e1, e2, wt, wsum;
148 COLOR ct;
149 FVECT ck0;
150 int i;
151 register int j;
152 register AMBVAL *av;
153 /* do this node */
154 wsum = 0.0;
155 for (av = at->alist; av != NULL; av = av->next) {
156 /*
157 * Ray strength test.
158 */
159 if (av->lvl > r->rlvl || av->weight < r->rweight-FTINY)
160 continue;
161 /*
162 * Ambient radius test.
163 */
164 e1 = 0.0;
165 for (j = 0; j < 3; j++) {
166 d = av->pos[j] - r->rop[j];
167 e1 += d * d;
168 }
169 e1 /= av->rad * av->rad;
170 if (e1 > ambacc*ambacc*1.21)
171 continue;
172 /*
173 * Normal direction test.
174 */
175 e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight;
176 if (e2 < 0.0) e2 = 0.0;
177 if (e1 + e2 > ambacc*ambacc*1.21)
178 continue;
179 /*
180 * Ray behind test.
181 */
182 d = 0.0;
183 for (j = 0; j < 3; j++)
184 d += (r->rop[j] - av->pos[j]) *
185 (av->dir[j] + r->ron[j]);
186 if (d*0.5 < -minarad*ambacc)
187 continue;
188 /*
189 * Jittering final test reduces image artifacts.
190 */
191 wt = sqrt(e1) + sqrt(e2);
192 wt *= .9 + .2*frandom();
193 if (wt > ambacc)
194 continue;
195 if (wt <= 1e-3)
196 wt = 1e3;
197 else
198 wt = 1.0 / wt;
199 wsum += wt;
200 copycolor(ct, av->val);
201 scalecolor(ct, wt);
202 addcolor(acol, ct);
203 }
204 if (at->kid == NULL)
205 return(wsum);
206 /* do children */
207 s *= 0.5;
208 for (i = 0; i < 8; i++) {
209 for (j = 0; j < 3; j++) {
210 ck0[j] = c0[j];
211 if (1<<j & i)
212 ck0[j] += s;
213 if (r->rop[j] < ck0[j] - OCTSCALE*s)
214 break;
215 if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
216 break;
217 }
218 if (j == 3)
219 wsum += sumambient(acol, r, at->kid+i, ck0, s);
220 }
221 return(wsum);
222 }
223
224
225 double
226 makeambient(acol, r) /* make a new ambient value */
227 COLOR acol;
228 register RAY *r;
229 {
230 AMBVAL amb;
231 FVECT gp, gd;
232
233 amb.rad = doambient(acol, r, gp, gd); /* compute ambient */
234 if (amb.rad == 0.0)
235 return(0.0);
236 /* store it */
237 VCOPY(amb.pos, r->rop);
238 VCOPY(amb.dir, r->ron);
239 amb.lvl = r->rlvl;
240 amb.weight = r->rweight;
241 copycolor(amb.val, acol);
242 VCOPY(amb.gpos, gp);
243 VCOPY(amb.gdir, gd);
244 /* insert into tree */
245 avinsert(&amb, &atrunk, thescene.cuorg, thescene.cusize);
246 avsave(&amb); /* write to file */
247 return(amb.rad);
248 }
249
250
251 static
252 avsave(av) /* save an ambient value */
253 AMBVAL *av;
254 {
255 #ifdef AMBFLUSH
256 static int nunflshed = 0;
257 #endif
258 if (ambfp == NULL)
259 return;
260 if (fwrite((char *)av, sizeof(AMBVAL), 1, ambfp) != 1)
261 goto writerr;
262 #ifdef AMBFLUSH
263 if (++nunflshed >= AMBFLUSH) {
264 if (fflush(ambfp) == EOF)
265 goto writerr;
266 nunflshed = 0;
267 }
268 #endif
269 return;
270 writerr:
271 error(SYSTEM, "error writing ambient file");
272 }
273
274
275 static
276 avinsert(aval, at, c0, s) /* insert ambient value in a tree */
277 AMBVAL *aval;
278 register AMBTREE *at;
279 FVECT c0;
280 double s;
281 {
282 FVECT ck0;
283 int branch;
284 register AMBVAL *av;
285 register int i;
286
287 if ((av = newambval()) == NULL)
288 goto memerr;
289 copystruct(av, aval);
290 VCOPY(ck0, c0);
291 while (s*(OCTSCALE/2) > av->rad*ambacc) {
292 if (at->kid == NULL)
293 if ((at->kid = newambtree()) == NULL)
294 goto memerr;
295 s *= 0.5;
296 branch = 0;
297 for (i = 0; i < 3; i++)
298 if (av->pos[i] > ck0[i] + s) {
299 ck0[i] += s;
300 branch |= 1 << i;
301 }
302 at = at->kid + branch;
303 }
304 av->next = at->alist;
305 at->alist = av;
306 return;
307 memerr:
308 error(SYSTEM, "out of memory in avinsert");
309 }