1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* Accumulate ray contributions for a set of materials |
6 |
* Initialization and calculation routines |
7 |
*/ |
8 |
|
9 |
#include "rcontrib.h" |
10 |
#include "source.h" |
11 |
#include "otypes.h" |
12 |
#include "platform.h" |
13 |
|
14 |
CUBE thescene; /* our scene */ |
15 |
OBJECT nsceneobjs; /* number of objects in our scene */ |
16 |
|
17 |
int dimlist[MAXDIM]; /* sampling dimensions */ |
18 |
int ndims = 0; /* number of sampling dimensions */ |
19 |
int samplendx = 0; /* index for this sample */ |
20 |
|
21 |
static void trace_contrib(RAY *r); /* our trace callback */ |
22 |
void (*trace)() = trace_contrib; |
23 |
|
24 |
int do_irrad = 0; /* compute irradiance? */ |
25 |
|
26 |
int rand_samp = 0; /* pure Monte Carlo sampling? */ |
27 |
|
28 |
double dstrsrc = 0.0; /* square source distribution */ |
29 |
double shadthresh = .03; /* shadow threshold */ |
30 |
double shadcert = .75; /* shadow certainty */ |
31 |
int directrelay = 3; /* number of source relays */ |
32 |
int vspretest = 512; /* virtual source pretest density */ |
33 |
int directvis = 1; /* sources visible? */ |
34 |
double srcsizerat = .2; /* maximum ratio source size/dist. */ |
35 |
|
36 |
COLOR cextinction = BLKCOLOR; /* global extinction coefficient */ |
37 |
COLOR salbedo = BLKCOLOR; /* global scattering albedo */ |
38 |
double seccg = 0.; /* global scattering eccentricity */ |
39 |
double ssampdist = 0.; /* scatter sampling distance */ |
40 |
|
41 |
double specthresh = .15; /* specular sampling threshold */ |
42 |
double specjitter = 1.; /* specular sampling jitter */ |
43 |
|
44 |
int backvis = 1; /* back face visibility */ |
45 |
|
46 |
int maxdepth = 8; /* maximum recursion depth */ |
47 |
double minweight = 5e-4; /* minimum ray weight */ |
48 |
|
49 |
char *ambfile = NULL; /* ambient file name */ |
50 |
COLOR ambval = BLKCOLOR; /* ambient value */ |
51 |
int ambvwt = 0; /* initial weight for ambient value */ |
52 |
double ambacc = 0; /* ambient accuracy */ |
53 |
int ambres = 256; /* ambient resolution */ |
54 |
int ambdiv = 350; /* ambient divisions */ |
55 |
int ambssamp = 0; /* ambient super-samples */ |
56 |
int ambounce = 1; /* ambient bounces */ |
57 |
char *amblist[AMBLLEN+1]; /* ambient include/exclude list */ |
58 |
int ambincl = -1; /* include == 1, exclude == 0 */ |
59 |
|
60 |
int account; /* current accumulation count */ |
61 |
RNUMBER raysleft; /* number of rays left to trace */ |
62 |
long waitflush; /* how long until next flush */ |
63 |
|
64 |
int lastray = 0; /* last ray number sent */ |
65 |
int lastdone = 0; /* last ray output */ |
66 |
|
67 |
/* Close output stream and free record */ |
68 |
static void |
69 |
closestream(void *p) |
70 |
{ |
71 |
STREAMOUT *sop = (STREAMOUT *)p; |
72 |
int status = 0; |
73 |
if (sop->outpipe) |
74 |
status = pclose(sop->ofp); |
75 |
else if (sop->ofp != stdout) |
76 |
status = fclose(sop->ofp); |
77 |
if (status) |
78 |
error(SYSTEM, "error closing output stream"); |
79 |
free(p); |
80 |
} |
81 |
|
82 |
LUTAB ofiletab = LU_SINIT(free,closestream); /* output file table */ |
83 |
|
84 |
#define OF_MODIFIER 01 |
85 |
#define OF_BIN 02 |
86 |
|
87 |
static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); } |
88 |
|
89 |
LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ |
90 |
|
91 |
static OBJECT traset[MAXTSET+1]={0}; /* trace include set */ |
92 |
|
93 |
/************************** INITIALIZATION ROUTINES ***********************/ |
94 |
|
95 |
void |
96 |
tranotify( /* record new modifier */ |
97 |
OBJECT obj |
98 |
) |
99 |
{ |
100 |
static int hitlimit = 0; |
101 |
OBJREC *o = objptr(obj); |
102 |
int i; |
103 |
|
104 |
if (obj == OVOID) { /* starting over */ |
105 |
traset[0] = 0; |
106 |
hitlimit = 0; |
107 |
return; |
108 |
} |
109 |
if (hitlimit || !ismodifier(o->otype)) |
110 |
return; |
111 |
for (i = nmods; i-- > 0; ) |
112 |
if (!strcmp(o->oname, modname[i])) { |
113 |
if (traset[0] >= MAXTSET) { |
114 |
error(WARNING, "too many same-named modifiers"); |
115 |
hitlimit++; |
116 |
return; /* should this be fatal? */ |
117 |
} |
118 |
insertelem(traset, obj); |
119 |
break; |
120 |
} |
121 |
} |
122 |
|
123 |
|
124 |
char * |
125 |
formstr( /* return format identifier */ |
126 |
int f |
127 |
) |
128 |
{ |
129 |
switch (f) { |
130 |
case 'a': return("ascii"); |
131 |
case 'f': return("float"); |
132 |
case 'd': return("double"); |
133 |
case 'c': return(COLRFMT); |
134 |
} |
135 |
return("unknown"); |
136 |
} |
137 |
|
138 |
|
139 |
/* Add modifier to our list to track */ |
140 |
MODCONT * |
141 |
addmodifier(char *modn, char *outf, char *binv, int bincnt) |
142 |
{ |
143 |
LUENT *lep = lu_find(&modconttab,modn); |
144 |
MODCONT *mp; |
145 |
EPNODE *ebinv; |
146 |
int i; |
147 |
|
148 |
if (lep->data != NULL) { |
149 |
sprintf(errmsg, "duplicate modifier '%s'", modn); |
150 |
error(USER, errmsg); |
151 |
} |
152 |
if (nmods >= MAXMODLIST) |
153 |
error(INTERNAL, "too many modifiers"); |
154 |
modname[nmods++] = modn; /* XXX assumes static string */ |
155 |
lep->key = modn; /* XXX assumes static string */ |
156 |
if (binv == NULL) |
157 |
binv = "0"; /* use single bin if unspecified */ |
158 |
ebinv = eparse(binv); |
159 |
if (ebinv->type == NUM) { /* check value if constant */ |
160 |
bincnt = (int)(evalue(ebinv) + 1.5); |
161 |
if (bincnt != 1) { |
162 |
sprintf(errmsg, "illegal non-zero constant for bin (%s)", |
163 |
binv); |
164 |
error(USER, errmsg); |
165 |
} |
166 |
} else if (bincnt <= 0) { |
167 |
sprintf(errmsg, |
168 |
"unspecified or illegal bin count for modifier '%s'", |
169 |
modn); |
170 |
error(USER, errmsg); |
171 |
} |
172 |
/* initialize results holder */ |
173 |
mp = (MODCONT *)malloc(sizeof(MODCONT)+sizeof(DCOLOR)*(bincnt-1)); |
174 |
if (mp == NULL) |
175 |
error(SYSTEM, "out of memory in addmodifier"); |
176 |
mp->outspec = outf; /* XXX assumes static string */ |
177 |
mp->modname = modn; /* XXX assumes static string */ |
178 |
mp->binv = ebinv; |
179 |
mp->nbins = bincnt; |
180 |
memset(mp->cbin, 0, sizeof(DCOLOR)*bincnt); |
181 |
/* allocate output streams */ |
182 |
for (i = bincnt; i-- > 0; ) |
183 |
getostream(mp->outspec, mp->modname, i, 1); |
184 |
lep->data = (char *)mp; |
185 |
return(mp); |
186 |
} |
187 |
|
188 |
|
189 |
/* add modifiers from a file list */ |
190 |
void |
191 |
addmodfile(char *fname, char *outf, char *binv, int bincnt) |
192 |
{ |
193 |
char *mname[MAXMODLIST]; |
194 |
int i; |
195 |
/* find the file & store strings */ |
196 |
if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) { |
197 |
sprintf(errmsg, "cannot find modifier file '%s'", fname); |
198 |
error(SYSTEM, errmsg); |
199 |
} |
200 |
for (i = 0; mname[i]; i++) /* add each one */ |
201 |
addmodifier(mname[i], outf, binv, bincnt); |
202 |
} |
203 |
|
204 |
|
205 |
void |
206 |
quit( /* quit program */ |
207 |
int code |
208 |
) |
209 |
{ |
210 |
if (nchild > 0) /* close children if any */ |
211 |
end_children(); |
212 |
exit(code); |
213 |
} |
214 |
|
215 |
|
216 |
/* Initialize our process(es) */ |
217 |
static void |
218 |
rcinit() |
219 |
{ |
220 |
int i; |
221 |
|
222 |
if (nproc > MAXPROCESS) |
223 |
sprintf(errmsg, "too many processes requested -- reducing to %d", |
224 |
nproc = MAXPROCESS); |
225 |
|
226 |
if ((nproc > 1) & (accumulate <= 0)) |
227 |
zero_record(0); /* prime our queue to accumulate */ |
228 |
|
229 |
if (recover) { /* recover previous output? */ |
230 |
if (accumulate <= 0) { |
231 |
reload_output(); |
232 |
if ((nproc > 1) & (accumulate <= 0)) |
233 |
queue_modifiers(); |
234 |
} else |
235 |
recover_output(); |
236 |
} |
237 |
if (yres > 0) { /* set up flushing & ray counts */ |
238 |
if (xres > 0) |
239 |
raysleft = (RNUMBER)xres*yres; |
240 |
else |
241 |
raysleft = yres; |
242 |
} else |
243 |
raysleft = 0; |
244 |
if ((account = accumulate) > 1) |
245 |
raysleft *= accumulate; |
246 |
waitflush = (yres > 0) & (xres > 1) ? 0 : xres; |
247 |
/* tracing to sources as well */ |
248 |
for (i = 0; i < nsources; i++) |
249 |
source[i].sflags |= SFOLLOW; |
250 |
|
251 |
if (nproc == 1 || in_rchild()) /* single process or child */ |
252 |
return; /* return to main processing loop */ |
253 |
|
254 |
parental_loop(); /* else run controller */ |
255 |
quit(0); /* parent musn't return! */ |
256 |
} |
257 |
|
258 |
/************************** MAIN CALCULATION PROCESS ***********************/ |
259 |
|
260 |
/* Our trace call to sum contributions */ |
261 |
static void |
262 |
trace_contrib(RAY *r) |
263 |
{ |
264 |
MODCONT *mp; |
265 |
int bn; |
266 |
RREAL contr[3]; |
267 |
|
268 |
if (r->ro == NULL || !inset(traset, r->ro->omod)) |
269 |
return; |
270 |
|
271 |
mp = (MODCONT *)lu_find(&modconttab,objptr(r->ro->omod)->oname)->data; |
272 |
|
273 |
if (mp == NULL) |
274 |
error(CONSISTENCY, "unexpected modifier in trace_contrib()"); |
275 |
|
276 |
worldfunc(RCCONTEXT, r); /* get bin number */ |
277 |
bn = (int)(evalue(mp->binv) + .5); |
278 |
if ((bn < 0) | (bn >= mp->nbins)) { |
279 |
error(WARNING, "bad bin number (ignored)"); |
280 |
return; |
281 |
} |
282 |
raycontrib(contr, r, PRIMARY); |
283 |
if (contrib) |
284 |
multcolor(contr, r->rcol); |
285 |
addcolor(mp->cbin[bn], contr); |
286 |
} |
287 |
|
288 |
|
289 |
static void |
290 |
rayirrad( /* compute irradiance rather than radiance */ |
291 |
RAY *r |
292 |
) |
293 |
{ |
294 |
r->rot = 1e-5; /* pretend we hit surface */ |
295 |
VSUM(r->rop, r->rorg, r->rdir, r->rot); |
296 |
r->ron[0] = -r->rdir[0]; |
297 |
r->ron[1] = -r->rdir[1]; |
298 |
r->ron[2] = -r->rdir[2]; |
299 |
r->rod = 1.0; |
300 |
/* compute result */ |
301 |
r->revf = raytrace; |
302 |
(*ofun[Lamb.otype].funp)(&Lamb, r); |
303 |
r->revf = rayirrad; |
304 |
} |
305 |
|
306 |
|
307 |
/* Evaluate ray contributions */ |
308 |
static void |
309 |
eval_ray(FVECT org, FVECT dir, double dmax) |
310 |
{ |
311 |
RAY thisray; |
312 |
/* set up ray */ |
313 |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
314 |
if (imm_irrad) { |
315 |
VSUM(thisray.rorg, org, dir, 1.1e-4); |
316 |
thisray.rdir[0] = -dir[0]; |
317 |
thisray.rdir[1] = -dir[1]; |
318 |
thisray.rdir[2] = -dir[2]; |
319 |
thisray.rmax = 0.0; |
320 |
thisray.revf = rayirrad; |
321 |
} else { |
322 |
VCOPY(thisray.rorg, org); |
323 |
VCOPY(thisray.rdir, dir); |
324 |
thisray.rmax = dmax; |
325 |
} |
326 |
samplendx++; /* call ray evaluation */ |
327 |
rayvalue(&thisray); |
328 |
} |
329 |
|
330 |
|
331 |
/* Accumulate and/or output ray contributions (child or only process) */ |
332 |
static void |
333 |
done_contrib() |
334 |
{ |
335 |
MODCONT *mp; |
336 |
int i; |
337 |
|
338 |
if (account <= 0 || --account) |
339 |
return; /* not time yet */ |
340 |
|
341 |
for (i = 0; i < nmods; i++) { /* output records & clear */ |
342 |
mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; |
343 |
mod_output(mp); |
344 |
memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); |
345 |
} |
346 |
end_record(); /* end lines & flush if time */ |
347 |
|
348 |
account = accumulate; /* reset accumulation counter */ |
349 |
} |
350 |
|
351 |
|
352 |
/* Principal calculation loop (called by main) */ |
353 |
void |
354 |
rcontrib() |
355 |
{ |
356 |
static int ignore_warning_given = 0; |
357 |
FVECT orig, direc; |
358 |
double d; |
359 |
/* initialize & fork */ |
360 |
rcinit(); |
361 |
/* load rays from stdin & process */ |
362 |
#ifdef getc_unlocked |
363 |
flockfile(stdin); /* avoid lock/unlock overhead */ |
364 |
#endif |
365 |
while (getvec(orig) == 0 && getvec(direc) == 0) { |
366 |
d = normalize(direc); |
367 |
if ((d == 0.0) & (accumulate != 1)) { |
368 |
if (!ignore_warning_given++) |
369 |
error(WARNING, |
370 |
"dummy ray(s) ignored during accumulation\n"); |
371 |
continue; |
372 |
} |
373 |
if (lastray+1 < lastray) |
374 |
lastray = lastdone = 0; |
375 |
++lastray; |
376 |
if (d == 0.0) { /* zero ==> flush */ |
377 |
if ((yres <= 0) | (xres <= 0)) |
378 |
waitflush = 1; /* flush right after */ |
379 |
} else { /* else compute */ |
380 |
eval_ray(orig, direc, lim_dist ? d : 0.0); |
381 |
} |
382 |
done_contrib(); /* accumulate/output */ |
383 |
++lastdone; |
384 |
if (raysleft && !--raysleft) |
385 |
break; /* preemptive EOI */ |
386 |
} |
387 |
if (accumulate <= 0 || account < accumulate) { |
388 |
if (account < accumulate) { |
389 |
error(WARNING, "partial accumulation in final record"); |
390 |
accumulate -= account; |
391 |
} |
392 |
account = 1; /* output accumulated totals */ |
393 |
done_contrib(); |
394 |
} |
395 |
if (raysleft) |
396 |
error(USER, "unexpected EOF on input"); |
397 |
lu_done(&ofiletab); /* close output files */ |
398 |
} |