6 |
|
* Initialization and calculation routines |
7 |
|
*/ |
8 |
|
|
9 |
+ |
#include "copyright.h" |
10 |
+ |
|
11 |
|
#include "rcontrib.h" |
10 |
– |
#include "source.h" |
12 |
|
#include "otypes.h" |
13 |
< |
#include "platform.h" |
13 |
> |
#include "source.h" |
14 |
|
|
15 |
|
char *shm_boundary = NULL; /* boundary of shared memory */ |
16 |
|
|
26 |
|
|
27 |
|
int do_irrad = 0; /* compute irradiance? */ |
28 |
|
|
29 |
< |
int rand_samp = 0; /* pure Monte Carlo sampling? */ |
29 |
> |
int rand_samp = 1; /* pure Monte Carlo sampling? */ |
30 |
|
|
31 |
< |
double dstrsrc = 0.0; /* square source distribution */ |
32 |
< |
double shadthresh = .03; /* shadow threshold */ |
31 |
> |
double dstrsrc = 0.9; /* square source distribution */ |
32 |
> |
double shadthresh = 0.; /* shadow threshold */ |
33 |
|
double shadcert = .75; /* shadow certainty */ |
34 |
|
int directrelay = 3; /* number of source relays */ |
35 |
|
int vspretest = 512; /* virtual source pretest density */ |
41 |
|
double seccg = 0.; /* global scattering eccentricity */ |
42 |
|
double ssampdist = 0.; /* scatter sampling distance */ |
43 |
|
|
44 |
< |
double specthresh = .15; /* specular sampling threshold */ |
44 |
> |
double specthresh = .02; /* specular sampling threshold */ |
45 |
|
double specjitter = 1.; /* specular sampling jitter */ |
46 |
|
|
47 |
|
int backvis = 1; /* back face visibility */ |
48 |
|
|
49 |
< |
int maxdepth = 8; /* maximum recursion depth */ |
50 |
< |
double minweight = 5e-4; /* minimum ray weight */ |
49 |
> |
int maxdepth = -10; /* maximum recursion depth */ |
50 |
> |
double minweight = 2e-3; /* minimum ray weight */ |
51 |
|
|
52 |
|
char *ambfile = NULL; /* ambient file name */ |
53 |
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
54 |
|
int ambvwt = 0; /* initial weight for ambient value */ |
55 |
< |
double ambacc = 0; /* ambient accuracy */ |
55 |
> |
double ambacc = 0.; /* ambient accuracy */ |
56 |
|
int ambres = 256; /* ambient resolution */ |
57 |
|
int ambdiv = 350; /* ambient divisions */ |
58 |
|
int ambssamp = 0; /* ambient super-samples */ |
64 |
|
RNUMBER raysleft; /* number of rays left to trace */ |
65 |
|
long waitflush; /* how long until next flush */ |
66 |
|
|
67 |
< |
int lastray = 0; /* last ray number sent */ |
68 |
< |
int lastdone = 0; /* last ray output */ |
67 |
> |
RNUMBER lastray = 0; /* last ray number sent */ |
68 |
> |
RNUMBER lastdone = 0; /* last ray output */ |
69 |
|
|
69 |
– |
/* Close output stream and free record */ |
70 |
– |
static void |
71 |
– |
closestream(void *p) |
72 |
– |
{ |
73 |
– |
STREAMOUT *sop = (STREAMOUT *)p; |
74 |
– |
int status = 0; |
75 |
– |
if (sop->outpipe) |
76 |
– |
status = pclose(sop->ofp); |
77 |
– |
else if (sop->ofp != stdout) |
78 |
– |
status = fclose(sop->ofp); |
79 |
– |
if (status) |
80 |
– |
error(SYSTEM, "error closing output stream"); |
81 |
– |
free(p); |
82 |
– |
} |
83 |
– |
|
84 |
– |
LUTAB ofiletab = LU_SINIT(free,closestream); /* output file table */ |
85 |
– |
|
86 |
– |
#define OF_MODIFIER 01 |
87 |
– |
#define OF_BIN 02 |
88 |
– |
|
70 |
|
static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); } |
71 |
|
|
72 |
|
LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ |
73 |
|
|
93 |
– |
static OBJECT traset[MAXTSET+1]={0}; /* trace include set */ |
94 |
– |
|
74 |
|
/************************** INITIALIZATION ROUTINES ***********************/ |
75 |
|
|
97 |
– |
void |
98 |
– |
tranotify( /* record new modifier */ |
99 |
– |
OBJECT obj |
100 |
– |
) |
101 |
– |
{ |
102 |
– |
static int hitlimit = 0; |
103 |
– |
OBJREC *o = objptr(obj); |
104 |
– |
int i; |
105 |
– |
|
106 |
– |
if (obj == OVOID) { /* starting over */ |
107 |
– |
traset[0] = 0; |
108 |
– |
hitlimit = 0; |
109 |
– |
return; |
110 |
– |
} |
111 |
– |
if (hitlimit || !ismodifier(o->otype)) |
112 |
– |
return; |
113 |
– |
for (i = nmods; i-- > 0; ) |
114 |
– |
if (!strcmp(o->oname, modname[i])) { |
115 |
– |
if (traset[0] >= MAXTSET) { |
116 |
– |
error(WARNING, "too many same-named modifiers"); |
117 |
– |
hitlimit++; |
118 |
– |
return; /* should this be fatal? */ |
119 |
– |
} |
120 |
– |
insertelem(traset, obj); |
121 |
– |
break; |
122 |
– |
} |
123 |
– |
} |
124 |
– |
|
125 |
– |
|
76 |
|
char * |
77 |
|
formstr( /* return format identifier */ |
78 |
|
int f |
90 |
|
|
91 |
|
/* Add modifier to our list to track */ |
92 |
|
MODCONT * |
93 |
< |
addmodifier(char *modn, char *outf, char *binv, int bincnt) |
93 |
> |
addmodifier(char *modn, char *outf, char *prms, char *binv, int bincnt) |
94 |
|
{ |
95 |
|
LUENT *lep = lu_find(&modconttab,modn); |
96 |
|
MODCONT *mp; |
101 |
|
sprintf(errmsg, "duplicate modifier '%s'", modn); |
102 |
|
error(USER, errmsg); |
103 |
|
} |
104 |
< |
if (nmods >= MAXMODLIST) |
105 |
< |
error(INTERNAL, "too many modifiers"); |
104 |
> |
if (nmods >= MAXMODLIST) { |
105 |
> |
sprintf(errmsg, "too many modifiers (%d limit)", MAXMODLIST); |
106 |
> |
error(INTERNAL, errmsg); |
107 |
> |
} |
108 |
> |
if (!strcmp(modn, VOIDID)) { |
109 |
> |
sprintf(errmsg, "cannot track '%s' modifier", VOIDID); |
110 |
> |
error(USER, errmsg); |
111 |
> |
} |
112 |
|
modname[nmods++] = modn; /* XXX assumes static string */ |
113 |
|
lep->key = modn; /* XXX assumes static string */ |
114 |
|
if (binv == NULL) |
133 |
|
error(SYSTEM, "out of memory in addmodifier"); |
134 |
|
mp->outspec = outf; /* XXX assumes static string */ |
135 |
|
mp->modname = modn; /* XXX assumes static string */ |
136 |
+ |
mp->params = prms; /* XXX assumes static string */ |
137 |
|
mp->binv = ebinv; |
138 |
+ |
mp->bin0 = 0; |
139 |
|
mp->nbins = bincnt; |
140 |
|
memset(mp->cbin, 0, sizeof(DCOLOR)*bincnt); |
141 |
< |
/* allocate output streams */ |
142 |
< |
for (i = bincnt; i-- > 0; ) |
143 |
< |
getostream(mp->outspec, mp->modname, i, 1); |
141 |
> |
/* figure out starting bin */ |
142 |
> |
while (!getostream(mp->outspec, mp->modname, mp->bin0, 1)) |
143 |
> |
mp->bin0++; |
144 |
> |
/* allocate other output streams */ |
145 |
> |
for (i = 0; ++i < mp->nbins; ) |
146 |
> |
getostream(mp->outspec, mp->modname, mp->bin0+i, 1); |
147 |
|
lep->data = (char *)mp; |
148 |
|
return(mp); |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
< |
/* add modifiers from a file list */ |
152 |
> |
/* Add modifiers from a file list */ |
153 |
|
void |
154 |
< |
addmodfile(char *fname, char *outf, char *binv, int bincnt) |
154 |
> |
addmodfile(char *fname, char *outf, char *prms, char *binv, int bincnt) |
155 |
|
{ |
156 |
|
char *mname[MAXMODLIST]; |
157 |
|
int i; |
158 |
|
/* find the file & store strings */ |
159 |
< |
if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) { |
159 |
> |
i = wordfile(mname, MAXMODLIST, getpath(fname, getrlibpath(), R_OK)); |
160 |
> |
if (i < 0) { |
161 |
|
sprintf(errmsg, "cannot find modifier file '%s'", fname); |
162 |
|
error(SYSTEM, errmsg); |
163 |
|
} |
164 |
+ |
if (i >= MAXMODLIST-1) { |
165 |
+ |
sprintf(errmsg, "too many modifiers (%d limit) in file '%s'", |
166 |
+ |
MAXMODLIST-1, fname); |
167 |
+ |
error(INTERNAL, errmsg); |
168 |
+ |
} |
169 |
|
for (i = 0; mname[i]; i++) /* add each one */ |
170 |
< |
addmodifier(mname[i], outf, binv, bincnt); |
170 |
> |
addmodifier(mname[i], outf, prms, binv, bincnt); |
171 |
|
} |
172 |
|
|
173 |
|
|
177 |
|
) |
178 |
|
{ |
179 |
|
if (nchild > 0) /* close children if any */ |
180 |
< |
end_children(); |
180 |
> |
end_children(code != 0); |
181 |
|
exit(code); |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
|
/* Initialize our process(es) */ |
186 |
|
static void |
187 |
< |
rcinit() |
187 |
> |
rcinit(void) |
188 |
|
{ |
189 |
|
int i; |
190 |
|
|
196 |
|
/* set shared memory boundary */ |
197 |
|
shm_boundary = strcpy((char *)malloc(16), "SHM_BOUNDARY"); |
198 |
|
} |
199 |
< |
if ((nproc > 1) & (accumulate <= 0)) |
200 |
< |
put_zero_record(0); /* prime our queue to accumulate */ |
234 |
< |
|
235 |
< |
if (recover) { /* recover previous output? */ |
236 |
< |
if (accumulate <= 0) { |
237 |
< |
reload_output(); |
238 |
< |
if (nproc > 1) |
239 |
< |
queue_modifiers(); |
240 |
< |
} else |
241 |
< |
recover_output(); |
242 |
< |
} |
199 |
> |
for (i = 0; i < nsources; i++) /* tracing to sources as well */ |
200 |
> |
source[i].sflags |= SFOLLOW; |
201 |
|
if (yres > 0) { /* set up flushing & ray counts */ |
202 |
|
if (xres > 0) |
203 |
|
raysleft = (RNUMBER)xres*yres; |
208 |
|
if ((account = accumulate) > 1) |
209 |
|
raysleft *= accumulate; |
210 |
|
waitflush = (yres > 0) & (xres > 1) ? 0 : xres; |
253 |
– |
/* tracing to sources as well */ |
254 |
– |
for (i = 0; i < nsources; i++) |
255 |
– |
source[i].sflags |= SFOLLOW; |
211 |
|
|
212 |
< |
if (nproc == 1 || in_rchild()) /* single process or child */ |
212 |
> |
if (nproc > 1 && in_rchild()) /* forked child? */ |
213 |
|
return; /* return to main processing loop */ |
214 |
|
|
215 |
< |
parental_loop(); /* else run controller */ |
215 |
> |
if (recover) { /* recover previous output? */ |
216 |
> |
if (accumulate <= 0) |
217 |
> |
reload_output(); |
218 |
> |
else |
219 |
> |
recover_output(); |
220 |
> |
} |
221 |
> |
if (nproc == 1) /* single process? */ |
222 |
> |
return; |
223 |
> |
/* else run appropriate controller */ |
224 |
> |
if (accumulate <= 0) |
225 |
> |
feeder_loop(); |
226 |
> |
else |
227 |
> |
parental_loop(); |
228 |
|
quit(0); /* parent musn't return! */ |
229 |
|
} |
230 |
|
|
235 |
|
trace_contrib(RAY *r) |
236 |
|
{ |
237 |
|
MODCONT *mp; |
238 |
+ |
double bval; |
239 |
|
int bn; |
240 |
|
RREAL contr[3]; |
241 |
|
|
242 |
< |
if (r->ro == NULL || !inset(traset, r->ro->omod)) |
242 |
> |
if (r->ro == NULL || r->ro->omod == OVOID) |
243 |
|
return; |
244 |
+ |
/* shadow ray not on source? */ |
245 |
+ |
if (r->rsrc >= 0 && source[r->rsrc].so != r->ro) |
246 |
+ |
return; |
247 |
|
|
248 |
|
mp = (MODCONT *)lu_find(&modconttab,objptr(r->ro->omod)->oname)->data; |
249 |
|
|
250 |
< |
if (mp == NULL) |
251 |
< |
error(CONSISTENCY, "unexpected modifier in trace_contrib()"); |
250 |
> |
if (mp == NULL) /* not in our list? */ |
251 |
> |
return; |
252 |
|
|
253 |
< |
worldfunc(RCCONTEXT, r); /* get bin number */ |
254 |
< |
bn = (int)(evalue(mp->binv) + .5); |
255 |
< |
if ((bn < 0) | (bn >= mp->nbins)) { |
256 |
< |
error(WARNING, "bad bin number (ignored)"); |
253 |
> |
worldfunc(RCCONTEXT, r); /* else set context */ |
254 |
> |
set_eparams((char *)mp->params); |
255 |
> |
if ((bval = evalue(mp->binv)) <= -.5) /* and get bin number */ |
256 |
> |
return; /* silently ignore negatives */ |
257 |
> |
if ((bn = (int)(bval + .5)) >= mp->nbins) { |
258 |
> |
sprintf(errmsg, "bad bin number (%d ignored)", bn); |
259 |
> |
error(WARNING, errmsg); |
260 |
|
return; |
261 |
|
} |
262 |
< |
raycontrib(contr, r, PRIMARY); |
262 |
> |
raycontrib(contr, r, PRIMARY); /* compute coefficient */ |
263 |
|
if (contrib) |
264 |
< |
multcolor(contr, r->rcol); |
264 |
> |
multcolor(contr, r->rcol); /* -> contribution */ |
265 |
|
addcolor(mp->cbin[bn], contr); |
266 |
|
} |
267 |
|
|
268 |
|
|
269 |
+ |
/* Evaluate irradiance contributions */ |
270 |
|
static void |
271 |
< |
rayirrad( /* compute irradiance rather than radiance */ |
297 |
< |
RAY *r |
298 |
< |
) |
271 |
> |
eval_irrad(FVECT org, FVECT dir) |
272 |
|
{ |
273 |
< |
r->rot = 1e-5; /* pretend we hit surface */ |
274 |
< |
VSUM(r->rop, r->rorg, r->rdir, r->rot); |
275 |
< |
r->ron[0] = -r->rdir[0]; |
276 |
< |
r->ron[1] = -r->rdir[1]; |
277 |
< |
r->ron[2] = -r->rdir[2]; |
278 |
< |
r->rod = 1.0; |
279 |
< |
/* compute result */ |
280 |
< |
r->revf = raytrace; |
281 |
< |
(*ofun[Lamb.otype].funp)(&Lamb, r); |
282 |
< |
r->revf = rayirrad; |
273 |
> |
RAY thisray; |
274 |
> |
|
275 |
> |
VSUM(thisray.rorg, org, dir, 1.1e-4); |
276 |
> |
thisray.rdir[0] = -dir[0]; |
277 |
> |
thisray.rdir[1] = -dir[1]; |
278 |
> |
thisray.rdir[2] = -dir[2]; |
279 |
> |
thisray.rmax = 0.0; |
280 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
281 |
> |
/* pretend we hit surface */ |
282 |
> |
thisray.rt = thisray.rot = 1e-5; |
283 |
> |
thisray.rod = 1.0; |
284 |
> |
VCOPY(thisray.ron, dir); |
285 |
> |
VSUM(thisray.rop, org, dir, 1e-4); |
286 |
> |
samplendx++; /* compute result */ |
287 |
> |
(*ofun[Lamb.otype].funp)(&Lamb, &thisray); |
288 |
|
} |
289 |
|
|
290 |
|
|
291 |
< |
/* Evaluate ray contributions */ |
291 |
> |
/* Evaluate radiance contributions */ |
292 |
|
static void |
293 |
< |
eval_ray(FVECT org, FVECT dir, double dmax) |
293 |
> |
eval_rad(FVECT org, FVECT dir, double dmax) |
294 |
|
{ |
295 |
|
RAY thisray; |
296 |
|
/* set up ray */ |
297 |
+ |
VCOPY(thisray.rorg, org); |
298 |
+ |
VCOPY(thisray.rdir, dir); |
299 |
+ |
thisray.rmax = dmax; |
300 |
|
rayorigin(&thisray, PRIMARY, NULL, NULL); |
320 |
– |
if (imm_irrad) { |
321 |
– |
VSUM(thisray.rorg, org, dir, 1.1e-4); |
322 |
– |
thisray.rdir[0] = -dir[0]; |
323 |
– |
thisray.rdir[1] = -dir[1]; |
324 |
– |
thisray.rdir[2] = -dir[2]; |
325 |
– |
thisray.rmax = 0.0; |
326 |
– |
thisray.revf = rayirrad; |
327 |
– |
} else { |
328 |
– |
VCOPY(thisray.rorg, org); |
329 |
– |
VCOPY(thisray.rdir, dir); |
330 |
– |
thisray.rmax = dmax; |
331 |
– |
} |
301 |
|
samplendx++; /* call ray evaluation */ |
302 |
|
rayvalue(&thisray); |
303 |
|
} |
305 |
|
|
306 |
|
/* Accumulate and/or output ray contributions (child or only process) */ |
307 |
|
static void |
308 |
< |
done_contrib() |
308 |
> |
done_contrib(void) |
309 |
|
{ |
310 |
|
MODCONT *mp; |
311 |
|
int i; |
326 |
|
|
327 |
|
/* Principal calculation loop (called by main) */ |
328 |
|
void |
329 |
< |
rcontrib() |
329 |
> |
rcontrib(void) |
330 |
|
{ |
331 |
|
static int ignore_warning_given = 0; |
332 |
|
FVECT orig, direc; |
339 |
|
#endif |
340 |
|
while (getvec(orig) == 0 && getvec(direc) == 0) { |
341 |
|
d = normalize(direc); |
342 |
< |
if ((d == 0.0) & (accumulate != 1)) { |
342 |
> |
if (nchild != -1 && (d == 0.0) & (accumulate == 0)) { |
343 |
|
if (!ignore_warning_given++) |
344 |
|
error(WARNING, |
345 |
|
"dummy ray(s) ignored during accumulation\n"); |
349 |
|
lastray = lastdone = 0; |
350 |
|
++lastray; |
351 |
|
if (d == 0.0) { /* zero ==> flush */ |
352 |
< |
if ((yres <= 0) | (xres <= 0)) |
353 |
< |
waitflush = 1; /* flush right after */ |
354 |
< |
} else { /* else compute */ |
355 |
< |
eval_ray(orig, direc, lim_dist ? d : 0.0); |
352 |
> |
if ((yres <= 0) | (xres <= 1)) |
353 |
> |
waitflush = 1; /* flush after */ |
354 |
> |
if (nchild == -1) |
355 |
> |
account = 1; |
356 |
> |
} else if (imm_irrad) { /* else compute */ |
357 |
> |
eval_irrad(orig, direc); |
358 |
> |
} else { |
359 |
> |
eval_rad(orig, direc, lim_dist ? d : 0.0); |
360 |
|
} |
361 |
|
done_contrib(); /* accumulate/output */ |
362 |
|
++lastdone; |
363 |
|
if (raysleft && !--raysleft) |
364 |
|
break; /* preemptive EOI */ |
365 |
|
} |
366 |
< |
if (accumulate <= 0 || account < accumulate) { |
366 |
> |
if (nchild != -1 && (accumulate <= 0) | (account < accumulate)) { |
367 |
|
if (account < accumulate) { |
368 |
|
error(WARNING, "partial accumulation in final record"); |
369 |
|
accumulate -= account; |
371 |
|
account = 1; /* output accumulated totals */ |
372 |
|
done_contrib(); |
373 |
|
} |
374 |
+ |
lu_done(&ofiletab); /* close output files */ |
375 |
|
if (raysleft) |
376 |
|
error(USER, "unexpected EOF on input"); |
403 |
– |
lu_done(&ofiletab); /* close output files */ |
377 |
|
} |