1 |
+ |
#ifndef lint |
2 |
+ |
static const char RCSid[] = "$Id$"; |
3 |
+ |
#endif |
4 |
+ |
|
5 |
|
/* |
6 |
< |
================================================================== |
6 |
> |
====================================================================== |
7 |
|
Photon map main module |
8 |
|
|
9 |
|
Roland Schregle (roland.schregle@{hslu.ch, gmail.com}) |
10 |
|
(c) Fraunhofer Institute for Solar Energy Systems, |
11 |
|
(c) Lucerne University of Applied Sciences and Arts, |
12 |
< |
supported by the Swiss National Science Foundation (SNSF, #147053) |
13 |
< |
================================================================== |
12 |
> |
supported by the Swiss National Science Foundation (SNSF, #147053) |
13 |
> |
====================================================================== |
14 |
|
|
15 |
|
$Id$ |
16 |
|
*/ |
27 |
|
#include "otypes.h" |
28 |
|
#include <time.h> |
29 |
|
#include <sys/stat.h> |
30 |
+ |
#include <sys/mman.h> |
31 |
+ |
#include <sys/wait.h> |
32 |
|
|
33 |
|
|
34 |
|
|
29 |
– |
extern char *octname; |
30 |
– |
|
31 |
– |
static char PmapRevision [] = "$Revision$"; |
32 |
– |
|
33 |
– |
|
34 |
– |
|
35 |
– |
/* Photon map lookup functions per type */ |
36 |
– |
void (*pmapLookup [NUM_PMAP_TYPES])(PhotonMap*, RAY*, COLOR) = { |
37 |
– |
photonDensity, photonPreCompDensity, photonDensity, volumePhotonDensity, |
38 |
– |
photonDensity, NULL |
39 |
– |
}; |
40 |
– |
|
41 |
– |
|
42 |
– |
|
43 |
– |
void colorNorm (COLOR c) |
44 |
– |
/* Normalise colour channels to average of 1 */ |
45 |
– |
{ |
46 |
– |
const float avg = colorAvg(c); |
47 |
– |
|
48 |
– |
if (!avg) |
49 |
– |
return; |
50 |
– |
|
51 |
– |
c [0] /= avg; |
52 |
– |
c [1] /= avg; |
53 |
– |
c [2] /= avg; |
54 |
– |
} |
55 |
– |
|
56 |
– |
|
57 |
– |
|
58 |
– |
void loadPmaps (PhotonMap **pmaps, const PhotonMapParams *parm) |
59 |
– |
{ |
60 |
– |
unsigned t; |
61 |
– |
struct stat octstat, pmstat; |
62 |
– |
PhotonMap *pm; |
63 |
– |
PhotonMapType type; |
64 |
– |
|
65 |
– |
for (t = 0; t < NUM_PMAP_TYPES; t++) |
66 |
– |
if (setPmapParam(&pm, parm + t)) { |
67 |
– |
/* Check if photon map newer than octree */ |
68 |
– |
if (pm -> fileName && octname && |
69 |
– |
!stat(pm -> fileName, &pmstat) && !stat(octname, &octstat) && |
70 |
– |
octstat.st_mtime > pmstat.st_mtime) { |
71 |
– |
sprintf(errmsg, "photon map in file %s may be stale", |
72 |
– |
pm -> fileName); |
73 |
– |
error(USER, errmsg); |
74 |
– |
} |
75 |
– |
|
76 |
– |
/* Load photon map from file and get its type */ |
77 |
– |
if ((type = loadPhotonMap(pm, pm -> fileName)) == PMAP_TYPE_NONE) |
78 |
– |
error(USER, "failed loading photon map"); |
79 |
– |
|
80 |
– |
/* Assign to appropriate photon map type (deleting previously |
81 |
– |
* loaded photon map of same type if necessary) */ |
82 |
– |
if (pmaps [type]) { |
83 |
– |
deletePhotons(pmaps [type]); |
84 |
– |
free(pmaps [type]); |
85 |
– |
} |
86 |
– |
pmaps [type] = pm; |
87 |
– |
|
88 |
– |
/* Check for invalid density estimate bandwidth */ |
89 |
– |
if (pm -> maxGather > pm -> heapSize) { |
90 |
– |
error(WARNING, "adjusting density estimate bandwidth"); |
91 |
– |
pm -> minGather = pm -> maxGather = pm -> heapSize; |
92 |
– |
} |
93 |
– |
} |
94 |
– |
} |
95 |
– |
|
96 |
– |
|
97 |
– |
|
35 |
|
void savePmaps (const PhotonMap **pmaps, int argc, char **argv) |
36 |
|
{ |
37 |
|
unsigned t; |
43 |
|
} |
44 |
|
|
45 |
|
|
109 |
– |
|
110 |
– |
void cleanUpPmaps (PhotonMap **pmaps) |
111 |
– |
{ |
112 |
– |
unsigned t; |
113 |
– |
|
114 |
– |
for (t = 0; t < NUM_PMAP_TYPES; t++) { |
115 |
– |
if (pmaps [t]) { |
116 |
– |
deletePhotons(pmaps [t]); |
117 |
– |
free(pmaps [t]); |
118 |
– |
} |
119 |
– |
} |
120 |
– |
} |
121 |
– |
|
122 |
– |
|
46 |
|
|
47 |
|
static int photonParticipate (RAY *ray) |
48 |
|
/* Trace photon through participating medium. Returns 1 if passed through, |
68 |
|
colorNorm(ray -> rcol); |
69 |
|
VCOPY(ray -> rorg, ray -> rop); |
70 |
|
|
71 |
< |
if (albedo > FTINY) |
71 |
> |
if (albedo > FTINY && ray -> rlvl > 0) |
72 |
|
/* Add to volume photon map */ |
73 |
< |
if (ray -> rlvl > 0) addPhoton(volumePmap, ray); |
73 |
> |
newPhoton(volumePmap, ray); |
74 |
|
|
75 |
|
/* Absorbed? */ |
76 |
< |
if (pmapRandom(rouletteState) > albedo) return 0; |
76 |
> |
if (pmapRandom(rouletteState) > albedo) |
77 |
> |
return 0; |
78 |
|
|
79 |
|
/* Colour bleeding without attenuation (?) */ |
80 |
|
multcolor(ray -> rcol, ray -> albedo); |
159 |
|
|
160 |
|
|
161 |
|
static void preComputeGlobal (PhotonMap *pmap) |
162 |
< |
/* Precompute irradiance from global photons for final gathering using |
163 |
< |
the first finalGather * pmap -> heapSize photons in the heap. Returns |
164 |
< |
new heap with precomputed photons. */ |
162 |
> |
/* Precompute irradiance from global photons for final gathering for |
163 |
> |
a random subset of finalGather * pmap -> numPhotons photons, and builds |
164 |
> |
the photon map, discarding the original photons. */ |
165 |
> |
/* !!! NOTE: PRECOMPUTATION WITH OOC CURRENTLY WITHOUT CACHE !!! */ |
166 |
|
{ |
167 |
< |
unsigned long i, nuHeapSize; |
168 |
< |
unsigned j; |
169 |
< |
Photon *nuHeap, *p; |
170 |
< |
COLOR irrad; |
171 |
< |
RAY ray; |
172 |
< |
float nuMinPos [3], nuMaxPos [3]; |
167 |
> |
unsigned long i, numPreComp; |
168 |
> |
unsigned j; |
169 |
> |
PhotonIdx pIdx; |
170 |
> |
Photon photon; |
171 |
> |
RAY ray; |
172 |
> |
PhotonMap nuPmap; |
173 |
|
|
174 |
< |
repComplete = nuHeapSize = finalGather * pmap -> heapSize; |
174 |
> |
repComplete = numPreComp = finalGather * pmap -> numPhotons; |
175 |
|
|
176 |
|
if (photonRepTime) { |
177 |
< |
sprintf(errmsg, |
178 |
< |
"Precomputing irradiance for %ld global photons...\n", |
254 |
< |
nuHeapSize); |
177 |
> |
sprintf(errmsg, "Precomputing irradiance for %ld global photons...\n", |
178 |
> |
numPreComp); |
179 |
|
eputs(errmsg); |
180 |
|
fflush(stderr); |
181 |
|
} |
182 |
|
|
183 |
< |
p = nuHeap = (Photon*)malloc(nuHeapSize * sizeof(Photon)); |
184 |
< |
if (!nuHeap) |
185 |
< |
error(USER, "can't allocate photon heap"); |
186 |
< |
|
187 |
< |
for (j = 0; j <= 2; j++) { |
188 |
< |
nuMinPos [j] = FHUGE; |
265 |
< |
nuMaxPos [j] = -FHUGE; |
266 |
< |
} |
183 |
> |
/* Copy photon map for precomputed photons */ |
184 |
> |
memcpy(&nuPmap, pmap, sizeof(PhotonMap)); |
185 |
> |
|
186 |
> |
/* Zero counters, init new heap and extents */ |
187 |
> |
nuPmap.numPhotons = 0; |
188 |
> |
initPhotonHeap(&nuPmap); |
189 |
|
|
190 |
+ |
for (j = 0; j < 3; j++) { |
191 |
+ |
nuPmap.minPos [j] = FHUGE; |
192 |
+ |
nuPmap.maxPos [j] = -FHUGE; |
193 |
+ |
} |
194 |
+ |
|
195 |
|
/* Record start time, baby */ |
196 |
|
repStartTime = time(NULL); |
197 |
< |
#ifdef SIGCONT |
198 |
< |
signal(SIGCONT, pmapPreCompReport); |
199 |
< |
#endif |
197 |
> |
#ifdef SIGCONT |
198 |
> |
signal(SIGCONT, pmapPreCompReport); |
199 |
> |
#endif |
200 |
|
repProgress = 0; |
274 |
– |
memcpy(nuHeap, pmap -> heap, nuHeapSize * sizeof(Photon)); |
201 |
|
|
202 |
< |
for (i = 0, p = nuHeap; i < nuHeapSize; i++, p++) { |
203 |
< |
ray.ro = NULL; |
204 |
< |
VCOPY(ray.rop, p -> pos); |
202 |
> |
photonRay(NULL, &ray, PRIMARY, NULL); |
203 |
> |
ray.ro = NULL; |
204 |
> |
|
205 |
> |
for (i = 0; i < numPreComp; i++) { |
206 |
> |
/* Get random photon from stratified distribution in source heap to |
207 |
> |
* avoid duplicates and clutering */ |
208 |
> |
pIdx = firstPhoton(pmap) + |
209 |
> |
(unsigned long)((i + pmapRandom(pmap -> randState)) / |
210 |
> |
finalGather); |
211 |
> |
getPhoton(pmap, pIdx, &photon); |
212 |
|
|
213 |
< |
/* Update min and max positions & set ray normal */ |
214 |
< |
for (j = 0; j < 3; j++) { |
215 |
< |
if (p -> pos [j] < nuMinPos [j]) nuMinPos [j] = p -> pos [j]; |
216 |
< |
if (p -> pos [j] > nuMaxPos [j]) nuMaxPos [j] = p -> pos [j]; |
284 |
< |
ray.ron [j] = p -> norm [j] / 127.0; |
285 |
< |
} |
213 |
> |
/* Init dummy photon ray with intersection at photon position */ |
214 |
> |
VCOPY(ray.rop, photon.pos); |
215 |
> |
for (j = 0; j < 3; j++) |
216 |
> |
ray.ron [j] = photon.norm [j] / 127.0; |
217 |
|
|
218 |
< |
photonDensity(pmap, &ray, irrad); |
219 |
< |
setPhotonFlux(p, irrad); |
218 |
> |
/* Get density estimate at photon position */ |
219 |
> |
photonDensity(pmap, &ray, ray.rcol); |
220 |
> |
|
221 |
> |
/* Append photon to new heap from ray */ |
222 |
> |
newPhoton(&nuPmap, &ray); |
223 |
> |
|
224 |
> |
/* Update progress */ |
225 |
|
repProgress++; |
226 |
|
|
227 |
|
if (photonRepTime > 0 && time(NULL) >= repLastTime + photonRepTime) |
228 |
|
pmapPreCompReport(); |
229 |
< |
#ifdef SIGCONT |
230 |
< |
else signal(SIGCONT, pmapPreCompReport); |
231 |
< |
#endif |
229 |
> |
#ifdef SIGCONT |
230 |
> |
else signal(SIGCONT, pmapPreCompReport); |
231 |
> |
#endif |
232 |
|
} |
233 |
|
|
234 |
< |
#ifdef SIGCONT |
235 |
< |
signal(SIGCONT, SIG_DFL); |
300 |
< |
#endif |
234 |
> |
/* Flush heap */ |
235 |
> |
flushPhotonHeap(&nuPmap); |
236 |
|
|
237 |
< |
/* Replace & rebuild heap */ |
238 |
< |
free(pmap -> heap); |
239 |
< |
pmap -> heap = nuHeap; |
305 |
< |
pmap -> heapSize = pmap -> heapEnd = nuHeapSize; |
306 |
< |
VCOPY(pmap -> minPos, nuMinPos); |
307 |
< |
VCOPY(pmap -> maxPos, nuMaxPos); |
237 |
> |
#ifdef SIGCONT |
238 |
> |
signal(SIGCONT, SIG_DFL); |
239 |
> |
#endif |
240 |
|
|
241 |
+ |
/* Trash original pmap, replace with precomputed one */ |
242 |
+ |
deletePhotons(pmap); |
243 |
+ |
memcpy(pmap, &nuPmap, sizeof(PhotonMap)); |
244 |
+ |
|
245 |
|
if (photonRepTime) { |
246 |
< |
eputs("Rebuilding global photon heap...\n"); |
246 |
> |
eputs("Rebuilding precomputed photon map...\n"); |
247 |
|
fflush(stderr); |
248 |
|
} |
249 |
< |
|
250 |
< |
balancePhotons(pmap, NULL); |
249 |
> |
|
250 |
> |
/* Rebuild underlying data structure, destroying heap */ |
251 |
> |
buildPhotonMap(pmap, NULL, NULL, 1); |
252 |
|
} |
253 |
|
|
254 |
|
|
255 |
|
|
256 |
< |
void distribPhotons (PhotonMap **pmaps) |
256 |
> |
typedef struct { |
257 |
> |
unsigned long numPhotons [NUM_PMAP_TYPES], |
258 |
> |
numEmitted, numComplete; |
259 |
> |
} PhotonCnt; |
260 |
> |
|
261 |
> |
|
262 |
> |
|
263 |
> |
void distribPhotons (PhotonMap **pmaps, unsigned numProc) |
264 |
|
{ |
265 |
< |
EmissionMap emap; |
266 |
< |
char errmsg2 [128]; |
267 |
< |
unsigned t, srcIdx, passCnt = 0, prePassCnt = 0; |
268 |
< |
double totalFlux = 0; |
269 |
< |
PhotonMap *pm; |
265 |
> |
EmissionMap emap; |
266 |
> |
char errmsg2 [128], shmFname [255]; |
267 |
> |
unsigned t, srcIdx, proc; |
268 |
> |
double totalFlux = 0; |
269 |
> |
int shmFile, stat, pid; |
270 |
> |
PhotonMap *pm; |
271 |
> |
PhotonCnt *photonCnt; |
272 |
|
|
273 |
|
for (t = 0; t < NUM_PMAP_TYPES && !pmaps [t]; t++); |
274 |
+ |
|
275 |
|
if (t >= NUM_PMAP_TYPES) |
276 |
< |
error(USER, "no photon maps defined"); |
276 |
> |
error(USER, "no photon maps defined in distribPhotons"); |
277 |
|
|
278 |
|
if (!nsources) |
279 |
< |
error(USER, "no light sources"); |
279 |
> |
error(USER, "no light sources in distribPhotons"); |
280 |
|
|
281 |
|
/* =================================================================== |
282 |
|
* INITIALISATION - Set up emission and scattering funcs |
285 |
|
emap.maxPartitions = MAXSPART; |
286 |
|
emap.partitions = (unsigned char*)malloc(emap.maxPartitions >> 1); |
287 |
|
if (!emap.partitions) |
288 |
< |
error(INTERNAL, "can't allocate source partitions"); |
288 |
> |
error(INTERNAL, "can't allocate source partitions in distribPhotons"); |
289 |
|
|
290 |
|
/* Initialise all defined photon maps */ |
291 |
|
for (t = 0; t < NUM_PMAP_TYPES; t++) |
292 |
< |
initPhotonMap(pmaps [t], t); |
292 |
> |
if (pmaps [t]) { |
293 |
> |
initPhotonMap(pmaps [t], t); |
294 |
> |
/* Open photon heapfile */ |
295 |
> |
initPhotonHeap(pmaps [t]); |
296 |
> |
/* Per-subprocess target count */ |
297 |
> |
pmaps [t] -> distribTarget /= numProc; |
298 |
> |
} |
299 |
|
|
300 |
|
initPhotonEmissionFuncs(); |
301 |
|
initPhotonScatterFuncs(); |
307 |
|
/* Get photon sensor modifiers */ |
308 |
|
getPhotonSensors(photonSensorList); |
309 |
|
|
310 |
< |
/* Seed RNGs for photon distribution */ |
311 |
< |
pmapSeed(randSeed, partState); |
312 |
< |
pmapSeed(randSeed, emitState); |
313 |
< |
pmapSeed(randSeed, cntState); |
314 |
< |
pmapSeed(randSeed, mediumState); |
315 |
< |
pmapSeed(randSeed, scatterState); |
316 |
< |
pmapSeed(randSeed, rouletteState); |
317 |
< |
|
310 |
> |
/* Set up shared mem for photon counters (zeroed by ftruncate) */ |
311 |
> |
#if 0 |
312 |
> |
snprintf(shmFname, 255, PMAP_SHMFNAME, getpid()); |
313 |
> |
shmFile = shm_open(shmFname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); |
314 |
> |
#else |
315 |
> |
strcpy(shmFname, PMAP_SHMFNAME); |
316 |
> |
shmFile = mkstemp(shmFname); |
317 |
> |
#endif |
318 |
> |
|
319 |
> |
if (shmFile < 0) |
320 |
> |
error(SYSTEM, "failed opening shared memory file in distribPhotons"); |
321 |
> |
|
322 |
> |
if (ftruncate(shmFile, sizeof(*photonCnt)) < 0) |
323 |
> |
error(SYSTEM, "failed setting shared memory size in distribPhotons"); |
324 |
> |
|
325 |
> |
photonCnt = mmap(NULL, sizeof(*photonCnt), PROT_READ | PROT_WRITE, |
326 |
> |
MAP_SHARED, shmFile, 0); |
327 |
> |
|
328 |
> |
if (photonCnt == MAP_FAILED) |
329 |
> |
error(SYSTEM, "failed mapping shared memory in distribPhotons"); |
330 |
> |
|
331 |
|
if (photonRepTime) |
332 |
|
eputs("\n"); |
333 |
|
|
334 |
|
/* =================================================================== |
335 |
|
* FLUX INTEGRATION - Get total photon flux from light sources |
336 |
|
* =================================================================== */ |
337 |
< |
for (srcIdx = 0; srcIdx < nsources; srcIdx++) { |
337 |
> |
for (srcIdx = 0; srcIdx < nsources; srcIdx++) { |
338 |
|
unsigned portCnt = 0; |
339 |
|
emap.src = source + srcIdx; |
340 |
|
|
341 |
< |
do { |
341 |
> |
do { /* Need at least one iteration if no ports! */ |
342 |
|
emap.port = emap.src -> sflags & SDISTANT ? photonPorts + portCnt |
343 |
|
: NULL; |
344 |
|
photonPartition [emap.src -> so -> otype] (&emap); |
372 |
|
if (totalFlux < FTINY) |
373 |
|
error(USER, "zero flux from light sources"); |
374 |
|
|
375 |
< |
/* Record start time and enable progress report signal handler */ |
376 |
< |
repStartTime = time(NULL); |
377 |
< |
#ifdef SIGCONT |
378 |
< |
signal(SIGCONT, pmapDistribReport); |
379 |
< |
#endif |
380 |
< |
repProgress = prePassCnt = 0; |
381 |
< |
|
382 |
< |
if (photonRepTime) |
383 |
< |
eputs("\n"); |
418 |
< |
|
419 |
< |
/* =================================================================== |
420 |
< |
* 2-PASS PHOTON DISTRIBUTION |
421 |
< |
* Pass 1 (pre): emit fraction of target photon count |
422 |
< |
* Pass 2 (main): based on outcome of pass 1, estimate remaining number |
423 |
< |
* of photons to emit to approximate target count |
424 |
< |
* =================================================================== */ |
425 |
< |
do { |
426 |
< |
double numEmit; |
427 |
< |
|
428 |
< |
if (!passCnt) { |
429 |
< |
/* INIT PASS 1 */ |
430 |
< |
/* Skip if no photons contributed after sufficient iterations; make |
431 |
< |
* it clear to user which photon maps are missing so (s)he can |
432 |
< |
* check the scene geometry and materials */ |
433 |
< |
if (++prePassCnt > maxPreDistrib) { |
434 |
< |
sprintf(errmsg, "too many prepasses"); |
435 |
< |
|
436 |
< |
for (t = 0; t < NUM_PMAP_TYPES; t++) |
437 |
< |
if (pmaps [t] && !pmaps [t] -> heapEnd) { |
438 |
< |
sprintf(errmsg2, ", no %s photons stored", pmapName [t]); |
439 |
< |
strcat(errmsg, errmsg2); |
440 |
< |
} |
441 |
< |
|
442 |
< |
error(USER, errmsg); |
443 |
< |
break; |
444 |
< |
} |
445 |
< |
|
446 |
< |
/* Num to emit is fraction of minimum target count */ |
447 |
< |
numEmit = FHUGE; |
375 |
> |
/* MAIN LOOP */ |
376 |
> |
for (proc = 0; proc < numProc; proc++) { |
377 |
> |
if (!(pid = fork())) { |
378 |
> |
/* SUBPROCESS ENTERS HERE. |
379 |
> |
All opened and memory mapped files are inherited */ |
380 |
> |
unsigned passCnt = 0, prePassCnt = 0; |
381 |
> |
unsigned long lastNumPhotons [NUM_PMAP_TYPES]; |
382 |
> |
unsigned long localNumEmitted = 0; /* Num photons emitted by this |
383 |
> |
subprocess alone */ |
384 |
|
|
385 |
< |
for (t = 0; t < NUM_PMAP_TYPES; t++) |
386 |
< |
if (pmaps [t]) |
387 |
< |
numEmit = min(pmaps [t] -> distribTarget, numEmit); |
388 |
< |
|
389 |
< |
numEmit *= preDistrib; |
390 |
< |
} |
391 |
< |
|
456 |
< |
else { |
457 |
< |
/* INIT PASS 2 */ |
458 |
< |
/* Based on the outcome of the predistribution we can now estimate |
459 |
< |
* how many more photons we have to emit for each photon map to |
460 |
< |
* meet its respective target count. This value is clamped to 0 in |
461 |
< |
* case the target has already been exceeded in the pass 1. Note |
462 |
< |
* repProgress is the number of photons emitted thus far, while |
463 |
< |
* heapEnd is the number of photons stored in each photon map. */ |
464 |
< |
double maxDistribRatio = 0; |
465 |
< |
|
466 |
< |
/* Set the distribution ratio for each map; this indicates how many |
467 |
< |
* photons of each respective type are stored per emitted photon, |
468 |
< |
* and is used as probability for storing a photon by addPhoton(). |
469 |
< |
* Since this biases the photon density, addPhoton() promotes the |
470 |
< |
* flux of stored photons to compensate. */ |
471 |
< |
for (t = 0; t < NUM_PMAP_TYPES; t++) |
472 |
< |
if ((pm = pmaps [t])) { |
473 |
< |
pm -> distribRatio = (double)pm -> distribTarget / |
474 |
< |
pm -> heapEnd - 1; |
475 |
< |
|
476 |
< |
/* Check if photon map "overflowed", i.e. exceeded its target |
477 |
< |
* count in the prepass; correcting the photon flux via the |
478 |
< |
* distribution ratio is no longer possible, as no more |
479 |
< |
* photons of this type will be stored, so notify the user |
480 |
< |
* rather than deliver incorrect results. |
481 |
< |
* In future we should handle this more intelligently by |
482 |
< |
* using the photonFlux in each photon map to individually |
483 |
< |
* correct the flux after distribution. */ |
484 |
< |
if (pm -> distribRatio <= FTINY) { |
485 |
< |
sprintf(errmsg, |
486 |
< |
"%s photon map overflow in prepass, reduce -apD", |
487 |
< |
pmapName [t]); |
488 |
< |
error(INTERNAL, errmsg); |
489 |
< |
} |
385 |
> |
/* Seed RNGs from PID for decorellated photon distribution */ |
386 |
> |
pmapSeed(randSeed + proc, partState); |
387 |
> |
pmapSeed(randSeed + proc, emitState); |
388 |
> |
pmapSeed(randSeed + proc, cntState); |
389 |
> |
pmapSeed(randSeed + proc, mediumState); |
390 |
> |
pmapSeed(randSeed + proc, scatterState); |
391 |
> |
pmapSeed(randSeed + proc, rouletteState); |
392 |
|
|
491 |
– |
maxDistribRatio = max(pm -> distribRatio, maxDistribRatio); |
492 |
– |
} |
493 |
– |
|
494 |
– |
/* Normalise distribution ratios and calculate number of photons to |
495 |
– |
* emit in main pass */ |
393 |
|
for (t = 0; t < NUM_PMAP_TYPES; t++) |
394 |
< |
if ((pm = pmaps [t])) |
395 |
< |
pm -> distribRatio /= maxDistribRatio; |
396 |
< |
|
397 |
< |
if ((numEmit = repProgress * maxDistribRatio) < FTINY) |
398 |
< |
/* No photons left to distribute in main pass */ |
399 |
< |
break; |
400 |
< |
} |
401 |
< |
|
402 |
< |
/* Set completion count for progress report */ |
506 |
< |
repComplete = numEmit + repProgress; |
507 |
< |
|
508 |
< |
/* PHOTON DISTRIBUTION LOOP */ |
509 |
< |
for (srcIdx = 0; srcIdx < nsources; srcIdx++) { |
510 |
< |
unsigned portCnt = 0; |
511 |
< |
emap.src = source + srcIdx; |
512 |
< |
|
394 |
> |
lastNumPhotons [t] = 0; |
395 |
> |
|
396 |
> |
/* ============================================================= |
397 |
> |
* 2-PASS PHOTON DISTRIBUTION |
398 |
> |
* Pass 1 (pre): emit fraction of target photon count |
399 |
> |
* Pass 2 (main): based on outcome of pass 1, estimate remaining |
400 |
> |
* number of photons to emit to approximate target |
401 |
> |
* count |
402 |
> |
* ============================================================= */ |
403 |
|
do { |
404 |
< |
emap.port = emap.src -> sflags & SDISTANT ? photonPorts + portCnt |
515 |
< |
: NULL; |
516 |
< |
photonPartition [emap.src -> so -> otype] (&emap); |
404 |
> |
double numEmit; |
405 |
|
|
406 |
< |
if (photonRepTime) { |
407 |
< |
if (!passCnt) |
408 |
< |
sprintf(errmsg, "PREPASS %d on source %s ", |
409 |
< |
prePassCnt, source [srcIdx].so -> oname); |
410 |
< |
else |
411 |
< |
sprintf(errmsg, "MAIN PASS on source %s ", |
412 |
< |
source [srcIdx].so -> oname); |
413 |
< |
|
414 |
< |
if (emap.port) { |
415 |
< |
sprintf(errmsg2, "via port %s ", |
416 |
< |
photonPorts [portCnt].so -> oname); |
417 |
< |
strcat(errmsg, errmsg2); |
406 |
> |
if (!passCnt) { |
407 |
> |
/* INIT PASS 1 */ |
408 |
> |
/* Skip if no photons contributed after sufficient |
409 |
> |
* iterations; make it clear to user which photon maps are |
410 |
> |
* missing so (s)he can check geometry and materials */ |
411 |
> |
if (++prePassCnt > maxPreDistrib) { |
412 |
> |
sprintf(errmsg, |
413 |
> |
"proc %d, source %s: too many prepasses", |
414 |
> |
proc, source [srcIdx].so -> oname); |
415 |
> |
|
416 |
> |
for (t = 0; t < NUM_PMAP_TYPES; t++) |
417 |
> |
if (pmaps [t] && !pmaps [t] -> numPhotons) { |
418 |
> |
sprintf(errmsg2, ", no %s photons stored", |
419 |
> |
pmapName [t]); |
420 |
> |
strcat(errmsg, errmsg2); |
421 |
> |
} |
422 |
> |
|
423 |
> |
error(USER, errmsg); |
424 |
> |
break; |
425 |
|
} |
426 |
+ |
|
427 |
+ |
/* Num to emit is fraction of minimum target count */ |
428 |
+ |
numEmit = FHUGE; |
429 |
|
|
430 |
< |
sprintf(errmsg2, "(%lu partitions)...\n", emap.numPartitions); |
431 |
< |
strcat(errmsg, errmsg2); |
432 |
< |
eputs(errmsg); |
433 |
< |
fflush(stderr); |
430 |
> |
for (t = 0; t < NUM_PMAP_TYPES; t++) |
431 |
> |
if (pmaps [t]) |
432 |
> |
numEmit = min(pmaps [t] -> distribTarget, numEmit); |
433 |
> |
|
434 |
> |
numEmit *= preDistrib; |
435 |
|
} |
436 |
< |
|
437 |
< |
for (emap.partitionCnt = 0; emap.partitionCnt < emap.numPartitions; |
438 |
< |
emap.partitionCnt++) { |
439 |
< |
double partNumEmit; |
440 |
< |
unsigned long partEmitCnt; |
436 |
> |
else { |
437 |
> |
/* INIT PASS 2 */ |
438 |
> |
/* Based on the outcome of the predistribution we can now |
439 |
> |
* estimate how many more photons we have to emit for each |
440 |
> |
* photon map to meet its respective target count. This |
441 |
> |
* value is clamped to 0 in case the target has already been |
442 |
> |
* exceeded in the pass 1. */ |
443 |
> |
double maxDistribRatio = 0; |
444 |
> |
|
445 |
> |
/* Set the distribution ratio for each map; this indicates |
446 |
> |
* how many photons of each respective type are stored per |
447 |
> |
* emitted photon, and is used as probability for storing a |
448 |
> |
* photon by newPhoton(). Since this biases the photon |
449 |
> |
* density, newPhoton() promotes the flux of stored photons |
450 |
> |
* to compensate. */ |
451 |
> |
for (t = 0; t < NUM_PMAP_TYPES; t++) |
452 |
> |
if ((pm = pmaps [t])) { |
453 |
> |
pm -> distribRatio = (double)pm -> distribTarget / |
454 |
> |
pm -> numPhotons - 1; |
455 |
> |
|
456 |
> |
/* Check if photon map "overflowed", i.e. exceeded its |
457 |
> |
* target count in the prepass; correcting the photon |
458 |
> |
* flux via the distribution ratio is no longer |
459 |
> |
* possible, as no more photons of this type will be |
460 |
> |
* stored, so notify the user rather than deliver |
461 |
> |
* incorrect results. In future we should handle this |
462 |
> |
* more intelligently by using the photonFlux in each |
463 |
> |
* photon map to individually correct the flux after |
464 |
> |
* distribution. */ |
465 |
> |
if (pm -> distribRatio <= FTINY) { |
466 |
> |
sprintf(errmsg, "%s photon map overflow in " |
467 |
> |
"prepass, reduce -apD", pmapName [t]); |
468 |
> |
error(INTERNAL, errmsg); |
469 |
> |
} |
470 |
> |
|
471 |
> |
maxDistribRatio = max(pm -> distribRatio, |
472 |
> |
maxDistribRatio); |
473 |
> |
} |
474 |
|
|
475 |
< |
/* Get photon origin within current source partishunn and |
476 |
< |
* build emission map */ |
477 |
< |
photonOrigin [emap.src -> so -> otype] (&emap); |
478 |
< |
initPhotonEmission(&emap, pdfSamples); |
479 |
< |
|
480 |
< |
/* Number of photons to emit from ziss partishunn -- |
481 |
< |
* proportional to flux; photon ray weight and scalar flux |
482 |
< |
* are uniform (the latter only varying in RGB). */ |
483 |
< |
partNumEmit = numEmit * colorAvg(emap.partFlux) / totalFlux; |
484 |
< |
partEmitCnt = (unsigned long)partNumEmit; |
553 |
< |
|
554 |
< |
/* Probabilistically account for fractional photons */ |
555 |
< |
if (pmapRandom(cntState) < partNumEmit - partEmitCnt) |
556 |
< |
partEmitCnt++; |
475 |
> |
/* Normalise distribution ratios and calculate number of |
476 |
> |
* photons to emit in main pass */ |
477 |
> |
for (t = 0; t < NUM_PMAP_TYPES; t++) |
478 |
> |
if ((pm = pmaps [t])) |
479 |
> |
pm -> distribRatio /= maxDistribRatio; |
480 |
> |
|
481 |
> |
if ((numEmit = localNumEmitted * maxDistribRatio) < FTINY) |
482 |
> |
/* No photons left to distribute in main pass */ |
483 |
> |
break; |
484 |
> |
} |
485 |
|
|
486 |
< |
/* Integer counter avoids FP rounding errors */ |
487 |
< |
while (partEmitCnt--) { |
488 |
< |
RAY photonRay; |
486 |
> |
/* Update shared completion counter for prog.report by parent */ |
487 |
> |
photonCnt -> numComplete += numEmit; |
488 |
> |
|
489 |
> |
/* PHOTON DISTRIBUTION LOOP */ |
490 |
> |
for (srcIdx = 0; srcIdx < nsources; srcIdx++) { |
491 |
> |
unsigned portCnt = 0; |
492 |
> |
emap.src = source + srcIdx; |
493 |
> |
|
494 |
> |
do { /* Need at least one iteration if no ports! */ |
495 |
> |
emap.port = emap.src -> sflags & SDISTANT |
496 |
> |
? photonPorts + portCnt : NULL; |
497 |
> |
photonPartition [emap.src -> so -> otype] (&emap); |
498 |
> |
|
499 |
> |
if (photonRepTime && !proc) { |
500 |
> |
if (!passCnt) |
501 |
> |
sprintf(errmsg, "PREPASS %d on source %s ", |
502 |
> |
prePassCnt, source [srcIdx].so -> oname); |
503 |
> |
else |
504 |
> |
sprintf(errmsg, "MAIN PASS on source %s ", |
505 |
> |
source [srcIdx].so -> oname); |
506 |
> |
|
507 |
> |
if (emap.port) { |
508 |
> |
sprintf(errmsg2, "via port %s ", |
509 |
> |
photonPorts [portCnt].so -> oname); |
510 |
> |
strcat(errmsg, errmsg2); |
511 |
> |
} |
512 |
> |
|
513 |
> |
sprintf(errmsg2, "(%lu partitions)\n", |
514 |
> |
emap.numPartitions); |
515 |
> |
strcat(errmsg, errmsg2); |
516 |
> |
eputs(errmsg); |
517 |
> |
fflush(stderr); |
518 |
> |
} |
519 |
|
|
520 |
< |
/* Emit photon based on PDF and trace through scene until |
521 |
< |
* absorbed/leaked */ |
522 |
< |
emitPhoton(&emap, &photonRay); |
523 |
< |
tracePhoton(&photonRay); |
520 |
> |
for (emap.partitionCnt = 0; emap.partitionCnt < emap.numPartitions; |
521 |
> |
emap.partitionCnt++) { |
522 |
> |
double partNumEmit; |
523 |
> |
unsigned long partEmitCnt; |
524 |
> |
|
525 |
> |
/* Get photon origin within current source partishunn |
526 |
> |
* and build emission map */ |
527 |
> |
photonOrigin [emap.src -> so -> otype] (&emap); |
528 |
> |
initPhotonEmission(&emap, pdfSamples); |
529 |
> |
|
530 |
> |
/* Number of photons to emit from ziss partishunn -- |
531 |
> |
* proportional to flux; photon ray weight and scalar |
532 |
> |
* flux are uniform (the latter only varying in RGB). |
533 |
> |
* */ |
534 |
> |
partNumEmit = numEmit * colorAvg(emap.partFlux) / |
535 |
> |
totalFlux; |
536 |
> |
partEmitCnt = (unsigned long)partNumEmit; |
537 |
> |
|
538 |
> |
/* Probabilistically account for fractional photons */ |
539 |
> |
if (pmapRandom(cntState) < partNumEmit - partEmitCnt) |
540 |
> |
partEmitCnt++; |
541 |
> |
|
542 |
> |
/* Update local and shared (global) emission counter */ |
543 |
> |
photonCnt -> numEmitted += partEmitCnt; |
544 |
> |
localNumEmitted += partEmitCnt; |
545 |
> |
|
546 |
> |
/* Integer counter avoids FP rounding errors during |
547 |
> |
* iteration */ |
548 |
> |
while (partEmitCnt--) { |
549 |
> |
RAY photonRay; |
550 |
> |
|
551 |
> |
/* Emit photon based on PDF and trace through scene |
552 |
> |
* until absorbed/leaked */ |
553 |
> |
emitPhoton(&emap, &photonRay); |
554 |
> |
tracePhoton(&photonRay); |
555 |
> |
} |
556 |
> |
|
557 |
> |
/* Update shared global photon count for each pmap */ |
558 |
> |
for (t = 0; t < NUM_PMAP_TYPES; t++) |
559 |
> |
if (pmaps [t]) { |
560 |
> |
photonCnt -> numPhotons [t] += |
561 |
> |
pmaps [t] -> numPhotons - lastNumPhotons [t]; |
562 |
> |
lastNumPhotons [t] = pmaps [t] -> numPhotons; |
563 |
> |
} |
564 |
> |
} |
565 |
|
|
566 |
< |
/* Record progress */ |
567 |
< |
repProgress++; |
568 |
< |
|
569 |
< |
if (photonRepTime > 0 && |
570 |
< |
time(NULL) >= repLastTime + photonRepTime) |
571 |
< |
pmapDistribReport(); |
572 |
< |
#ifdef SIGCONT |
573 |
< |
else signal(SIGCONT, pmapDistribReport); |
574 |
< |
#endif |
566 |
> |
portCnt++; |
567 |
> |
} while (portCnt < numPhotonPorts); |
568 |
> |
} |
569 |
> |
|
570 |
> |
for (t = 0; t < NUM_PMAP_TYPES; t++) |
571 |
> |
if (pmaps [t] && !pmaps [t] -> numPhotons) { |
572 |
> |
/* Double preDistrib in case a photon map is empty and |
573 |
> |
* redo pass 1 --> possibility of infinite loop for |
574 |
> |
* pathological scenes (e.g. absorbing materials) */ |
575 |
> |
preDistrib *= 2; |
576 |
> |
break; |
577 |
|
} |
578 |
+ |
|
579 |
+ |
if (t >= NUM_PMAP_TYPES) { |
580 |
+ |
/* No empty photon maps found; now do pass 2 */ |
581 |
+ |
passCnt++; |
582 |
+ |
#if 0 |
583 |
+ |
if (photonRepTime) |
584 |
+ |
eputs("\n"); |
585 |
+ |
#endif |
586 |
|
} |
587 |
< |
|
588 |
< |
portCnt++; |
589 |
< |
} while (portCnt < numPhotonPorts); |
587 |
> |
} while (passCnt < 2); |
588 |
> |
|
589 |
> |
/* Unmap shared photon counters */ |
590 |
> |
#if 0 |
591 |
> |
munmap(photonCnt, sizeof(*photonCnt)); |
592 |
> |
close(shmFile); |
593 |
> |
#endif |
594 |
> |
|
595 |
> |
/* Flush heap buffa for every pmap one final time; this is required |
596 |
> |
* to prevent data corruption! */ |
597 |
> |
for (t = 0; t < NUM_PMAP_TYPES; t++) |
598 |
> |
if (pmaps [t]) { |
599 |
> |
#if 0 |
600 |
> |
eputs("Final flush\n"); |
601 |
> |
#endif |
602 |
> |
flushPhotonHeap(pmaps [t]); |
603 |
> |
fclose(pmaps [t] -> heap); |
604 |
> |
#ifdef DEBUG_PMAP |
605 |
> |
sprintf(errmsg, "Proc %d: total %ld photons\n", getpid(), |
606 |
> |
pmaps [t] -> numPhotons); |
607 |
> |
eputs(errmsg); |
608 |
> |
#endif |
609 |
> |
} |
610 |
> |
|
611 |
> |
exit(0); |
612 |
|
} |
613 |
+ |
else if (pid < 0) |
614 |
+ |
error(SYSTEM, "failed to fork subprocess in distribPhotons"); |
615 |
+ |
} |
616 |
+ |
|
617 |
+ |
/* PARENT PROCESS CONTINUES HERE */ |
618 |
+ |
/* Record start time and enable progress report signal handler */ |
619 |
+ |
repStartTime = time(NULL); |
620 |
+ |
#ifdef SIGCONT |
621 |
+ |
signal(SIGCONT, pmapDistribReport); |
622 |
+ |
#endif |
623 |
+ |
|
624 |
+ |
if (photonRepTime) |
625 |
+ |
eputs("\n"); |
626 |
+ |
|
627 |
+ |
/* Wait for subprocesses to complete while reporting progress */ |
628 |
+ |
proc = numProc; |
629 |
+ |
while (proc) { |
630 |
+ |
while (waitpid(-1, &stat, WNOHANG) > 0) { |
631 |
+ |
/* Subprocess exited; check status */ |
632 |
+ |
if (!WIFEXITED(stat) || WEXITSTATUS(stat)) |
633 |
+ |
error(USER, "failed photon distribution"); |
634 |
|
|
635 |
+ |
--proc; |
636 |
+ |
} |
637 |
+ |
|
638 |
+ |
/* Nod off for a bit and update progress */ |
639 |
+ |
sleep(1); |
640 |
+ |
/* Update progress report from shared subprocess counters */ |
641 |
+ |
repEmitted = repProgress = photonCnt -> numEmitted; |
642 |
+ |
repComplete = photonCnt -> numComplete; |
643 |
+ |
|
644 |
|
for (t = 0; t < NUM_PMAP_TYPES; t++) |
645 |
< |
if (pmaps [t] && !pmaps [t] -> heapEnd) { |
646 |
< |
/* Double preDistrib in case a photon map is empty and redo |
647 |
< |
* pass 1 --> possibility of infinite loop for pathological |
648 |
< |
* scenes (e.g. absorbing materials) */ |
649 |
< |
preDistrib *= 2; |
650 |
< |
break; |
645 |
> |
if ((pm = pmaps [t])) { |
646 |
> |
#if 0 |
647 |
> |
/* Get photon count from heapfile size for progress update */ |
648 |
> |
fseek(pm -> heap, 0, SEEK_END); |
649 |
> |
pm -> numPhotons = ftell(pm -> heap) / sizeof(Photon); */ |
650 |
> |
#else |
651 |
> |
/* Get global photon count from shmem updated by subprocs */ |
652 |
> |
pm -> numPhotons = photonCnt -> numPhotons [t]; |
653 |
> |
#endif |
654 |
|
} |
591 |
– |
|
592 |
– |
if (t >= NUM_PMAP_TYPES) { |
593 |
– |
/* No empty photon maps found; now do pass 2 */ |
594 |
– |
passCnt++; |
595 |
– |
if (photonRepTime) |
596 |
– |
eputs("\n"); |
597 |
– |
} |
598 |
– |
} while (passCnt < 2); |
655 |
|
|
656 |
+ |
if (photonRepTime > 0 && time(NULL) >= repLastTime + photonRepTime) |
657 |
+ |
pmapDistribReport(); |
658 |
+ |
#ifdef SIGCONT |
659 |
+ |
else signal(SIGCONT, pmapDistribReport); |
660 |
+ |
#endif |
661 |
+ |
} |
662 |
+ |
|
663 |
|
/* =================================================================== |
664 |
< |
* POST-DISTRIBUTION - Set photon flux and build kd-tree, etc. |
664 |
> |
* POST-DISTRIBUTION - Set photon flux and build data struct for photon |
665 |
> |
* storage, etc. |
666 |
|
* =================================================================== */ |
667 |
< |
#ifdef SIGCONT |
668 |
< |
signal(SIGCONT, SIG_DFL); |
669 |
< |
#endif |
667 |
> |
#ifdef SIGCONT |
668 |
> |
signal(SIGCONT, SIG_DFL); |
669 |
> |
#endif |
670 |
|
free(emap.samples); |
671 |
|
|
672 |
|
/* Set photon flux (repProgress is total num emitted) */ |
673 |
< |
totalFlux /= repProgress; |
673 |
> |
totalFlux /= photonCnt -> numEmitted; |
674 |
|
|
675 |
+ |
/* Photon counters no longer needed, unmap shared memory */ |
676 |
+ |
munmap(photonCnt, sizeof(*photonCnt)); |
677 |
+ |
close(shmFile); |
678 |
+ |
#if 0 |
679 |
+ |
shm_unlink(shmFname); |
680 |
+ |
#else |
681 |
+ |
unlink(shmFname); |
682 |
+ |
#endif |
683 |
+ |
|
684 |
|
for (t = 0; t < NUM_PMAP_TYPES; t++) |
685 |
|
if (pmaps [t]) { |
686 |
|
if (photonRepTime) { |
688 |
|
eputs(errmsg); |
689 |
|
fflush(stderr); |
690 |
|
} |
691 |
< |
|
692 |
< |
balancePhotons(pmaps [t], &totalFlux); |
691 |
> |
|
692 |
> |
/* Build underlying data structure; heap is destroyed */ |
693 |
> |
buildPhotonMap(pmaps [t], &totalFlux, NULL, numProc); |
694 |
|
} |
695 |
< |
|
695 |
> |
|
696 |
|
/* Precompute photon irradiance if necessary */ |
697 |
|
if (preCompPmap) |
698 |
|
preComputeGlobal(preCompPmap); |
625 |
– |
} |
626 |
– |
|
627 |
– |
|
628 |
– |
|
629 |
– |
void photonDensity (PhotonMap *pmap, RAY *ray, COLOR irrad) |
630 |
– |
/* Photon density estimate. Returns irradiance at ray -> rop. */ |
631 |
– |
{ |
632 |
– |
unsigned i; |
633 |
– |
PhotonSQNode *sq; |
634 |
– |
float r; |
635 |
– |
COLOR flux; |
636 |
– |
|
637 |
– |
setcolor(irrad, 0, 0, 0); |
638 |
– |
|
639 |
– |
if (!pmap -> maxGather) |
640 |
– |
return; |
641 |
– |
|
642 |
– |
/* Ignore sources */ |
643 |
– |
if (ray -> ro) |
644 |
– |
if (islight(objptr(ray -> ro -> omod) -> otype)) |
645 |
– |
return; |
646 |
– |
|
647 |
– |
pmap -> squeueEnd = 0; |
648 |
– |
findPhotons(pmap, ray); |
649 |
– |
|
650 |
– |
/* Need at least 2 photons */ |
651 |
– |
if (pmap -> squeueEnd < 2) { |
652 |
– |
#ifdef PMAP_NONEFOUND |
653 |
– |
sprintf(errmsg, "no photons found on %s at (%.3f, %.3f, %.3f)", |
654 |
– |
ray -> ro ? ray -> ro -> oname : "<null>", |
655 |
– |
ray -> rop [0], ray -> rop [1], ray -> rop [2]); |
656 |
– |
error(WARNING, errmsg); |
657 |
– |
#endif |
658 |
– |
|
659 |
– |
return; |
660 |
– |
} |
661 |
– |
|
662 |
– |
if (pmap -> minGather == pmap -> maxGather) { |
663 |
– |
/* No bias compensation. Just do a plain vanilla estimate */ |
664 |
– |
sq = pmap -> squeue + 1; |
665 |
– |
|
666 |
– |
/* Average radius between furthest two photons to improve accuracy */ |
667 |
– |
r = max(sq -> dist, (sq + 1) -> dist); |
668 |
– |
r = 0.25 * (pmap -> maxDist + r + 2 * sqrt(pmap -> maxDist * r)); |
669 |
– |
|
670 |
– |
/* Skip the extra photon */ |
671 |
– |
for (i = 1 ; i < pmap -> squeueEnd; i++, sq++) { |
672 |
– |
getPhotonFlux(sq -> photon, flux); |
673 |
– |
#ifdef PMAP_EPANECHNIKOV |
674 |
– |
/* Apply Epanechnikov kernel to photon flux (dists are squared) */ |
675 |
– |
scalecolor(flux, 2 * (1 - sq -> dist / r)); |
676 |
– |
#endif |
677 |
– |
addcolor(irrad, flux); |
678 |
– |
} |
679 |
– |
|
680 |
– |
/* Divide by search area PI * r^2, 1 / PI required as ambient |
681 |
– |
normalisation factor */ |
682 |
– |
scalecolor(irrad, 1 / (PI * PI * r)); |
683 |
– |
|
684 |
– |
return; |
685 |
– |
} |
686 |
– |
else |
687 |
– |
/* Apply bias compensation to density estimate */ |
688 |
– |
biasComp(pmap, irrad); |
689 |
– |
} |
690 |
– |
|
691 |
– |
|
692 |
– |
|
693 |
– |
void photonPreCompDensity (PhotonMap *pmap, RAY *r, COLOR irrad) |
694 |
– |
/* Returns precomputed photon density estimate at ray -> rop. */ |
695 |
– |
{ |
696 |
– |
Photon *p; |
697 |
– |
|
698 |
– |
setcolor(irrad, 0, 0, 0); |
699 |
– |
|
700 |
– |
/* Ignore sources */ |
701 |
– |
if (r -> ro && islight(objptr(r -> ro -> omod) -> otype)) |
702 |
– |
return; |
703 |
– |
|
704 |
– |
if ((p = find1Photon(preCompPmap, r))) |
705 |
– |
getPhotonFlux(p, irrad); |
706 |
– |
} |
707 |
– |
|
708 |
– |
|
709 |
– |
|
710 |
– |
void volumePhotonDensity (PhotonMap *pmap, RAY *ray, COLOR irrad) |
711 |
– |
/* Photon volume density estimate. Returns irradiance at ray -> rop. */ |
712 |
– |
{ |
713 |
– |
unsigned i; |
714 |
– |
PhotonSQNode *sq; |
715 |
– |
float gecc2, r, ph; |
716 |
– |
COLOR flux; |
717 |
– |
|
718 |
– |
setcolor(irrad, 0, 0, 0); |
719 |
– |
|
720 |
– |
if (!pmap -> maxGather) |
721 |
– |
return; |
722 |
– |
|
723 |
– |
pmap -> squeueEnd = 0; |
724 |
– |
findPhotons(pmap, ray); |
725 |
– |
|
726 |
– |
/* Need at least 2 photons */ |
727 |
– |
if (pmap -> squeueEnd < 2) |
728 |
– |
return; |
729 |
– |
|
730 |
– |
if (pmap -> minGather == pmap -> maxGather) { |
731 |
– |
/* No bias compensation. Just do a plain vanilla estimate */ |
732 |
– |
gecc2 = ray -> gecc * ray -> gecc; |
733 |
– |
sq = pmap -> squeue + 1; |
734 |
– |
|
735 |
– |
/* Average radius between furthest two photons to improve accuracy */ |
736 |
– |
r = max(sq -> dist, (sq + 1) -> dist); |
737 |
– |
r = 0.25 * (pmap -> maxDist + r + 2 * sqrt(pmap -> maxDist * r)); |
738 |
– |
|
739 |
– |
/* Skip the extra photon */ |
740 |
– |
for (i = 1 ; i < pmap -> squeueEnd; i++, sq++) { |
741 |
– |
/* Compute phase function for inscattering from photon */ |
742 |
– |
if (gecc2 <= FTINY) |
743 |
– |
ph = 1; |
744 |
– |
else { |
745 |
– |
ph = DOT(ray -> rdir, sq -> photon -> norm) / 127; |
746 |
– |
ph = 1 + gecc2 - 2 * ray -> gecc * ph; |
747 |
– |
ph = (1 - gecc2) / (ph * sqrt(ph)); |
748 |
– |
} |
749 |
– |
|
750 |
– |
getPhotonFlux(sq -> photon, flux); |
751 |
– |
scalecolor(flux, ph); |
752 |
– |
addcolor(irrad, flux); |
753 |
– |
} |
754 |
– |
|
755 |
– |
/* Divide by search volume 4 / 3 * PI * r^3 and phase function |
756 |
– |
normalization factor 1 / (4 * PI) */ |
757 |
– |
scalecolor(irrad, 3 / (16 * PI * PI * r * sqrt(r))); |
758 |
– |
|
759 |
– |
return; |
760 |
– |
} |
761 |
– |
|
762 |
– |
else |
763 |
– |
/* Apply bias compensation to density estimate */ |
764 |
– |
volumeBiasComp(pmap, ray, irrad); |
699 |
|
} |