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 |
+ |
|
17 |
|
CUBE thescene; /* our scene */ |
18 |
|
OBJECT nsceneobjs; /* number of objects in our scene */ |
19 |
|
|
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 */ |
31 |
> |
double dstrsrc = 0.9; /* square source distribution */ |
32 |
|
double shadthresh = .03; /* shadow threshold */ |
33 |
|
double shadcert = .75; /* shadow certainty */ |
34 |
|
int directrelay = 3; /* number of source relays */ |
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 */ |
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 |
|
|
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 |
– |
|
70 |
|
static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); } |
71 |
|
|
72 |
|
LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ |
73 |
|
|
91 |
– |
static OBJECT traset[MAXTSET+1]={0}; /* trace include set */ |
92 |
– |
|
74 |
|
/************************** INITIALIZATION ROUTINES ***********************/ |
75 |
|
|
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 |
– |
|
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; |
103 |
|
} |
104 |
|
if (nmods >= MAXMODLIST) |
105 |
|
error(INTERNAL, "too many modifiers"); |
106 |
+ |
if (!strcmp(modn, VOIDID)) { |
107 |
+ |
sprintf(errmsg, "cannot track '%s' modifier", VOIDID); |
108 |
+ |
error(USER, errmsg); |
109 |
+ |
} |
110 |
|
modname[nmods++] = modn; /* XXX assumes static string */ |
111 |
|
lep->key = modn; /* XXX assumes static string */ |
112 |
|
if (binv == NULL) |
131 |
|
error(SYSTEM, "out of memory in addmodifier"); |
132 |
|
mp->outspec = outf; /* XXX assumes static string */ |
133 |
|
mp->modname = modn; /* XXX assumes static string */ |
134 |
+ |
mp->params = prms; |
135 |
|
mp->binv = ebinv; |
136 |
|
mp->nbins = bincnt; |
137 |
|
memset(mp->cbin, 0, sizeof(DCOLOR)*bincnt); |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
< |
/* add modifiers from a file list */ |
146 |
> |
/* Add modifiers from a file list */ |
147 |
|
void |
148 |
< |
addmodfile(char *fname, char *outf, char *binv, int bincnt) |
148 |
> |
addmodfile(char *fname, char *outf, char *prms, char *binv, int bincnt) |
149 |
|
{ |
150 |
|
char *mname[MAXMODLIST]; |
151 |
|
int i; |
155 |
|
error(SYSTEM, errmsg); |
156 |
|
} |
157 |
|
for (i = 0; mname[i]; i++) /* add each one */ |
158 |
< |
addmodifier(mname[i], outf, binv, bincnt); |
158 |
> |
addmodifier(mname[i], outf, prms, binv, bincnt); |
159 |
|
} |
160 |
|
|
161 |
|
|
165 |
|
) |
166 |
|
{ |
167 |
|
if (nchild > 0) /* close children if any */ |
168 |
< |
end_children(); |
168 |
> |
end_children(code != 0); |
169 |
|
exit(code); |
170 |
|
} |
171 |
|
|
179 |
|
if (nproc > MAXPROCESS) |
180 |
|
sprintf(errmsg, "too many processes requested -- reducing to %d", |
181 |
|
nproc = MAXPROCESS); |
182 |
< |
|
183 |
< |
if ((nproc > 1) & (accumulate <= 0)) |
184 |
< |
zero_record(0); /* prime our queue to accumulate */ |
185 |
< |
|
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(); |
182 |
> |
if (nproc > 1) { |
183 |
> |
preload_objs(); /* preload auxiliary data */ |
184 |
> |
/* set shared memory boundary */ |
185 |
> |
shm_boundary = strcpy((char *)malloc(16), "SHM_BOUNDARY"); |
186 |
|
} |
187 |
+ |
for (i = 0; i < nsources; i++) /* tracing to sources as well */ |
188 |
+ |
source[i].sflags |= SFOLLOW; |
189 |
|
if (yres > 0) { /* set up flushing & ray counts */ |
190 |
|
if (xres > 0) |
191 |
|
raysleft = (RNUMBER)xres*yres; |
196 |
|
if ((account = accumulate) > 1) |
197 |
|
raysleft *= accumulate; |
198 |
|
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; |
199 |
|
|
200 |
< |
if (nproc == 1 || in_rchild()) /* single process or child */ |
200 |
> |
if (nproc > 1 && in_rchild()) /* forked child? */ |
201 |
|
return; /* return to main processing loop */ |
202 |
|
|
203 |
< |
parental_loop(); /* else run controller */ |
203 |
> |
if (recover) { /* recover previous output? */ |
204 |
> |
if (accumulate <= 0) |
205 |
> |
reload_output(); |
206 |
> |
else |
207 |
> |
recover_output(); |
208 |
> |
} |
209 |
> |
if (nproc == 1) /* single process? */ |
210 |
> |
return; |
211 |
> |
/* else run appropriate controller */ |
212 |
> |
if (accumulate <= 0) |
213 |
> |
feeder_loop(); |
214 |
> |
else |
215 |
> |
parental_loop(); |
216 |
|
quit(0); /* parent musn't return! */ |
217 |
|
} |
218 |
|
|
223 |
|
trace_contrib(RAY *r) |
224 |
|
{ |
225 |
|
MODCONT *mp; |
226 |
+ |
double bval; |
227 |
|
int bn; |
228 |
|
RREAL contr[3]; |
229 |
|
|
230 |
< |
if (r->ro == NULL || !inset(traset, r->ro->omod)) |
230 |
> |
if (r->ro == NULL || r->ro->omod == OVOID) |
231 |
|
return; |
232 |
+ |
/* shadow ray not on source? */ |
233 |
+ |
if (r->rsrc >= 0 && source[r->rsrc].so != r->ro) |
234 |
+ |
return; |
235 |
|
|
236 |
|
mp = (MODCONT *)lu_find(&modconttab,objptr(r->ro->omod)->oname)->data; |
237 |
|
|
238 |
< |
if (mp == NULL) |
239 |
< |
error(CONSISTENCY, "unexpected modifier in trace_contrib()"); |
238 |
> |
if (mp == NULL) /* not in our list? */ |
239 |
> |
return; |
240 |
|
|
241 |
< |
worldfunc(RCCONTEXT, r); /* get bin number */ |
242 |
< |
bn = (int)(evalue(mp->binv) + .5); |
243 |
< |
if ((bn < 0) | (bn >= mp->nbins)) { |
244 |
< |
error(WARNING, "bad bin number (ignored)"); |
241 |
> |
worldfunc(RCCONTEXT, r); /* else set context */ |
242 |
> |
set_eparams((char *)mp->params); |
243 |
> |
if ((bval = evalue(mp->binv)) <= -.5) /* and get bin number */ |
244 |
> |
return; /* silently ignore negatives */ |
245 |
> |
if ((bn = (int)(bval + .5)) >= mp->nbins) { |
246 |
> |
sprintf(errmsg, "bad bin number (%d ignored)", bn); |
247 |
> |
error(WARNING, errmsg); |
248 |
|
return; |
249 |
|
} |
250 |
< |
raycontrib(contr, r, PRIMARY); |
250 |
> |
raycontrib(contr, r, PRIMARY); /* compute coefficient */ |
251 |
|
if (contrib) |
252 |
< |
multcolor(contr, r->rcol); |
252 |
> |
multcolor(contr, r->rcol); /* -> contribution */ |
253 |
|
addcolor(mp->cbin[bn], contr); |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
+ |
/* Evaluate irradiance contributions */ |
258 |
|
static void |
259 |
< |
rayirrad( /* compute irradiance rather than radiance */ |
291 |
< |
RAY *r |
292 |
< |
) |
259 |
> |
eval_irrad(FVECT org, FVECT dir) |
260 |
|
{ |
261 |
< |
r->rot = 1e-5; /* pretend we hit surface */ |
262 |
< |
VSUM(r->rop, r->rorg, r->rdir, r->rot); |
263 |
< |
r->ron[0] = -r->rdir[0]; |
264 |
< |
r->ron[1] = -r->rdir[1]; |
265 |
< |
r->ron[2] = -r->rdir[2]; |
266 |
< |
r->rod = 1.0; |
267 |
< |
/* compute result */ |
268 |
< |
r->revf = raytrace; |
269 |
< |
(*ofun[Lamb.otype].funp)(&Lamb, r); |
270 |
< |
r->revf = rayirrad; |
261 |
> |
RAY thisray; |
262 |
> |
|
263 |
> |
VSUM(thisray.rorg, org, dir, 1.1e-4); |
264 |
> |
thisray.rdir[0] = -dir[0]; |
265 |
> |
thisray.rdir[1] = -dir[1]; |
266 |
> |
thisray.rdir[2] = -dir[2]; |
267 |
> |
thisray.rmax = 0.0; |
268 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
269 |
> |
/* pretend we hit surface */ |
270 |
> |
thisray.rt = thisray.rot = 1e-5; |
271 |
> |
thisray.rod = 1.0; |
272 |
> |
VCOPY(thisray.ron, dir); |
273 |
> |
VSUM(thisray.rop, org, dir, 1e-4); |
274 |
> |
samplendx++; /* compute result */ |
275 |
> |
(*ofun[Lamb.otype].funp)(&Lamb, &thisray); |
276 |
|
} |
277 |
|
|
278 |
|
|
279 |
< |
/* Evaluate ray contributions */ |
279 |
> |
/* Evaluate radiance contributions */ |
280 |
|
static void |
281 |
< |
eval_ray(FVECT org, FVECT dir, double dmax) |
281 |
> |
eval_rad(FVECT org, FVECT dir, double dmax) |
282 |
|
{ |
283 |
|
RAY thisray; |
284 |
|
/* set up ray */ |
285 |
+ |
VCOPY(thisray.rorg, org); |
286 |
+ |
VCOPY(thisray.rdir, dir); |
287 |
+ |
thisray.rmax = dmax; |
288 |
|
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 |
– |
} |
289 |
|
samplendx++; /* call ray evaluation */ |
290 |
|
rayvalue(&thisray); |
291 |
|
} |
319 |
|
static int ignore_warning_given = 0; |
320 |
|
FVECT orig, direc; |
321 |
|
double d; |
322 |
< |
/* initialize & fork */ |
322 |
> |
/* initialize (& fork more of us) */ |
323 |
|
rcinit(); |
324 |
|
/* load rays from stdin & process */ |
325 |
|
#ifdef getc_unlocked |
326 |
< |
flockfile(stdin); /* avoid lock/unlock overhead */ |
326 |
> |
flockfile(stdin); /* avoid mutex overhead */ |
327 |
|
#endif |
328 |
|
while (getvec(orig) == 0 && getvec(direc) == 0) { |
329 |
|
d = normalize(direc); |
330 |
< |
if ((d == 0.0) & (accumulate != 1)) { |
330 |
> |
if (nchild != -1 && (d == 0.0) & (accumulate == 0)) { |
331 |
|
if (!ignore_warning_given++) |
332 |
|
error(WARNING, |
333 |
|
"dummy ray(s) ignored during accumulation\n"); |
337 |
|
lastray = lastdone = 0; |
338 |
|
++lastray; |
339 |
|
if (d == 0.0) { /* zero ==> flush */ |
340 |
< |
if ((yres <= 0) | (xres <= 0)) |
341 |
< |
waitflush = 1; /* flush right after */ |
342 |
< |
} else { /* else compute */ |
343 |
< |
eval_ray(orig, direc, lim_dist ? d : 0.0); |
340 |
> |
if ((yres <= 0) | (xres <= 1)) |
341 |
> |
waitflush = 1; /* flush after */ |
342 |
> |
if (nchild == -1) |
343 |
> |
account = 1; |
344 |
> |
} else if (imm_irrad) { /* else compute */ |
345 |
> |
eval_irrad(orig, direc); |
346 |
> |
} else { |
347 |
> |
eval_rad(orig, direc, lim_dist ? d : 0.0); |
348 |
|
} |
349 |
|
done_contrib(); /* accumulate/output */ |
350 |
|
++lastdone; |
351 |
|
if (raysleft && !--raysleft) |
352 |
|
break; /* preemptive EOI */ |
353 |
|
} |
354 |
< |
if (accumulate <= 0 || account < accumulate) { |
354 |
> |
if (nchild != -1 && (accumulate <= 0) | (account < accumulate)) { |
355 |
|
if (account < accumulate) { |
356 |
|
error(WARNING, "partial accumulation in final record"); |
357 |
|
accumulate -= account; |
359 |
|
account = 1; /* output accumulated totals */ |
360 |
|
done_contrib(); |
361 |
|
} |
362 |
+ |
lu_done(&ofiletab); /* close output files */ |
363 |
|
if (raysleft) |
364 |
|
error(USER, "unexpected EOF on input"); |
397 |
– |
lu_done(&ofiletab); /* close output files */ |
365 |
|
} |