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 |
|
|
14 |
– |
char *shm_boundary = NULL; /* boundary of shared memory */ |
15 |
– |
|
15 |
|
CUBE thescene; /* our scene */ |
16 |
|
OBJECT nsceneobjs; /* number of objects in our scene */ |
17 |
|
|
19 |
|
int ndims = 0; /* number of sampling dimensions */ |
20 |
|
int samplendx = 0; /* index for this sample */ |
21 |
|
|
22 |
< |
static void trace_contrib(RAY *r); /* our trace callback */ |
24 |
< |
void (*trace)() = trace_contrib; |
22 |
> |
void (*trace)() = NULL; /* trace call (NULL before rcinit) */ |
23 |
|
|
24 |
|
int do_irrad = 0; /* compute irradiance? */ |
25 |
|
|
26 |
|
int rand_samp = 1; /* pure Monte Carlo sampling? */ |
27 |
|
|
28 |
< |
double dstrsrc = 0.0; /* square source distribution */ |
29 |
< |
double shadthresh = .03; /* shadow threshold */ |
28 |
> |
double dstrsrc = 0.9; /* square source distribution */ |
29 |
> |
double shadthresh = 0.; /* shadow threshold */ |
30 |
|
double shadcert = .75; /* shadow certainty */ |
31 |
|
int directrelay = 3; /* number of source relays */ |
32 |
|
int vspretest = 512; /* virtual source pretest density */ |
38 |
|
double seccg = 0.; /* global scattering eccentricity */ |
39 |
|
double ssampdist = 0.; /* scatter sampling distance */ |
40 |
|
|
41 |
< |
double specthresh = .15; /* specular sampling threshold */ |
41 |
> |
double specthresh = .02; /* specular sampling threshold */ |
42 |
|
double specjitter = 1.; /* specular sampling jitter */ |
43 |
|
|
44 |
|
int backvis = 1; /* back face visibility */ |
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 */ |
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 */ |
64 |
|
RNUMBER lastray = 0; /* last ray number sent */ |
65 |
|
RNUMBER lastdone = 0; /* last ray output */ |
66 |
|
|
67 |
< |
static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); } |
67 |
> |
static void trace_contrib(RAY *r); /* our trace callback */ |
68 |
|
|
69 |
+ |
static void mcfree(void *p) { epfree((*(MODCONT *)p).binv,1); free(p); } |
70 |
+ |
|
71 |
|
LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ |
72 |
|
|
73 |
– |
static OBJECT traset[MAXTSET+1]={0}; /* trace include set */ |
74 |
– |
|
73 |
|
/************************** INITIALIZATION ROUTINES ***********************/ |
74 |
|
|
75 |
< |
void |
78 |
< |
tranotify( /* record new modifier */ |
79 |
< |
OBJECT obj |
80 |
< |
) |
81 |
< |
{ |
82 |
< |
static int hitlimit = 0; |
83 |
< |
OBJREC *o = objptr(obj); |
84 |
< |
int i; |
85 |
< |
|
86 |
< |
if (obj == OVOID) { /* starting over */ |
87 |
< |
traset[0] = 0; |
88 |
< |
hitlimit = 0; |
89 |
< |
return; |
90 |
< |
} |
91 |
< |
if (hitlimit || !ismodifier(o->otype)) |
92 |
< |
return; |
93 |
< |
for (i = nmods; i-- > 0; ) |
94 |
< |
if (!strcmp(o->oname, modname[i])) { |
95 |
< |
if (traset[0] >= MAXTSET) { |
96 |
< |
error(WARNING, "too many same-named modifiers"); |
97 |
< |
hitlimit++; |
98 |
< |
return; /* should this be fatal? */ |
99 |
< |
} |
100 |
< |
insertelem(traset, obj); |
101 |
< |
break; |
102 |
< |
} |
103 |
< |
} |
104 |
< |
|
105 |
< |
|
106 |
< |
char * |
75 |
> |
const char * |
76 |
|
formstr( /* return format identifier */ |
77 |
|
int f |
78 |
|
) |
81 |
|
case 'a': return("ascii"); |
82 |
|
case 'f': return("float"); |
83 |
|
case 'd': return("double"); |
84 |
< |
case 'c': return(COLRFMT); |
84 |
> |
case 'c': return(NCSAMP==3 ? COLRFMT : SPECFMT); |
85 |
|
} |
86 |
|
return("unknown"); |
87 |
|
} |
89 |
|
|
90 |
|
/* Add modifier to our list to track */ |
91 |
|
MODCONT * |
92 |
< |
addmodifier(char *modn, char *outf, char *binv, int bincnt) |
92 |
> |
addmodifier(char *modn, char *outf, char *prms, char *binv, int bincnt) |
93 |
|
{ |
94 |
|
LUENT *lep = lu_find(&modconttab,modn); |
95 |
|
MODCONT *mp; |
100 |
|
sprintf(errmsg, "duplicate modifier '%s'", modn); |
101 |
|
error(USER, errmsg); |
102 |
|
} |
103 |
< |
if (nmods >= MAXMODLIST) |
104 |
< |
error(INTERNAL, "too many modifiers"); |
103 |
> |
if (!strcmp(modn, VOIDID)) { |
104 |
> |
sprintf(errmsg, "cannot track '%s' modifier", VOIDID); |
105 |
> |
error(USER, errmsg); |
106 |
> |
} |
107 |
> |
if (nmods >= modasiz) { /* need bigger modifier array */ |
108 |
> |
modasiz += modasiz/2 + 64; |
109 |
> |
if (modname == NULL) |
110 |
> |
modname = (char **)malloc(modasiz*sizeof(char *)); |
111 |
> |
else |
112 |
> |
modname = (char **)realloc(modname, modasiz*sizeof(char *)); |
113 |
> |
if (modname == NULL) |
114 |
> |
error(SYSTEM, "Out of memory in addmodifier()"); |
115 |
> |
} |
116 |
|
modname[nmods++] = modn; /* XXX assumes static string */ |
117 |
|
lep->key = modn; /* XXX assumes static string */ |
118 |
|
if (binv == NULL) |
132 |
|
error(USER, errmsg); |
133 |
|
} |
134 |
|
/* initialize results holder */ |
135 |
< |
mp = (MODCONT *)malloc(sizeof(MODCONT)+sizeof(DCOLOR)*(bincnt-1)); |
135 |
> |
mp = (MODCONT *)malloc(mcsize(bincnt)); |
136 |
|
if (mp == NULL) |
137 |
|
error(SYSTEM, "out of memory in addmodifier"); |
138 |
|
mp->outspec = outf; /* XXX assumes static string */ |
139 |
|
mp->modname = modn; /* XXX assumes static string */ |
140 |
+ |
mp->params = prms; /* XXX assumes static string */ |
141 |
|
mp->binv = ebinv; |
142 |
+ |
mp->bin0 = 0; |
143 |
|
mp->nbins = bincnt; |
144 |
< |
memset(mp->cbin, 0, sizeof(DCOLOR)*bincnt); |
145 |
< |
/* allocate output streams */ |
146 |
< |
for (i = bincnt; i-- > 0; ) |
147 |
< |
getostream(mp->outspec, mp->modname, i, 1); |
144 |
> |
memset(mp->cbin, 0, DCOLORSIZ*bincnt); |
145 |
> |
/* figure out starting bin */ |
146 |
> |
while (!getostream(mp->outspec, mp->modname, mp->bin0, 1)) |
147 |
> |
mp->bin0++; |
148 |
> |
/* allocate other output streams */ |
149 |
> |
for (i = 0; ++i < mp->nbins; ) |
150 |
> |
getostream(mp->outspec, mp->modname, mp->bin0+i, 1); |
151 |
|
lep->data = (char *)mp; |
152 |
|
return(mp); |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
< |
/* add modifiers from a file list */ |
156 |
> |
/* Add modifiers from a file list */ |
157 |
|
void |
158 |
< |
addmodfile(char *fname, char *outf, char *binv, int bincnt) |
158 |
> |
addmodfile(char *fname, char *outf, char *prms, char *binv, int bincnt) |
159 |
|
{ |
160 |
< |
char *mname[MAXMODLIST]; |
161 |
< |
int i; |
162 |
< |
/* find the file & store strings */ |
163 |
< |
if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) { |
164 |
< |
sprintf(errmsg, "cannot find modifier file '%s'", fname); |
160 |
> |
char *path = getpath(fname, getrlibpath(), R_OK); |
161 |
> |
char mod[MAXSTR]; |
162 |
> |
FILE *fp; |
163 |
> |
|
164 |
> |
if (path == NULL || (fp = fopen(path, "r")) == NULL) { |
165 |
> |
if (path == NULL) |
166 |
> |
sprintf(errmsg, "cannot find modifier file '%s'", fname); |
167 |
> |
else |
168 |
> |
sprintf(errmsg, "cannot load modifier file '%s'", path); |
169 |
|
error(SYSTEM, errmsg); |
170 |
|
} |
171 |
< |
for (i = 0; mname[i]; i++) /* add each one */ |
172 |
< |
addmodifier(mname[i], outf, binv, bincnt); |
171 |
> |
while (fgetword(mod, sizeof(mod), fp) != NULL) |
172 |
> |
addmodifier(savqstr(mod), outf, prms, binv, bincnt); |
173 |
> |
fclose(fp); |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
+ |
/* Check if we have any more rays left (and report progress) */ |
178 |
+ |
int |
179 |
+ |
morays(void) |
180 |
+ |
{ |
181 |
+ |
static RNUMBER total_rays; |
182 |
+ |
static time_t tstart, last_report; |
183 |
+ |
time_t tnow; |
184 |
+ |
|
185 |
+ |
if (!raysleft) |
186 |
+ |
return(1); /* unknown total, so nothing to do or say */ |
187 |
+ |
|
188 |
+ |
if (report_intvl > 0 && (tnow = time(0)) >= last_report+report_intvl) { |
189 |
+ |
if (!total_rays) { |
190 |
+ |
total_rays = raysleft; |
191 |
+ |
tstart = tnow; |
192 |
+ |
} else { |
193 |
+ |
sprintf(errmsg, "%.2f%% done after %.3f hours\n", |
194 |
+ |
100.-100.*raysleft/total_rays, |
195 |
+ |
(1./3600.)*(tnow - tstart)); |
196 |
+ |
eputs(errmsg); |
197 |
+ |
} |
198 |
+ |
last_report = tnow; |
199 |
+ |
} |
200 |
+ |
return(--raysleft); |
201 |
+ |
} |
202 |
+ |
|
203 |
+ |
|
204 |
+ |
/* Quit program */ |
205 |
|
void |
206 |
< |
quit( /* quit program */ |
206 |
> |
quit( |
207 |
|
int code |
208 |
|
) |
209 |
|
{ |
210 |
|
if (nchild > 0) /* close children if any */ |
211 |
< |
end_children(); |
211 |
> |
end_children(code != 0); |
212 |
> |
else if (nchild < 0) |
213 |
> |
_exit(code); /* avoid flush() in child */ |
214 |
|
exit(code); |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
|
/* Initialize our process(es) */ |
219 |
|
static void |
220 |
< |
rcinit() |
220 |
> |
rcinit(void) |
221 |
|
{ |
222 |
|
int i; |
223 |
|
|
224 |
|
if (nproc > MAXPROCESS) |
225 |
|
sprintf(errmsg, "too many processes requested -- reducing to %d", |
226 |
|
nproc = MAXPROCESS); |
227 |
< |
if (nproc > 1) { |
228 |
< |
preload_objs(); /* preload auxiliary data */ |
229 |
< |
/* set shared memory boundary */ |
230 |
< |
shm_boundary = strcpy((char *)malloc(16), "SHM_BOUNDARY"); |
231 |
< |
} |
212 |
< |
if ((nproc > 1) & (accumulate <= 0)) |
213 |
< |
put_zero_record(0); /* prime our queue to accumulate */ |
214 |
< |
|
215 |
< |
if (recover) { /* recover previous output? */ |
216 |
< |
if (accumulate <= 0) { |
217 |
< |
reload_output(); |
218 |
< |
if (nproc > 1) |
219 |
< |
queue_modifiers(); |
220 |
< |
} else |
221 |
< |
recover_output(); |
222 |
< |
} |
227 |
> |
if (nproc > 1) |
228 |
> |
cow_memshare(); /* preload auxiliary data */ |
229 |
> |
trace = trace_contrib; /* set up trace call-back */ |
230 |
> |
for (i = 0; i < nsources; i++) /* tracing to sources as well */ |
231 |
> |
source[i].sflags |= SFOLLOW; |
232 |
|
if (yres > 0) { /* set up flushing & ray counts */ |
233 |
|
if (xres > 0) |
234 |
|
raysleft = (RNUMBER)xres*yres; |
239 |
|
if ((account = accumulate) > 1) |
240 |
|
raysleft *= accumulate; |
241 |
|
waitflush = (yres > 0) & (xres > 1) ? 0 : xres; |
233 |
– |
/* tracing to sources as well */ |
234 |
– |
for (i = 0; i < nsources; i++) |
235 |
– |
source[i].sflags |= SFOLLOW; |
242 |
|
|
243 |
< |
if (nproc == 1 || in_rchild()) /* single process or child */ |
243 |
> |
if (nproc > 1 && in_rchild()) /* forked child? */ |
244 |
|
return; /* return to main processing loop */ |
245 |
|
|
246 |
< |
parental_loop(); /* else run controller */ |
246 |
> |
if (recover) { /* recover previous output? */ |
247 |
> |
if (accumulate <= 0) |
248 |
> |
reload_output(); |
249 |
> |
else |
250 |
> |
recover_output(); |
251 |
> |
} |
252 |
> |
if (nproc == 1) /* single process? */ |
253 |
> |
return; |
254 |
> |
/* else run appropriate controller */ |
255 |
> |
if (accumulate <= 0) |
256 |
> |
feeder_loop(); |
257 |
> |
else |
258 |
> |
parental_loop(); |
259 |
|
quit(0); /* parent musn't return! */ |
260 |
|
} |
261 |
|
|
266 |
|
trace_contrib(RAY *r) |
267 |
|
{ |
268 |
|
MODCONT *mp; |
269 |
+ |
double bval; |
270 |
|
int bn; |
271 |
< |
RREAL contr[3]; |
271 |
> |
SCOLOR contr; |
272 |
> |
DCOLORV *dvp; |
273 |
> |
int i; |
274 |
|
|
275 |
< |
if (r->ro == NULL || !inset(traset, r->ro->omod)) |
275 |
> |
if (r->ro == NULL || r->ro->omod == OVOID) |
276 |
|
return; |
277 |
+ |
/* shadow ray not on source? */ |
278 |
+ |
if (r->rsrc >= 0 && source[r->rsrc].so != r->ro) |
279 |
+ |
return; |
280 |
|
|
281 |
|
mp = (MODCONT *)lu_find(&modconttab,objptr(r->ro->omod)->oname)->data; |
282 |
|
|
283 |
< |
if (mp == NULL) |
284 |
< |
error(CONSISTENCY, "unexpected modifier in trace_contrib()"); |
283 |
> |
if (mp == NULL) /* not in our list? */ |
284 |
> |
return; |
285 |
|
|
286 |
< |
worldfunc(RCCONTEXT, r); /* get bin number */ |
287 |
< |
bn = (int)(evalue(mp->binv) + .5); |
288 |
< |
if ((bn < 0) | (bn >= mp->nbins)) { |
289 |
< |
error(WARNING, "bad bin number (ignored)"); |
286 |
> |
worldfunc(RCCONTEXT, r); /* else set context */ |
287 |
> |
set_eparams((char *)mp->params); |
288 |
> |
if ((bval = evalue(mp->binv)) <= -.5) /* and get bin number */ |
289 |
> |
return; /* silently ignore negatives */ |
290 |
> |
if ((bn = (int)(bval + .5)) >= mp->nbins) { |
291 |
> |
sprintf(errmsg, "bad bin number (%d ignored)", bn); |
292 |
> |
error(WARNING, errmsg); |
293 |
|
return; |
294 |
|
} |
295 |
< |
raycontrib(contr, r, PRIMARY); |
295 |
> |
raycontrib(contr, r, PRIMARY); /* compute coefficient */ |
296 |
|
if (contrib) |
297 |
< |
multcolor(contr, r->rcol); |
298 |
< |
addcolor(mp->cbin[bn], contr); |
297 |
> |
smultscolor(contr, r->rcol); /* -> contribution */ |
298 |
> |
dvp = mcbin(mp, bn); |
299 |
> |
for (i = 0; i < NCSAMP; i++) /* add it in */ |
300 |
> |
*dvp++ += contr[i]; |
301 |
|
} |
302 |
|
|
303 |
|
|
304 |
+ |
/* Evaluate irradiance contributions */ |
305 |
|
static void |
306 |
< |
rayirrad( /* compute irradiance rather than radiance */ |
277 |
< |
RAY *r |
278 |
< |
) |
306 |
> |
eval_irrad(FVECT org, FVECT dir) |
307 |
|
{ |
308 |
< |
r->rot = 1e-5; /* pretend we hit surface */ |
309 |
< |
VSUM(r->rop, r->rorg, r->rdir, r->rot); |
310 |
< |
r->ron[0] = -r->rdir[0]; |
311 |
< |
r->ron[1] = -r->rdir[1]; |
312 |
< |
r->ron[2] = -r->rdir[2]; |
313 |
< |
r->rod = 1.0; |
314 |
< |
/* compute result */ |
315 |
< |
r->revf = raytrace; |
316 |
< |
(*ofun[Lamb.otype].funp)(&Lamb, r); |
317 |
< |
r->revf = rayirrad; |
308 |
> |
RAY thisray; |
309 |
> |
|
310 |
> |
VSUM(thisray.rorg, org, dir, 1.1e-4); |
311 |
> |
thisray.rdir[0] = -dir[0]; |
312 |
> |
thisray.rdir[1] = -dir[1]; |
313 |
> |
thisray.rdir[2] = -dir[2]; |
314 |
> |
thisray.rmax = 0.0; |
315 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
316 |
> |
/* pretend we hit surface */ |
317 |
> |
thisray.rxt = thisray.rot = 1e-5; |
318 |
> |
thisray.rod = 1.0; |
319 |
> |
VCOPY(thisray.ron, dir); |
320 |
> |
VSUM(thisray.rop, org, dir, 1e-4); |
321 |
> |
samplendx++; /* compute result */ |
322 |
> |
(*ofun[Lamb.otype].funp)(&Lamb, &thisray); |
323 |
|
} |
324 |
|
|
325 |
|
|
326 |
< |
/* Evaluate ray contributions */ |
326 |
> |
/* Evaluate radiance contributions */ |
327 |
|
static void |
328 |
< |
eval_ray(FVECT org, FVECT dir, double dmax) |
328 |
> |
eval_rad(FVECT org, FVECT dir, double dmax) |
329 |
|
{ |
330 |
|
RAY thisray; |
331 |
|
/* set up ray */ |
332 |
+ |
VCOPY(thisray.rorg, org); |
333 |
+ |
VCOPY(thisray.rdir, dir); |
334 |
+ |
thisray.rmax = dmax; |
335 |
|
rayorigin(&thisray, PRIMARY, NULL, NULL); |
300 |
– |
if (imm_irrad) { |
301 |
– |
VSUM(thisray.rorg, org, dir, 1.1e-4); |
302 |
– |
thisray.rdir[0] = -dir[0]; |
303 |
– |
thisray.rdir[1] = -dir[1]; |
304 |
– |
thisray.rdir[2] = -dir[2]; |
305 |
– |
thisray.rmax = 0.0; |
306 |
– |
thisray.revf = rayirrad; |
307 |
– |
} else { |
308 |
– |
VCOPY(thisray.rorg, org); |
309 |
– |
VCOPY(thisray.rdir, dir); |
310 |
– |
thisray.rmax = dmax; |
311 |
– |
} |
336 |
|
samplendx++; /* call ray evaluation */ |
337 |
|
rayvalue(&thisray); |
338 |
|
} |
340 |
|
|
341 |
|
/* Accumulate and/or output ray contributions (child or only process) */ |
342 |
|
static void |
343 |
< |
done_contrib() |
343 |
> |
done_contrib(void) |
344 |
|
{ |
345 |
|
MODCONT *mp; |
346 |
|
int i; |
351 |
|
for (i = 0; i < nmods; i++) { /* output records & clear */ |
352 |
|
mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; |
353 |
|
mod_output(mp); |
354 |
< |
memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); |
354 |
> |
memset(mp->cbin, 0, DCOLORSIZ*mp->nbins); |
355 |
|
} |
356 |
|
end_record(); /* end lines & flush if time */ |
357 |
|
|
361 |
|
|
362 |
|
/* Principal calculation loop (called by main) */ |
363 |
|
void |
364 |
< |
rcontrib() |
364 |
> |
rcontrib(void) |
365 |
|
{ |
366 |
|
static int ignore_warning_given = 0; |
367 |
|
FVECT orig, direc; |
374 |
|
#endif |
375 |
|
while (getvec(orig) == 0 && getvec(direc) == 0) { |
376 |
|
d = normalize(direc); |
377 |
< |
if (nchild != -1 && (d == 0.0) & (accumulate != 1)) { |
377 |
> |
if (nchild != -1 && (d == 0.0) & (accumulate == 0)) { |
378 |
|
if (!ignore_warning_given++) |
379 |
|
error(WARNING, |
380 |
|
"dummy ray(s) ignored during accumulation\n"); |
384 |
|
lastray = lastdone = 0; |
385 |
|
++lastray; |
386 |
|
if (d == 0.0) { /* zero ==> flush */ |
387 |
< |
if ((yres <= 0) | (xres <= 0)) |
388 |
< |
waitflush = 1; /* flush right after */ |
389 |
< |
account = 1; |
390 |
< |
} else { /* else compute */ |
391 |
< |
eval_ray(orig, direc, lim_dist ? d : 0.0); |
387 |
> |
if ((yres <= 0) | (xres <= 1)) |
388 |
> |
waitflush = 1; /* flush after */ |
389 |
> |
if (nchild == -1) |
390 |
> |
account = 1; |
391 |
> |
} else if (imm_irrad) { /* else compute */ |
392 |
> |
eval_irrad(orig, direc); |
393 |
> |
} else { |
394 |
> |
eval_rad(orig, direc, lim_dist ? d : 0.0); |
395 |
|
} |
396 |
|
done_contrib(); /* accumulate/output */ |
397 |
|
++lastdone; |
398 |
< |
if (raysleft && !--raysleft) |
398 |
> |
if (!morays()) |
399 |
|
break; /* preemptive EOI */ |
400 |
|
} |
401 |
|
if (nchild != -1 && (accumulate <= 0) | (account < accumulate)) { |
406 |
|
account = 1; /* output accumulated totals */ |
407 |
|
done_contrib(); |
408 |
|
} |
409 |
+ |
lu_done(&ofiletab); /* close output files */ |
410 |
|
if (raysleft) |
411 |
|
error(USER, "unexpected EOF on input"); |
384 |
– |
lu_done(&ofiletab); /* close output files */ |
412 |
|
} |